Micro Example
In-App Purchase involves a fair amount of configuration. In order to get a glimpse at a complete integration, you'll find here a minimal example and the expected result when run on a device.
This example is a Cordova application. The index.html does nothing but load js/index.js.
js/index.js
index.html
1
document.addEventListener('deviceready', onDeviceReady);
2
3
function onDeviceReady() {
4
refreshUI();
5
store.register({type: store.CONSUMABLE, id: 'my_product'});
6
store.when('my_product')
7
.updated(refreshUI)
8
.approved(finishPurchase);
9
store.refresh();
10
}
11
12
function finishPurchase(p) {
13
localStorage.goldCoins = (localStorage.goldCoins | 0) + 10;
14
p.finish();
15
refreshUI();
16
}
17
18
function refreshUI() {
19
const product = store.get('my_product');
20
const button = `<button onclick="store.order('my_product')">Purchase</button>`;
21
22
document.getElementsByTagName('body')[0].innerHTML = `
23
<div>
24
<pre>
25
Gold: ${localStorage.goldCoins | 0}
26
27
Product.state: ${product.state}
28
.title: ${product.title}
29
.descr: ${product.description}
30
.price: ${product.price}
31
32
</pre>
33
${product.canPurchase ? button : ''}
34
</div>`;
35
}
Copied!
1
<!DOCTYPE html>
2
<html>
3
<body>
4
<script type="text/javascript" src="cordova.js"></script>
5
<script type="text/javascript" src="js/index.js"></script>
6
</body>
7
</html>
Copied!
Launching this on a device...
Note that it's a simple example that doesn't handle error cases, but it's fully functional. In-App Purchases don't have to be super hard!
Last modified 2yr ago
Copy link