1

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?

1 Answers1

1

When using the prebuilt checkout page, you provide Stripe with the success url to send the user to when they successfully complete a payment. This is done like so:

checkout_session = stripe.checkout.Session.create(
                        line_items=[{'price': price, 'quantity': 1}],
                        payment_method_types=['card'],
                        mode='payment',
                        success_url="http://yoursite.com/order/success?session_id={CHECKOUT_SESSION_ID}"
                        cancel_url=domain + cancelURL,
                    )

return redirect(checkout_session.url)

This will create a Stripe checkout session and send the user to it. Once the user successfully pays, Stripe will send them to the success_url you provided and put in the correct checkout session id.

You will have to use webhooks to get the data you want, save it to your database, and then query the database in the success page view. Getting the checkout session id from the success page URL will depend on if you are using a GET like in the example above or as part of the URL like https://yoursite.com/order/12345. If you use the GET method, see here for more info.

pycode81
  • 124
  • 1
  • 9
  • Actually this part of the url it works. It's mentioned in stripe docs ant that's why I put it. Every time I pay it gives me a url lile this: /orders/success/?session_id=abcd123abc123etc. If I take this session_id and write session = stripe.checkout.Session.retrieve('abcd123abc123etc') everything works fine. The problem is how to put that id as a variable in session=etc – Alexdimirop Apr 24 '22 at 20:40
  • You are trying to use the retrieve function from Stripe. Are you using this URL when the user first checks out successfully or is this when the user wants to revisit a success page? Not sure why a user would want to revisit a success page. – pycode81 Apr 24 '22 at 20:47
  • After the payment I use stripe webhook to take the session information that contains, name,address,id etc. I want to pass some of these information to success page in order the user to see invoice information. In order to do retrieve the session in succes page stripe docs says to use this url. The whole payment is a pay as a guest payment it's not for a logged in user. – Alexdimirop Apr 24 '22 at 20:54
  • @Alexdimirop are you using the Stripe pre-built checkout page or are you using the payment intent API on your own website with the Stripe Javascript bundle? – pycode81 Apr 24 '22 at 22:26
  • I'm using the pre-built checkout page – Alexdimirop Apr 24 '22 at 22:44
  • @Alexdimirop See the answer now. I have edited since you have provided more info. – pycode81 Apr 24 '22 at 23:00
  • 2
    Thanks for the answer. I changed it to: session_id = request.GET.get('session_id', '') session = stripe.checkout.Session.retrieve(session_id) and it works fine. Thanks! – Alexdimirop Apr 24 '22 at 23:23