I'm integrating my Rails 4 app with Recurly. I'm using Recurly.js v3 to get a billing token and then v2 of the API via the Ruby Gem.
Everything was working both locally and on heroku until I changed the API credentials (RECURLY_SUBDOMAIN
, RECURLY_JS_PRIVATE_KEY
, RECURLY_API_KEY
) in my .env file over to a different recurly account for production. Now I'm getting an error response from Recurly:
>>ERROR -- Recurly: <=== 404 Not Found (948.3ms)
>><error>
>> <symbol>token_invalid</symbol>
>> <description>Token is either invalid or expired</description>
>></error>
The token is not expired as the request for the token information takes place directly before the API Subscription request. And I can't find information on why a token would be invalid. It's already passed the Recurly.js validations on the card as it has returned the token in the first place so I think it's not a problem with the card.
Here is my call to recurly.js for a token - this is executed on submit of views\devise\registrations\new.html.erb
form:
$(document).on("page:change", function(){
if($('.registrations.new, .registrations.edit').length){
if(!recurly.configured)
recurly.configure('<%=Recurly.js.private_key%>') ;
var form = $('#card-form');
var subscription = {
setupForm: function() {
return form.submit(function() {
event.preventDefault();
if ($('#card_number').length) {
recurly.token(form, subscription.tokenHandler);
return false;
} else {
return true;
}
});
},
tokenHandler: function(err, token) {
//console.log(token); at this point returns a different token each time
if(err){
//error message rendered
} else {
//card token applied to field in devise registrations form and form submitted
$('#user_card_token').val(token.id)
$('#card-form')[0].submit()
}
}
};
subscription.setupForm();
}
});
I have created a before_create hook on my devise user that calls the Recurly API:
In models/user.rb
before_create :create_subscription
def create_subscription
#logger.info card_token at this point returns the token received in the js above
subscription = Recurly::Subscription.create!(
plan_code: plan,
account: {
account_code: recurly_account_code,
email: email,
first_name: first_name,
last_name: last_name,
billing_info: {token_id: card_token}
}
)
rescue
errors.add :base, "Card Error"
false
end
That is where the error occurs. Is there a way to find any more information on the validity of the token? Or any other suggestions for troubleshooting?
Thanks for any help
It looks like you're using the wrong key to configure Recurly.js.
recurly.configure('<%=Recurly.js.private_key%>') ;
should be
recurly.configure('PUBLIC_KEY');
Where the PUBLIC_KEY is the Public Key found on your API Access page here: https://app.recurly.com/go/developer/api_access