Home > OS >  TypeError: stripe.customers.listPaymentMethods is not a function
TypeError: stripe.customers.listPaymentMethods is not a function

Time:07-16

When trying to list the payment methods owned by the customer from the backend server, I'm getting this error. Stripe documents have an API on the same name but it doesn't seem to work in my case. I need to fetch all the payment methods in my backend and send them back to the client side for future use.

API reference: https://stripe.com/docs/api/payment_methods/list

Code:

app.post("/savedCards", cors(), async (req, res) => {
  let { customerId, paymentMethodId } = req.body;
   const paymentMethods = await stripe.customers.listPaymentMethods(
   'cus_M3nQBIvz9qykuG',
   {type: 'card'});
   console.log(paymentMethods);

   res.status(200).send(paymentMethods.data[0].card);
  } catch (err) {
    console.log(err);
    res.status(500).json({ message: "Internal server error" });
  }
});

Error:

TypeError: stripe.customers.listPaymentMethods is not a function
    at C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\index.js:72:51
    at Layer.handle [as handle_request] (C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\node_modules\express\lib\router\layer.js:95:5)
\server\node_modules\cors\lib\index.js:214:15)
    at C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\node_modules\cors\lib\index.js:219:13
    at optionsCallback (C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\node_modules\cors\lib\index.js:199:9)     
    at corsMiddleware (C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\node_modules\cors\lib\index.js:204:7)      
    at Layer.handle [as handle_request] (C:\Users\rekha d\OneDrive\Desktop\test_cards\card-section\server\node_modules\express\lib\router\layer.js:95:5)

CodePudding user response:

You need at least version v8.180.0 of the Stripe Node library to list payment methods this way.

CodePudding user response:

The error is telling you that listPaymentMethods isn't a function. It looks like per Stripe docs, it should be stripe.paymentMethods.list, rather than stripe.customers.listPaymentMethods:

const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');

const paymentMethods = await stripe.paymentMethods.list({
  customer: 'cus_9utnxg47pWjV1e',
  type: 'card',
});

Or, as mentioned in the comments below, you can upgrade to the newer version of the stripe-node library where listPaymentMethod is a valid method.

  • Related