Take payments using Stripe
Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.
In the App Browser, click the + next to Services.
You’ll see a list of available services and integrations. Click on Stripe.
You must now connect Anvil to a Stripe account. You have two options.
If you already have a Stripe account, you can log in to it from Anvil to connect your app to Stripe.
Click ‘Yes - connect my existing account’. You’ll get a Stripe log in window - log in to your account.
If you do not already have a Stripe account, you’ll need one in order to take payments. Click ‘No - I need to create an account’. You’ll get a Stripe sign-up window, where you can enter a few details. Once you’ve signed up, you’ll be back in the Anvil Editor.
Form1 in the App Browser.
Go to the Code view to see the code for
__init__ method, add these lines:
c = stripe.checkout.charge(currency="GBP", amount=100) print(c)
Now click the ‘Run’ button at the top of the screen.
Your app will run and display a payment form. The Stripe Service is running in test mode, so no money will actually change hands.
For the card number, enter
4242424242424242 (This is a Visa test card number). Put anything in the other fields (the expiry date
must be in the future).
Hit the ‘Pay’ button. The Stripe payment form will disappear and your main app will finish loading. Your transaction details have been printed on the Output Panel:
The transaction details contain
'result': 'succeeded' - so Stripe has registered the transaction!
To take payments for real, hit the ‘Switch to Live mode’ button in the Stripe Service:
The warning goes away and your transactions will now be enacted for real:
Click on the button below to clone a finished version of this app into your account.
Of course, you’ll need to log in to your own Stripe account - the cloned app does not retain the connection to Stripe, for obvious security reasons.
Want more depth on this subject?
As well as presenting a pre-made payment form, Anvil has Python methods for making Stripe payments programmatically. This uses Stripe’s token system that allows transmitting card details while complying with financial regulations.
The Python API allows you to:
- Make a payment programmatically using a card details token,
- associate transactions with a customer record,
- store the card details token against a customer record,
- and set up recurring subscriptions.
You can also use the raw Stripe Python API if you prefer.
Want another quickstart?
Every quickstart is on the Quickstarts page.