-
-
Notifications
You must be signed in to change notification settings - Fork 535
v13 tutorials: Unlock Features with a Non Consumable
A non-consumable product is a product that can only be purchased once, and the user will then own forever. A good example of that is an in-app purchased used to unlock a feature in the app.
Let's continue from were we were in the First Steps Tutorial and implement a non-consumable purchase.
The plugin puts all definitions in the CdvPurchase
namespace, for example:
-
CdvPurchase.store
- the global object to access the features of the plugin. -
CdvPurchase.ProductType
- enumeration with the list of product types. - etc.
For making the code more readable, let's add shortcuts in the beginning of the onDeviceReady
function:
function onDeviceReady() {
const {store, ProductType, Platform} = CdvPurchase;
/* ... */
}
Later, if we want shortcuts to other types from the CdvPurchase
namespace, we will add them there.
First, let's register our test non-consumable product by updating the call to CdvPurchase.store.register()
. Let's also making use of the shortcuts we just defined.
function onDeviceReady() {
/* ... */
store.register([{
type: ProductType.NON_CONSUMABLE,
id: 'test-non-consumable',
platform: Platform.TEST,
}]);
}
The code to handle the purchase flow remains the same:
store.when().approved(transaction => transaction.finish());
store.get('test-non-consumable').getOffer().order();
Then we'll update the onTransactionApproved()
function to
Full code:
const { store, ProductType, Platform } = CdvPurchase;
store.register([{
type: ProductType.NON_CONSUMABLE,
id: 'test-non-consumable',
platform: Platform.TEST,
}]);
store.when()
.approved(transaction => {
unlockFeature();
transaction.finish();
});
store.initialize([Platform.TEST]);
function unlockFeature() {
alert('full version unlocked');
}
function buy() {
CdvPurchase.store.get('test-non-consumable').getOffer().order();
}