public
Last active — forked from siddarth/gist:1379745

Stripe PHP simple example

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php
require 'path-to-Stripe.php';
 
if ($_POST) {
Stripe::setApiKey("YOUR-API-KEY");
$error = '';
$success = '';
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
Stripe_Charge::create(array("amount" => 1000,
"currency" => "usd",
"card" => $_POST['stripeToken']));
$success = 'Your payment was successful.';
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
 
?>
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Stripe Getting Started Form</title>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('YOUR-PUBLISHABLE-API-KEY');
 
function stripeResponseHandler(status, response) {
if (response.error) {
// re-enable the submit button
$('.submit-button').removeAttr("disabled");
// show the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// and submit
form$.get(0).submit();
}
}
 
$(document).ready(function() {
$("#payment-form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");
 
// createToken returns immediately - the supplied callback submits the form if there are no errors
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
return false; // submit from callback
});
});
</script>
</head>
<body>
<h1>Charge $10 with Stripe</h1>
<!-- to display errors returned by createToken -->
<span class="payment-errors"><?= $error ?></span>
<span class="payment-success"><?= $success ?></span>
<form action="" method="POST" id="payment-form">
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" class="card-number" />
</div>
<div class="form-row">
<label>CVC</label>
<input type="text" size="4" autocomplete="off" class="card-cvc" />
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" class="card-expiry-month"/>
<span> / </span>
<input type="text" size="4" class="card-expiry-year"/>
</div>
<button type="submit" class="submit-button">Submit Payment</button>
</form>
</body>
</html>

The point of using Stripe.js is so that credit card information never really leaves the client side except in the form of a token, correct? If so, shouldn't you also remove the credit card fields from the form submission when you add the token field on line 46? Or am I missing something?

Because those fields do not contain a "name" attribute, they will not be included in the data the form POSTs to your server.

On line 5 of this example you call Stripe directly to set the API key:
Stripe::setApiKey("YOUR-API-KEY");
But when I downloaded the SDK Stripe is an abstract class.

Ian - it's okay to call a static method on an abstract class in PHP. I'm guessing that the Stripe class has been declared abstract only to keep it from being instantiated. Someone please correct me if I'm wrong.

Right, sorry, the autoloader within my CodeIgniter installation was trying to instantiate it. Thank you.

Is there any example that takes full credentials from the user i.e. address,amount e.t.c

How do I create customer with this code?

you will create customer and this customer can save in your database if you want and in stripe.com customer as well.
$customer = Stripe_Customer::create(array(
"card" => $token,
"description" => "description")
);

I believe token is just use one time, and each time we need credit card information to make payment and create token?

is there is any way , that we will not take credit card information every time from user and make payment through token?

there is no need to take the credit card from user every time..the token is not one time and can be re-used. check this one. I'll do too as I'm planning to use it

Hi! When i put the require 'stripe/lib/Stripe.php'; the page is empty. What is the problem?

I want to use stripe with Canada country. Is their is any testing bank account we can create in stripe by accepting some details like account number and routing number. How can we can transfer money in Canadian bank through stripe. Please give me test details

AnandPhadnis , I suggest you lookup the latest details at Stripe.com . http://lmgtfy.com/?q=using+stripe+in+canada

I tried this but the dashboard didn't respond. Is there a next step?

What is "path-to-Stripe.php"?

@vignesh2014 the "path-to-Stripe.php" is referring to the Stripe.php file that is required. You can store it anywhere so just change the "path-to-" portion to the place you have it stored.

It will look something like this:

require_once('./lib/Stripe.php');

This is wonderful. Thanks! Any clues as to how to redirect the page to another upon success? Using form variables didn't work for me.

I'm a newbie. Please help me how to check in case the transaction is false(no money,card is test demo).
Thanks so much!

I'm a newbie. Where do I get the 'path-to-Stripe.php' file from?

To redirect to a new page on success, firstly make sure you are processing the response in PHP at the top of the page just like in this example and when you get a success response just use this line of code

header("Location: http://www.google.com");

change the google.com above to the whatever you want. If you write any html to the page first, even a simple tag, it wont redirect. That's why you have to do it at the top of the page in php before you write any html tags.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.