The Cocoon iOS AppStore InApp Purchases plugin is a Cordova plugin aimed at providing easy integration with  the iOS AppStore InApp Purchases  API.

Installation

To add the iOS AppStore 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 iOS plugin should be filtered in the plugins list.

Screen Shot 2016-01-18 at 13.32.39

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

Screen Shot 2016-01-18 at 13.34.06

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 iOS AppStore 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.

The autofinish flags will make the transaction to be finished automatically (iOS only) and will remove it from the transactions queue. You can manually finish the transaction calling the finishPurchase method. If you don’t call this method or set the autofinish to true you will keep on getting purchase complete callbacks again and again.  It is true by default.

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 iOS AppStore 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.

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 InApp Purchases API methods.

Example

exmaple_button

API

api_button

Testing

Testing the iOS AppStore InApp purchases integration requires configurations in iTunes Connect and the Apple Developer Center. We also recommend that you read the InApp purchases guide before start setting up the plugin.

  • Enable InApp purchases for your App ID in the Apple Developer Center.

Screen Shot 2016-01-18 at 17.33.41

Screen Shot 2016-01-18 at 17.35.11

Screen Shot 2016-01-18 at 17.35.24

  • Create products for your application.

Screen Shot 2016-01-18 at 17.44.53 Screen Shot 2016-01-18 at 17.44.45

  • Make sure that when testing in the device you are logged out from the App Store in your phone. You need to open your app while logged out from the AppStore and log in with a sandbox account.
  • Install the iOS AppStore 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.
  • Launch your app from the Developer App using a zip or a url.

All this process is tedious so we recommend to use the Developer App.