#josephmalam - webhook errors

1 messages · Page 1 of 1 (latest)

raw hatch
#

Hello. One moment while I catch up here

dusk hamlet
#

Hello, sure, thank you

raw hatch
#

That's a common node error. It happens when some library or piece of code modifies the inbound request body

dusk hamlet
#

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();
});
raw hatch
#

Gotcha. Are you just running this on your local machine?

dusk hamlet
#

Nope, trying to get it working on production server now

raw hatch
#

Ah ok

#

So this works fine locally?

dusk hamlet
#

I haven't actually tested it locally, as I'm not sure how to get the hook to connect to localhost

raw hatch
#

That will help you debug the error

dusk hamlet
#

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?

raw hatch
#

What OS do you have?

dusk hamlet
#

macOS

raw hatch
#

And you're trying to install via homebrew?

#

with this: brew install stripe/stripe-cli/stripe

dusk hamlet
#

Yep

raw hatch
#

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

dusk hamlet
#

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?

raw hatch
#

First try exiting terminal then opening a fresh window

#

Does it work then

dusk hamlet
#

Nope 😦

raw hatch
#

Where did you install

#

It's probably not on your path

dusk hamlet
#

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

raw hatch
#

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

dusk hamlet
#

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

raw hatch
#

You'll point the listen command with forward-to to your webhook endpoint running locally

lime summit
#

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)

dusk hamlet
#

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?

lime summit
#

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

dusk hamlet
#

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
      );
lime summit
#

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)

dusk hamlet
#

Might that be bodyparser?

let bodyParser = require('body-parser');
lime summit
#

Yep, body parser can do that. How is it applied?

dusk hamlet
#
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?

lime summit
dusk hamlet
#

Ok great, thank you for the help!

lime summit
#

NP!