How To Implement Recurring Application Charge API To Shopify Apps


Did you know that you can earn money when you develop Shopify apps? Yes, you can!

When you create Shopify apps, you’re obviously allowed to either charge your users only for one-time or monthly. You can even not charge them at all.

You can do this by implementing Billing API which is divided into four different API resources:

  • RecurringApplicationCharge
  • ApplicationCharge
  • UsageCharge
  • ApplicationCredit

In this article, we’ll be focusing more on implementing monthly charges to users who use your Shopify apps which of course we’re talking about RecurringApplicationCharge.


before we go deeper into that topic. Let’s understand first…

What is Billing API?

Billing API is an API used by Shopify apps for collecting a one-time charge, a fixed monthly fee, or a monthly fee based on the usage of the users.

Like what we have mentioned before, Billing API is divided into four API resources:

  • RecurringApplicationCharge which is used for charging users every 30-days;
  • ApplicationCharge used especially by apps that provide free-trial then charge a one-time payment after the trial ended;
  • UsageCharge used for charging users depending on their usage, and
  • ApplicationCredit is used for future purchases inside the app hence the name ApplicationCredit.

There are several reasons why you should use Billing API for charging your users.

  • The payment appears directly on the merchant’s Shopify invoice. They don’t have to enter their credit card details to pay for the apps.
  • Higher conversion rates because payments come directly from Shopify.
  • Tax is 100% correct and charged automatically
  • Lastly, Shopify handles all the chargebacks.


How it works?

When you implement Billing API to you Shopify apps, the API usually process in the following flow:


Triggers the Billing charge

This can be done by the merchant either by installing the app or by clicking on a button.


The app creates the charge

The app will create the Billing which can be either one-time charge or recurring charge.


Shopify verifies the charge

Shopify will verify the charge created by the app and once verified, Shopify will redirect the merchant to the confirmation_url


Merchant confirms the charge

If the merchant confirms the charge, he/she will be redirected to the return_url  specified in the API. Otherwise notify that the charge was declined.


Implement Recurring Billing API

Creating a charge using the RecurringApplicationCharge resource is very easy and it takes just a couple of lines of codes to be able to create a charge.

If you have been following our Shopify app development tutorial series, you have the advantage of learning this course much easier because we’ll be using the very same method as before.

Read moreHow to build Shopify apps in 10 minutes

Creating Recurring Application Charge

In this course, we’ll assume again that you have followed our Shopify app development course and you’re using the very same files like ours.

To start, open your index.php file and add the following HTML code.

<hr> <strong>Aha!</strong> <p>You're still using our free trial. Click the upgrade button below to have access to more awesome features!</p> <a href="upgrade.php?<?php echo $_SERVER['QUERY_STRING']; ?>" target="_blank">Upgrade</a>

To help you catch up. This is our full index.php file. Feel free to copy it. Just make sure to replace the value of $token and $shop variable.

<?php require_once("inc/functions.php"); $requests = $_GET; $hmac = $_GET['hmac']; $serializeArray = serialize($requests); $requests = array_diff_key($requests, array( 'hmac' => '' )); ksort($requests); $token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $shop = "weeklyhow"; //change this to your store subdomain. //for example: // //your $shop variable is: //test-store ?> <!DOCTYPE html> <html> <head> <title>Shopify Example App</title> </head> <body> <h1>Shopify App Example by WeeklyHow</h1> <hr> <strong>Aha!</strong> <p>You're still using our free trial. Click the upgrade button below to have access to more awesome features!</p> <a href="upgrade.php?<?php echo $_SERVER['QUERY_STRING']; ?>" target="_blank">Upgrade</a> </body> </html>

Next, we’re going to create a new file.

Name it upgrade.php

We’ll do the very same thing here:

  • Require the functions.php
  • Get the $_GET variable
  • Get the hmac
  • Declare $token and $shop variables.

Your upgrade.php file should also look like this:

<?php require_once("inc/functions.php"); $requests = $_GET; $hmac = $_GET['hmac']; $serializeArray = serialize($requests); $requests = array_diff_key($requests, array( 'hmac' => '' )); ksort($requests); $token = "xxxxxxxxxxxxxxxxxxxxx"; $shop = "weeklyhow"; ?>

Next, we’re finally going to create the Billing API object. To do that, we’ll use the following code.

$array = array( 'recurring_application_charge' => array( 'name' => 'Example Plan', 'test' => true, //remove this line before sending to app store 'price' => 15.0, 'return_url' => '' . $_SERVER['QUERY_STRING'] ) ); $charge = shopify_call($token, $shop, "/admin/api/2019-10/recurring_application_charges.json", $array, 'POST'); $charge = json_decode($charge['response'], JSON_PRETTY_PRINT);

Make sure to copy the code above and write it below the $shop variable.

Next, once the code above is processed successfully, our $charge variable will give us a confirmation URL that we can use to redirect our users to the payment confirmation page.

To do that, we can use the following code:

header('Location: ' . $charge['recurring_application_charge']['confirmation_url'] ); exit();

And that’s it! We can now save the scripts and see the results.

Shopify App example by weeklyhow

There’s our result! Now, if we click the upgrade link, we should be redirected to the payment confirmation page like below:

Billing API recurring application charge tutorial

If in case you received an error like below, that means you’re using a development store or your billing API is not on test mode.

Billing API application charge upgrade your account error

To successfully create a charge. Make sure you test the app with your upgraded Shopify store.


Activating the charge

If the merchant accepts the confirmation and paid your app for the monthly subscription, you need to activate the charge so the merchant can enjoy all the benefits awaiting for the merchant.

To do that, we’re going to modify our index.php once again and add the following code. Just below the $shop variable.

if( isset($_GET['charge_id']) && $_GET['charge_id'] != '' ) { $charge_id = $_GET['charge_id']; $array = array( 'recurring_application_charge' => array( "id" => $charge_id, "name" => "Example Plan", "api_client_id" => rand(1000000, 9999999), "price" => "1.00", "status" => "accepted", "return_url" => "", "billing_on" => null, "test" => true, "activated_on" => null, "trial_ends_on" => null, "cancelled_on" => null, "trial_days" => 0, "decorated_return_url" => "" . $charge_id ) ); $activate = shopify_call($token, $shop, "/admin/api/2019-10/recurring_application_charges/".$charge_id."/activate.json", $array, 'POST'); $activate = json_decode($activate['response'], JSON_PRETTY_PRINT); print_r($activate); }

The code above will simply check if the Billing API has successfully provided a charge_id. If so, then we create an array of data to pass onto the next API call which will activate the charge.

Make sure you replace the value of the return_url with your development store URL or if you are using a database, just retrieve the merchant’s store URL.

By saving and running your Shopify app, you should see the following.

Billing API recurring application charge tutorial
Recurring Application Charge API for Shopify Billing API tutorial PHP

Not only that, but you will also receive an email that says your charge is approved which is awesome!

Test charge email for Shopify recurring charge API tutorial PHP


Congratulation! Now you can earn money with your Shopify app. But wait a minute, what’s the next step? After activating their subscription, how can I give the merchants the benefits of paying for the app?

The answer is very simple. Just verify if the merchant’s subscription is currently active. If it is, then the app should provide more features. Otherwise, give only the features that are only available for non-paying merchants.

Database is your friend here.

Using database, you can store their subscription’s status and there you can check if the merchant’s subscription is active or not.

We have written an article that can help you integrate your Shopify app with MySQL database.

React to this topic
Notify of
Newest Most Voted
Inline Feedbacks
View all comments