Created
December 9, 2011 00:00
-
-
Save jamiequint/1449341 to your computer and use it in GitHub Desktop.
Stripe Form Javascript Translated to CoffeeScript
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
Stripe.setPublishableKey('YOUR_PUBLISHABLE_KEY'); | |
$("#payment-form").submit((event) -> | |
# disable the submit button to prevent repeated clicks | |
$('.submit-button').attr("disabled", "disabled") | |
Stripe.createToken({ | |
number: $('.card-number').val(), | |
cvc: $('.card-cvc').val(), | |
exp_month: $('.card-expiry-month').val(), | |
exp_year: $('.card-expiry-year').val(), | |
}, stripeResponseHandler) | |
# prevent the form from submitting with the default action | |
return false | |
) | |
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 | |
form$ = $("#payment-form") | |
# token contains id, last4, and card type | |
token = response['id'] | |
# insert the token into the form so it gets submitted to the server | |
form$.append("<input type='hidden' name='stripe_token' value='" + token + "'/>") | |
# and submit | |
form$.get(0).submit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment