#paras-checkout
1 messages · Page 1 of 1 (latest)
https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=checkout should explain this in detail, what part are you stuck on?
I think the error is self-explanatory, can I help?
what does that mean?
like when user subscribe something i want attach user id (discord user) with that object
you can use metadata on the CheckoutSession
i.e. add metadata to the CheckoutSession when you create it, with any user information you need, and then you look for that metadata while handling the successful webhook https://stripe.com/docs/payments/checkout/fulfill-orders
and can i acess that metada on webhook?
yes
what is event name when user charged on subscription?
@daring quarry on which event of webhook i get user paid successfully?
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.
thank you so much ❤️
for listening webhook what i have to do? just adding url in webhook
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.
i am getting error 401
any context on that?
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.
i have to give endpoint secret?
can you clarify?
like stripe didn't posting req on my endpoint
👋 taking over for my colleague. Let me catch up.
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);
});
```
as my colleague mentioned, your app is most likely protected by an authentication middleware. that shouldn't be the case for the /webhook endpoint
i am using gitpod
I'm not familiar, what is gitpod?
online IDE that use vs code
regardless of your IDE I'm talking about your code
yes there is no authorization
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
yes i did same thing still getting 401 error
Hi! I'm taking over this thread.
thank you
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.
but all get request works fine
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.
i fixed this
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);
});
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.
from where can i get secret?
nvm got it
i added secret still same error coming
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?
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.
when i click on resend it sending headers array as undefined
Can you try to replace req.headers['stripe-signature'] by req.headers['Stripe-Signature']
ohk
And is sig still undefined?
first line of output is sig
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.
how can i check it is in raw?
So I think you need to replace this bodyParser.raw({ type: 'application/json' }) by this express.raw({type: 'application/json'}).
i did still same issue coming
Can you log the payload variable, to see if it's a JSON object or a raw string?
yes it's json object
So it means it's not the raw body, that's why it's failing.
brother i fixed that i have to pass this as a second parameter
const header = stripe.webhooks.generateTestHeaderString({
payload: payload,
secret,
});
👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!