Created
July 18, 2014 14:40
-
-
Save Austio/204ce14916f6f149e6b5 to your computer and use it in GitHub Desktop.
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
<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