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 :-
- App must be created in itunesconnect.
- Accounts must be activated with agreement, tax and banking details.
- Products must be defined in your app.
- Sandbox user must be defined for testing.
- Logout from apple account in device and login from sandbox user while purchasing product.
- Add Apple root certificate in app/assets folder of your titanium applicaiton. You can download certificate from here https://www.apple.com/certificateauthority/
- Ti.storekit iOS module.
Integrating module code in titanium :
- Add module(ti.storekit) in titanium application.
- Import module using require('ti.storekit') in controller.
- 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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Wadhwa
Chandan is an Android Apps developer with good experience in building native Android applications.