I am integrating a front end app with Stripe for payment processing.
I am following the quick start docs and nearly have everything working, however I am not being automatically redirected. I do get a successful response which contains a link so it wouldn’t be too difficult to pull this out of the response. However, I don’t want to do this if it isn’t the intended implementation.
My backend:
@app.post('/create-checkout-session')
async def create_checkout_session(request: Request):
data = await request.json()
price_id = data['price_id']
try:
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': price_id,
'quantity': 1,
},
],
mode='subscription',
success_url='http://localhost:3000' + '?success=true',
cancel_url='http://localhost:3000' + '?canceled=true',
)
except Exception as e:
print(e)
return str(e)
return redirect(checkout_session.url, code=303)
My front end api call:
fetch("{backend}/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify({ price_id: "price_1P3O6zRsJS40xuhRCkN4vrML" }),
});
And the response to the api call
{
"headers": {
"Content-Type": "text/html; charset=utf-8",
"Content-Length": "905",
"Location": "https://checkout.stripe.com/c/pay/cs_test_a1ODicN9C2lxgpSowo6RVXl8G1FaBFwaJrkgKwtuPKE6LBSIV5izrxyT67#fidkdWxOYHwnPyd1blpxYHZxWjA0Sn9cR1ZXdk9WMTV9cG1Xcnd0N38wN2prTnBHbUcxTDNUd3V0fW9uSFRWUEdhS29WdF1cXH1jbmRSXX1Tdnd2Yn1MazVwaTNkMHU3cXdKY2tIM2J9VTdrNTU8R092cjZudCcpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl"
},
"_status": "303 SEE OTHER",
"_status_code": 303,
"direct_passthrough": false,
"_on_close": [],
"response": [
"<!doctype html>n<html lang=en>n<title>Redirecting...</title>n<h1>Redirecting...</h1>n<p>You should be redirected automatically to the target URL: <a href="https://checkout.stripe.com/c/pay/cs_test_a1ODicN9C2lxgpSowo6RVXl8G1FaBFwaJrkgKwtuPKE6LBSIV5izrxyT67#fidkdWxOYHwnPyd1blpxYHZxWjA0Sn9cR1ZXdk9WMTV9cG1Xcnd0N38wN2prTnBHbUcxTDNUd3V0fW9uSFRWUEdhS29WdF1cXH1jbmRSXX1Tdnd2Yn1MazVwaTNkMHU3cXdKY2tIM2J9VTdrNTU8R092cjZudCcpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl">https://checkout.stripe.com/c/pay/cs_test_a1ODicN9C2lxgpSowo6RVXl8G1FaBFwaJrkgKwtuPKE6LBSIV5izrxyT67#fidkdWxOYHwnPyd1blpxYHZxWjA0Sn9cR1ZXdk9WMTV9cG1Xcnd0N38wN2prTnBHbUcxTDNUd3V0fW9uSFRWUEdhS29WdF1cXH1jbmRSXX1Tdnd2Yn1MazVwaTNkMHU3cXdKY2tIM2J9VTdrNTU8R092cjZudCcpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl</a>. If not, click the link.n"
]
}
If it’s important the redirect
call in my python endpoint is from flask
2
Answers
I provide my sample code. I used Node.js/Express backend and React/Redux frontend.
Backend code :
Frontend code:
You can use stripe-js in your react app.
Is your frontend app is configured to follow the server-side redirect? If not, that might be the issue here. You should try tweaking your
fetch
call to something like: