Skip to content

Instantly share code, notes, and snippets.

@jamiequint
Created December 9, 2011 00:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiequint/1449341 to your computer and use it in GitHub Desktop.
Save jamiequint/1449341 to your computer and use it in GitHub Desktop.
Stripe Form Javascript Translated to CoffeeScript
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