Namespace: InApp

Cocoon. InApp

Cocoon.InApp class provides a multiplatform, easy to use and secure in-app purchase API. Built-in support for local and server-side receipt validation, consumable and non-consumable purchase tracking and local products cache. Single JavaScript API for multiple IAP providers.

Example

// Basic usage, register callbacks first
service = Cocoon.InApp;
service.on("purchase", {
    start: function(productId) {
        console.log("purchase started " + productId);
    },
    error: function(productId, error) {
        console.log("purchase failed " + productId + " error: " + JSON.stringify(error));
    },
    complete: function(purchase) {
        console.log("purchase completed " + JSON.stringify(purchase));
    }
});

// Service initialization
service.initialize({
    autofinish: true
}, 
function(error){
    if(error){
        console.log("Error: " + error);
    }
});

// Fetching products from server 
service.fetchProducts(productIds, function(products, error){
   if(error){
       console.log("Error: " + error);
   }
   else {
       var next = [];
       for (var i = 0; i < products.length; ++i) {
           var product = products[i];
           console.log(product);
       }
   } 
});   

// Purchasing products
service.purchase(product.productId, 3, function(error) { // Optional sugar callback
     if(error){
          console.log("Error: " + error);
     }
     else {
          console.log("Successfully purchased);    
     }
});

Methods

<static> canPurchase() → {boolean}

This method allows you to check is the InApp service is available and enabled in this platform. Not all iOS and Android devices will have the InApp service available or enabled. so you should check if it is before calling any other method.

Returns:

True if the service is available and false otherwise. Cocoon.InApp.canPurchase();

Type
boolean

<static> consume(productId, quantity, callback)

Consumes a purchase. This makes that product to be purchasable again (on Android).

Parameters:
Name Type Description
productId string

The id or alias of the product to be consumed.

quantity number

The quantity to be consumed, default value 1.

callback function

The callback function. It receives the following parameters:

  • Consumed - The quantity consumed.
  • Error.
Example
Cocoon.InApp.consume(product.productId, 3, function(consumed, error) {
    if(error){
         console.log("Error: " + error);
    }
    else{
         console.log("Consumed items: " + consumed);
    }       
});

<static> fetchProducts(productIds, callback)

Fetches the products information from the store.

Parameters:
Name Type Description
productIds object

Array of ids of products.

callback function

The callback function.

Example
Cocoon.InApp.fetchProducts(["magic.sword", "health.potion"], function(products, error){
    if(error){
         console.log("Error: " + error);
    }
    else{
         console.log(JSON.stringify(products));
    }     
});

<static> finishPurchase(transactionId)

Finishes a purchase transaction and removes the transaction from the transaction queue. You don't need to finish purchases if the autoFinishPurchases param is enabled in initialization (enabled by default) This method must be called after a purchase finishes successfully and the "success" event inside of the "on purchase products" callback has been received. If the purchase includes some asset to download from an external server this method must be called after the asset has been successfully downloaded. If you do not finish the transaction because the asset has not been correctly downloaded the "purchase" event will be called again later on.

Parameters:
Name Type Description
transactionId string

The transactionId of the purchase to finish.

Example
Cocoon.InApp.finishPurchase(product.TransactionId);

<static> getProducts() → {object}

Returns all the locally cached InApp products.

Returns:

An array of Cocoon.InApp.Product that represents all the local products.

Type
object
Example
var products = Cocoon.InApp.getProducts();

<static> initialize(params, callback)

Starts the InApp Service. This will make the system to initialize the InApp callbacks will start to be received after calling this method. Because of this, you should have set your event handler before calling this method, so you won't lose any callback.

Parameters:
Name Type Description
params Cocoon.InApp.Settings

The initialization params.

callback function

The callback function.It receives the following parameters:

  • Error.
Example
Cocoon.InApp.initialize({
    autofinish: true
}, function(error){
     if(error){
          console.log("Error: " + error);
     }
});

<static> isPurchased(productId) → {boolean}

Returns if a product has been already purchased or not.

Parameters:
Name Type Description
productId string

The product id of the product to be checked.

Returns:

A boolean that indicates whether the product has been already purchased.

Type
boolean
Example
console.log(Cocoon.InApp.isPurchased(productId));

<static> productForId(productId) → {Cocoon.InApp.Product}

Gets the product information given a product indetifier. It uses a local cache, so fetchProducts have to be called before if products are not saved from previous execution.

Parameters:
Name Type Description
productId string

The product id of the product to be checked.

Returns:

The product. var product = Cocoon.InApp.productForId(productId);

Type
Cocoon.InApp.Product

<static> purchase(productId, quantity, callback)

Requests a product purchase given its product id.

Parameters:
Name Type Description
productId string

The id or alias of the product to be purchased.

quantity number

The quantity to be purchased, default value 1.

callback function

The callback function. It receives the following parameters:

  • Error.
Example
Cocoon.InApp.purchase(product.productId, 1, function(error) {
    if(error){
         console.log("Error: " + error);
    }
    else {
         console.log("Successfully purchased);    
    }
});

<static> restorePurchases(callback)

Restores all the purchases from the platform's market. For each already purchased product the event "purchase" will be called.

Parameters:
Name Type Description
callback function

The callback function. It receives the following parameters:

  • Error.
Example
Cocoon.InApp.restorePurchases(function(error) {
   if (error){
      console.log("Error: " + error);
   } else {
      console.log("Purchases restored");
   }
});

<static> setLudeiServerValidationHandler()

Use Ludei's server to validate purchases. To enable validation using Ludei's server you first need to create an account in Ludei's Cloud server and create a project with you bundleId.

Example
Cocoon.InApp.setLudeiServerValidationHandler();

<static> setValidationHandler(validationHandler)

Sets a custom function to validate purchases with your own server

Parameters:
Name Type Description
validationHandler function
Example
Cocoon.InApp.setValidationHandler(function(receipt, productId, completion){
     ... //Custom validation code
     console.log("Custom validation: " + receipt + " " + productId);
     completion(true); //call completion function with true param if validation succeeds
});

<static> stockOfProduct(productId) → {number}

Returns the quantity of available items for a specific productId.

Parameters:
Name Type Description
productId string

The product id of the product to be checked.

Returns:

A Number that indicates the available quantity of a productId to consume.

Type
number
Example
console.log(Cocoon.InApp.stockOfProduct(product.productId));

Events

On purchase

Allows to listen to events about the purchasing process.

  • The callback 'start' receives a parameter the product id of the product being purchased when the purchase of a product starts.
  • The callback 'complete' receives as parameter the Cocoon.InApp.PurchaseInfo object of the product being purchased when the purchase of a product is completed.
  • The callback 'error' receives a parameters the product id and an error message when the purchase of a product fails.
Example
Cocoon.InApp.on("purchase", {
   start: function(productId) {
       console.log("purchase started " + productId);
   },
   error: function(productId, error) {
       console.log("purchase failed " + productId + " error: " + JSON.stringify(error));
   },
   complete: function(purchase) {
       console.log("purchase completed " + JSON.stringify(purchase));
   }
});

Members

<static> Product

The object that represents a product in the store.

Properties:
Name Type Description
Cocoon.InApp.Product object

The object itself

Properties
Name Type Description
productId string

The id of the product.

title string

The title of the product.

description string

The description of the product.

localizedPrice number

The price of the product in local currency.

price number

The price of the product.

<static> PurchaseInfo

The object that represents the information of a purchase.

Properties:
Name Type Description
Cocoon.InApp.PurchaseInfo object

The object itself

Properties
Name Type Description
productId string

The product id of the purchase.

transactionId string

The transaction id of the purchase.

purchaseDate timestamp

The date when the purchase was completed.

quantity number

The number of products of the productId kind purchased in this transaction.

<static> Settings

The object that represents a product in the store.

Properties:
Name Type Description
Cocoon.InApp.Settings object

The object itself

Properties
Name Type Description
autofinish boolean

If True, the transactions will finish automatically.

Atomic Plugins - JavaScript Documentation by Ludei, DocStrap Copyright © 2012-2013
The contributors to the JSDoc3 and DocStrap projects.