Easily Create Stripe Tokens and Cards For Testing

Jean-Marc Amon
2 min readFeb 20, 2017

--

When you are building a payment flow with Stripe there are a lot of different flows for you to test. These include successful charges and a plethora of failed options. Such as invalid CVC, invalid address, expired cards, declines, and more.

To make testing all these different flows easier, Jacob Bennett created a StripeTestToken package so you can easily create tokens for each case.

Testing Successful Charges

To test successful charges you can use one of the following methods from the package:

  • validVisa
  • validVisaDebit
  • validMastercard
  • validMastercardDebit
  • validMastercardPrepaid
  • validAmex
  • validDiscover
  • validDinersClub
  • validJCB

Here is an example using the StripeTestToken::validVisa() method to create a valid Visa card token and allows the charge to go through successfully.

use JacobBennett\StripeTestToken;
use Stripe\Charge;

StripeTestToken::setApiKey('your_stripe_secret_test_key');

Charge::create([
'amount' => 500,
'curreny' => 'usd',
'source' => StripeTestToken::validVisa(),
]);

Testing Failed Transactions

Testing failed transactions work in a similar fashion, but you will want to wrap it in a try/catch so you can consume the Stripe exception:

try {

Charge::create([
'amount' => 500,
'curreny' => 'usd',
'source' => StripeTestToken::cvcFail(),
]);

} catch (\Stripe\Error\Card $e) {
// handle errors
}

Other failed methods include:

  • addressZipFail
  • addressFail
  • zipFail
  • addressZipUnavailable
  • cvcFail
  • customerChargeFail
  • successWithReview
  • declineCard
  • declineFraudulentCard
  • declineIncorrectCvc
  • declineExpiredCard
  • declineProcessingError
  • declineIncorrectNumber

Generating Test Card Numbers

If you are utilizing browser testing you may want to test the checkout flow using card numbers and this feature is included as well. Instead of calling StripeTestToken::validVisa() you utilize StripeCardNumber::validVisa().

This StripeTestToken package looks like a great addition to your testing workflow when you are working with Stripe.

--

--

Jean-Marc Amon

#Laravel and #WordPress Enthusiast, Software Engineer, Writer at http://marclabs.com and http://passionariat.wordpress.com From Côte d’Ivoire