Retrieve stripe session id at success page
I am using stripe with django and I want to pass some information I received from the checkout session to success page.
After reading the documentation https://stripe.com/docs/payments/checkout/custom-success-page#modify-success-url I modified success url to
MY_DOMAIN = 'http://127.0.0.1:8000/orders'
success_url=MY_DOMAIN + '/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url=YOUR_DOMAIN + '/cancel/',
metadata={
"price_id": price_id,
}
)
def Success(request):
session = stripe.checkout.Session.retrieve('session_id')
return render(request, 'orders/success.html')
But this gives me an error:
Invalid checkout.session id: session_id
If I manually put instead of "session_id" the actual id that is printed in the url everything works fine.
So my question is what should I write instead of 'session_id' in order to retrieve the session?
Your success_url varibale is just a string. You cannot pass variables into a string which is what it looks like you are trying to do.
You need to use either an f string or concatenate the session id with the part of the URL that does not change.
Additionally, in your success view, you are not passing context in the render return. It should be something like:
def Success(request):
session = stripe.checkout.Session.retrieve('session_id')
context = {'stripe_session_id':session}
return render(request, 'orders/success.html', context)
Then in your template, you can access the session id or whatever other info you pass into the context.