Adrian Rosebrock
Adrian Rosebrock

Reputation: 917

Updating credit card or allowing user to use different card in balanced payments

I am using balanced payments to allow users to purchase services on my site. When users are ready to checkout, they login/create an account on my site, then enter their credit card information. The card is charged and they are on their way.

As I mentioned above, my site offers a service so I charge their card every month automatically via a cronjob. I simply use the following one liner to grab the buyer:

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

Simple enough. However, credit cards are lost, they expire, users want to use a different credit card, etc. How do I go about allowing a user to edit their credit card or provide a new one? Do I simply create a separate page that allows them to enter their new credit card information via:

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

Finally, how does balanced decide which chard to use? How do I let users manage which credit card they would like to use? Do I simply store the card_uri and let them select which card URI to use?

Upvotes: 2

Views: 333

Answers (2)

mjallday
mjallday

Reputation: 10092

If you look at the source for the balanced-python library you can get hints about what parameters to use. I appreciate it may be a bit confusing for first-timers so thanks for asking a question.

Adding a card to an existing account:

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)

Charging a specific card:

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)

Displaying cards for a user

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']

Here's a complete example which may help:

import balanced


key = balanced.APIKey().save()
balanced.configure(key.secret)
balanced.Marketplace().save()

buyer = balanced.Account(email_address='[email protected]').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()

Upvotes: 3

Roland Smith
Roland Smith

Reputation: 43523

A lot of this is covered in the documentation;

you may charge a specific card, if you don't, balanced will charge the most recently added card on this account.

Upvotes: 1

Related Questions