Skip to content

Instantly share code, notes, and snippets.

@Austio
Created July 18, 2014 14:40
Show Gist options
  • Save Austio/204ce14916f6f149e6b5 to your computer and use it in GitHub Desktop.
Save Austio/204ce14916f6f149e6b5 to your computer and use it in GitHub Desktop.
<script>
// Configure recurly.js
recurly.configure('<%= @recurly_public_key %>');
//grab some dom
var form = $('form');
//var methodRadio = $('input[name=payment-method-select]');
var box = form.find('.payment');
var inputs = form.find('input, select, button');
var submit = form.find('button');
var recurrence = $('.recurrence').val();
// On form, we stop submission to go get the token
$('form').on('submit', function (event) {
event.preventDefault();
// Reset the errors display
$(".paymenterrors").empty();
// Disable the submit button
$('.submit').attr("disabled",true);
$('.submit').val("Processing...this will take a few moments.");
var form = this;
recurly.token(form, function (err, token) {
// send any errors to the error function below
if (err) error(err);
// Otherwise we continue with the form submission
else {
checkSubmitValid(token);
}
});
});
// Error handling function to expose errors to the customer
function error (err) {
$(".paymenterrors").empty().append(err.message);
$(".submit").removeAttr("disabled");
$('.submit').val("Submit");
if (err.fields) {
for (var i = 0, field; field = err.fields[i++];) {
$(".paymenterrors").append("</br></br>Please verify the " + field + " field.");
}
}
}
function checkSubmitValid(token) {
$.ajax({
type: "POST",
url: '/payment_info',
data: {
'recurly_token': token,
'recurrence': recurrence
/* etc */
},
complete: function (response) {
var response = response.responseText;
if (response.search("Error Processing:") == -1) {
$(".paymenterrors").empty().append("Submitting.... Please Wait");
$("#recurly-token").val(response);
$('form').unbind('submit').submit();
}
else {
$(".paymenterrors").empty().append(response).append("</br></br>");
$(".submit").removeAttr("disabled");
$('.submit').val("Submit");
}
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment