Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Add In App Purchases to your cordova application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 515
    Comment on it

    In this post, you will get to know of integrating in app purchases in your cordova application.

    cordova-plugin-inapppurchase is used to add in app purchases into your application for both android and iOS platform.

    Installing the plugin:
     

    $ cordova plugin add cordova-plugin-inapppurchase

    You can check the procedure of creating accounts for both platforms here : Apple's In-App Purchase Programming Guide and Google's In-app Billing Overview .

    After creating the accounts you would need to configure your application and adding the products that you want to offer on iTunes Connect and Google Play Developer Console for both iOS and Android platforms.

    After configuring application, you will get your app bundle id like com.companyname.appname and product’s id like com.companyname.appname.productname. Then you need to set that bundle id in your config.xml file and the product ids will be used in your js file.

    In your iOS application you can proceed further but in your android application, you need to add Android Billing Key to the manifest.json file inside your www folder. You will get this key under Services and APIs section in your google developer console.

    { "play_store_key": "<Base64-encoded public key from the Google Play Store>" }

    You need to sign and publish your application as alpha or beta version on android. Also you can set a tester account for testing the IAP in your application both on android and iOS.

    Loading the products:
    As the plugin provides the details of all the products stored in Google or Apple. You can get the details by calling the function inAppPurchase.getProducts(). you have to pass an array of different product Ids in this function to get the whole information of the product.
     

    inAppPurchase
      .getProducts(productIds)
      .then(function (products) {
        var products = products;
      })
      .catch(function (err) {
        console.log(err);
      });

    Buying the products:
    You can purchase the product by calling the function inAppPurchase.buy() when the product is loaded.
     

    inAppPurchase
      .buy(productId)
      .then(function (data) {
        console.log(JSON.stringify(data));
        // The consume() function should only be called after purchasing consumable products
        // otherwise, you should skip this step
        return inAppPurchase.consume(data.type, data.receipt, data.signature);
      })
      .then(function () {
        console.log('consume done!');
      })
      .catch(function (err) {
        console.log(err);
      });

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: