Integrating In App Purchases in Titanium iOS

Posted By : Chandan Wadhwa | 31-Mar-2016

In-App purchase is a native way to buy product or add some extra features in app. Many time you came into the situation where you were asked to pay for some extra options / features in an mobile App. This are totally optional functionality, but depending on what is being offered we sometimes get new functionality.

 

Initial requirements for In-App Purchases :-

 
  1. App must be created in itunesconnect.
  2. Accounts must be activated with  agreement, tax and banking details.
  3. Products must be defined in your app.
  4. Sandbox user must be defined for testing.
  5. Logout from apple account in device and login from sandbox user while purchasing product.
  6. Add Apple root certificate in app/assets folder of your titanium applicaiton. You can download certificate from here https://www.apple.com/certificateauthority/
  7. Ti.storekit iOS module.

Integrating module code in titanium :

  1. Add module(ti.storekit) in titanium application.
  2. Import module using require('ti.storekit') in controller.
  3. Define UI in Xml 

  


       Add code in controller

 

Initializing module objects :-

function init() {
    storekit.receiptVerificationSandbox = Ti.App.deployType !== 'production';
    storekit.bundleVersion = 'define your build vesion';
    storekit.bundleIdentifier = 'your application Id as define in TiApi.xml';
    storekit.addEventListener('transactionState', transactionStateChanged);
    storekit.addTransactionObserver();
}
 

Clear memory after use :-

 

 

function cleanUp() { 
    storekit.removeTransactionObserver();
    storekit.removeEventListener('transactionState', transactionStateChanged);
    storekit = null;
}
 

 

Request For a certain product:-

 

function requestProduct() {
    storekit.requestProducts(['au.com.example.smallCreditPackage'], function (evt) {
        if (!evt.success) {
            alert(‘Sorry, the App Store seems to be down right now. Please try again soon.’);
        } else if (evt.invalid) {
            alert(‘Invalid product.’);
        } else {
            purchaseProduct(evt.products[0]);
        }
    });
}
function purchaseProduct(product) {
    storekit.purchase({product: product});
};
 

 

Defining transaction state :-

 

var transactionStateChanged = function(evt) {
    switch (evt.state) {
        case storekit.TRANSACTION_STATE_FAILED:
            if (evt.cancelled) {
                alert('Purchase cancelled');
            } else {
                alert(evt.message); 
            }
   
            evt.transaction && evt.transaction.finish();
            break;
        case storekit.TRANSACTION_STATE_PURCHASED:
            if(storekit.validateReceipt()) {
                Ti.API.info(JSON.stringify(evt.receipt.toString()));
                alert('Purchase completed!');
            }
  
            evt.transaction && evt.transaction.finish();
            break;
    }
};
 

For this example i am explaining only TRANSACTION_STATE_FAILED and TRANSACTION_STATE_PURCHASED. There are also some other events you can define as per your requirements.

When successful buy screen will be look like this.

 

 


Thanks.

About Author

Author Image
Chandan Wadhwa

Chandan is an Android Apps developer with good experience in building native Android applications.

Request for Proposal

Name is required

Comment is required

Sending message..