简体   繁体   中英

Create subscription with Stripe in Lambda function fails with internal server error

I'm having issues creating a subscription with stripe using a lambda function, this code runs fine locally but it is returning "internal server error" when posting to lambda, there are no logs in cloudwatch.

code for lambda function is below

    exports.handler = async (event, context, callback) => {
  // GET ITEMS
  const { productId, customerId, currency = "gbp" } = JSON.parse(event.body);
  let paymentMethods;
  
try {
  paymentMethods = await stripe.paymentMethods.list({
  customer: customerId,
  type: 'card',
});
} catch (e) {
   return {
    statusCode: 200, // http status code
    body: JSON.stringify({
     "error" : true,
     "msg" : "error retrieving payment method",
      "data" : null
    })
  };
}

console.log(paymentMethods)
  


  
  
// Setup subscription
  try {
    const subscription = await stripe.subscriptions.create({
      customer: customerId,
      default_payment_method: paymentMethods.data[0].id,
      items: [
        {price: productId},
      ],
    });
    
    console.log(subscription)
    
  
  // SUCCESS
  return {
    statusCode: 200, // http status code
    body: JSON.stringify({
      "data" :  "success",
      "error" : false,
      "msg" : null
    })
  };
  
  // ERROR
} catch (e) {
  console.log(e);
    return {
    statusCode: 200, // http status code
    body: JSON.stringify({
     "error" : true,
     "msg" : "error",
      "data" : null
    })
  };
}
  
  
};

I can see the log for the payment methods but the log for the subscription is never shown, this does, however, create the subscription correctly in the stripe portal against that customer but no response is found from aws just a 500 internal server error.

any help would be greatly appreciated.

turns out I had my lambda function set to a 3-second timeout, as soon as I increased it the function worked fine, hopefully. this might help someone out with the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM