Skip to content

Instantly share code, notes, and snippets.

@tylermenezes
Created March 29, 2012 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylermenezes/2231929 to your computer and use it in GitHub Desktop.
Save tylermenezes/2231929 to your computer and use it in GitHub Desktop.
Stripe Example for Leland
<?php
// ________ ___________ _________ ________ ________ ____ __ ______ ____ ___ ____ ______
// /_ __/ / / / _/ ___/ / _/ ___/ /_ __/ / / / ____/ / __ \/ / / / __ \ / __ \/ | / __ \/_ __/
// / / / /_/ // / \__ \ / / \__ \ / / / /_/ / __/ / /_/ / /_/ / /_/ / / /_/ / /| | / /_/ / / /
// / / / __ // / ___/ / _/ / ___/ / / / / __ / /___ / ____/ __ / ____/ / ____/ ___ |/ _, _/ / /
// /_/ /_/ /_/___//____/ /___//____/ /_/ /_/ /_/_____/ /_/ /_/ /_/_/ /_/ /_/ |_/_/ |_| /_/
// This is what gets executed whenever the page is submitted.
// Standard stuff:
// First we load up the Stripe library:
require_once("Stripe.php");
// Then we set the API key:
Stripe::setApiKey("YOUR PRIVATE KEY HERE");
$error = ""; // We'll initialize this to an empty string. It gets printed to the page regardless, so if
// we change it to a non-empty string later, it'll get shown to the user.
if(isset($_POST["stripeToken"])){
try{
// A switch statement is like a shorthand IF statement. Just make sure each case ends with "break"
// (which means "endif").
switch($_POST["package"]){
case "1":
$amount = 1000;
$description = "Charge for package 1";
break;
case "2":
$amount = 2000;
$description = "Charge for package 2";
break;
}
// Now we charge the card:
$charge = Stripe_Charge::create(array(
"amount" => $amount,
"currency" => "usd",
"card" => $_POST['stripeToken'],
"description" => $description)
);
// Now charge the card!
if($charge->paid){
// Payment was successful! Do something!
//
// If you want to redirect someone do:
// header("Location: http://new.com/location/");
//
// If you want to send an email use:
// mail("to@something.com", "subject", "Message", "From: Your Service <you@yourdomain.com>");
}else{
$error = $charge->failure_message;
}
}catch (Exception $ex){
$error = $ex->getMessage();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Candela Hosting</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="fb:page_id" content="102509959850465" />
<link href="main.css" type="text/css" rel="stylesheet" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body>
<div id="bodybox">
<div id="roundshadow">
<img alt="" src="title.png" color="#222" width="798px" />
<?php
// ________ _ _ __ __ __ ____
// /_ __/ /_ (_)____ (_)____ / /_/ /_ ___ ____ ____ ___ ______ ___ ___ ____ / /_ / __/___ _________ ___
// / / / __ \/ / ___/ / / ___/ / __/ __ \/ _ \ / __ \/ __ `/ / / / __ `__ \/ _ \/ __ \/ __/ / /_/ __ \/ ___/ __ `__ \
// / / / / / / (__ ) / (__ ) / /_/ / / / __/ / /_/ / /_/ / /_/ / / / / / / __/ / / / /_ / __/ /_/ / / / / / / / /
// /_/ /_/ /_/_/____/ /_/____/ \__/_/ /_/\___/ / .___/\__,_/\__, /_/ /_/ /_/\___/_/ /_/\__/ /_/ \____/_/ /_/ /_/ /_/
// /_/ /____/
//
// Anything which has a name="..." paramater gets sent to your server. Anything which is missing that isn't. That's why the
// credit card number never hits your server (and why you don't have to go through a security audit to use this).
?>
<div id="cboxcontent">
<h1>Something</h1>
<span class="payment-errors"><?php echo $error; ?></span>
<form action="" method="POST" id="payment-form">
<div class="form-row">
<label>Select Package</label>
<select name="package">
<option value="1">Package 1</option>
<option value="2">Package 2</option>
</select>
</div>
<hr />
<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>
</div>
</div>
</div>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<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('pk_euVubOUzewxcRTpWfKcKKtrFcg1BU');
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
});
});
if (window.location.protocol === 'file:') {
alert("stripe.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@stripe.com if you need assistance.");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment