Skip to content

Instantly share code, notes, and snippets.

@boucher
Forked from siddarth/gist:1379745
Created February 6, 2012 07:09
Show Gist options
  • Save boucher/1750375 to your computer and use it in GitHub Desktop.
Save boucher/1750375 to your computer and use it in GitHub Desktop.
Stripe PHP simple example
<?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>
@SamAsEnd
Copy link

@deemi DON'T USE A PRICE FIELD ON YOUR FORM PLEASE!

    Stripe_Charge::create(array("amount" => 1000,
                                "currency" => "usd",
                                "card" => $_POST['stripeToken']));

the "amount" key is for the price and fetch that from the db of some where safe!

@LuceroGera
Copy link

Hello my friend,
I was looking your example and I want to look functional but I can't because I don't know what do you have in this file (path-to-Stripe.php)?
Can you help me please?
Thanks,
Lucero

@garimaagarwal111
Copy link

line number 72 73 says undefined index

@vikas2804
Copy link

Hi,

Can anyone tell me how can i add more fields(name, city, address1 etc.) and save in stripe while creating new user?
Please

@big89
Copy link

big89 commented Dec 28, 2016

If anyone wants integrate stripe payment gateway api, you can contact me. Thanks !!!

@jdc-cunningham
Copy link

jdc-cunningham commented Dec 10, 2017

Just a note for anyone regarding the stripe.php path

Apparently you just require the init.php from the main folder (extracted) for non-composer install

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR.'stripe-php-5.7.0/init.php');

Found from here:

https://stackoverflow.com/questions/28846062/stripe-php-fatal-error-class-stripe-charge-not-found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment