So I recently had a consulting opportunity where the client needed to be sent to a dynamic page after a controller action in rails. That is easy in the normal rails context, however this all had to be done in a javascript content due to it being a javascript based subscription form for sign up. I ended up with a nice little parlor trick that is attached below.
A little background, this client used recurly to do recurring billing and recurly uses a javascript system to securely handle creditcard data. On submit, javascript sends recurly the credit card data and they send you back a token representing the data in their system or you get back errors that you can display to the browser.
Once you have the token there are a couple ways you can approach charging it to add a subscription. The way that I do it is by setting up a subscription/order controller in rails, that then takes care of creating the subscription through an Ajax request. That allows us to catch any errors (i.e. not enough funds) without forcing the user to re-enter any non-credit card data on the site.
Normally after a successful subscription, you will let the form continue to a subscription/registrations controller and that will take care of the setup.
In this app, there were multiple paths that the user could go based on what they order and their progress in the system. So i needed to redirect them dynamically. The way that i did that is on success instead of allowing the form to continue using javascript, i called the function below.
[ruby]
function redirectOrder(url){
var plan = $(‘#recurly_plan_code’).val();
var form = $(‘<form action="’ + url + ‘" method="get">’ +
‘<input type="text" name="recurly_plan_code" value="’ + plan + ‘" />’ +
‘</form>’);
$(‘body’).append(form);
form.submit();
}
[/ruby]
Leave a Reply