Seamlessly receiving payment from customers and presenting a secure payment process are crucial for any online business. In this python tutorial, I would walk you through how to utilize Stripe API, which is one of the most well-known payment processors – Stripe. You can learn how to integrate it with your flask application.
Core Tools and Python Modules: Flask, Stripe API, OS (Actual modules depend on the application you build)
- Stripe API Account Creation and API Key
- Create the products you are going to sell on Stripe
- Python Side Script: checkout, success, cancel route
- Checkout Function Testing
- Full Python Script of Stripe API and Flask Integration to Receive Payment
Stripe Account Creation and Stripe API Key
Stripe is a payment service provider that business owners can use to accept dozens of payment methods, from credit cards, and digital payments, to buy now pay later services. It charges a fee on each transaction. Stripe works best if you want to accept online payments. With added hardware, you can also use it for in-person payments. For more details regarding things you need to look into a payment processor, please check out this article.
First thing first, we need to sign up for a Stripe account. A thing you need to be aware of is the country selection as it is related to the receivable side of the bank account. Once you select, you can’t modify the account settings using the current email.
Then, you need to verify your account using the phone number and a few minor information is optional to input. After that, it’s done and the testing mode is ready, and we can go to our profile page and copy the API secret key. We would use it in a moment.
Last but not least, the testing mode is not activated yet. If all things are ready and in place, we need to fill in more personal information, which includes the bank account information to get the active account. However, for developing the scripts and setting up the integration with your flask application, the testing mode is okay to go.
Create the products you are going to sell on Stripe
The other thing is we need to create the products that would be listed on our webshops or applications.
Creating the product has some information relevant to your product and selling strategy. For example, we need to tell Stripe if it’s a one-time checkout or recurring payment. And for recurring payments, we need to set the billing period as well.
Also, you can set the product as a bundle deal, or an individual item pricing. In the end, it would be related to other fields, such as fulfillment, so we need to carefully set up based on our business status.
Last but not least, we need to copy the product price ID and keep it as it will be used in a moment.
Python Side Script: checkout, success, cancel route
First thing first, we need to install the Stripe package on your laptop. For example, I am using Python3 in MacOS, so I would input the command:
pip3 install stripe.
Then, it’s time to code the server-side script. We need to add the stripe secret key and create at least three routes, which are checkout, success, and cancellation.
Stripe.api_key = ‘abcdefg’
In the checkout route, the line_item is one of the most important parts, which tells the stripe what the product exactly is. It’s in a dictionary data format. If you have tons of products, you separately set up a dictionary variable.
## Stripe payment ##
checkout_session = stripe.checkout.Session.create(
'price': 'your product price id',
success_url=url_for('your thank you page route',_external=True),
cancel_url=url_for(the cancellation page routel',_external=True)
except Exception as e:
return redirect(checkout_session.url, code=303)
return render_template("success.html", user=current_user)
return render_template("cancel.html", user=current_user)
Also, it requires to add on the thank you page and payment cancellation page URL in the checkout route as well. So we need to create two routes separately and also the HTML pages as well.
For more details about the Flask application, please check out this article
The checkout button, basically, sits on the product page. So the coding depends on your actual business needs. Here is a sample
<a href="/checkout" class="btn btn-primary">Buy Now</a>
Once the script is done, Stripe API provides us to test the checkout using different dummy payment codes, such as Mastercard, visa card, etc. People don’t need to use the authentic card to test the checkout function and receive the payment.
Stripe dashboard is powerful in terms of intelligent data. Basically, people can grab tons of customer purchase insight. I’ll release another article regarding how to deep-dive leverage payment processors to increase marketing productivity.
Full Python Script of Stripe API and Flask Integration to Receive Payment
If you are interested in the full python Tutorial 53 – Stripe API and Flask Integration to Receive Online Payment, please subscribe to our newsletter by adding the message “Python Tutorial 53”. We would send you the script immediately to your mailbox.
I hope you enjoy reading Python Tutorial 53 – Stripe API and Flask Integration to Receive Online Payment. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support my channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article to your social network with the hashtag #easy2digital
- Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)