#Arghya Das

1 messages ยท Page 1 of 1 (latest)

rigid anchorBOT
drowsy mango
#

hi

round merlin
#

Hi ๐Ÿ‘‹ how can we help?

drowsy mango
#

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...

round merlin
#

You should not update your database on a redirect. You should only update your database when you receive the checkout.session.succeeded webhook event.

drowsy mango
#

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

round merlin
#

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)

drowsy mango
#

yes

#

but can I add more metadata in the session id?

round merlin
drowsy mango
#

cause if I can then I would like to add my item id and the balance so I can calculate it

drowsy mango
#

using stripe cli is very confusing

#

as I am using localhost I can't register the endpoint

round merlin
drowsy mango
#

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?

pure parrot
#

๐Ÿ‘‹ stepping in

drowsy mango
#

yes

#

there is a error that payload needs to be in string

pure parrot
#

Can you show me the code you are running?

drowsy mango
#

yes this is the code

#

returning 404

pure parrot
#

Okay yeah you don't want to use bodyParser here as it will manipulate the raw body which will cause signature verification to fail

drowsy mango
#

then what should be that part?

pure parrot
#

You don't need that at all

#

Not for a webhook endpoint

#

Just remove bodyParser altogether

drowsy mango
#

so it should be

pure parrot
drowsy mango
#

dont close this okay

pure parrot
#

You can set express.raw({type: 'application/json'}) instead

drowsy mango
#

I need some time

pure parrot
#

Sure

drowsy mango
#

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?

pure parrot
#

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"?

drowsy mango
#

when I run this code

#

it is loggin this

#

also i want only for checkout.session.completed.

pure parrot
#

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

drowsy mango
#

but i only need that

#

so i can only work with if right?

pure parrot
#

What does "didn't work" mean?

drowsy mango
#

tried to console log true

pure parrot
#

Please be way more detailed

drowsy mango
#

ok wait

#

I tried to use this

#

this is the error

pure parrot
#

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

drowsy mango
#

oh yes

#

let me try it again

#

sorry sorry wait

pure parrot
#

What is eventEffect?

drowsy mango
pure parrot
#

Can you show me your updated code

drowsy mango
#

yes this is

pure parrot
#

Okay can you show me how you initialize your express server?

#

Are you using express.json?

drowsy mango
#

yes

#

app.use

#

yes

pure parrot
#

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. } });

drowsy mango
#

i think problem is something else

#

i think i cant find the sig

pure parrot
#

The error is clearly telling you that you used a js object

drowsy mango
#

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

pure parrot
#

You put it in place of wherever you are setting app.use right now

drowsy mango
#

ohk

#

hey

#

is this not correct

#

error is event is not defined

pure parrot
#

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.

drowsy mango
#

this is something stripe has

#

for webhook signing

pure parrot
#

No. const payloadString = JSON.stringify(payload, null, 2); is you manipulating the payload

drowsy mango
#

the error gave me a link

pure parrot
#

Why are you doing that

drowsy mango
#

the error gave me this link

pure parrot
#

It is sent when you trigger an event

drowsy mango
#

i am using this locally

pure parrot
#

That's fine. You can forward using the CLI to test locally

drowsy mango
#

so this code is not correct

#

?

pure parrot
drowsy mango
#

๐Ÿ˜ข

pure parrot
#

That code works for mocking

#

But you don't need to do that

drowsy mango
#

oh

#

let me correct it

#

so i dont need to verify the signature

drowsy mango
pure parrot
#

Yes you do want to verify the signature

drowsy mango
#

this code dont have that

pure parrot
#

?

drowsy mango
pure parrot
#

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

drowsy mango
#

let me run it again

#

sig is causing error

#

wait

#

this looks correct?

pure parrot
#

Yep that looks fine as far as I can tell

drowsy mango
#

error is

#

this

#

๐Ÿ˜ข

pure parrot
#

Yep did you change from using express.json for this endpoint?

#

Like I showed you above?

drowsy mango
#

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

#

?

pure parrot
#

No that should be up above where you initialize express

drowsy mango
#

app.use(express.json());

this should be there?

#

or should i remove it?

pure parrot
#

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

drowsy mango
#

ok

#

thanks

brazen wasp
#

Here to answer any further questions after your try this out, since bismarck needs to step away

drowsy mango
#

yes yes

#

this is the code

#

help me

#

๐Ÿ˜ข

brazen wasp
#

Have you confirmed your code is entering the if (req.originalUrl === 'api//webhook') { path?

#

ie, not parsing the json

drowsy mango
#

how to do that?

#

set the like this

#

this is at the top where I initialized it

#

it is at the bottom

brazen wasp
#

Sure, but when you test it, what is happening?

#

You'll need to add some logging and inspect the server logs

drowsy mango
#

this error is I am getting

brazen wasp
#

Yes but i mean in this block, which path is being taken?

drowsy mango
#

i don't know where to set that

#

I am not good with middleware

brazen wasp
#

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'

drowsy mango
#

oh I remove that?

brazen wasp
#

you need to not parse the json for webhook requests

drowsy mango
#

where to put server log to get the original url

brazen wasp
#

In this block, on each codepath

drowsy mango
#

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

brazen wasp
#

Sure, good luck!

#

Tomorrow just summarize any new issue in new message in #dev-help

drowsy mango
#

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?

brazen wasp
#

Font is not currently a customizable detail of Checkout

drowsy mango
#

ohh

drowsy mango
#

sir I will talk to you tomorrow morning

#

thanks for helping me

#

my problem is 75% done