I’ve been working on hooking up my Rails, Shopify embedded app to the Shopify billing API. It seems simple enough:
- You create a new Shopify RecurringApplicationCharge object through the API, submit it to Shopify.
- They return the object with additional fields filled in, including a ‘confirmation_url’ which you can redirect to.
- This confirmation URL has a charge_id param, which you can use to activate the billing.
I can get through the above fine and can even see the ‘charge_id’ param in the log as well as when I run params.inspect . However, I can’t use it. Every time I try and assign params to an instance variable I get ‘nil class’.
Controller
class HomeController < AuthenticatedController
before_action :require_login, except: [:activate_charge]
skip_before_action :verify_authenticity_token, only: [:activate_charge]
before_action :require_subscription, except: [:activate_charge]
def require_subscription
unless ShopifyAPI::RecurringApplicationCharge.current
@recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.new
@recurring_application_charge.name = "Monthly Plan"
@recurring_application_charge.price = 15.0
@recurring_application_charge.return_url = "https://xxxxx.ngrok.io/activatecharge"
@recurring_application_charge.test = true
@recurring_application_charge.trial_days = 7
@recurring_application_charge.save
if @recurring_application_charge.id
#@tokens[:confirmation_url] = @recurring_application_charge.confirmation_url
fullpage_redirect_to(@recurring_application_charge.T)
end
end
end
def activate_charge
@params = params[:charge_id]
#@recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.find(params[:charge_id])
#@recurring_application_charge.status == "accepted" ? @recurring_application_charge.activate : redirect_to('/')
#@recurring_application_charge.save
redirect_to('/')
end
def recurring_application_charge_params
params.require(:recurring_application_charge).permit(:name, :price, :capped_amount, :terms, :trial_days, :charge_id)
end
params.inspect
<ActionController::Parameters {"charge_id"=>"12271520309", "hmac"=>"f7a75bba157ef40b3144d8d0e8ceb8b37628ea294fbe16887b623c0d311d84da", "locale"=>"en-GB", "session"=>"f8b8ef448a1d9883e4884c858e0bc39d789d53d2eeffffb34c42f5bc390af6f1", "shop"=>"xxxxx.myshopify.com", "timestamp"=>"1578555752", "controller"=>"home", "action"=>"activate_charge"} permitted: true>
Some clues
-
I have an API controller which I use for other things, this is not authenticated by Shopify or Clearance. I can get ‘charge_id’ param if I use this controller (although this controller cant be use for other reasons).
-
I’ve played around with trying to make sure strong params allow for it, but I’m not writing to a model, so this shouldn’t matter?
-
On an older Shopify page on how to set billing up, they have this line of code in the first object creation phase, that I haven’t been able to implement:
@tokens[:confirmation_url] = recurring_application_charge.confirmation_url
Any help would be great, I can’t figure out if this is a really simple problem or whether there’s an authentication issue I’m completely oblivious to.
Thanks!
Edit
The Return, when params.class and params.inspect is called
ActionController::Parameters
<ActionController::Parameters {"charge_id"=>"12284756045", "hmac"=>"9ff4d8a30d623f2d03d0226ff3d98c780e451615d630c149697e5b2dd4ffd4b5", "locale"=>"en-GB", "session"=>"f8b8ef448a1d9883e4884c852e0bc39d789d53d2eeffffb34c42f5bc390af6f1", "shop"=>"xxxx.myshopify.com", "timestamp"=>"1578737591", "controller"=>"home", "action"=>"activate_charge"} permitted: false>
Another piece of info, if I add the following to the controller:
params = params.to_h[:charge_id]
Then call params[“charge_id”], from the view I can get the charge_id returned.
However, when I try and assign it to @charge_id in the controller, then try and call @charge_id in the view to test it, it returns nothing :S
params = params.to_h[:charge_id]
@charge_id = params["charge_id"]
3
Answers
Ugh, anticlimax. I noticed that none of the code in the activate_charge method was actually running. I then figured out that having the require_subscription and activate_charge method in the private section of my controller was causing the issue.
The
ActionController::Parameters
use"charge_id"
, not:charge_id
. Maybe you should try that. Or work withparams.to_h[:charge_id]
, asto_h
will return a HashWithIndifferentAccess.params convert your id to string while you may be assigning it to an id which is not string which means you should:
Share more details and I guess we will be able to solve this issue.
UPDATE (Debugging help):
For beginner controller debugging (like you prefer, I guess), before your line where you are getting
@params = params[:charge_id]
:Now go to console where your server is running and after reloading the page (in browser), Ctrl+Shift+F and enter
*******
. This will find the place where your params are printed. GLHF