Reputation: 347
I'm trying to implement Stripe and they have this code listed in their documentation:
<script>
Stripe.setPublishableKey('pk_test_key');
jQuery(function($) {
$('#new_subscription').submit(function(event) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('input[type="submit"]').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
function stripeResponseHandler(status, response) {
var $form = $('#new_subscription');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('input[type="submit"]').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
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>
However I'd like to be able to move that out of the view, and into a javascript file. As many know the rails javascript files use coffeescript. So what I was wondering is how I would convert this into something that can successfully run in the js.coffee file?
Upvotes: 0
Views: 34
Reputation: 4171
It's really good practice to do it manually and get a deeper understanding of the differences between coffeescript and js.
I usually get lazy and just use js2coffee.
There may be some parts that have to run in the view, but those parts are usually pretty obvious as they rely on keys or other things that the Rails server knows but the js file wouldn't have access to.
Upvotes: 1