2

我正在使用平衡付款来允许用户在我的网站上购买服务。当用户准备结帐时,他们在我的网站上登录/创建一个帐户,然后输入他们的信用卡信息。该卡已收费,他们正在路上。

正如我上面提到的,我的网站提供了一项服务,所以我每个月都会通过 cronjob 自动从他们的卡上收费。我干脆用下面一个班轮来抢买家:

buyer = balanced.Account.query.filter(email_address = emailAddress)[0]

很简单。但是,信用卡丢失、过期、用户想要使用不同的信用卡等。我该如何允许用户编辑他们的信用卡或提供新的信用卡?我是否只是创建一个单独的页面,允许他们通过以下方式输入新的信用卡信息:

buyer = balanced.Marketplace.my_marketplace.create_buyer(emailAddress, card_uri = cardURI)

最后,balanced如何决定使用哪个chard?如何让用户管理他们想使用的信用卡?我是否只是存储 card_uri 并让他们选择要使用的卡 URI?

4

2 回答 2

3

如果您查看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()
于 2012-10-25T00:10:15.543 回答
1

很多内容都包含在文档中;

您可以从特定的卡中收取费用,如果您不这样做,balanced 将收取此帐户中最近添加的卡的费用。

于 2012-10-24T21:23:11.663 回答