#testDeveloper
1 messages · Page 1 of 1 (latest)
hi! any extra context you can share beyond our last thread earlier today?
have you added logs to see what the code is doing, how far it gets, and what values the variables have?
yes have added logs but the code where i am implementing mongodb related queries that portion dosen't work
also there is something in the logs
like connection:close
after event triggers and the log of req.body
i think i making a variable for req.body
const event = req.body
is that where i am wrong?
shall i pass req.body directly everywhere?
Also , the domain is http could that be the reason?
@polar pumice
unfortunately none of that means much to me, can you share some actual logs or screenshots and exact code?
const event = req.body; // Handle the event console.log('event', event); // console.log('event', event.type); if (event.type) { const retrievePayment = await StripeService.BalancePaymentTransactionList( { payoutId: event.data.object.id, stripeAccount: event.account }, ); console.log('retrivePayment', retrievePayment); const paymentIds = retrievePayment.data.map(payment => payment.source); console.log('paymentIds', paymentIds); await TransactionModel.updateMany( { paymentId: { $in: paymentIds } }, { $set: { payoutDate: new Date(new Date(0).setUTCSeconds(event.data.object.arrival_date)), }, }, ); console.log('payout.date', new Date(new Date(0).setUTCSeconds(event.data.object.arrival_date))); }
ok. And which of those console.log statements result in something getting logged, and what value do they log?
the whole req.body
not sure what that means
this line
console.log('event', event);
does that execute and log something?
why does your code not call stripe.webhooks.constructEvent(...)?
well let's fix that first.
what about it was not working properly?
Repeating myself from a couple of hours ago:
[09:59]karllekko: are you using the right signing secret whsec_xxx?
[10:00]karllekko: to debug this you'll need to add logging to log every value you pass to constructEvent (the payload, the secret, and the signature header) and then we can try to have a look at what part is wrong
if you've added those logs and can share what they log I can try to help you.
Not able to get a log after value = stripe.webhooks.constructEvent( payloadString, header, WEBHOOK_ENDPOINT_SECRET, ); console.log('value', value);
construct event
So it means that calling stripe.webhooks.constructEvent returns an error. What's the error exactly in your server logs?
Before calling that function, can you log payloadString, header, and WEBHOOK_ENDPOINT_SECRET?
payloadString {
"id": "evt_1MKBCT4YkynKp6Nj4rHYB9CM",
"object": "event",
"account": "acct_1MJVrI4YkynKp6Nj",
"api_version": "2022-08-01",
"created": 1672278389,
"data": {
"object": {
"id": "po_1MKB404YkynKp6NjFQaobvV2",
"object": "payout",
"amount": 990000,
"arrival_date": 1672272000,
"automatic": true,
"balance_transaction": "txn_1MKB414YkynKp6NjjlLN6ZwH",
"created": 1672277864,
"currency": "usd",
"description": "STRIPE PAYOUT",
"destination": "ba_1MJVrR4YkynKp6Nj0Zt1wEj2",
"failure_balance_transaction": null,
"failure_code": null,
"failure_message": null,
"livemode": false,
"metadata": {},
"method": "standard",
"original_payout": null,
"reversed_by": null,
"source_type": "card",
"statement_descriptor": null,
"status": "paid",
"type": "bank_account"
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": null,
"idempotency_key": null
},
"type": "payout.paid",
"format": "request",
"path": "/webhook/endpoint",
"headers": {
"host": "d8dd-210-16-81-119.in.ngrok.io",
"user-agent": "Stripe/1.0 (+https://stripe.com/docs/webhooks)",
"content-length": "1048",
"accept": "/; q=0.5, application/xml",
"cache-control": "no-cache",
"content-type": "application/json; charset=utf-8",
"stripe-signature": "t=1672318838,v1=2c5a91a4d34db3157ea30f5eed19597a55346daa8e5ad3afed7822a89e11c2ba,v0=102537324e5534f35792572bbf9be1b31e8f9dc4cef964d1da948a00d3513409",
"x-forwarded-for": "54.187.216.72",
"x-forwarded-proto": "https",
"accept-encoding": "gzip"
},
"timestamp": "2022-12-29T13:00:38.752Z"
}
not getting logs after payloadstring
const header = stripe.webhooks.generateTestHeaderString({ payload: payloadString, WEBHOOK_ENDPOINT_SECRET, });
not getting logs after this line
Can you share your code with the console.log? It's hard to follow what exactly you are doing.
const endpointSecret = WEBHOOK_ENDPOINT_SECRET; const stripe = new Stripe(STRIPE_SECRET_KEY); const event = req.body; console.log('event', event); const payloadString = JSON.stringify(req.body, null, 2); console.log('payloadString', payloadString); const header = stripe.webhooks.generateTestHeaderString({ payload: payloadString, WEBHOOK_ENDPOINT_SECRET, }); console.log('header', header); console.log('webhookSecret', WEBHOOK_ENDPOINT_SECRET); let value; // 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 = req.headers['stripe-signature']; try { value = stripe.webhooks.constructEvent( payloadString, header, WEBHOOK_ENDPOINT_SECRET, ); console.log('value', value); } catch (err) { console.log('⚠️ Webhook signature verification failed.', err.message); } } console.log('value', value);
Pasting your code in a more legible format:
const endpointSecret = WEBHOOK_ENDPOINT_SECRET;
const stripe = new Stripe(STRIPE_SECRET_KEY);
const event = req.body;
console.log('event', event);
const payloadString = JSON.stringify(req.body, null, 2);
console.log('payloadString', payloadString);
const header = stripe.webhooks.generateTestHeaderString({
payload: payloadString,
WEBHOOK_ENDPOINT_SECRET,
});
console.log('header', header);
console.log('webhookSecret', WEBHOOK_ENDPOINT_SECRET);
let value;
// 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 = req.headers['stripe-signature'];
try {
value = stripe.webhooks.constructEvent(
payloadString,
header,
WEBHOOK_ENDPOINT_SECRET,
);
console.log('value', value);
} catch (err) {
console.log('⚠️ Webhook signature verification failed.', err.message);
}
}
value = stripe.webhooks.constructEvent(
payloadString,
header,
WEBHOOK_ENDPOINT_SECRET,
);
Here it looks like you have an extra , that you need to delete maybe?
what is extra ?
Also why are you calling stripe.webhooks.generateTestHeaderString?
as per this reference
because it says webhook verification failed
I would recommend to follow this tutorial instead: https://stripe.com/docs/webhooks/quickstart
it thows signature verification failed
Can you paste your code correctly so it's easy to read on Discord?
Can you add console.log to your code, something like this:
console.log(req.body);
console.log("---");
console.log(signature);
console.log("---");
console.log(WEBHOOK_ENDPOINT_SECRET)
value = stripe.webhooks.constructEvent(
req.body,
signature,
WEBHOOK_ENDPOINT_SECRET,
);
And share the three values you see (req.body, signature, WEBHOOK_ENDPOINT_SECRET)
const stripe = new Stripe(STRIPE_SECRET_KEY);
const event = req.body;
console.log('req', req.body);
console.log('.......');
console.log('event', event);
console.log('.......');
console.log('webhookSecret', WEBHOOK_ENDPOINT_SECRET);
console.log('.......');
let value;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (WEBHOOK_ENDPOINT_SECRET) { /
/ // Get the signature sent by Stripe
const signature = req.headers['stripe-signature'];
console.log('signature', signature);
console.log('.......');
try {
value = stripe.webhooks.constructEvent(
req.body,
signature,
WEBHOOK_ENDPOINT_SECRET,
);
console.log('value', value);
console.log('.......');
} catch (err) {
console.log('⚠️ Webhook signature verification failed.', err.message); } } console.log('value', value);
console.log('.......');
So can you share the values returned by this:
console.log('req', req.body);
console.log('.......');
console.log('event', event);
console.log('.......');
console.log('webhookSecret', WEBHOOK_ENDPOINT_SECRET);
And this
console.log('signature', signature);
req {
id: 'evt_1MKBCT4YkynKp6Nj4rHYB9CM',
object: 'event',
account: 'acct_1MJVrI4YkynKp6Nj',
api_version: '2022-08-01',
created: 1672278389,
data: {
object: {
id: 'po_1MKB404YkynKp6NjFQaobvV2',
object: 'payout',
amount: 990000,
arrival_date: 1672272000,
automatic: true,
balance_transaction: 'txn_1MKB414YkynKp6NjjlLN6ZwH',
created: 1672277864,
currency: 'usd',
description: 'STRIPE PAYOUT',
destination: 'ba_1MJVrR4YkynKp6Nj0Zt1wEj2',
failure_balance_transaction: null,
failure_code: null,
failure_message: null,
livemode: false,
metadata: {},
method: 'standard',
original_payout: null,
reversed_by: null,
source_type: 'card',
statement_descriptor: null,
status: 'paid',
type: 'bank_account'
}
},
livemode: false,
pending_webhooks: 1,
request: { id: null, idempotency_key: null },
type: 'payout.paid',
format: 'request',
path: '/webhook/endpoint',
headers: {
host: 'd8dd-210-16-81-119.in.ngrok.io',
'user-agent': 'Stripe/1.0 (+https://stripe.com/docs/webhooks)',
'content-length': '1048',
accept: '/; q=0.5, application/xml',
'cache-control': 'no-cache',
'content-type': 'application/json; charset=utf-8',
'stripe-signature': 't=1672321799,v1=abcb8774e9d99b33d663b949fdd26736415f9bf1c97099d04ab9d5de8448f4cb,v0=1fc68d4a8b348b49e05a6417625d156bbb6b6bea1b3847da2653ab9d01fcb329',
'x-forwarded-for': '54.187.216.72',
'x-forwarded-proto': 'https',
'accept-encoding': 'gzip'
},
timestamp: 2022-12-29T13:49:59.783Z
}
.......
webhookSecret whsec_sDP5P3pxG0olENtKd6rpeNAiOemYrIgL
.......
signature t=1672321799,v1=abcb8774e9d99b33d663b949fdd26736415f9bf1c97099d04ab9d5de8448f4cb,v0=1fc68d4a8b348b49e05a6417625d156bbb6b6bea1b3847da2653ab9d01fcb329
.......
⚠️ Webhook signature verification failed. 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
value undefined
.......
event = req.body they are same
Thanks, give me a few minutes to look into this.
So it looks like you are using the wrong webhook secret key, as was mentioned by my collegue a while ago.
Your webhook secret should look like this whsec_CD...ApjP, and you can find it in your dashboard.
whsec_sDP5P3pxG0olENtKd6rpeNAiOemYrIgL i am using the same i got from dashboard
we_1MJjiFGAMQUY18Un5khJot2A
But you have two webhook endpoint in your dashboard that are pointing to the same URL
So half of your event will succeed and half will fail, since each webhook endpoint is expecting a different webhook secret.
The event you shared evt_1MKBCT4YkynKp6Nj4rHYB9CM was send twice to your server. It suceeded once and failed once.
shall i delete one
yes
deleted one
still the same
⚠️ Webhook signature verification failed. 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
I am resending it again and again for testing
@raw lance
Hey soma had to step out but I can help. Catching up now...
⚠️ Webhook signature verification failed. 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
This error usually either comes from passing the wrong webhook secret or from passing the webhook body in incorrectly. Have you double checked your webhook secret?
yes double checked
Then it sounds like your integration is manipulating the POST request's body before passing it in to our verify function
Are you still getting a behavior where some events succeed and some fail?
If you are using the same code it always works on one server but not the other then it does sound like the incorrect secret key is being used.
To be clear you aren't using the same secret key for both correct?
Interesting, maybe the old webhook secret was cached somehow?
updated environment variables
but
if (event.type === 'payout.paid') {
console.log('event', event.id);
console.log('.......');
const retrievePayment = await StripeService.BalancePaymentTransactionList( { payoutId: event.data.object.id, stripeAccount: event.account }, );
console.log('retrivePayment', retrievePayment.data);
console.log('.......');
const paymentIds = retrievePayment.data.map(payment => payment.source); console.log('paymentIds', paymentIds);
console.log('.......');
await TransactionModel.updateMany( { paymentId: { $in: paymentIds } }, { $set: { payoutDate: new Date(new Date(0).setUTCSeconds(event.data.object.arrival_date)), }, }, ); console.log('payout.paidDate', new Date(new Date(0).setUTCSeconds(event.data.object.arrival_date)));
console.log('.......'); }
after the console.log('event', event.id); it is not working
but working on my local server
and not on dev server
If i remove variable declaration will that help ?
In what way is it not working? Are you getting some kind of error?
All i can affirm is that if i log inside that loop that comes
that means i am going inside that loop
Can you try debugging a bit more to see exactly where and how it fails?
Unfortunately I can't tell that from just this code
Our node library does I believe. Are you running in to some issue with your calls with async callbacks?
i think so but why they are working on local and not on dev
In what way are they not working?
they are not executing
So they never return?
yes
The code just gets stuck there indefinitely?
unless another api endpoint is hit
i am trying to save some data in mongodb
but it dosen't get saved
anthing which has await in front of it does not execute on dev server
Interesting, if this is happening beyond just the Stripe API, it sounds like it may have more to do with your server's setup.
Actually, if you remove the Stripe code here, do the awaits for other code start working again?
do they work without sync ?
i mean without async ?
stripe.webhooks.constructEvent for example this works
right?
That call does look to be synchronous in our sample code.
I mean there are a lot of aspects to that question. If your server is freezing every time anything async is called, even outside of Stripe, I think troubleshooting that should happen before looking in to the best way to work with these Stripe calls synchronously or async.
If you remove all stripe code from this endpoint code and make an async call when this webhook endpoint is his, do things still freeze up?
Basically, if even non-Stripe code is the issue here, it sounds like the root cause of this is outside of the Stripe code here
ok