The Cocoon Google Play Store InApp Purchases plugin is a Cordova plugin aimed at providing easy integration with  the  Google Play Store InApp Purchases  API.

Installation

To add the Google Play Store InApp Purchases plugin to your project first go to your project configuration and once there go to the Setting  section and click on the “Plugins” tab. You should see something like this:

New Cocoon

If you click on the “Cocoon” category you will see al the Cocoon available plugins.  Over the plugins list you will see a dropdown menu set to “All” by default. Click on the menu to see the available Cocoon plugins categories, you should see a list of plugins with at least the following groups:

Screen Shot 2016-01-05 at 16.40.33

Select the “In-App Purchases” category and the In-App Purchases for Google Play plugin should be filtered in the plugins list.

Screen Shot 2016-01-15 at 10.55.38

Now click on install and your project will include the In-App Purchases for Google Play plugin in the next compilation.

Screen Shot 2016-01-15 at 10.56.30

Parameters

This plugins doesn’t need any parameters.

Integration

Cordova needs to be specifically loaded in your index.html so it can start the Cordova framework and load the plugins that you have installed in your project. So first thing is to add the “cordova.js” script tag to your index.html.

That’s the only code script you need to include in your index.html for the plugin to be ready to be used. Cordova will do the rest.

Important: Remember to wait for the “deviceready” event before using the plugin as that is the moment when the plugin will be fully initialized.

Usage

Let’s see some code snippets showing how to use the Cocoon Google Play Store InApp Purchases plugin in your application.

Initialization

First thing you need to do is to setup the purchases events listeners for purchase, error and complete as you can start receiving callbacks as soon as you initialize the service.

Fetch products

This methods returns all the products that you have created in the store so you can list them to the user.

Important: The Google Play Store InApp Purchases plugin only supports managed products and it doesn’t support subscriptions.

Get cached products

If you have already called the fetchProducts method and you just want to get the already fetched ones, you can just call getProducts.

Purchase products

This method starts a purchase for a productId. You can optionally pass a callback for errors although you will also be notified by the service error callback.

Consume product (Android only)

In Android you have to call this method so the store knows that the user has consumed this consumable otherwise the Android store won’t let you purchase that product id again. I.e. You sell extra lives for $0.89 each. A user buys one. If you don’t consume that item after the purchase the user won’t be able to buy another extra life. This is not a Cocoon implementation related issue is how the Google Play Store works. You have more information on this here.

Check if a product has been purchased

Purchases verification

The purchases should be verified to check that the purchase is legit. You have two ways of doing this.

Custom verification

You can get the verification call back with the productId and the receipt so you can just acknowledge it or verify it in your own server. You can read how to do it here.

Ludei verification

Another option is let the Ludei servers to verify the purchase for you. We are currently working on this verification mode for the Cocoon cloud so it doesn’t work at the moment of writing this.

Other API methods

Please see the full API below and the example for more Google Play Store InApp Purchases API methods.

Example

exmaple_button

API

api_button

Testing

Testing the Google Play Store InApp Purchases integration is not easy, the process requires configurations in the Google Developer Console:

  • Setup the app and set it ready to be published
  • Setup the Alpha testing in your project in the Developer Console. You can setup the alpha testing mode your prefer but remember to create a list with the email addresses of the alpha testers. Ideally these emails will be the same as in the Games Services testing list.

Screen Shot 2016-01-14 at 16.44.05

  • Create products for your application and publish them

Screen Shot 2016-01-18 at 13.03.45

  • Install theGoogle Play Store InApp Purchases plugin in your Cocoon project.
  • Compile a signed Developer App. It’s important that it’s signed as it’s a requirement for being able to test it later.
  • Upload your APK  to the Alpha and publish it. Don’t worry, the application is not visible to the general public, it’s only accessible by the alpha tester that have the link. You will see the link after the APK is processed. Once the link is available the alpha testers can use it to install the APK and start testing the APK.
  • Launch you app from the Developer App using a zip or a url.

All this process is tedious so we recommend to use the Developer App so you don’t have to upload a new APK each time you modify your code.