Redirect from successful stripe checkout
Asked Answered
C

1

7

I am using Stripes payment gateway with C# .Net. I started with creating a checkout which redirects the user to Stripe's payment gateway (I didnt want the hassle of maintaining the card numbers etc so this way it forwards to Stripe to ask for the card details, process the payment etc) https://stripe.com/docs/payments/checkout/one-time - this was done in code-behind.

I set my success URL as www.example.com/myHandler.ashx (Generic Handler)- this URL sets the order id as successful. My handler code is similar to https://stripe.com/docs/webhooks/build (since im using a Generic Handler i'm using forms and not MVC)

After a successful payment, the payment is recorded to my database.

Considering this is a webhook, how do i display/redirect to a thank you page?

Cameron answered 18/12, 2019 at 13:18 Comment(2)
your success URL is the URL where the user will be redirected towards. So this page should be the page to show a thank you.Elamite
Ok but how would i record that its a successful payment and carry out database tasks? I can convert the handler to a page but i dont think i fully follow?Cameron
E
9

You have more than one way to solve this.

Option 1:
Combine the success page with your webhook. This means it will display the page to the user and update the state of the transaction on the backend.

your url would look like this
SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}"
Then use the session ID to handle the state change like you would with your webhook but return the html page to the user (Technically this wouldn't be a webhook).

Based on your question it could be that the SuccessUrl gives a session object as body because otherwise your current code shouldn't be working but I'm not sure.

To get the session object based on the Id you can make another call to stripe see: https://stripe.com/docs/api/checkout/sessions/retrieve#retrieve_checkout_session

Once payment is successful, the Checkout Session will contain a reference to the Customer, and either the successful PaymentIntent or an active Subscription.

Option 2:
Create a webhook separately from your thank you page. This means that the SuccessUrl would be your thank you page.
The webhook needs to be defined separately and globally with stripe. You can look here on how this is done: https://stripe.com/docs/webhooks/configure

Elamite answered 18/12, 2019 at 13:36 Comment(6)
Let me try out option 1 first. The link i provided to tackle processing the database tasks shows stripeEvent.Type == Events.PaymentIntentSucceeded from a post request - how do i gain access to the order details i.e. So i have the session id but how do i load the record to take action against?Cameron
I'll take a look at this shortly but one more question before i dive into this. Does the session ID have to be part of the success URL? I am using MetaData to hold other data, so could i just use MD to also hold the session ID?Cameron
Ohh yes you should also be able to put it in the MetaData. I just used this example because it's also in the official docsElamite
So i have a SessionService (from the link you provided) but how do i associate that with a successful payment to process this further? Previously i had Stripe.PaymentIntent in addition the Response doesnt include the MetaData, do i need to make another call?Cameron
Not sure what you mean? how were you able to get the PaymentIntent previously? can't you just keep it mostly the same as you had before but return a view in asp.net?Elamite
Option 1 is not a good idea. You want your "Thank you page" to rely only on checkout.Session. In your thank you page, you should tell the customer nothing more than "We confirm your order" and any data that's present in the checkout.Session object. Separately from all this, you use your webhook that is called by Stripe to complete the transaction, fulfill order, send a confirmation email to the customer, etc. It should be based on PaymentIntent or Subscription object. This is the preferred method as described in Stripe documentation.Howlyn

© 2022 - 2024 — McMap. All rights reserved.