Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last 4 digits of credit card after successful Stripe payment

I have the following code that processes a charge on a users credit card using Stripe.

// Create the charge on Stripe's servers - this will charge the user's card
    try {
        $charge = Stripe_Charge::create(array(
          "amount" => $grandTotal, // amount in cents, again
          "currency" => "usd",
          "card" => $token,
          "description" => "Candy Kingdom Order")
        );
    } catch(Stripe_CardError $e) {
      // The card has been declined
    }

Now I want to be able to show the last 4 digits of the card that was used on the order confirmation page. But I want to do it in a way that it does NOT store the full card number. JUST the last 4. I'm not sure how to retrieve this information, if it's even possible. Pleas help?

like image 221
Mitch Evans Avatar asked Feb 01 '14 02:02

Mitch Evans


1 Answers

The API Documentation has the answer you are looking for. $charge->card->last4 should have the value you are looking for. Using your example, the following would work:

$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;
} catch(Stripe_CardError $e) {
  // The card has been declined
}
like image 145
Stefan Nuxoll Avatar answered Oct 26 '22 17:10

Stefan Nuxoll