Bulchsu
Bulchsu

Reputation: 710

How to process payment on the backend side using Stripe?

I am new to Stripe and payments in general. I've found few articles on the internet with the examples and guidelines eg. this one. As i noticed the algorithm for creating the payment looks like this:

  1. Client app fetches the publishable Stripe key from the server
  2. Server application creates the checkout session, client app fetches the checkout session id using retrieved publishable key
  3. Client app redirects to checkout
  4. User finishes the payment and being redirect back to client app

Please correct me if i'm wrong. In general i don't understand one thing - how the server application knows that the payment is completed successfully or not? Should i redirect the flow from stripe checkout to backend first, process the result and the from the backend call the frontend again? Or should i somehow use the checkout session to check has it been completed? Shall i use some kind of cron then to process pending checkout sessions? Thanks in advance for any help. Regards

Upvotes: 1

Views: 1593

Answers (1)

Pompey
Pompey

Reputation: 1354

Basically, what you lay out is viable. You can check the Session status when the client is directed back to your server, but you will want to check this status at least one other way, either via a webhook or the cron job you mention.

Should i redirect the flow from stripe checkout to backend first, process the result and the from the backend call the frontend again?

This is possible. Stripe allows you to add the {CHECKOUT_SESSION_ID} template parameter to your Checkout's success URL, when the user is redirected after their checkout, that template will be replaced with the actual Checkout Session ID which you can use to retrieve the Session and its status.

That being said, it is possible for a Customer to make a payment but have their connection cut out before navigating back to your page. So, if you rely on that redirect the customer will be charged but you will never know to fulfill their order. That leads to unhappy customers so Stripe typically recommends setting up a webhook endpoint on your server[2] so that they can send you a checkout.session.completed event to notify you that the customer has finished their Checkout Session. That way, even if a customer never gets to your success page, you will know to fulfill their order.

[1] https://stripe.com/docs/payments/checkout/custom-success-page#modify-success-url

[2] https://stripe.com/docs/payments/checkout/fulfill-orders

Upvotes: 1

Related Questions