I currently have an app live in the Shopify app store, but i'm wanting to add a recurring application charge. My app is based on the shopify_app gem and I used the default generator to create it.
I have no clue how or where or to test adding the code necessary to create an application charge.
Any help would be greatly appreciated!
Solved! Go to the solution
Pretty much tells most of the things you need to know. Can you specify which areas are you having issues in?
Thanks for the reply! That page refers to a ruby app, mine is a rails app using the shopify_app and shopify_api gem.
Shopify provides this:
:name => "Basic Plan",
:price => 15.0,
:trial_days => 7,
:test => true
But do I put that in an after authentication job? Do I put it in a concern or a controller? I've tried putting in my after auth job, but when I uninstall my app then reinstall it, I can't tell if the application charge was created. I wanted to know how other users in the community have implemented their charges using Ruby on Rails.
Your using the Shopify App gem so it should of had created the Shops table there, add a column activated:bool or plan:int if you have multiple plans. Next in your main/landing page/home/dashboard/root controller action, add a guard clause something like:
redirect to choose_plan_path unless shop.plan # assuming its nil by default # or unless shop.activated recurring_charge = ShopifyAPI::RecurringApplicationCharge.new(params_hash) if recurring_charge.save redirect_to recurring_charge.confirmation_url end end
Then your user will be redirected to a page where they can accept or decline the, if they accept the user will be redirected to the return_url you passed in the RecurringCharge parameters, there you can check the charge details and update the activated/plan column accordingly. Afterwards redirect to root/homepage.
Also you should add checks for shop.activated in your controller actions just to be sure no one is using it without paying but I doubt that anyone will, but its good to have checks.
Note: This is my version you can customize it according to your needs/flow.
@Marslan Thanks for this!
I almost have it working. What is the confirmation_url method? I can get the "approve charge" screen to show up, but when i click approve, the app says "waiting for confirmation, then just redirects back to the app charge screen".
A confirmation_url is an URL that you redirect the User so they can accept the charge. It is populated by Shopify when you create/save the recurring_charge.
Also if your seeing the accept charge page then you are already successfully redirecting to the confirmation_url. Next step is has focus on the return_url.
Really appreciate the help!
I have in my method
unless @current_shop.active_billing recurring_charge = ShopifyAPI::RecurringApplicationCharge.new( name: "Activation", price: 9.99, trial_days: 7, return_url: "myurl.com/activatecharge" ) if recurring_charge.save redirect_to recurring_charge.confirmation_url end end
then i have in my routes:
get "/activatecharge", to: "mycontroller#activatecharge"
And the method:
def activatecharge recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.find(request.params['charge_id']) recurring_application_charge.status == "accepted" ? recurring_application_charge.activate : redirect(root_path) redirect root_path end
It's still just loading and going back to the charge screen. Here's a recording =>
This is an accepted solution.
Then your user will be redirected to a page where they can accept or decline the charge, if they accept the user will be redirected to the return_url you passed in the RecurringCharge parameters, there you can check the charge details and update the activated/plan column accordingly. Afterwards redirect to root/homepage.