Problem:
When user selects a product and hit btn to pay, it redirects him to stripe, and new order is created. But if user changes mind and in stripe checkout cancels the payment, my order is already created, how to prevent this issue? How to make sure order will be created after payment?
Using NEXT.JS
/api/stripe file
const session = await stripe.checkout.sessions.create({
// add products and creating session , then redirect user to stripe
})
const order = new Order({
// new order is creating
})
return res.(status....)
Solution:
The way it works is that you first create a Checkout Session. Then, you redirect the customer to the Checkout Session URL that is provided on the response from your Checkout creation. Once redirected, if the customer does not enter their payment details to complete the payment, you should not fulfill the order. Instead of creating the order right after the Checkout Session creation like you’re doing on your code, you should set up a webhook to listen to events to fulfill their order after the payment. In this case, you would want to listen to checkout.session.completed
event when a customer completes a checkout. Then, you would fulfill the order on your end.