Reputation: 15578
I have integrated stripe to make payment.
Frontend get a session id from one of the server API
@Post("getPaymentSessionId")
async getPaymentSessionId(@Body() paymentInfo: PaymentInfo): Promise<PaymentSession> {
const transformedItem = {
price_data: {
currency: 'usd',
product_data: {
// images: [paymentInfo.imageUrl],
name: paymentInfo.name,
},
unit_amount: Math.round(paymentInfo.price * 100),
},
description: paymentInfo.description,
quantity: paymentInfo.quantity,
};
const session = await this.stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [transformedItem],
mode: 'payment',
success_url: paymentInfo.redirectURL + '&status=1',
cancel_url: paymentInfo.redirectURL + '&status=0',
metadata: {
images: paymentInfo.imageUrl,
},
});
return { id: session.id };
}
then frontend opens the payment screen using stripe apis by using returned session id. After the payment is done, stripe will redirect to the mentioned url with status that was provided in getPaymentSessionId
. Now on the froneend side, m making a order(calling an api /makeOrder
) on my server if payment was sucessfull. But how can i check on the server that payment was sucessfull? Is there any code or transaction returned by stripe that i can send /makeORder
to make sure that payment is made by the same user??
Upvotes: 0
Views: 1157
Reputation: 1971
The idea is that you don't fulfill your order in success_url. It's a discouraged pattern since your customer could have lost their internet connection before ever coming to your success_url, or accidentally closed the browser tab, etc.
The better place to fulfill orders is listening to checkout.session.completed webhook event: https://stripe.com/docs/payments/checkout/fulfill-orders#fulfill
Upvotes: 1