#paras-checkout

1 messages · Page 1 of 1 (latest)

daring quarry
white briar
#

@daring quarry how can i attach user id with subsription?

daring quarry
daring quarry
white briar
daring quarry
#

you can use metadata on the CheckoutSession

white briar
daring quarry
#

yes

white briar
#

what is event name when user charged on subscription?

white briar
#

@daring quarry on which event of webhook i get user paid successfully?

daring quarry
#

the link I posted above tells you. It's checkout.session.completed for the first payment and then you listen to invoice.paid for the recurring payments.

white briar
#

thank you so much ❤️

#

for listening webhook what i have to do? just adding url in webhook

daring quarry
#

I mean that is part of it. You have to host a server and write code that handles the incoming webhook and does things with it for your business requirements, and replies back to Stripe.

white briar
#

i am getting error 401

daring quarry
white briar
#

      console.log(req.body)
    })
#

no logs came

daring quarry
#

that means your server returned 401.

#

probably your server needs a username and password ,or some other form of authentication?

#

you need to disable that for the webhook URL since that's a request that's going to come from Stripe's servers and won't be authenticated.

white briar
#

i have to give endpoint secret?

daring quarry
#

can you clarify?

white briar
cyan slate
#

👋 taking over for my colleague. Let me catch up.

white briar
#

thank you

#
      const payload = request.body;
      const sig = request.headers['stripe-signature'];
    console.log(req.body)
      let event;
    
      try {
        event = stripe.webhooks.constructEvent(payload, sig, "we_1LUp1xSCn57i63EAbmeRRn47");
      } catch (err) {
        return response.status(400).send(`Webhook Error: ${err.message}`);
      }
    
      response.status(200);
    });
    ```
cyan slate
#

as my colleague mentioned, your app is most likely protected by an authentication middleware. that shouldn't be the case for the /webhook endpoint

cyan slate
#

I'm not familiar, what is gitpod?

white briar
cyan slate
#

regardless of your IDE I'm talking about your code

cyan slate
#

I'll take a look

#

I haven't forgotten about you

#

if you do a simple curl or a Post request to that endpoint you will see that it fails with the 401 Unauthorized message

#

this is either because of special permissions requested by the gitpod application and you need to configure it in a way to accept Unauthorized messages or in you're code you need to find the authentication middleware and apply it to all your application except the /webhooks route

#

let me know if you need any more help

white briar
fickle briar
#

Hi! I'm taking over this thread.

white briar
#

thank you

fickle briar
#

As mentioned earlier, if your server returns a 401 error, it probably means that your server needs a username and password, or some other form of authentication.

white briar
fickle briar
#

You need to check your server configuration. From Stripe point of view we do send you the event, but your server is returning a 401. So you need to fix that on your end.

white briar
#

now getting StripeSignatureVerificationError

#
    console.log(req.body)
  
    const payload = req.body;
    const sig = req.headers['stripe-signature'];
    let event;

    try {
      event = stripe.webhooks.constructEvent(payload, sig, "we_1LUp1xSCn57i63EAbmeRRn47");
    } catch (err) {
      console.log(err)
      return res.status(200).send(`Webhook Error: ${err.message}`);
    }

    res.status(200);
  });
fickle briar
#

The third parameter for the constructEvent should be the webhook endpoint secret, which should like this: whsec_xxxx. Here it looks like you passed the webook endpoint ID.

white briar
#

nvm got it

white briar
fickle briar
#

Can you log the values of payload and sig to see if they are defined?

#

Also can you tripe check that you used the correct whsec_xxx?

white briar
#

ohk

#

const sig = req.headers['stripe-signature']

#

this receving undefined

fickle briar
#

That's odd. You get that error when Stripe sent your server a webhook event?

#

Just to be clear: if you access your webhook URL directly yourself, then it's expected to get that error. It's Stipe who should be accessing your webhook endpoint.

white briar
fickle briar
#

Can you try to replace req.headers['stripe-signature'] by req.headers['Stripe-Signature']

white briar
#

ohk

fickle briar
#

And is sig still undefined?

white briar
fickle briar
#

got it! So we are making progress. I think the remaining issue is with your body.

#

you need to make sure that req.body is the raw body for the signature to match.

fickle briar
#

So I think you need to replace this bodyParser.raw({ type: 'application/json' }) by this express.raw({type: 'application/json'}).

fickle briar
#

Can you log the payload variable, to see if it's a JSON object or a raw string?

white briar
#

api_version: '2022-08-01',

fickle briar
#

So it means it's not the raw body, that's why it's failing.

white briar
#
 const header = stripe.webhooks.generateTestHeaderString({
      payload: payload,
      secret,
    });
cyan slate
#

👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!