Skip to content

Instantly share code, notes, and snippets.

@joelataylor
Created June 24, 2014 21:02
Show Gist options
  • Save joelataylor/aed58c43e133ee1b0240 to your computer and use it in GitHub Desktop.
Save joelataylor/aed58c43e133ee1b0240 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Stripe Fun</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_2V6rBH5IilzAezJ8K9fcpOkYT7VJf');
jQuery(function($) {
$('#payment-form').submit(function(event) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
// First we check to make sure we have a phone number!
if ($('#phone').val() === '') { // you should do some proper validation here to make sure it's a valid phone number!
alert('Sorry, a Phone Number is required');
return false;
}
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// 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" />').val(token));
// and submit
$form.get(0).submit();
}
};
</script>
</head>
<body>
<h1>This is some Stripe fun</h1>
<form action="stripe.php" method="POST" id="payment-form">
<span class="payment-errors"></span>
<div class="form-row">
<label>
<span>* Phone Number</span>
<input type="phone" name="phone" id="phone" size="20"/>
</label>
</div>
<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number"/>
</label>
</div>
<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc"/>
</label>
</div>
<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" size="2" data-stripe="exp-month"/>
</label>
<span> / </span>
<input type="text" size="4" data-stripe="exp-year"/>
</div>
<button type="submit">Submit Payment</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment