22

I want to get the last 4 digits of a customers card using Stripe. I have already stored the Customer using:

      // Get the credit card details submitted by the form
      $token = $_POST['stripeToken'];

      // Create a Customer
      $StripeCustomer = \Stripe\Customer::create(array(
              "description" => "$username",
              "card" => $token
      ));

Now I'd like to access and then store the card's last 4 digits. (For context, I want to show users which card they have stored using Stripe for future payments - this is not a subscription service).

I have searched for a solution but a lot of the posts are saving the last4 digits AFTER a charge, and pull the information from the charge like:

$last4 = null;
try {
    $charge = Stripe_Charge::create(array(
    "amount" => $grandTotal, // amount in cents, again
    "currency" => "usd",
    "card" => $token,
    "description" => "Candy Kingdom Order")
);
$last4 = $charge->card->last4;

I would like to do the same BEFORE the charge , so I want to pull the last 4 from the Customer Object. The Stripe API documentation shows the attribute path for last4 from Customers,
customer->sources->data->last4

However, this does not seem to give me the correct last 4 digits.
$last4 = $StripeCustomer->sources->data->last4;

I think I am misunderstanding how to use attributes in the Stripe API. Could someone point me in the right direction?

jaewo0k
  • 245
  • 1
  • 3
  • 6

4 Answers4

35

$last4 = $StripeCustomer->sources->data[0]->last4;

sources->data is an array so you'd have to select the first card.

Side note: You're using the token twice, once to create the customer, and the second to create the charge, this will result in an error as the token can only be used once. You'd have to charge the customer instead of the token.

Matthew Arkin
  • 4,460
  • 2
  • 27
  • 28
  • Worked like a charm! Also to your side note: you are absolutely correct. That second piece of code that uses token to make the charge is pulled from another post. My code does in fact charge the customer object. Thanks again! – jaewo0k May 26 '15 at 09:17
  • 1
    Thanks! The documentation was so unclear about that, it was giving some weird method that I suspect was making a second call, this is much better! :) – NaturalBornCamper Jun 24 '16 at 07:18
  • Thanks Matthew. Helped me out as well. – z0mbieKale Aug 25 '16 at 07:21
  • What about `default_source`? – CMCDragonkai Sep 01 '16 at 13:09
  • `default_source` is a string containting the `ID of the default payment source for the customer.` so you would need another call to get the source object and read the `last4` from there. – dev_masta Aug 23 '19 at 01:37
  • As of version 2020-08-27, this doesn't work. Do we know how to get the last4 when creating the customer with the new version? – Stackedup Sep 02 '20 at 03:45
  • As of version 2020-08-27 the correct way to do this is via the https://stripe.com/docs/api/payment_methods/list – tsiege Dec 15 '21 at 14:37
4

For any one else who lands here from search engines, here's a link to the Stripe docs on how to get the last 4 digits of a card saved to the customer https://stripe.com/docs/api/customers/object#customer_object-sources-data-last4

Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
1

This API has changed since this was first answered. For API version 2020-08-27 you need to use the ListPaymentsMethod API on the Customer object.

The path to access via JSON would look like this

$paymentMethods = $stripe->paymentMethods->all([
  'customer' => 'cus_123',
  'type' => 'card',
]);
$last4 = $paymentMethods->data[0]->card->last4
tsiege
  • 468
  • 1
  • 4
  • 17
0

If you use Laravel Cashier, this code will be helpful for you.

$data = User::find(auth()->user()->id);
$payment = $data->defaultPaymentMethod();
$last4 = $payment->last4;
$brand = $payment->brand;
dd($payment);

You can get all information from this object.

ExpertWeblancer
  • 1,368
  • 1
  • 13
  • 28