#Arghya Das
1 messages ยท Page 1 of 1 (latest)
hi
Hi ๐ how can we help?
actually sorry for asking
but I have a silly problem which I can't solve
I am using stripe api to create a checkout.session and redirect user to pay
then after the payment I am redirecting user to a successful page with the session id
now remember they are paying for credits or like tokens which can be spent to use my saas
but problem is when I am redirecting them to the successpage I am calling an api in my backend to update the token the user have bought in my mongodb database
which was working fine until I saw that
refreshing or reloading the page also updating the tokens
as the session id is still in the url and status property is completed
I am using react and node.
thanks you...
You should not update your database on a redirect. You should only update your database when you receive the checkout.session.succeeded webhook event.
I tried webhook but couldn't understand it
if you can tell me in simple words what you just said ๐
also first I need to check the balance token in the database so how to do that in webhook
also I have two plans so based on buttons id I need to change the newtoken amount which will be added
I tried webhook but couldn't understand it
I would suggest understanding it, as it is a very bad practice to update your database on a page load (especially due to the issue you're experiencing right now)
This page explains it the best, I think: https://stripe.com/docs/payments/checkout/fulfill-orders
cause if I can then I would like to add my item id and the balance so I can calculate it
I have read it
using stripe cli is very confusing
as I am using localhost I can't register the endpoint
You can customize the success URL if you want to transport other data (e.g. item id) from Checkout to your success_url webpage: https://stripe.com/docs/payments/checkout/custom-success-page#modify-success-url
let me give 1 day I will try and let you know
if I am using in localhost
how can I verify the sig
and the endpoint secret where can I get that?
๐ stepping in
Have you taken a read through our Webhook docs: https://stripe.com/docs/webhooks ?
Can you show me the code you are running?
Okay yeah you don't want to use bodyParser here as it will manipulate the raw body which will cause signature verification to fail
then what should be that part?
You don't need that at all
Not for a webhook endpoint
Just remove bodyParser altogether
so it should be
dont close this okay
You can set express.raw({type: 'application/json'}) instead
I need some time
Sure
I will get back to you
I also listen to that url
i am using this as you refered but
why it is also logging the default one?
Correct you can use whatever endpoint URL you want
You just need to specify that same URL when you create your Webhook endpoint in Stripe
What do you mean by "also loggin the default one"?
when I run this code
it is loggin this
also i want only for checkout.session.completed.
Yep those logs look right based on your code above
You need to add a case statement for checkout.session.completed if you want to listen for that
i did that but didn't work
What does "didn't work" mean?
tried to console log true
Please be way more detailed
Yep so did you look at the payload variable?
Are you initializing it anywhere?
Doesn't look like it
Seems like you want to be passing event or request.body to your constructEvent() function
What is eventEffect?
Take a look back at https://stripe.com/docs/webhooks/quickstart
i change that variable name
Can you show me your updated code
yes this is
Okay can you show me how you initialize your express server?
Are you using express.json?
K yeah that will also manipulate the raw body
So try something like: app.use((req, res, next) => { if (req.originalUrl === '/api/webhook') { next(); // Do nothing with the body because I need it in a raw state. } else { express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe. } });
The error is clearly telling you that you used a js object
nono wait
this is the latest code I made
and it is ok returning 200
now how can I handle the event
if you can make that snippet I mean where to put it
You put it in place of wherever you are setting app.use right now
I really don't understand why you are doing the above. Your framework is taking a raw string, turning it into json, and then you are turning it back into a raw string
Don't use this hackiness
Just do it correctly and stop your framework from manipulating the raw body.
No. const payloadString = JSON.stringify(payload, null, 2); is you manipulating the payload
the error gave me a link
Why are you doing that
the error gave me this link
It is sent when you trigger an event
i am using this locally
That's fine. You can forward using the CLI to test locally
๐ข
based on this
Yes you do want to verify the signature
this code dont have that
?
in this
It does?
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`โ ๏ธ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}```
That is the code from that doc
Which performs verification
Yep that looks fine as far as I can tell
Yep did you change from using express.json for this endpoint?
Like I showed you above?
for this?
if (req.originalUrl === '/api/webhook') {
next(); // Do nothing with the body because I need it in a raw state.
} else {
express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe.
}
});```
I need to put after this code
?
No that should be up above where you initialize express
So it should look something like: ```const express = require("express");
const app = express();
const stripe = require("stripe")('sk_test_123');
app.use(express.static("."));
// change this --> app.use(express.json());
app.use((req, res, next) => {
if (req.originalUrl === '/webhook') {
next(); // Do nothing with the body because I need it in a raw state.
} else {
express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe.
}
});
But yeah you need to tailor that to whatever you have set
Anyway I need to step away but my colleague will step in to help if you have more questions
Here to answer any further questions after your try this out, since bismarck needs to step away
Have you confirmed your code is entering the if (req.originalUrl === 'api//webhook') { path?
ie, not parsing the json
how to do that?
set the like this
this is at the top where I initialized it
it is at the bottom
Sure, but when you test it, what is happening?
You'll need to add some logging and inspect the server logs
this error is I am getting
Yes but i mean in this block, which path is being taken?
You also have app.use(express.json()); at the top apply to all requests
you dont want that
that's what the if/else block is trying to avoid'
oh I remove that?
you need to not parse the json for webhook requests
where to put server log to get the original url
In this block, on each codepath
it is returning 200
let me try once more
sir perfect
now I only have left some things
can we continue tomorrow in this thread?
it already 2 am here
can you keep open this thread?
sir one more question before good night
that checkout session creating the checkout page
the font the page is using can I change that somehow
using my own website font so it feels more intregrated?
Font is not currently a customizable detail of Checkout
ohh