0
I’m studying Ordova and I’ve reached the point of adding Billing to my Apks So I’m using the plug "Cordova-plugin-Purchase", with the example code below (taken from the page itself https://gist.github.com/j3k0/3324bb8e759fef4b3054b834a5a88500), which works perfectly but for only one product!
My problem/question is the following: How to adjust the code below so I can have various different products with different values...
- I tried to duplicate the functions by adjusting their names and variables
- also tried to adjust the store.Register object
But I did not succeed... I appreciate the help!
Here’s the code I’m using to study:
<html>
<div class="app" style="border:1px solid red;">
<p id="gold-coins">
gold:
</p>
<div id="consumable1-purchase">
wait please...
</div>
</div>
</html>
<script>
//
// Consumable
//
document.addEventListener('deviceready', initStore);
document.addEventListener('deviceready', refreshGoldCoinsUI);
console.log("_____billing: ")
// $('#findResults').append("<br>_____billing: ")
function initStore() {
if (!window.store) {
$('#findResults').append('<br>Store not available');
console.log('Store not available');
return;
}
store.verbosity = store.INFO;
store.register({
id: 'value500',
alias: 'value500',
type: store.CONSUMABLE
});
store.error(function(error) {
$('#findResults').append('<br>ERROR ' + error.code + ': ' + error.message);
console.log('ERROR ' + error.code + ': ' + error.message);
});
store.when('value500').updated(refreshProductUI);
store.when('value500').approved(function(p) {
p.verify();
});
store.when('value500').verified(finishPurchase);
store.refresh();
}
//
// update tue amount of coins to your game!
//
function refreshGoldCoinsUI() {
document.getElementById('gold-coins').innerHTML =
'Gold: <strong>' + (window.localStorage.goldCoins | 0) + '</strong>';
}
// show it
function refreshProductUI(product) {
const info = product.loaded
? `<h1>${product.title}</h1>` +
`<p>${product.description}</p>` +
`<p>${product.price}</p>`
: '<p>Retrieving info...</p>';
const button = product.canPurchase
? '<button onclick="purchaseConsumable1()">Buy now!</button>'
: '';
const el = document.getElementById('consumable1-purchase');
el.innerHTML = info + button;
}
function purchaseConsumable1() {
store.order('value500');
}
function finishPurchase(p) {
window.localStorage.goldCoins = (window.localStorage.goldCoins | 0) + 10;
p.finish();
refreshGoldCoinsUI();
}
</script>