#Daniel M-webhooks

1 messages · Page 1 of 1 (latest)

trail cedar
#

👋 happy to help

dusky vortex
#

awesome!

#

so yeah, it seems like that function literally returns nothing

trail cedar
#

are you using the raw body?

dusky vortex
#

probably not, i see

#

express.raw({type: 'application/json'})

#

u mean this?

trail cedar
#

yes

dusky vortex
#
const rawBody = JSON.stringify(request.body);
```like so?
trail cedar
#

you can find the code snippet here

#

you would still use request.body

dusky vortex
#

okay, does it also work if i forward he function?

#
router.post(
  "/stripe_webhooks",
  express.raw({ type: "application/json" }),
  (req, res) => {
    updateStripePaymentStatus(req, res);
  }
);
#

like that

trail cedar
#

yes

dusky vortex
#

still getting the error with that D:

trail cedar
#

what's the error?

dusky vortex
#
TypeError: Cannot read properties of undefined (reading 'type')
    at exports.updateStripePaymentStatus (/Users/danielmielke/Code/solidcheck/backend/routes/payments/paymentStatus.js:28:17)
    at /Users/danielmielke/Code/solidcheck/backend/routes/payments/payment.js:82:5
    at Layer.handle [as handle_request] (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/route.js:137:13)
    at rawParser (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/node_modules/body-parser/lib/types/raw.js:58:7)
    at Layer.handle [as handle_request] (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:341:12)
    at next (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/danielmielke/Code/solidcheck/backend/node_modules/express/lib/router/index.js:323:13)
[nodemon] app crashed - waiting for file changes before starting...
trail cedar
#

could you please share the code for updateStripePaymentStatus?

dusky vortex
#
const stripe = require("stripe")(
  "sk_test_...
);

const endpointSecret =
  "whsec_...;

const { Webhook } = require("coinbase-commerce-node");

const FileModel = require("../../models/Order");

exports.updateStripePaymentStatus = async (request, response) => {
  const sig = request.headers["stripe-signature"];

  let event;

  try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Handle the event
  switch (event.type) {
    case "payment_intent.succeeded":
      const paymentIntent = event.data.object;

      const doc = await FileModel.findOne({ paymentId: paymentIntent.id });

      doc.paymentStatus = paymentIntent.status;

      doc.save();

      break;

    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`);
  }

  // Return a 200 response to acknowledge receipt of the event
  response.send();
};
trail cedar
#

how are you testing your code?

dusky vortex
#

using ngrok and

#

test webhooks

#

(stripe cli)

trail cedar
#

do you have an event id you could share please?

dusky vortex
#

which event id do oyu mean or where do i find it?

#

payment_intent.created [evt_3L7zvgLkVu8CqnKe2MCiHnsy]

trail cedar
#

the event id evt_xxx is the id of any received event

#

yes thanks 🙂

dusky vortex
#

so seems like the raw body passthrough doesnt work

#
router.post(
  "/stripe_webhooks",
  express.raw({ type: "application/json" }),
  (req, res) => {
    updateStripePaymentStatus(req, res);
  }
);
trail cedar
#

this is what I was expecting to be the issue from the first place

#

but I'm not seeing why it is still not working

#

have you changed the express.raw recently?

dusky vortex
#

yes

trail cedar
#

have you tried restarting the express server?

dusky vortex
#

yes using nodemon

trail cedar
#

sometimes routes get cached

#

it's preferable to kill your server and restart

dusky vortex
#

ok 1 sec

#

still not working

#

aah

trail cedar
#

could you please log the req.body before updateStripePaymentStatus(req,res);?

dusky vortex
#
{
  id: 'evt_3L8047LkVu8CqnKe0sXt5z8F',
  object: 'event',
  api_version: '2020-08-27',
  created: 1654599315,
  data: {
    object: {
      id: 'pi_3L8047LkVu8CqnKe0enEnYQk',
      object: 'payment_intent',
      amount: 1000,
      amount_capturable: 0,
      amount_details: [Object],
      amount_received: 0,
      application: null,
      application_fee_amount: null,
      automatic_payment_methods: [Object],
      canceled_at: null,
      cancellation_reason: null,
      capture_method: 'automatic',
      charges: [Object],
      client_secret: 'pi_3L8047LkVu8CqnKe0enEnYQk_secret_RV5m6uVqvpAugd4bCIdRgDiBr',
      confirmation_method: 'automatic',
      created: 1654599315,
      currency: 'eur',
      customer: null,
      description: null,
      invoice: null,
      last_payment_error: null,
      livemode: false,
      metadata: {},
      next_action: null,
      on_behalf_of: null,
      payment_method: null,
      payment_method_options: [Object],
      payment_method_types: [Array],
      processing: null,
      receipt_email: null,
      review: null,
      setup_future_usage: null,
      shipping: null,
      source: null,
      statement_descriptor: null,
      statement_descriptor_suffix: null,
      status: 'requires_payment_method',
      transfer_data: null,
      transfer_group: null
    }
  },
  livemode: false,
  pending_webhooks: 2,
  request: {
    id: 'req_Fc0aFmpEnM2JaW',
    idempotency_key: '28f515ff-69ca-4852-8371-8dadce8c85da'
  },
  type: 'payment_intent.created'
}
#

