Skip to content

Instantly share code, notes, and snippets.

@elivz
Created January 8, 2012 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elivz/1577051 to your computer and use it in GitHub Desktop.
Save elivz/1577051 to your computer and use it in GitHub Desktop.
Stripe integration code
$(document).ready(function() {
// Load the Stripe script
$.getScript('https://js.stripe.com/v1/', function() {
Stripe.setPublishableKey('pk_MoKd7A4QHY6s7OJBXUghBQQN3uCT2');
$('#checkout').show();
});
var $form = $("form[name='checkout']").submit(function(event) {
// Disable the submit button to prevent repeated clicks
$('.submit').attr("disabled", true);
// Create the secure token that will be submitted to the server
var amount = $('#order-total').val().replace(/\$|,/g, '') * 100; // Total amount in cents
Stripe.createToken({
name: $('#billing-name').val(),
address_line1: $('#address1').val(),
address_zip: $('#zip').val(),
number: $('#card-number').val(),
cvc: $('#card-cvc').val(),
exp_month: $('#card-expiry-month').val(),
exp_year: $('#card-expiry-year').val()
}, amount, stripeResponseHandler);
// Don't submit the form just yet
return false;
});
function stripeResponseHandler(status, response) {
$('.submit').attr("disabled", false);
if (response.error) {
// If there was an error generating the token
alert(response.error.message);
} 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='payment[token]' value='" + token + "'/>");
// And submit
$form.unbind('submit');
$form.get(0).submit();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment