-
-
Save sageworksstudio/0987a3a87ce945a75827 to your computer and use it in GitHub Desktop.
Stripe PHP example
This file contains hidden or 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 | |
require('./lib/Stripe.php'); | |
if ($_POST) { | |
// Set your secret key: remember to change this to your live secret key in production | |
Stripe::setApiKey("SECRET_KEY"); | |
$error = ''; | |
$success = ''; | |
// Create the charge on Stripe's servers - this will charge the user's card | |
try { | |
if (!isset($_POST['stripeToken'])) | |
throw new Exception("The Stripe token was not generated correctly."); | |
Stripe_Charge::create(array( | |
"amount" => 1500, // amount in cents, again | |
"currency" => "usd", | |
"card" => $_POST['stripeToken'], | |
"description" => "[email protected]")); | |
$success = 'Your payment was successful.'; | |
} catch(Exception $e) { | |
$error = $e->getMessage(); | |
} | |
} | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Stripe Test Form</title> | |
</head> | |
<body> | |
<h1>Very minimal Stripe test</h1> | |
<h2>Charge $15 with Stripe</h2> | |
<p>Test Credit Card: '4242424242424242'</p> | |
<p>Use '123' for expirey and any future date for month/year.</p> | |
<!-- to display errors returned by createToken --> | |
<span class="payment-errors"><?php if (isset($error)){echo $error;} ?></span> | |
<span class="payment-success"><?php if (isset($success)){echo $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> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<script src="https://js.stripe.com/v2/"></script> | |
<script> | |
/*jslint browser:true */ | |
/*global $, jQuery*/ | |
(function ($) { | |
'use strict'; | |
// This identifies your website in the createToken call below | |
Stripe.setPublishableKey('PUBLISHABLE_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 + ' Error ' + status); | |
} else { | |
var form$ = $("#payment-form"), | |
token = response.id; // token contains id, last4, and card type | |
// 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 () { | |
// 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 | |
}); | |
}); | |
}(jQuery)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment