KikeReto / InAppPurchase-Android

1 stars 4 forks source link

In app billing documentation

Requirements

Phonegap 3.0, Android 2.2.1+

Support

For free community support, please use the issue tracker. To get professional non-free support for the plugin, please contact me at gcharhon(at)smartmobilesoftware.com.

If you find this plugin useful, please donate via BitCoin to support it: 17JK27E4vbzPrJbBAtvjUVN3LrFcATtRA1

Installation

Automatic

We recommend this way to install the plugin into your project.

  1. Clone this project into your repository
  2. Run at the root of your project:
    cordova plugin add /path/to/your/cloned/plugin/AndroidInAppBilling/v3

    or

    phonegap local plugin add /path/to/your/cloned/plugin/AndroidInAppBilling/v3

Manually

The manual steps are not working on Phonegap 3.1+. Theses steps are not maintained anymore. Check the issue #32 for more info.

<feature name="InAppBillingPlugin">
      <param name="android-package" value="com.smartmobilesoftware.inappbilling.InAppBillingPlugin"/>
</feature>
    Phonegap.define('Phonegap/plugin_list', function(require, exports, module) {
    module.exports = [
        {
            "file": "plugins/com.smartmobilesoftware.inappbilling/www/inappbilling.js",
            "id": "com.smartmobilesoftware.inappbilling.InAppBillingPlugin",
            "clobbers": [
                "inappbilling"
        ]
        }
    ]
    });

Finish setting up your app

Usage

Initialization

Initialize the billing plugin. The plugin must be inialized before calling any other methods.

inappbilling.init(success, error, options)

parameters

Optional Initialization

inappbilling.init(success, error, options, skus)

parameters

Retrieve owned products

The list of owned products are retrieved from the local database.

inappbilling.getPurchases(success, fail)

parameters

Purchase

Purchase an item. You cannot buy an item that you already own.

inappbilling.buy(success, fail, productId)

parameters

Subscribe

Subscribe to an item

inappbilling.subscribe(success, fail, subcriptionId)

parameters

Consume

Consume an item. You can consume an item that you own. Example of consumable items : food, additional life pack, etc. Example of non-consumable item: levels pack. Once an item is consumed, it is not owned anymore.

inappbilling.consumePurchase(success, fail, productId)

parameters

Get Product(s) Details

Load the available product(s) to inventory. Not needed if you use the init(success, error, options, skus) method. Can be used to update inventory if you need to add more skus.

    inappbilling.getProductDetails(success, fail, skus)

Get Available Product(s)

The list of the available product(s) in inventory.

    inappbilling.getAvailableProducts(success, fail)

Quick example

inappbilling.init(successInit,errorCallback, {showLog:true, key: 'myOwnKeyRSABase64App'})

function successInit(result) {
    // display the extracted text
    alert(result);
    // make the purchase
    inappbilling.buy(successPurchase, errorCallback,"gas");

}
function errorCallback(error) {
   alert(error);
}

function successPurchase(productId) {
   alert("Your item has been purchased!");
}

Full example

<!DOCTYPE HTML>
<html>
    <head>
        <title>In App Billing</title>
        <script type="text/javascript" charset="utf-8" src="https://github.com/KikeReto/InAppPurchase-Android/raw/master/phonegap.js"></script>
        <script type="text/javascript" charset="utf-8" src="https://github.com/KikeReto/InAppPurchase-Android/raw/master/inappbilling.js"></script>
        <script type="text/javascript" charset="utf-8">
            function successHandler (result) {
                var strResult = "";
                if(typeof result === 'object') {
                    strResult = JSON.stringify(result);
                } else {
                    strResult = result;
                }
                alert("SUCCESS: \r\n"+strResult );
            }

            function errorHandler (error) {
                alert("ERROR: \r\n"+error );
            }

            // Click on init button
            function init(){
                // Initialize the billing plugin
                //myOwnKeyRSABase64App FROM GOOGLE DEVELOPERS CONSOLE
                inappbilling.init(successHandler, errorHandler, {showLog:true, key: 'myOwnKeyRSABase64App'});
            }

            // Click on purchase button
            function buy(){
                // make the purchase
                inappbilling.buy(successHandler, errorHandler,"gas");

            }

            // Click on ownedProducts button
            function ownedProducts(){
                // Initialize the billing plugin
                inappbilling.getPurchases(successHandler, errorHandler);

            }

            // Click on Consume purchase button
            function consumePurchase(){

                inappbilling.consumePurchase(successHandler, errorHandler, "gas");
            }

            // Click on subscribe button
            function subscribe(){
                // make the purchase
                inappbilling.subscribe(successHandler, errorHandler,"infinite_gas");

            }

            // Click on Query Details button
            function getDetails(){
                // Query the store for the product details
                inappbilling.getProductDetails(successHandler, errorHandler, ["gas","infinite_gas"]);

            }

            // Click on Get Available Products button
            function getAvailable(){
                // Get the products available for purchase.
                inappbilling.getAvailableProducts(successHandler, errorHandler);

            }

        </script>

    </head>
    <body>
        <h1>Hello World</h1>
        <button onclick="init();">Initalize the billing plugin</button>
        <button onclick="buy();">Purchase</button>
        <button onclick="ownedProducts();">Owned products</button>
        <button onclick="consumePurchase();">Consume purchase</button>
        <button onclick="subscribe();">Subscribe</button>
        <button onclick="getDetails();">Query Details</button>
        <button onclick="getAvailable();">Get Available Products</button>
    </body>
</html>

Common issues

If you have an issue, make sure that you can answer to theses questions: Did you create your item in the Developer Console? Is the id for your item the same in the Developer Console and in your app? Is your item active? Have you waited at least a few hours since you activated your item and uploaded your apk on the Developer Console? Are you using a different Google account than your developer account to make the purchase? Are you testing on a real device, rather than the emulator? Are you using a signed apk? Is the version code of your app the same as the one uploaded on the Developer Console?

If any of these questions is answered with a "no", you probably need to fix that.

MIT License

Copyright (c) 2012-2014 Guillaume Charhon - Smart Mobile Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.