Help with Ruby on Rails App Application Charge

Solved
Highlighted
Tourist
8 0 0

Hi,

 

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.

 

I've looked at: https://community.shopify.com/c/Shopify-APIs-SDKs/One-Time-Application-Charge-Example-for-Shopify-Ra...

and https://help.shopify.com/en/api/tutorials/adding-billing-to-your-app

and https://github.com/Shopify/billing-demo-app

 

Any help would be greatly appreciated! 

0 Likes
Highlighted
Shopify Partner
43 4 8

https://help.shopify.com/en/api/tutorials/adding-billing-to-your-app

Pretty much tells most of the things you need to know. Can you specify which areas are you having issues in?

Need some help or work done? Hire me!
0 Likes
Highlighted
Tourist
8 0 0

Hi @Marslan

 

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:

 

ShopifyAPI::RecurringApplicationCharge.create({
: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.

 

Thanks!

0 Likes
Highlighted
Shopify Partner
43 4 8

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

 

If your app is embedded you might need to pass the confirmation_url into JavaScript and and redirect from there using JS otherwise you'll run into i-frame issues.

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.

Need some help or work done? Hire me!
0 Likes
Highlighted
Tourist
8 0 0

@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".

 

Any insight?

0 Likes
Highlighted
Shopify Partner
43 4 8

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.

https://help.shopify.com/en/api/reference/billing/recurringapplicationcharge

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.

https://help.shopify.com/en/api/tutorials/adding-billing-to-your-app#step-2-activate-the-charge

Need some help or work done? Hire me!
1 Like
Highlighted
Tourist
8 0 0

Hi @Marslan 

 

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 => 

http://dev.seedlogic.com.s3.amazonaws.com/Screen%20Recording%202020-01-11%20at%203.07.47%20PM.mov

 

Any thoughts?

 

 

0 Likes
Highlighted
Shopify Partner
43 4 8

This is an accepted solution.


@Marslan wrote:

 

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.

 


 

Need some help or work done? Hire me!
0 Likes
Highlighted
Tourist
8 0 0

With some tweaking, I got it to work.

 

Appreciate your help!

0 Likes