Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion coinbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#from decimal import Decimal

from coinbase.config import COINBASE_ENDPOINT
from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer, CoinbaseError
from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer, CoinbaseError, CoinbasePaymentButton


class CoinbaseAccount(object):
Expand Down Expand Up @@ -465,4 +465,49 @@ def generate_receive_address(self, callback_url=None):
return response.json()['address']


def create_button(self, name, price, price_currency='BTC',
button_type='buy_now', callback_url=None,
**kwargs):
"""
Create a new payment button, page, or iframe.

Some required parameters are documented, but the rest are supported
as keyword-arguments.

See https://coinbase.com/api/doc/1.0/buttons/create.html for details.

:param name: The name of the item to be purchased, donated for, or
subscribed.
:param price: The price, in whatever currency specified. Preferably a
string or Decimal.
:param price_currency: The ISO currency in which the price is listed.
Eg, BTC or USD.
:param button_type: Choices are 'buy_now', 'donation', and
'subscription'
:param callback_url: The URL to receive instant payment notifications

:return: The embeddable HTML string
"""
url = COINBASE_ENDPOINT + '/buttons'
request_data = {
"button": {
"name":name,
"price_string":unicode(price),
"price_currency_iso":price_currency,
"button_type":button_type
}
}
if callback_url is not None:
request_data['button']['callback_url'] = callback_url
request_data['button'].update(kwargs)
response = self.session.post(url=url, data=json.dumps(request_data),
params=self.global_request_params)
resp_data = response.json()
if not resp_data['success'] or 'button' not in resp_data:
error_msg = 'Error creating button'
if 'errors' in resp_data:
error_msg += ':' + u'\n'.join(resp_data)
else:
error_msg += '.'
raise RuntimeError(error_msg)
return CoinbasePaymentButton(**resp_data['button'])
3 changes: 2 additions & 1 deletion coinbase/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from transfer import CoinbaseTransfer
from contact import CoinbaseContact
from user import CoinbaseUser
from error import CoinbaseError
from error import CoinbaseError
from button import CoinbasePaymentButton
8 changes: 8 additions & 0 deletions coinbase/models/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__author__ = 'mhluongo'

class CoinbasePaymentButton(object):

def __init__(self, *args, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)

15 changes: 14 additions & 1 deletion coinbase/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,17 @@ def test_getting_user_details(self):
user = self.account.get_user_details()

this(user.id).should.equal("509f01da12837e0201100212")
this(user.balance).should.equal(1225.86084181)
this(user.balance).should.equal(1225.86084181)

@httprettified
def test_creating_a_button(self):

HTTPretty.register_uri(HTTPretty.POST, "https://coinbase.com/api/v1/buttons",
body='''{"button": {"style": "buy_now_large", "code": "b123456783q812e381cd9d39a5783277", "name": "Test Button", "info_url": null, "text": "Pay With Bitcoin", "price": {"cents": 2000, "currency_iso": "USD"}, "include_email": false, "custom": "", "cancel_url": null, "auto_redirect": false, "success_url": null, "variable_price": false, "include_address": false, "callback_url": null, "type": "buy_now", "choose_price": false, "description": ""}, "success": true}''',
content_type='text/json')

button = self.account.create_button('Test Button', '20.00', 'USD')

this(button.code).should.equal('b123456783q812e381cd9d39a5783277')
this(button.name).should.equal('Test Button')
this(button.price['cents']).should.equal(2000)
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
'httplib2>=0.8',
'requests>=1.1.0',
'oauth2client>=1.1'
]
],
tests_require=[
'sure>=1.2.5',
'httpretty>=0.8.0',
],
)