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:
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.
But…
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:
There are several reasons why you should use Billing API for charging your users.
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 more: How 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>
Code language: HTML, XML (xml)
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:
//test-store.myshopify.com
//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>
Code language: PHP (php)
Next, we’re going to create a new file.
Name it upgrade.php
We’ll do the very same thing here:
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";
?>
Code language: PHP (php)
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' => 'https://weeklyhow.myshopify.com/admin/apps/exampleapp-14/?' . $_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);
Code language: PHP (php)
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();
Code language: PHP (php)
And that’s it! We can now save the scripts and see the results.
There’s our result! Now, if we click the upgrade link, we should be redirected to the payment confirmation page like below:
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.
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" => "https://weeklyhow.myshopfy.com/admin/apps/exampleapp-14",
"billing_on" => null,
"test" => true,
"activated_on" => null,
"trial_ends_on" => null,
"cancelled_on" => null,
"trial_days" => 0,
"decorated_return_url" => "https://weeklyhow.myshopfy.com/admin/apps/exampleapp-14/?charge_id=" . $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);
}
Code language: PHP (php)
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.
Not only that, but you will also receive an email that says your charge is approved which is awesome!
Conclusion
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.
Hello Sir,
I have created a Shopify app. I am going to live this public app. But I have set pricing in APP Listing “Free to Install”. Then which billing API I have to use for my app and how? Can you help me?
Hi Sam,
Thanks for reading our guide! If your app is set for free then you don’t need to apply a billing/charge API to your application.
Check our guide on how to publish your free Shopify app here: https://weeklyhow.com/publishing-apps-to-shopify-app-store/
Good luck 🙂
Hello Sir, if I have to enable 7 days free trial then do I need to enter it in upgrade.php as “trial_days” => 7, and in index.php after accepting the charge set it to “trial_days” => 0.
Hi Abrarul,
No, you have to give them the same value 🙂
Hello Sir,
I have gone through your guide for publishing a free public app. and I have set “Free to install” Pricing option in the App listing section. But my app have been rejected by Shopify guy with the reason that your App is not using Billing API.
They also provide me the link: https://shopify.dev/concepts/app-store/getting-your-app-approved/app-requirements#b-billing
Can you guide me on why my app has been rejected?
Should I use the BIlling API as it is “Free to Install”?
Hi, I am building an app by your tutorial. Want to apply recurring application charge with free trial. Everything works by your tutorial. Merchant can pay by pushing Upgrade button. After submit my app I got this message from support: “Your app is not using the Billing API. All charges and fees associated with your service must exclusively use the Billing API when installed through the Shopify App Store”. I dont exactly understand what should I do? Is I should add your example billing code to install proccess?
Hi there
I’m building an public app and I’m using recurring charges in it.
I’m confused about one thing that will I need to create recurring charge, every month, in Shopify from server side or shopify will automatically handle subsequent monthly charges by itself once the first charge is approved.
I’m would be really thankful if you could help me with this.
Hello
in my case when clicking the upgrade button it goes to my hosting’s upgrade.php file (where are file like index.php,function.php exist etc) and then screen blank
Please help me what was wrong with me
Thank you