Last active
August 29, 2020 19:37
-
-
Save alairock/a69dedef9373059d8150 to your computer and use it in GitHub Desktop.
Get a Stripe token in PHP/Laravel. Helpful when testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/stripetoken', function() | |
{ | |
$client = new \GuzzleHttp\Client(); | |
$pubKey = 'pk_test_xxxxxxxxxxxxx'; | |
$cardNumber = "4242424242424242"; | |
$cvc = "123"; | |
$expMonth = "11"; | |
$expYear = "2018"; | |
$headers = [ | |
'Pragma' => 'no-cache', | |
'Origin' => 'https://js.stripe.com', | |
'Accept-Encoding' => 'gzip, deflate', | |
'Accept-Language' => 'en-US,en;q=0.8', | |
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36', | |
'Content-Type' => 'application/x-www-form-urlencoded', | |
'Accept' => 'application/json', | |
'Cache-Control' => 'no-cache', | |
'Referer' => 'https://js.stripe.com/v2/channel.html?stripe_xdm_e=http%3A%2F%2Fwww.beanstalk.dev&stripe_xdm_c=default176056&stripe_xdm_p=1', | |
'Connection' => 'keep-alive' | |
]; | |
$postBody = [ | |
'key' => $pubKey, | |
'payment_user_agent' => 'stripe.js/Fbebcbe6', | |
'card[number]' => $cardNumber, | |
'card[cvc]' => $cvc, | |
'card[exp_month]' => $expMonth, | |
'card[exp_year]' => $expYear, | |
]; | |
$response = $client->post('https://api.stripe.com/v1/tokens', [ | |
'headers' => $headers, | |
'form_params' => $postBody | |
]); | |
$response = json_decode($response->getbody()->getContents()); | |
return $response->id; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really Appreciated