Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created October 5, 2020 06:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hansemannn/de2a54f19227c915b51afc9a3bbaadd9 to your computer and use it in GitHub Desktop.
Save hansemannn/de2a54f19227c915b51afc9a3bbaadd9 to your computer and use it in GitHub Desktop.
Titanium iOS In-App-Purchase / Storekit

Titanium In-App-Purchasing

Support the native in-app-purchasing API's in Titanium.

Example

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);

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);
        }
    });
}

License

UNLICENSED

Copyright

(c) 2020-present Hans Knoechel and Prashant Saini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment