#ctech-webhooks

1 messages · Page 1 of 1 (latest)

tender falcon
#

hi, reading...

indigo field
#

Hy, please let me know if you need any other detail.

tender falcon
#

can you share an example of some event ID evt_xxx that is not doing what you expect? Are you saying that sometimes you only get customer.subscription.updated and not others? That's quite normal though!

Calles end before that.
what does that mean?

indigo field
#

at stripe end it shows every event get called and but the inner function that handle that event didn't complete its functionality.

#

``

#

can I share my code with you?

tender falcon
#

I'm not sure how anyone except yourself or folks at your company could help debug that though? Stripe is only able to see if we deliver the event or not, what your code does with it after that is something you as the developer of your company would debug.

tender falcon
indigo field
#

yes you are absolutely right, but when I debug line by line on my local system it works fine

#

but same code didn't work on production so my concern was may be stripe end event before it perform my fuctionality

tender falcon
#

if you have an evt_xxx ID I can tell you if we delivered it from our server as best as I can.

indigo field
#

this is the webhook code that called from stripe

exports.stripeWebhook = (req, res) => {
const event = req.body;
try {
// Handle the event
const starttime = new Date();
switch (event.type) {
case 'customer.subscription.updated':
const subscription = event.data.object;
// Then define and call a method to handle the updated subscription
updatedSubscription(subscription);
res.json({received: true, 'updatedSubscription': true,'starttime': starttime,'time': new Date()});
break;
case 'customer.created':
const customer = event.data.object;
// Occurs whenever a new customer is created.
addCustomrIdToUser(customer);
res.json({received: true, 'addCustomrIdToUser': true, 'starttime': starttime, 'time': new Date()});
break;
case 'customer.subscription.created':
const newSubscription = event.data.object;
// Then define and call a method to handle the newSubscription
addSubscriptionToDB(newSubscription);
res.json({received: true, 'addSubscriptionToDB': true,'starttime': starttime,'time': new Date()});
break;
case 'invoice.payment_succeeded':
const invoice = event.data.object;
// Then define and call a method to handle the successful attachment of a PaymentMethod.
updatedSubscriptionStatus(invoice);
res.json({received: true, 'updatedSubscriptionStatus': true,'starttime': starttime,'time': new Date()});
break;
// ... handle other event types
default:
console.log(Unhandled event type ${event.type});

}

} catch (e) {
res.status(400).send(e.message);
}
}

it call following four inner functions

indigo field
tender falcon
#

that's a lot of code, anything in particular you want input on?

#

have you looked at logs on your server for any errors?

indigo field
#

yes in your documentation it was written to send response 2xx before any complex functionality so I'm sending response before I call function to save data in my db

tender falcon
#

makes sense! but maybe your database access is failing in some way?

indigo field
#

but each time I make request "Buy subscription" it performs first event correctly then sometimes 2nd and 3rd event also

#

but when I request for "cancel membership" and it call just one event "customer.subscription.updated" the related handler function executes but never perform db opration

#

I can see my console log messages in netlify logs it nevere throws any error just end before I call my db opertaion

tender falcon
#

I can't really tell you anything about your own database

indigo field
#

any code structure that make stripe event waits until my functionality done?

tender falcon
#

well you have to respond within about 30 seconds, so that's why we tell you not to do that.

#

anyway that is not the answer. The answer here is for you to add logs and debugging to your code and track what is happening on the production server to track down the issue.

indigo field
#

yes I'm tracking logs on production and it didn't throw any error just stops before my db operation starts

#

and works fine on local

rancid kindle
#

Hey taking over from @tender falcon – let me know if there's any specific questions I can answer

indigo field
#

Hy @rancid kindle , there is strange issue I'm facing, I'm listening for four stripe events and those then call handler function and each function have one db operation(create, update) to perform. But sometimes my function run correctly and sometimes not.

#

sometimes it saves data in my database and sometimes skip

#

i'm debugging the issue on localhost and there isn't any error

rancid kindle
#

Taking a look at some events

#

So for evt_1KNIH2F22dA0lj4q3mcizPCy. It was a invoice.payment_succeeded event, which will have triggered your updatedSubscriptionStatus function

#

The issue is you're calling that synchronously so res.json will always send irregardless of success/failure of the updatedSubscriptionStatus call

#

So something is throwing an error in updatedSubscriptionStatus, you need to work through that with logs to debug the issue