Site icon Easy2Digital

Python Tutorial 53 – Stripe API and Flask Integration to Receive Online Payment

stripe api

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 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.

8 Things to Look Into a Payment Processor for your eCommerce Website

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 ##
@app.route("/checkout", methods=['POST’])
def checkout():
checkout_session = stripe.checkout.Session.create(
'price': 'your product price id',
'quantity': 1
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 str(e)

return redirect(checkout_session.url, code=303)

def success():
return render_template("success.html", user=current_user)

def cancel():
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

Python Tutorial 26: Create a Shopify Bot Web Application Using Flask and Heroku

Front-end side script: Stripe Javascript and buy now button

In Stripe API, the checkout page would be redirected to the Stripe server and then come back to your webshop after the checkout. So Stripe requires users to add two lines of Javascript onto the page embedded with the checkout button.

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>

Checkout Testing

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.

Exit mobile version