Auto-Renewing Subscription on Android
In this guide, we will build a small application with a subscription that works on Android with Google Play.
We will proceed in steps: setup, initialization, presentation and purchase.
First some setup.
Install NodeJS and Cordova
Setup your Cordova project
Prepare an Application on Google Play
Install the In-App Purchases plugin
Build a Release APK
Create a Product on Google Play
Upload a Release APK to Google Play
Prepare Test Accounts
Of couse you can skip the first few steps if you already have a working application you want to integrate the code into.
Once we have a Cordova application with IAP support enabled and everything is in place on Google Play, we will get into some coding.
Initialize the in-app purchase plugin
Handle the purchase events
Deliver our product
Secure the transactions
Setup
1. Install Dependencies
Needless to say, make sure you have the tools installed on your machine. Developing from a mac is generally recommended for doing iOS development, it's way easier. If you only plan on doing Android, then everything will work.
During the writing of this guide, I've been using the following environment:
NodeJS v10.12.0
Cordova v8.1.2
macOS 10.14.1
I'm not saying it won't work with different version. If you start fresh, it might be a good idea to use an up-to-date environment.
2. Create Cordova Project
Making sure we have a Cordova project that we can build for Android and/or iOS.
Create the project
If it isn't already created:
For details about what those parameters are:
Note, feel free to pick a different project ID and name. Remember whatever values you put in here.
Let's head into our cordova project's directory (should match whatever we used in the previous step.
Add Android platform
Will output:
Let's check if that builds.
Which outputs:
Alright, seems like we have no problems with our Android build chain. If you do have problems, fixing it is out of scope from this guide but it's required!
3. Create Google Play Application
Make sure we have a Google Play application created and configured.
Create the App
Open the Google Play Console.
Click "Create Application", fill in the required fields.
Need more help? I recommend you check Google's own documentation. It's well detailed, easy to follow and probably the most up-to-date resource you can find.
Retrieve the Billing Key
We need to inform the plugin of our app's BILLING_KEY
. That piece of information can be found on the Google Play Publisher Console.
So head again to your Google Play Console.
In the "All Applications" menu, go to the application you want to setup. In my case "Cordova Purchase Demo".
From there, find the "Monetization setup" section (on the left-side panel).
Under Licensing, you'll find this long Base64-encoded string (an RSA public key). Keep it around for later reference. That's your Billing Key.
The Billing Key will be required to install the plugin on Android and setup receipt validation.
4. Install Cordova Purchase Plugin
To install the plugin, we will use the usual cordova plugin add
command. There is little subtleties on Android.
When you need Android support, you need to setup your BILLING_KEY
.
You can find that piece of information on the Google Play Publisher Console, as explained here.
Now let's try to build.
Successful build?
All good! Seems like we can build an app with support for the Billing API.
Let's now prepare a release APK.
5. Android Release APK
To generate a release build, I generally use the following script: android-release.sh
The script calls cordova build android --release
with the correct command line arguments. It requires you have generated a keystore
file for your application already.
If you haven't generated a keystore file for your application yet, you can use the following command line:
I'll ask you a few questions. The only tricky one is "Do you wan't to use the same password for the alias?", the answer is yes. Please note that the above command defines the keystore's alias
as release, you can use any value, but just remember the value you chose.
Keep the android-release.keystore
file in a safe place, backup it everywhere you can! Don't loose it, don't loose the password. You won't EVER be able to update your app on Google Play without it!
Then build.
Replace $KEYSTORE_ALIAS
and $KEYSTORE_PASSWORD
with whatever match your those from your keystore
file...
The output should end with a line like this:
There you go, this is your first release APK.
6. Upload to Google Play
Once you have built your release APK, you need to upload it to Google Play in order to be able to test In-App Purchases. In-App Purchase is not enabled in "debug build". In order to test in-app purchase, your APK needs to be signed with your release signing key. In order for Google to know your release signing key for this application, you need to upload a release APK:
Signed with this key.
Have the BILLING permission enabled
it is done when you add the plugin to your project, so make sure you didn't skip this step.
Google already provides detailed resource on how to upload a release build. What we want here is to:
create an internal testing release
upload it
publish it (privately probably).
Once you went over those steps, you can test your app with in-app purchase enabled without uploading to Google Play each time, but you need to sign the APK with the same "release" signing key.
Note that it might up to 24 hours for your IAP to work after you uploaded the first release APK.
6. Create In-App Products
There is still a bit more preparatory work: we need to setup our in-app product.
Back in the "Google Play Console", open the "Store presence" ⇒ "In-app products" section.
If you haven't yet uploaded an APK, it'll warn you that you need to upload a release APK.
Once this is done, you can create a product. Google offers 2 kinds of products:
Managed Products
Subscriptions
The latest is for auto-renewing subscriptions, in all other cases, you should a "Managed Product".
Click the CREATE button.
Fill in all the required information (title, description, prices).
Make sure the Status is ACTIVE.
SAVE
And we're done!
There's might be some delay between creating a product on the Google Play Console and seeing it in your app. If your product doesn't show up after 24h, then you should start to worry.
8. Prepare Test Accounts
To test your Google Play Billing implementation with actual in-app purchases, you must use a test account. By default, the only test account registered is the one that's associated with your developer account. You can register additional test accounts by using the Google Play Console.
Navigate to Settings > Account details.
In the License Testing section, add your tester's email addresses to Gmail accounts with testing access field.
Save your changes.
Testers can begin making purchases of your in-app products within 15 minutes.
Coding
Initialization
Assuming you're starting from a blank project, we'll add the minimal amount of HTML for the purpose of this tutorial. Let's replace the <body>
from the www/index.html
file with the below.
Make sure to comment out Cordova template project's CSS.
We enabled the 'unsafe-inline'
Content-Security-Policy
by adding it to the default-src
section.
Since we'll be using Fovea.Billing, you also need to add your validation server to default-src, find your in the cordova setup documentation. On my case it's https://reeceipt-validation.fovea.cc
.
Let's now or edit the JavaScript file js/index.js
. Replace the content with the code below, which will setup a minimal application framework and render some HTML into the page based on the app state.
Now let's initialize the in-app purchase plugin, where indicated in the onDeviceReady
function.
Here's a little explanation:
We start by registering the product with ID my_subscription1
and my_subscription2
.
We declare the products as renewable subscriptions (store.PAID_SUBSCRIPTION
). ⇒ API Documentation.
We setup the link to the receipt validation server. If you're using Fovea.Billing, you'll find it here.
We setup an error handler. It will display the last error message for 10 seconds on top of the screen.
Finally, we perform the initial refresh()
of all product states. ⇒ API Documentation.
Whatever your setup is, you should make sure the initialization code is executed as soon as the javascript application starts. You have to be ready to handle IAP events as soon as possible.
Presentation
Let's now display the subscription status, as it is provided by the native platform. Before the call to store.refresh()
we will add an handler for the update
event:
This hander is called whenever there's a change in our products' state.
Now we can display some information about our products from the render()
function:
Whenever anything happens to our product, the interface will now be updated to reflect the current state.
Displaying your product information this way, i.e. exactly as loaded from the Store, is required by Apple and Google. Do otherwise and they might simply reject your application.
You can also disconnect the event handler with store.off()
so your subscription view is only updated when it's visible.
If you want a bit more background information about all of this, please check the introduction's displaying products section and the ⇒ API Documentation for full details about the fields found for a product.
Purchase
So far so good, but what if we could actually initiate a purchase? To do so, we'll add a purchase button for both products. We already added placeholders for the purchase buttons, let's create them. Before displaying a purchase button, we need to make sure the user can actually purchase the item. It could be impossible for a few reasons: there's already a purchase in progress, the product is already owned, the feature is disabled for the user (Child Mode).
In our render()
function, we update the code that initialized purchaseProduct1
and purchaseProduct2
.
We could make this a little nicer by changing the button labels to "Upgrade" or "Downgrade" when the other product is owned
, I will leave this as an exercise to the reader.
Now, let's build and test!
Extra step for Android
If using the Fovea validation service, expiryDate
and some other features of the API for an auto-renewing Android subscription will only be available if you complete the "Connect With Google" step using the explainer here.
Testing
To test on Android with In-App Purchases enabled, I always chose to run my app through Android Studio. This way, I can see the logs from both the javascript and native sides, which is useful.
To create a build, first update the Android project on the console, run
Run.
Purchase Flow
We already added a "Buy" button. This button calls the store.order()
method which initiates the purchase flow for a product.
At this point, the code starts the process but the purchase will remain "processing" forever, in the approved
state.
For a product in the approved
state, the transaction has been approved by the user's banking institurion but it won't be finalized until you inform them to do so. You have to deliver whatever the user purchased before finalizing.
I already introduced the purchase flow in the introduction of this guide, you can check the purchase process section if you need a refresher. The official documentation provides even more details. ⇒ API Documentation
When the user is done with the native interface (i.e. has entered his/her password and confirmed), your app receives the approved
event. So let's add more handlers to the onDeviceReady()
function, before the call to store.refresh()
.
That's enough for a local implementation (where we don't need to inform a server of changes to the subscription status). Let's try the whole thing now. Repeat the steps from the testing section above:
Run from Android Studio and here you go! You should be able to purchase your subscriptions.
Full source for this tutorial below:
Last updated