'How to install Appery In-app purchase cordova plugin
I have installed and activated in app purchase plugin:https://github.com/j3k0/cordova-plugin-purchase in appery cordova plugins after that I entered Npm modules,Dependancies, Providers. After that on index page/screen i added event when page create run TypeScript : document.addEventListener('deviceready', onDeviceReady);
`function onDeviceReady() {
const state = {};
function setState(attr) {
Object.assign(state, attr);
render(state);
}
setState({
error: '',
status: 'Loading...',
product1: {},
product2: {},
});
// We will initialize the in-app purchase plugin here.
function render() {
const body = document.getElementsByTagName('body')[0];
body.innerHTML = `
<pre>
${state.error}
subscription: ${state.status}
</pre>
`;
}
}
// We should first register all our products or we cannot use them in the app.
store.register([{
id: 'my_subscription1',
type: store.PAID_SUBSCRIPTION,
}, {
id: 'my_subscription2',
type: store.PAID_SUBSCRIPTION,
}]);
// Setup the receipt validator service.
store.validator = '<<< YOUR_RECEIPT_VALIDATION_URL >>>';
// Show errors for 10 seconds.
store.error(function(error) {
setState({ error: `ERROR ${error.code}: ${error.message}` });
setTimeout(function() {
setState({ error: `` });
}, 10000);
});
// Later, we will add our events handlers here
// Load informations about products and purchases
store.refresh();
}
// Called when any subscription product is updated
store.when('subscription').updated(function() {
const product1 = store.get('my_subscription1') || {};
const product2 = store.get('my_subscription2') || {};
let status = 'Please subscribe below';
if (product1.owned || product2.owned)
status = 'Subscribed';
else if (product1.state === 'approved' || product2.state === 'approved')
status = 'Processing...';
setState({ product1, product2, status });
});
function render() {
const purchaseProduct1 = '';
const purchaseProduct2 = '';
const body = document.getElementsByTagName('body')[0];
body.innerHTML = `
<pre>
${state.error}
subscription: ${state.status}
id: ${state.product1.id || ''}
title: ${state.product1.title || ''}
state: ${state.product1.state || ''}
descr: ${state.product1.description || ''}
price: ${state.product1.price || ''}
expiry: ${state.product1.expiryDate || ''}
</pre>
${purchaseProduct1}
<pre>
id: ${state.product2.id || ''}
title: ${state.product2.title || ''}
descr: ${state.product2.description || ''}
price: ${state.product2.price || ''}
state: ${state.product2.state || ''}
expiry: ${state.product2.expiryDate || ''}
</pre>
${purchaseProduct2}
`;
}
// button for product 1
const purchaseProduct1 = state.product1.canPurchase
? `<button onclick="store.order('my_subscription1')">Subscribe</button>` : '';
// same for product 2
const purchaseProduct2 = state.product2.canPurchase
? `<button onclick="store.order('my_subscription2')">Subscribe</button>` : '';`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
