Home > Net >  How do you retrieve a list of ALL Stripe paymentMethods?
How do you retrieve a list of ALL Stripe paymentMethods?

Time:11-22

The Stripe API says to use the following if you want a specific customer's payment methods ...

$stripe->customers->allPaymentMethods(
  'cus_Hjrd3I2sHt12Rf',
  ['type' => 'card']
);

... and then shows the following for how to return all payment methods:

$stripe->paymentMethods->all([
  'customer' => 'cus_Hjrd3I2sHt12Rf',
  'type' => 'card'
]);

What I want is this ...

$stripe->paymentMethods->all([
  'type' => 'card'
]);

... however this doesn't work. It spits out ...

{
    "object": "list",
    "data": [],
    "has_more": false,
    "url": "\/v1\/payment_methods"
}

... but does fine when the customer is included (there are thousands of records).

How do I retrieve ALL payment methods, not just a single customers?

Bonus round: How do you constrain all results by date, so I can retrieve all payment methods added within the last X days? The usual ['created' => ['gte' => 1668595343]] doesn't work as expected.

Note: Every other $stripe->BLAH->all( ['created' => ['gte' => 1668595343]] ) I've tried appears to work correctly. Only paymentMethods seems to deviate.

CodePudding user response:

For context, https://stripe.com/docs/api/payment_methods/list only works if you're using Stripe Treasury.

Returns a list of PaymentMethods for Treasury flows. If you want to list the PaymentMethods attached to a Customer for payments, you should use the List a Customer’s PaymentMethods API instead.

Since you're not using Stripe Treasury, PaymentMethods can only be listed per Customer and type is required too.

So the only way for you to retrieve all PaymentMethods is to list all Customers, iterate through each Customer, and list their PaymentMethods.

Example

$customers = $stripe->customers->all();

foreach ($customers->autoPagingIterator() as $customer) {
  $paymentMethods = $stripe->customers->allPaymentMethods(
    $customer->id,
    ['type' => 'card']
  );
  foreach ($paymentMethods->autoPagingIterator() as $paymentMethod) {
    echo $paymentMethod;
  }
}
  • Related