#codeninja

1 messages · Page 1 of 1 (latest)

sick creekBOT
toxic yew
#

The error pretty much said it isn't it 🙂

#

The Payload need to be a string or a Buffer

neat folio
#

Yup tried to convert to string as well as buffer but it then said

No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

#

let event = stripe.webhooks.constructEvent(
JSON.stringify(request.body),
sig,
endpointSecret
);

#

const jsonString = JSON.stringify(request.body);
const myBuffer = Buffer.from(jsonString, 'utf-8');

  let event = stripe.webhooks.constructEvent(
    myBuffer,
    sig,
    endpointSecret
  );
toxic yew
#

if you printout JSON.stringify(request.body) what do you have?

#

And is that node.js?

neat folio
#

That is what I have actually used

toxic yew
#

Looks good enough. How about the downloaded example code?

neat folio
#

app.post(
'/api/webhook',
express.raw({ type: 'application/json' }),
(request, response) => {
let event = request.body;
console.log(
'EVENNNNNNNNNNNNNNT request.body ',
JSON.stringify(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();

}
);

#

Just fyi it also throws an error even the request.body is stringify as I have mentioned earlier

sick creekBOT
ivory radish
#

Hey! Taking over for my colleague. Let me catch up.

#

When dealing with webhook event, you need to do nothing to the body request

neat folio
#

Hey thanks @ivory radish but I did not do anything to body request on my initial tests

ivory radish
#

There must be some middelware in your express app that does modify the request

#

try to run the file I shared with you in your tests

neat folio
#

I did try that and it works, I think you are correct and we have middleware some where. I can take it form here thanks for your help!