Summary

Taking payments is a big step for any project, and Stripe Checkout keeps it simple: Stripe handles the payment page and card details, and the application only needs to start a checkout and act on the result.

The two key ideas are to never trust the browser about a payment (always ask Stripe), and to use webhooks for fulfilment, because the browser might never come back.

The example code includes test_payments.py. It doesn’t talk to Stripe at all: the tests replace the Stripe calls with made-up checkouts, to check that paid checkouts make the user a member, while unpaid ones and other users’ checkouts don’t. It also sends the webhook a correctly signed event, and a wrongly signed one.

Code

The example code at this point is tagged ch11-payments in the example repository. To get it:

git clone https://github.com/codewalkr/fastapi-example-for-you.git
cd fastapi-example-for-you
git checkout ch11-payments