|
45 | 45 | #from decimal import Decimal |
46 | 46 |
|
47 | 47 | from coinbase.config import COINBASE_ENDPOINT |
48 | | -from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer, CoinbaseError |
| 48 | +from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer, CoinbaseError, CoinbasePaymentButton |
49 | 49 |
|
50 | 50 |
|
51 | 51 | class CoinbaseAccount(object): |
@@ -465,4 +465,49 @@ def generate_receive_address(self, callback_url=None): |
465 | 465 | return response.json()['address'] |
466 | 466 |
|
467 | 467 |
|
| 468 | + def create_button(self, name, price, price_currency='BTC', |
| 469 | + button_type='buy_now', callback_url=None, |
| 470 | + **kwargs): |
| 471 | + """ |
| 472 | + Create a new payment button, page, or iframe. |
| 473 | +
|
| 474 | + Some required parameters are documented, but the rest are supported |
| 475 | + as keyword-arguments. |
| 476 | + |
| 477 | + See https://coinbase.com/api/doc/1.0/buttons/create.html for details. |
468 | 478 |
|
| 479 | + :param name: The name of the item to be purchased, donated for, or |
| 480 | + subscribed. |
| 481 | + :param price: The price, in whatever currency specified. Preferably a |
| 482 | + string or Decimal. |
| 483 | + :param price_currency: The ISO currency in which the price is listed. |
| 484 | + Eg, BTC or USD. |
| 485 | + :param button_type: Choices are 'buy_now', 'donation', and |
| 486 | + 'subscription' |
| 487 | + :param callback_url: The URL to receive instant payment notifications |
| 488 | +
|
| 489 | + :return: The embeddable HTML string |
| 490 | + """ |
| 491 | + url = COINBASE_ENDPOINT + '/buttons' |
| 492 | + request_data = { |
| 493 | + "button": { |
| 494 | + "name":name, |
| 495 | + "price_string":unicode(price), |
| 496 | + "price_currency_iso":price_currency, |
| 497 | + "button_type":button_type |
| 498 | + } |
| 499 | + } |
| 500 | + if callback_url is not None: |
| 501 | + request_data['button']['callback_url'] = callback_url |
| 502 | + request_data['button'].update(kwargs) |
| 503 | + response = self.session.post(url=url, data=json.dumps(request_data), |
| 504 | + params=self.global_request_params) |
| 505 | + resp_data = response.json() |
| 506 | + if not resp_data['success'] or 'button' not in resp_data: |
| 507 | + error_msg = 'Error creating button' |
| 508 | + if 'errors' in resp_data: |
| 509 | + error_msg += ':' + u'\n'.join(resp_data) |
| 510 | + else: |
| 511 | + error_msg += '.' |
| 512 | + raise RuntimeError(error_msg) |
| 513 | + return CoinbasePaymentButton(**resp_data['button']) |
0 commit comments