Search code examples
c#asp.netstripe-paymentswebhooksgeneric-handler

Redirect from successful stripe checkout


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?


Solution

  • 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