Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Last4 Digits of Card using Customer Object - Stripe API with PHP

Tags:

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?

like image 828
jaewo0k Avatar asked May 25 '15 22:05

jaewo0k


People also ask

Can you see card details in Stripe?

To view payment details, simply go to Payments in your Stripe dashboard. Here you will see a list of your most recent payments and can filter or navigate to narrow down to find specific records.

How do I get my Stripe customer ID?

Just use the following line of code you will get the customer id. $customer = $stripe->customers()->create([ 'email' => $_POST['stripeEmail'], 'source' => $_POST['stripeToken'], 'plan' => trim($plan) ]); echo $customer['id']; This will help you.

How do I find my Stripe client secret key?

Getting Publishable Key and Secret KeyChoose Settings and then select API keys on the sidebar of the Stripe dashboard. On the API keys page find the Publishable key and Secret key fields and copy their values. Paste these keys into the corresponding fields of the Stripe Connect payment method settings.


1 Answers

$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.

like image 159
Matthew Arkin Avatar answered Sep 24 '22 00:09

Matthew Arkin