Home > Enterprise >  How can get the array of stripe customer card api PHP
How can get the array of stripe customer card api PHP

Time:06-24

I wanted to know how can I get the data of the stripe api array in PHP. This is my code:

require_once('vendor/autoload.php');


$stripe = new \Stripe\StripeClient(
    'KEY'
  );
  $stripe->customers->retrieveSource(
    'CUSTOMER_ID',
    'CARD_ID',
  );
 print_r($stripe);

I tried to use print_r to get the result on screen but is not showing me that data, just showing me api details of stripe. How can I get the card detail from customer in STRIPE?

CodePudding user response:

Every API requests you make with Stripe's API will return an object. You have to store the response in a variable if you want to access it.

If we take your example you want this

$stripe = new \Stripe\StripeClient('sk_test_123');
$card = $stripe->customers->retrieveSource('cus_123','card_ABC');

With that the $card variable is now an instead of the Card class in the library with all the properties you care about.

Now, this is old code that is mostly deprecated. In 2018, Stripe released a new API called the PaymentMethods API which unifies all payment method types together from card to ACH/SEPA/BACS Debit to many others like iDEAL and Konbini. The legacy objects like card_123 work with that API too.

I would strongly recommend using the new API instead so call either the Retrieve PaymentMethod API to retrieve a specific one like this:

$stripe = new \Stripe\StripeClient('sk_test_123');
$pm = $stripe->paymentMethods->retrieve('pm_123');

Or you can call the List PaymentMethods API instead with the type parameter to find all the cards attaches to that customer. This returns a list of PaymentMethods and you can paginate through it as documented here:

$stripe = new \Stripe\StripeClient('sk_test_123');
$paymentMethods = $stripe->customers->allPaymentMethods(
  'cus_9utnxg47pWjV1e',
  [
    'type' => 'card',
  ]
);
foreach ($paymentMethods->autoPagingIterator() as $paymentMethod) {
  // Do something with $paymentMethod
}
  • Related