Next.js Discord

Discord Forum

Stripe payment issues, duplication and incomplete payment

Answered
African Slender-snouted Crocodil… posted this in #help-forum
Open in Discord
African Slender-snouted CrocodileOP
The code snippets are in the text

The payment gateway is working fine and all, but sometimes even if I get the message that payment was successful, there's no entry in my db. I check the webhook screen and there are bunch of incomplete payments. When I come to check my db later on, I see bunch of payment entries in the db even when I am not using it, which I am guessing is because I go back from the payment screen? Even if payment is successful and it gets inserted in db, I am seeing two entries in the db at the same time. What is the problem here?
Answered by B33fb0n3
Your code looks good, but you need to change some things:
1. When you update your quantity, I don't see any price change of the paymentintent. If the customer confirms the paymentintent now, the quantity of one item will be used instead of the amount of the quantity
2. The way how you initialize your stripe instance serverisde looks incorrect for me. Normally its
const stripe = require('stripe')('sk...')

instead of
const stripe = new Stripe('sk...')

Maybe both syntax are ok, because you said, that the gateway works fine...

And your initial problem on why your database is a bit out of date:
3. Use the correct webhook event. You are currently using charge.succeeded. Instead use the payment_intent.succeeded event.
View full answer

8 Replies

@African Slender-snouted Crocodile The code snippets are in the text The payment gateway is working fine and all, but sometimes even if I get the message that payment was successful, there's no entry in my db. I check the webhook screen and there are bunch of incomplete payments. When I come to check my db later on, I see bunch of payment entries in the db even when I am not using it, which I am guessing is because I go back from the payment screen? Even if payment is successful and it gets inserted in db, I am seeing two entries in the db at the same time. What is the problem here?
Your code looks good, but you need to change some things:
1. When you update your quantity, I don't see any price change of the paymentintent. If the customer confirms the paymentintent now, the quantity of one item will be used instead of the amount of the quantity
2. The way how you initialize your stripe instance serverisde looks incorrect for me. Normally its
const stripe = require('stripe')('sk...')

instead of
const stripe = new Stripe('sk...')

Maybe both syntax are ok, because you said, that the gateway works fine...

And your initial problem on why your database is a bit out of date:
3. Use the correct webhook event. You are currently using charge.succeeded. Instead use the payment_intent.succeeded event.
Answer
@B33fb0n3 Your code looks good, but you need to change some things: 1. When you update your quantity, I don't see any price change of the paymentintent. If the customer confirms the paymentintent now, the quantity of one item will be used instead of the amount of the quantity 2. The way how you initialize your stripe instance serverisde looks incorrect for me. Normally its tsx const stripe = require('stripe')('sk...') instead of tsx const stripe = new Stripe('sk...') Maybe both syntax are ok, because you said, that the gateway works fine... And your initial problem on why your database is a bit out of date: 3. Use the correct webhook event. You are currently using ``charge.succeeded``. Instead use the ``payment_intent.succeeded`` event.
African Slender-snouted CrocodileOP
Hello, thanks for answering

1) Yes I am working on the quantity thing but I want to fix the duplication issue first because I just wake up to see bunch of entries in my db (supabase + prisma orm) even when I am not making any payments, and I have bunch of incomplete payments on the stripe payment page

2) Yes I think mine is fine too

3) I made it to payment_intent.succeeded, first time after fix when I did payment, it was double so I cleared the db and tried again, after that yes there was no duplication. Do you know why the sudden db entries even when I am not paying? Is it the incomplete payment? Do I need to handle when I go to purchase screen but go back again?
@African Slender-snouted Crocodile Hello, thanks for answering 1) Yes I am working on the quantity thing but I want to fix the duplication issue first because I just wake up to see bunch of entries in my db (supabase + prisma orm) even when I am not making any payments, and I have bunch of incomplete payments on the stripe payment page 2) Yes I think mine is fine too 3) I made it to payment_intent.succeeded, first time after fix when I did payment, it was double so I cleared the db and tried again, after that yes there was no duplication. Do you know why the sudden db entries even when I am not paying? Is it the incomplete payment? Do I need to handle when I go to purchase screen but go back again?
Normally you have something like a checkout session (either from yourself inside your db or from stripe) and after the payment is completed (not succeeded or failed or pending) you should close this session. Like that the user can't access this page anymore to duplicate his purchase.

I done that though cookies. When the session is created I set a cookie "cartSecret" on the client and only users with this cookie can access this page (it's secure against attackers and reliable). After the user entered all his informations and clicked on purchase (payment complete) I remove the cookie and he can no longer access the page. Maybe you want to do that too?
@B33fb0n3 happy to help. Please mark solution
African Slender-snouted CrocodileOP
how do I do that? sorry I am new here
Original message was deleted
next time just follow these steps to mark solution