Stripe
If you want to make money, you need to charge users. Thankfully, this is as easy as ever. Providers like Stripe take care of processing payments with any method imaginable. Simply create an account on Stripe, this will have test mode enabled by default, meaning you can pay here without being charged. Also make sure you have the Stripe CLI (see installation) installed, we will need it later. Get the STRIPE_SECRET_KEY and add it to your.env
file

building our webhook
In the background, stripe will charge the user, and inform us when certain events like a successfull or cancelled payment happens by communicating with our webhook. A webhook is simply said an endpoint where Stripe can send a request to. Openpayment_bp.py
and take a look at the “/stripe/webhook” route:
payment_bp.py
.env
. This ensures, that only Stripe can send to this endpoint
Go to the webhook section, add a new webhook with the events payment_intent.payment_failed
, payment_intent.succeeded
, and checkout.session.completed
.
Then copy the webhook key as “STRIPE_ENDPOINT_SECRET” to .env
.

- sending an email
- calling
set_user_package
function (or refunding if it fails)
creating the checkout
Both of this is set when creating the Checkout Session in “/checkout”.payment_bp.py

stripe_packages
array and map the items to their “package_ids”. Create two (or how many you need)
of those products and add them to the .env
file.
payment_bp.py
Pointing Stripe to our webhook
Lets get back to the webhook part. How does Stripe know where it is located? Once you have deployed your application at it is accessible on the World Wide Web (www), you can set the IP or domain name of it inside the Stripe Settings. While we work locally, however, we will use a technique called pooling. This means we will regularly ping the Stripe servers and see if any updates came through. This is made possible using the Stripe CLI.bash local.sh
and not while inside Docker (see structure).
Later once we have deployed to a domain, you can provide the URL to your webhook inside
the Stripe interface.
When an event occurs, Stripe will automatically send a request there, without you needing to use the Stripe CLI.
There you will have the option to receive only the events you need, these are:
checkout.session.completed
to see when the user filled out the checkout frompayment_intent.succeeded
to see when the payment has actually gone throughpayment_intent.failed
if the payment failed
send_email
functions to notify the user about his payments going through or being
cancelled. Read the next chapter on emails to see how we accomplish this