Micro Example
js/index.js
document.addEventListener('deviceready', onDeviceReady);
var myProduct;
function onDeviceReady() {
const {store, ProductType, Platform} = CdvPurchase;
refreshUI();
store.register([{
type: ProductType.CONSUMABLE,
id: 'my_product',
platform: Platform.TEST,
]});
store.when()
.productUpdated(refreshUI)
.approved(finishPurchase);
store.initialize([Platform.TEST]);
}
function finishPurchase(transaction) {
localStorage.goldCoins = (localStorage.goldCoins | 0) + 10;
transaction.finish();
refreshUI();
}
function refreshUI() {
const {store, ProductType, Platform} = CdvPurchase;
myProduct = store.get('my_product', Platform.TEST);
const myTransaction = store.findInLocalReceipts(myProduct);
const button = `<button onclick="myProduct.getOffer().order()">Purchase</button>`;
document.getElementsByTagName('body')[0].innerHTML = `
<div>
<pre>
Gold: ${localStorage.goldCoins | 0}
Product.state: ${myTransaction ? myTransaction.state : ''}
.title: ${myProduct ? myProduct.title : ''}
.descr: ${myProduct ? myProduct.description : ''}
.price: ${myProduct ? myProduct.pricing.price : ''}
</pre>
${myProduct.canPurchase ? button : ''}
</div>`;
}index.html
Last updated