Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created January 13, 2021 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hansemannn/98412a600ffab672cfe7a4989b22f169 to your computer and use it in GitHub Desktop.
Save hansemannn/98412a600ffab672cfe7a4989b22f169 to your computer and use it in GitHub Desktop.
iOS In-App-Billing Module
var IAP = require('ti.iap');
var PRODUCT_IDENTIFIER = 'YOUR_PRODUCT_IDENTIFIER';
var SHARED_SECRET = 'YOUR_SHARED_SECRET';
var win = Ti.UI.createWindow({ backgroundColor: '#fff', title: 'IAP Demo' });
win.addEventListener('open', onOpen);
// Retrieve products info
var btn1 = Ti.UI.createButton({ title: 'Retrieve products info', top: 50 });
btn1.addEventListener('click', retrieveProductsInfo);
win.add(btn1);
// Purchase product
var btn2 = Ti.UI.createButton({ title: 'Purchase product', top: 100 });
btn2.addEventListener('click', purchase);
win.add(btn2);
// Verify receipt
var btn3 = Ti.UI.createButton({ title: 'Verify receipt', top: 150 });
btn3.addEventListener('click', verifyReceipt);
win.add(btn3);
// Restore purchases
var btn4 = Ti.UI.createButton({ title: 'Restore purchases', top: 200 });
btn4.addEventListener('click', restorePurchases);
win.add(btn4);
// Restore purchases
var btn5 = Ti.UI.createButton({ title: 'Verify subscription', top: 250 });
btn5.addEventListener('click', verifySubscription);
win.add(btn5);
// Code redemption (iOS only)
var btn5 = Ti.UI.createButton({ title: 'Show code redemption (iOS only)', top: 300 });
btn5.addEventListener('click', verifySubscription);
win.add(btn5);
var nav = Ti.UI.createNavigationWindow({ window: win });
nav.open();
function onOpen() {
// Initialize the in-app-purchase flow
IAP.initialize();
// Whether or not store payments should be added
IAP.shouldAddStorePaymentHandler(true);
// Completes pending purchases, e.g. if installed via App Store
IAP.completeTransactions(function(event) {
console.warn(event);
});
}
function retrieveProductsInfo() {
IAP.retrieveProductsInfo({
identifiers: [PRODUCT_IDENTIFIER],
callback: function (event) {
console.warn(event);
}
});
}
function purchase() {
IAP.purchase({
identifier: PRODUCT_IDENTIFIER,
quantity: 1,
atomically: true,
callback: function (event) {
console.warn(event);
}
});
}
function verifyReceipt() {
IAP.verifyReceipt({
service: 'sandbox',
sharedSecret: SHARED_SECRET,
productId: PRODUCT_IDENTIFIER,
callback: function (event) {
console.warn(event);
}
});
}
function restorePurchases() {
IAP.restorePurchases({
atomically: true,
callback: function (event) {
console.warn(event);
}
});
}
function verifySubscription() {
IAP.verifySubscription({
productId: PRODUCT_IDENTIFIER,
subscriptionType: IAP.SUBSCRIPTION_TYPE_AUTO_RENEWABLE, // OR: SUBSCRIPTION_TYPE_NON_RENEWING
validDuration: new Date().getTime(), // Only required if "subscriptionType" is "SUBSCRIPTION_TYPE_NON_RENEWING"
receipt: { exampleKey: 'exampleValue', anotherKey: 'anotherValue' }, // Pass your receipt info
callback: function (event) {
console.warn(event);
}
});
}
function presentCodeRedemptionSheet() {
IAP.presentCodeRedemptionSheet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment