#Daniel M-webhooks
1 messages · Page 1 of 1 (latest)
are you using the raw body?
yes
const rawBody = JSON.stringify(request.body);
```like so?
you can find the code snippet here
you would still use request.body
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
yes
still getting the error with that D:
what's the error?
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...
could you please share the code for updateStripePaymentStatus?
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();
};
how are you testing your code?
do you have an event id you could share please?
which event id do oyu mean or where do i find it?
payment_intent.created [evt_3L7zvgLkVu8CqnKe2MCiHnsy]
also this error occurs ```
StripeSignatureVerificationError: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing
so seems like the raw body passthrough doesnt work
router.post(
"/stripe_webhooks",
express.raw({ type: "application/json" }),
(req, res) => {
updateStripePaymentStatus(req, res);
}
);
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?
yes
have you tried restarting the express server?
yes using nodemon
could you please log the req.body before updateStripePaymentStatus(req,res);?
{
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
how did you log it?
router.post(
"/stripe_webhooks",
express.raw({ type: "application/json" }),
(req, res) => {
console.log(req.body);
updateStripePaymentStatus(req, res);
}
);
it should be a text and not a json object
yes
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?
no you should JSON.stringify(req.body)
thats what i did or not
are you using const rawbody.... in your code now?
yes
you should only use request.body
the raw part is only necessary in the express.raw...
you mean express.raw(rawBody) or what?
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
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}`);
}
...
[400] POST http://localhost:4000/api/payment/stripe_webhooks [evt_3L80DqLkVu8CqnKe3asl6a6D]
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}`);
}
... ```
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
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
probably yeah...
same problem here, the jason just wont get converted..
what is the endpointSecret you're using?
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);
I might mislead you by saying String
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
what I meant is the bytes as is without formatting it or anything
i see
ok, yeah that makes sense now
please let me know if it works or if you need any more help
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
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
possibly, I'm not really familiar with all the details of Express specifically. Are you still having difficulty?