Ionic

How To Add In App Purchases To Your Cordova Or Ionic 1.x App

Posted by Weston Ganger on March 28, 2016

In App Purchases have been a real pain point in Cordova apps because the most popular plugin library(cordova-plugin-purchase) is in a complete state of disarray from documentation to bugs and should be scrapped from your apps.

I found another plugin, cordova-plugin-inapppurchase which was created by Alex Disler. This plugin is super simple and actually works without problems but hardly anyone knows about it which is a shame. I cannot recommend this plugin enough!

Heres the implementation for both iOS and Android


document.addEventListener("deviceready", function(){
  if(window.inAppPurchase){
    inAppPurchase.getProducts(['myapp.adfree','myapp.otherpurchase']).then(function(products){
      console.log(products);
    }).catch(function(error){
      console.log(error);
    });
  
    var restorePurchases = function(){
      inAppPurchase.restorePurchases().then(function(data){
        if(data.productId === 'myapp.adfree'){
          /* remove ads */
        }elsif(data.productId === 'myapp.otherpurchase'){
          /* do work */
        }
      }).catch(function(error){
        console.log(error);
      });
    };
    
    var purchase = function(productId){
      inAppPurchase.buy(productId).then(function(data){
        if(data.productId === 'myapp.adfree'){
          /* remove ads */
        }elsif(data.productId === 'myapp.otherpurchase'){
          /* do work */
        }
  
        // This line required for Android purchases, does not affect iOS purchases
        return inAppPurchase.consume(data.type, data.receipt, data.signature);
      }).catch(function(error){
        console.log(error);
      });
    };
  }
}, false)

Just like that it works, no messing around. I recommend you check out all of Alex Disler’s plugins and repo’s because they are of extremely high quality.


Related External Links:


Posted in Ionic and Tagged with cordova in-app-purchase 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts