Stripe
- Stripe Backend for python
- It is necessary to handle strip checkout sessions, the client (frontend) only forwards the request.
Prerequisiteβ
- Have a working flask API see Flask.md
Codeβ
- Code to create POST endpoint using Stripe
- This will redirect user to a checkout page, and redirect back to your application on successful payment (or not).
- all the keys should be generated by your stripe account in their site. (Except for your secret key which you will generate yourself.)
- see Token Validation
@app.route('/submit-session', methods=['POST'])
def create_checkout_session():
print("accepted submit-session request")
stripe.api_key = STRIPE_KEY
SESSION_TOKEN = 'INVALID'
try:
SESSION_TOKEN = generate_token(SECRET_KEY)
checkout_session = stripe.checkout.Session.create(
line_items=[
{
# Provide the exact Price ID (for example, pr_1234) of the product you want to sell
'price': PRODUCT_KEY,
'quantity': 1,
},
],
mode='payment',
allow_promotion_codes='true',
success_url=DOMAIN_NAME + '#upload?success=true&token=' + SESSION_TOKEN,
cancel_url=DOMAIN_NAME+ '?canceled=true',
)
print("DOMAIN {}".format(DOMAIN_NAME))
print("checkout session created {}".format(checkout_session.url))
return redirect(checkout_session.url, code=303)
except Exception as e:
print(e)
return str(e)