Created
July 12, 2013 03:49
-
-
Save sumeetjain/5981258 to your computer and use it in GitHub Desktop.
http://railscasts.com/episodes/288-billing-with-stripe Converted CoffeeScript to JS.
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 subscription; | |
jQuery(function() { | |
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')); | |
return subscription.setupForm(); | |
}); | |
subscription = { | |
setupForm: function() { | |
return $('#new_subscription').submit(function() { | |
$('input[type=submit]').attr('disabled', true); | |
if ($('#card_number').length) { | |
subscription.processCard(); | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
}, | |
processCard: function() { | |
var card; | |
card = { | |
number: $('#card_number').val(), | |
cvc: $('#card_code').val(), | |
expMonth: $('#card_month').val(), | |
expYear: $('#card_year').val() | |
}; | |
return Stripe.createToken(card, subscription.handleStripeResponse); | |
}, | |
handleStripeResponse: function(status, response) { | |
if (status === 200) { | |
$('#subscription_stripe_card_token').val(response.id); | |
return $('#new_subscription')[0].submit(); | |
} else { | |
$('#stripe_error').text(response.error.message); | |
return $('input[type=submit]').attr('disabled', false); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment