Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a Stripe test card token for testing

Tags:

I am using Stripe in my app. I want to write an integration test for placing a payment that checks Stripe that a payment was created. I'm using Stripe.js.

In my test I need a card token to perform the test charge. Usually this token would be generated client side with stripe.js and sent in the request to perform the charge. As this is a server-side only test is there some way I can generate a token from within the test?

For reference the test would be something like this (uses php but the principle is the same):

/** @test **/ public function it_creates_a_charge() {     $order = factory(Order::class)->create();     $stripe_token = Stripe::generateToken([                                           'card' => '4242424242424242'                                           'exp'  => '04/2017',                                           'cvc'  => '123'                                           ]); // does not exist afaik      $response = $this->post('charges/store', [                 'stripe_token' => $stripe_token,                 'order_id' => $order->id,                 //etc                 ]);      // assertions... } 

Essentially I'm asking if there's something within the Stripe API that allows server-side token generation.

like image 377
harryg Avatar asked Jan 05 '16 19:01

harryg


People also ask

Can you create a dummy Stripe test?

Creating a Test Account Click “Sign In”. On the Sign In page, click “Sign up”, next to “Don't have an account?”. Click “skip this step” and you'll be able to access a temporary, unnamed account that can be used for testing purposes. On the resulting page (what Stripe calls your dashboard), click “Your Account”.

How do you test Stripe in test mode?

While in Test Mode there are no fees of any kind— from us, from Stripe, from anybody. To view your test charges, toggle 'Viewing test data' on your Stripe Dashboard in the top menu. Or, find them right inside the Collect app! They'll be on the Charges screen while the app is in Test Mode.

What is Stripe card token?

Tokenization is the process Stripe uses to collect sensitive card or bank account details, or personally identifiable information (PII), directly from your customers in a secure manner. A token representing this information is returned to your server to use.


2 Answers

Stripe provides an API call to create tokens from the server:

\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");  \Stripe\Token::create(array(   "card" => array(     "number" => "4242424242424242",     "exp_month" => 1,     "exp_year" => 2017,     "cvc" => "314"   ) )); 

edit: Stripe now provides ready-to-use test tokens like tok_visa at https://stripe.com/docs/testing#cards.

like image 100
ceejayoz Avatar answered Oct 03 '22 04:10

ceejayoz


You don't need to create tokens with fake credit cards for testing anymore. Stripe now provides a list of pre-made tokens for this purpose :

Stripe docs : Test card numbers and tokens

like image 21
Littletime Avatar answered Oct 03 '22 04:10

Littletime