Creating Charge from Card (Stripe)
On our previous post, we learned how do we collect payment information from our users securely. Today we will learn to create a charge with the payment information provided by the user using Python.
Installing Stripe Package
pip install stripe
Get the Token
After installation of the package, we first need to get the token from Stripe.js, in the previous post i have shown how can we retrieve token from the payment information.
Alternatively, we can create a token using the Stripe API:
import stripe
stripe.api_key = "your_stripe_api_key"
stripe.Token.create(
card={
'number': '4242424242424242',
'exp_month': 12,
'exp_year': 2020,
'cvc': '123',
},
)
Create a Charge
Now, using the token generated, we can create a charge by using Charge
object.
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "your_stripe_api_key"
# Token is created using Checkout or Elements!
# Get the payment token ID submitted by the form:
token = request.form['stripeToken'] # Using Flask
charge = stripe.Charge.create(
amount=999,
currency='usd',
description='Example charge',
source=token,
)
amount
is a positive integer representing how much to charge in the smallest currency unit (e.g., 100 cents to charge $1.00 or 100 to charge ¥100, a zero-decimal currency). The minimum amount is $0.50 US or equivalent in charge currency.
Congratulations! you have made your first charge :)
Extra Information
There may be a case where you may want to first assure if the card payment can be successful and then charge the customer after some day. Stripe provides a two-step card payment where you first authorize a charge and then charge(capture) the customer.
When a customer authorizes a charge, the certain amount which is authorized is held in customer card for 7 days. we cannot hold the amount for more than 7 days. No,w if we capture the amount before 7 days, the amount will be deducted from the customer's card.
First, to authorize the amount, while creating the Card
object, we must pass capture=False
Authorize the card
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "your_stripe_api_key"
# Token is created using Checkout or Elements!
# Get the payment token ID submitted by the form:
token = request.form['stripeToken'] # Using Flask
charge = stripe.Charge.create(
amount=999,
currency='usd',
description='Example charge',
source=token,
capture=False,
)
Capture it
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "your_stripe_api_key"
charge = stripe.Charge.capture('ch_ilygjpGNxzW912up19Xh')
Here, ch_ilygjpGNxzW912up19Xh
is the token returned when authorizing the charge.
We can also capture less amount than the amount defined when authorizing by adding amount
parameter.