Skip to content

v13 tutorials: Unlock Features with a Non Consumable

Jean-Christophe Hoelt edited this page Nov 7, 2022 · 5 revisions

DRAFT

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.

Prelude

The plugin puts all definitions in the CdvPurchase namespace, for example:

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.

Initialization

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();
}