its json

trail cedar
#

how did you log it?

dusky vortex
#
router.post(
  "/stripe_webhooks",
  express.raw({ type: "application/json" }),
  (req, res) => {
    console.log(req.body);
    updateStripePaymentStatus(req, res);
  }
);
trail cedar
#

it should be a text and not a json object

dusky vortex
#

yeah thats what i thought...

#

you mean a string?

trail cedar
#

yes

dusky vortex
#

const rawBody = JSON.stringify(request.body);

#

event = stripe.webhooks.constructEvent(rawBody, sig, endpointSecret);

#

{"id":"evt_3L80AILkVu8CqnKe38fbOupQ","object":"event","api_version":"2020-08-27","created":1654599698,"data":{"object":{"id":"pi_3L80AILkVu8CqnKe3vjl6Lvo","object":"payment_intent","amount":1000,"amount_capturable":0,"amount_details":{"tip":{}},"amount_received":0,"application":null,"application_fee_amount":null,"automatic_payment_methods":{"enabled":true},"canceled_at":null,"cancellation_reason":null,"capture_method":"automatic","charges":{"ob....

#

thats the output from the stringify

#

should be the same shouldnt it?

trail cedar
#

no you should JSON.stringify(req.body)

dusky vortex
#

thats what i did or not

trail cedar
#

are you using const rawbody.... in your code now?

dusky vortex
#

yes

trail cedar
#

you should only use request.body

#

the raw part is only necessary in the express.raw...

dusky vortex
#

you mean express.raw(rawBody) or what?

trail cedar
#

yes

#

this is the only part that was missing from your code

#

oh sorry no it's the express.raw({ type: "application/json" }), that was missing

dusky vortex
#

yeah I have that

#

so right now its

#
router.post(
  "/coinbase_webhooks",
  express.json({ type: "application/json" }),
  (req, res) => {
    updateCryptoPaymentStatus(req, res);
  }
);
#
exports.updateStripePaymentStatus = async (request, response) => {
  const rawBody = JSON.stringify(request.body);
  console.log(rawBody);
  const sig = request.headers["stripe-signature"];

  let event;

  try {
    event = stripe.webhooks.constructEvent(rawBody, sig, endpointSecret);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
  }
... 
trail cedar
#

your code should look like this

router.post( "/coinbase_webhooks", express.raw({ type: "application/json" }), (req, res) => { updateCryptoPaymentStatus(req, res); } );

#
  console.log(rawBody);
  const sig = request.headers["stripe-signature"];

  let event;

  try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
  }
... ```
dusky vortex
#

yeah that´s what I had before... But it didnt work either D:
Imma try to put the function straight into the route

#

idk why express wont turn it into text

trail cedar
#

yeah it's really odd

#

could you please check that the endpointSecret is correct

#

maybe we're looking at the problem for the wrong angle

dusky vortex
#

probably yeah...

dusky vortex
trail cedar
#

what is the endpointSecret you're using?

dusky vortex
#

whsec_f3f7a8e66a9ef6237476f35ebcd8bc5b5ed27eb55d43fb64095872c24ef0d84b

#

I mean the only part that needs to be a string is the body in here right?

#

stripe.webhooks.constructEvent(request.body, sig, endpointSecret);

trail cedar
#

I might mislead you by saying String

dusky vortex
#

or the only reason we do this

#

ooh ok

#

bc I did the same thing before with coinbase before

#

where I also needed the raw body

#

and there i did it with const rawBody = JSON.stringify(request.body);like i wrote before

trail cedar
#

what I meant is the bytes as is without formatting it or anything

dusky vortex
#

i see

trail cedar
#

JSON.stringify would not work

#

you need the raw middleware of express

dusky vortex
#

ok, yeah that makes sense now

trail cedar
#

please let me know if it works or if you need any more help

dusky vortex
#

app.use(bodyParser.urlencoded({ extended: false }));

#

could this be a problem?

#
<Buffer 7b 0a 20 20 22 69 64 22 3a 20 22 65 76 74 5f 33 4c 38 30 77 7a 4c 6b 56 75 38 43 71 6e 4b 65 32 68 31 54 39 58 73 4e 22 2c 0a 20 20 22 6f 62 6a 65 63 ... 2002 more bytes>
#

would this be the right text?

#

format

trail cedar
#

yes

#

it should be the raw bytes

dusky vortex
#
app.post(
  "/webhooks",
  express.raw({ type: "application/json" }),
  (request, response) => {
    console.log(request.body);
  }
);
app.use(bodyParser.urlencoded({ extended: false }));
#

ok so u see the second app.use with a bodyParser in it

#

seems like that middleware makes it for the following middlewares impossible to execute

cloud mortar
#

possibly, I'm not really familiar with all the details of Express specifically. Are you still having difficulty?

dusky vortex
#

no, seems like that was the issue

#

everything working now