#cnguyen85

1 messages · Page 1 of 1 (latest)

orchid bobcatBOT
bronze cloak
#

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 ?

severe sand
#

Hi. Let me help you with this.

bronze cloak
#

req_AS16ctCVBmZLZL

severe sand
#

Are you using Shopify?

bronze cloak
#

it's pssible bot not for this code

#

why ?

#

Do you know why it's slow to update metadata ?

#

is it normal ?

severe sand
#

No, that seems to long. Let me check.

bronze cloak
#

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

severe sand
#

Please give me a few more minutes to investigate

bronze cloak
#

ok

brittle star
#

👋 taking over for my colleague. Let me catch up.

#

where are you exactly executing that code?

bronze cloak
#

in a nodejs lambda

brittle star
#

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

bronze cloak
#

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

orchid bobcatBOT
brittle star
#

would you mind sharing all the code that runs inside of your lambda?

bronze cloak
#

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

boreal vale
#

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?

bronze cloak
#

mmmm let me check

#

ok it's faster in this way. Weird

boreal vale
#

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

bronze cloak
#

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 ?

boreal vale
#

About getting it for transactions in general? I can check in to how to do that

bronze cloak
#

thank you

boreal vale
#

For seeing this on refunds, you will need to reach out to our support team https://support.stripe.com/?contact=true

bronze cloak
#

ok and it's only for refund ?

#

for me it's for transaction too

orchid bobcatBOT