Created
September 11, 2017 08:43
-
-
Save daino92/1d230aef45eaa71fa378151560401048 to your computer and use it in GitHub Desktop.
JS Stripe Handler
This file contains 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
var public_key = $("#pk").val(); | |
Stripe.setPublishableKey(public_key); | |
var $form = $('#checkout-form'); //Grab the form | |
$form.submit(function(event){ | |
$('charge-error').addClass('hidden'); | |
$form.find('button').prop('disabled', true); | |
Stripe.card.createToken({ | |
number: $('#card-number').val(), | |
cvc: $('#card-cvc').val(), | |
exp_month: $('#card-expiry-month').val(), | |
exp_year: $('#card-expiry-year').val(), | |
name: $('#card-name').val() | |
}, stripeResponseHandler); | |
return false; | |
}); | |
function stripeResponseHandler(status, response) { | |
if (response.error) { //if problem... | |
$('charge-error').removeClass('hidden'); //Show errors on the form | |
$('charge-error').text(response.error.message); | |
$form.find('button').prop('disabled', false); | |
} else { //token was created | |
var token = response.id; //get the token ID | |
// Insert the token into the form so it gets submitted to the server: | |
$form.append($('<input type="hidden" name="stripeToken" />').val(token)); | |
$form.get(0).submit(); // Submit the form: | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment