如果您查看balance-python库的源代码,您可以获得有关使用哪些参数的提示。我很欣赏这对初学者来说可能有点混乱,所以感谢您提出问题。
将卡添加到现有帐户:
card = balanced.Card(**card_payload) # this could also just be a URI from a card tokenized with balanced.js
account = balanced.Account.query.filter(email_address = emailAddress).one()
account.add_card(card.uri)
为特定卡充电:
cards = account.cards
# some magic here to select the correct card (e.g. prompt user for which card)
card = magic_card_selection(cards)
debit = account.debit(amount_in_cents, source_uri=card.uri)
为用户显示卡片
cards = account.cards
for card in cards:
# display as you like, the card object has the following attributes:
print card.__dict__.keys()
['expiration_month', 'account', 'hash', 'name', 'expiration_year', 'created_at', 'brand', 'uri', 'card_type', 'is_valid', 'meta', 'last_four', 'id']
这是一个完整的示例,可能会有所帮助:
import balanced
key = balanced.APIKey().save()
balanced.configure(key.secret)
balanced.Marketplace().save()
buyer = balanced.Account(email_address='buyer@example.org').save()
# add a new card
card = balanced.Marketplace.my_marketplace.create_card(
name='First card',
card_number='5105105105105100',
expiration_month='12',
expiration_year='2020',
security_code='123',
)
buyer.add_card(card.uri)
# add another valid card
card = balanced.Marketplace.my_marketplace.create_card(
name='Second card',
card_number='5105105105105100',
expiration_month='12',
expiration_year='2020',
security_code='123',
)
buyer.add_card(card.uri)
amount_in_cents = 100 # $1.00
first_card = buyer.cards[0]
second_card = buyer.cards[1]
# charge a specific card
debit = buyer.debit(amount_in_cents, source_uri=first_card.uri)
debit2 = buyer.debit(amount_in_cents, source_uri=second_card.uri)
assert debit.source.uri == first_card.uri
assert debit2.source.uri == second_card.uri
# show all attributes on an object
print first_card.__dict__.keys()