#josephmalam - webhook errors
1 messages · Page 1 of 1 (latest)
Hello, sure, thank you
That's a common node error. It happens when some library or piece of code modifies the inbound request body
Try using the exact code from our integration builder: https://stripe.com/docs/webhooks/quickstart
I'm still getting the same error unfortunately with the exact code from that page:
const endpointSecret = '[hidden]';
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// 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);
}
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
console.log(`PaymentIntent for ${paymentIntent.amount} was successful!`);
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
default:
// Unexpected event type
console.log(`Unhandled event type ${event.type}.`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
Gotcha. Are you just running this on your local machine?
Nope, trying to get it working on production server now
I haven't actually tested it locally, as I'm not sure how to get the hook to connect to localhost
Highly recommend testing locally: https://stripe.com/docs/webhooks/test
That will help you debug the error
If it works locally, then I recommend taking a look at this Github issue thread: https://github.com/stripe/stripe-node/issues/356
I tried to install Stripe CLI, but I get this error:
==> Installing stripe from stripe/stripe-cli
Error: The following formula cannot be installed from bottle and must be
built from source.
Do you know how I can do that?
What OS do you have?
macOS
And you're trying to install via homebrew?
with this: brew install stripe/stripe-cli/stripe
Yep
You can try this:```To install the Stripe CLI on macOS without homebrew:
Download the latest mac-os tar.gz file from https://github.com/stripe/stripe-cli/releases/latest
Unzip the file: tar -xvf stripe_X.X.X_mac-os_x86_64.tar.gz
Optionally, install the binary in a location where you can execute it globally (for example, /usr/local/bin).```
I've installed it, but it still says command not found when I try stripe login
I think it may be because I didn't install globally, how do I do that?
Nope 😦
I opened the file and it installed, first I did it from the Downloads folder, then I did it from my home user folder, then from the desktop, but still says command not found even when running from those paths
If it's just local you could execute with ./stripe if you've cd'd to that directory
Otherwise you'd have to add the binary to a directory that has been added to your path
Or you can add a new directory where you installed it to your path
Ok great, I did that from the desktop and it's authenticated me. I've now tried ./stripe listen, is that what I should do? I resent a request but nothing happened
Use this guide: https://stripe.com/docs/webhooks/test
You'll point the listen command with forward-to to your webhook endpoint running locally
hey again
Yes when stripe listen is running, the console output should show events arriving when you trigger them with various API calls etc. As @raw hatch noted you can optionally forward these to a local server/endpoint.
(this shouldn't be used for production though, only testing)
Hi @lime summit
Thanks, I've got that working now @raw hatch
However, I'm still getting the same error on my localhost logs ("Webhook signature verification failed...")
Just to confirm, the endpoint secret is the same as the "signing secret" on the webhook endpoint dashboard page, right?
Ah yes that's a common challenge. You need to make sure you're using the raw body in the signature verification
Note that when using the CLI listen command it uses a different secret than the one from any of your dashboard-configured endpoints
if youre forwarding to a local server via CLI you need to use the secret the CLI produces
Okay, understood — I've tried that and still get the same error. How do I ensure it's the raw body? Sorry, I'm not entirely sure what that means
Is this not raw with the express.raw part?
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
That should be the raw body -- are you using anything earlier in your express setup that parses json bodies?
(often this is configured earlier in the stack, right after the app/router is initializaed)
Might that be bodyparser?
let bodyParser = require('body-parser');
Yep, body parser can do that. How is it applied?
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
Ohh I just saw a suggestion to put the hook before that ^
And I tried it and it worked
Is that the best way to get around that?
Nice, yes that works, or you can exclude your webhook endpoint from that middleware step, or copy/store the raw body for later usage, eg:
https://github.com/stripe-samples/accept-a-payment/blob/main/custom-payment-flow/server/node/server.js#L18-L28
Ok great, thank you for the help!
NP!