Mitch Evans
Mitch Evans

Reputation: 641

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?

Upvotes: 10

Views: 9036

Answers (1)

Stefan Nuxoll
Stefan Nuxoll

Reputation: 867

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
}

Upvotes: 21

Related Questions