#testDeveloper

1 messages · Page 1 of 1 (latest)

warm remnantBOT
polar pumice
#

hi! any extra context you can share beyond our last thread earlier today?

keen shard
#

yes the event gets triggered

#

but the conditions after that don't work

polar pumice
#

have you added logs to see what the code is doing, how far it gets, and what values the variables have?

keen shard
#

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

polar pumice
#

unfortunately none of that means much to me, can you share some actual logs or screenshots and exact code?

keen shard
#

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))); }

polar pumice
#

ok. And which of those console.log statements result in something getting logged, and what value do they log?

keen shard
#

the whole req.body

polar pumice
#

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(...)?

keen shard
#

i removed that
stripe.webhooks.constructEvent(...)

#

it was not working properly

polar pumice
#

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.

keen shard
#

Not able to get a log after value = stripe.webhooks.constructEvent( payloadString, header, WEBHOOK_ENDPOINT_SECRET, ); console.log('value', value);

#

construct event

raw lance
#

So it means that calling stripe.webhooks.constructEvent returns an error. What's the error exactly in your server logs?

keen shard
#

No error

#

but after that nothing in my code executes

raw lance
#

Before calling that function, can you log payloadString, header, and WEBHOOK_ENDPOINT_SECRET?

keen shard
#

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

raw lance
#

Can you share your code with the console.log? It's hard to follow what exactly you are doing.

keen shard
#

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);

raw lance
#

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?

keen shard
#

what is extra ?

raw lance
#

Also why are you calling stripe.webhooks.generateTestHeaderString?

keen shard
#

as per this reference

#

because it says webhook verification failed

raw lance
keen shard
#

it thows signature verification failed

raw lance
#

Can you paste your code correctly so it's easy to read on Discord?

keen shard
#

can you let me the option?

#

through which it can be done

raw lance
#

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)

keen shard
#

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('.......');

raw lance
#

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);
keen shard
#

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

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

#

event = req.body they are same

raw lance
#

Thanks, give me a few minutes to look into this.

keen shard
#

'evt_1MKBCT4YkynKp6Nj4rHYB9CM'

#

yes sure

raw lance
#

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.

keen shard
#

whsec_sDP5P3pxG0olENtKd6rpeNAiOemYrIgL i am using the same i got from dashboard

#

we_1MJjiFGAMQUY18Un5khJot2A

raw lance
#

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.

keen shard
#

shall i delete one

#

and try ?

raw lance
#

The event you shared evt_1MKBCT4YkynKp6Nj4rHYB9CM was send twice to your server. It suceeded once and failed once.

#

shall i delete one
yes

keen shard
#

deleted one

#

still the same

#

I am resending it again and again for testing

#

@raw lance

jagged valve
#

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?

keen shard
#

yes double checked

jagged valve
#

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?

keen shard
#

no it doesn't work on the development server

#

it works only on local server

jagged valve
#

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?

keen shard
#

got rectified

#

but now

jagged valve
#

Interesting, maybe the old webhook secret was cached somehow?

keen shard
#

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 ?

jagged valve
#

In what way is it not working? Are you getting some kind of error?

keen shard
#

All i can affirm is that if i log inside that loop that comes

#

that means i am going inside that loop

jagged valve
#

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

keen shard
#

it is like a normal api right ?

#

it supports async callback

jagged valve
#

Our node library does I believe. Are you running in to some issue with your calls with async callbacks?

keen shard
#

i think so but why they are working on local and not on dev

jagged valve
#

In what way are they not working?

keen shard
#

they are not executing

jagged valve
#

So they never return?

keen shard
#

yes

jagged valve
#

The code just gets stuck there indefinitely?

keen shard
#

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

jagged valve
#

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?

keen shard
#

do they work without sync ?

#

i mean without async ?

#

stripe.webhooks.constructEvent for example this works

#

right?

jagged valve
#

That call does look to be synchronous in our sample code.

keen shard
#

can other be used synchronously ?

#

ideally does webhook work with async stuff ?

jagged valve
#

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.

keen shard
#

only in webhook

#

otherwise it works

jagged valve
#

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

keen shard
#

ok