Monaca Docs
Search…
Cordova In-app Purchase Plugin
This Cordova/PhoneGap plugin is used for In-App purchases on iOS, Android and Windows apps.
In-app billing setup in the Google Play Console and App Store Connect is required in order to make the demo app to work.
To check the third party Cordova plugins, you need to create a custom build debugger (Android version or iOS version).

Demo

Enable the plugin in the Monaca IDE

    1.
    From the IDE menu, go to Config → Manage Cordova Plugins .
    2.
    Click the Import Cordova Plugin button. Then, you can choose to import the plugin using a ZIP file or a URL/package name.

Usage

The store API is mostly events based. As a user of this plugin, you will have to register listeners to changes happening to the products you register.
The core of the listening mechanism is the when() method. It allows you to be notified of changes to one or a set of products using a query mechanism:
1
store.when("product").updated(refreshScreen);
2
store.when("full version").owned(unlockApp);
3
store.when("subscription").approved(serverCheck);
4
store.when("downloadable content").downloaded(showContent);
5
etc.
Copied!
The updated event is fired whenever one of the fields of a product is changed (its owned status for instance).
This event provides a generic way to track the status of the purchases, to unlock features and to refresh the views accordingly.

API references

This section describes some of the main functions used in the demo. For complete API references, please refer to the repository.

store.verbosity

The verbosity property defines how much the code in the store.js file writes to the console. Available values:
    store.QUIET or 0 to disable all logging (default)
    store.ERROR or 1 to show only the error messages
    store.WARNING or 2 to show also the warnings
    store.INFO or 3 to show also the information messages
    store.DEBUG or 4 to show also the internal debugging messages.
1
store.verbosity
Copied!
Example
1
// Enable maximum logging level
2
store.verbosity = store.DEBUG;
Copied!

store.register()

Register a product to the store.
1
store.register(product);
Copied!
Parameter
Name
Type
Description
product
JSON Object
Product information
Example
1
store.register({
2
id: 'consumable1', // id without package name!
3
alias: 'Extra Life',
4
type: store.CONSUMABLE
5
});
Copied!

store.refresh()

Load the product data from the servers and restore whatever has already been purchased by the user.
1
store.refresh();
Copied!
Example
1
// ...
2
// register products and events handlers here
3
// ...
4
//
5
// then and only then, call refresh.
6
store.refresh();
Copied!

store.get(id/alias)

Retrieve a product by its id or alias.
1
store.get(id/alias)
Copied!
Name
Type
Description
id
String
Product ID
alias
String
Product name
Example
1
var product = store.get("consumable1");
Copied!

store.when(query, event, callback)

Register a callback for a product related event. For more details about this api, please refer to here.
1
store.when(query, event, callback)
Copied!
Return Value
    Promise
Example
1
store.when("subscription1", "approved", function(product) {
2
console.log("Subscription approved!");
3
});
Copied!

store.error(callback)

Register an error handler.
1
store.error(callback);
Copied!
Example
1
store.error(function(e){
2
console.log("ERROR " + e.code + ": " + e.message);
3
});
Copied!

store.ready(callback)

Register the callback to be called when the store is ready to be used.
If the store is already ready, callback is executed immediately. store.ready() without arguments will return the ready status.
1
store.ready(callback)
Copied!
Example
1
store.ready(function() {
2
console.log("Store is ready");
3
});
Copied!
Last modified 2mo ago