#cnguyen85
1 messages · Page 1 of 1 (latest)
Hi
I check the time speed of my function becase it was slow
and I see that it's because the following request take 5000ms
await stripe.customers.update(
stripeUserId,
{ metadata: metadataCustomerInfos }
);
DO you know why ? Is it normal ?
Hi. Let me help you with this.
Could you please share the Request ID req_xxx? https://support.stripe.com/questions/finding-the-id-for-an-api-request
req_AS16ctCVBmZLZL
Are you using Shopify?
it's pssible bot not for this code
why ?
Do you know why it's slow to update metadata ?
is it normal ?
No, that seems to long. Let me check.
And I put the timer only for this function
try {
const startC = Date.now();
await stripe.customers.update(
stripeUserId,
{ metadata: metadataCustomerInfos }
);
const endC = Date.now();
console.log(Execution time C: ${endC - startC} ms);
} catch (error) {
console.log("Stripe error to update metadata");
}
Please give me a few more minutes to investigate
ok
👋 taking over for my colleague. Let me catch up.
where are you exactly executing that code?
in a nodejs lambda
I mean in your webhook endpoint?
or where in your code?
when are you executing this code, that's basically what I want to understand
in AWS
AWS Lambda
So it's host in a AWS lambda in nodejs
and I call the endpoint with postman for example
but in the unfction, there is 3 calls to Stripe API
and it's only this one takes 5s
would you mind sharing all the code that runs inside of your lambda?
o
k
But with the request id, you can check how long time it takes to execute tue query ?
exports.handler = async (event) => {
const { stripeUserId = "", merchantId = "", CMS = "", slot = "post_purchase", websiteCustomerId = "" } = event.arguments
try {
const priceIdDiscount = process.env.ENV === 'dev' ? priceIdDiscountStaging : priceIdDiscountProd;
const trialPeriodDays = 7;
let metadataInfos;
let stripePriceId;
metadataInfos = { serviceURL: 'ycoop.co.uk', merchantId: merchantId, CMS: CMS, slot: slot };
stripePriceId = priceIdDiscount;
const startA = Date.now();
// Double check if there is already a subscription
// No status => If no value is supplied, all subscriptions that have not been canceled are returned.
const subscriptions = await stripe.subscriptions.list({
customer: stripeUserId,
price: stripePriceId
});
const endA = Date.now();
console.log(`Execution time A: ${endA - startA} ms`);
// Check if we have a default PM to create the subscription
const customer = await stripe.customers.retrieve(stripeUserId);
const startB = Date.now();
// Create the subscription
await stripe.subscriptions.create({
customer: stripeUserId,
default_payment_method: customer.invoice_settings.default_payment_method,
items: [{ price: stripePriceId }],
metadata: metadataInfos,
trial_period_days: trialPeriodDays
});
const endB = Date.now();
console.log(Execution time B: ${endB - startB} ms);
let metadataCustomerInfos = { websiteCustomerId: websiteCustomerId, merchantId: merchantId, CMS: CMS };
// Update customer info with metadata
// Update Stripe email
try {
const startC = Date.now();
await stripe.customers.update(
stripeUserId,
{ metadata: metadataCustomerInfos }
);
const endC = Date.now();
console.log(`Execution time C: ${endC - startC} ms`);
} catch (error) {
console.log("Stripe error to update metadata");
}
return {
statusCode: 200
};
} catch (error) {
const response = {
statusCode: 400,
error: {
code : 'ERROR-SUBSCRIBE-DISCOUNT-003',
message : 'Error to create the DISCOUNT subscription : ' + error
}
};
return response;
}
};
I'm not sure to understand how it will help you but ok
Yeah the request ID was helpful in seeing how long it took and what was holding the call up. Tarzan was trying to get more context to form a suggestion. Basically, it looks like that call is slower because it is waiting on an object lock on the Customer object for things related to the subscription creation.
Would it make sense for you to set the metadata first and then create the subscription? Or should the metadata only be set if the creation is successful?
Yeah, surprised to see that myself. And if doing this in production isn't feasible, the other option would be to move this call to the webhook event handler for the subscription being created. That way the things that that call was waiting on will have already happened
correct
thank you for your help ! I had never find it without you !
About the ARN for a transaction, can I ask to you ? or I have to ask to the support ?
About getting it for transactions in general? I can check in to how to do that
thank you
For seeing this on refunds, you will need to reach out to our support team https://support.stripe.com/?contact=true
Find help and support for Stripe. Our support site provides answers on all types of situations, including account information, charges and refunds, and subscriptions information. Get your questions answered and find international support for Stripe.