Easily Create Stripe Tokens and Cards For Testing

Easily Create Stripe Tokens and Cards For Testing

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.

Filed in: Laravel Packages / Stripe
Laravel News Partners

Laravel Jobs

Backend Software Engineer (PHP Developer)
Austin TX, Bonita Springs FL
Certified eSupport Corp
Laravel Developer
Glendale, CA (COVID Remote)
Jogg, Inc
Full-Stack Engineer (Mid to Senior)
Remote (EST, CST)
Voxie
Senior Laravel Developer (AI Applications)
Remote from anywhere in the UK or from our London office
GreenShoot Labs
Full Stack Engineer
Remote
Shelterluv

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.