Code Monkey home page Code Monkey logo

in-app-purchase's Introduction

in-app-purchase

©Nobuyori Takahashi < [email protected] >

Build Status

A node.js module for in-app purchase (in-app billing) for Apple, Google Play, Amazon Store, Roku, and Windows.

It supports Unity receipt also: Unity Documentation

Required node.js version

0.12.0 >=

Online Demo and Documention

Online Demo

How to install

npm install in-app-purchase

How to use

The module supports both Promise and callbacks.

var iap = require('in-app-purchase');
iap.config({
	
	/* Configurations for Amazon Store */
	amazonAPIVersion: 2, // tells the module to use API version 2
	secret: 'abcdefghijklmnoporstuvwxyz', // this comes from Amazon
	
	/* Configurations for Apple */
	applePassword: 'abcdefg...', // this comes from iTunes Connect (You need this to valiate subscriptions)	
	
	/* Configurations for Google Play */
	googlePublicKeyPath: 'path/to/public/key/directory/' // this is the path to the directory containing iap-sanbox/iap-live files
	googleAccToken: 'abcdef...', // optional, for Google Play subscriptions
	googleRefToken: 'dddd...', // optional, for Google Play subscritions
	googleClientID: 'aaaa', // optional, for Google Play subscriptions
	googleClientSecret: 'bbbb', // optional, for Google Play subscriptions

	/* Configurations for Roku */
	rokuApiKey: 'aaaa...', // this comes from Roku Developer Dashboard
	
	/* Configurations all platforms */
	test: true, // For Apple and Googl Play to force Sandbox validation only
	verbose: true // Output debug logs to stdout stream 
});
iap.setup()
  .then(() => {
    // iap.validate(...) automatically detects what type of receipt you are trying to validate
    iap.validate(receipt).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

function onSuccess(receipt, validatedData) {
	// receipt: it is the receipt that was validated...
	// validatedData: the actual content of the validated receipt
	var options = {
		ignoreCanceled: true, // Apple ONLY (for now...): purchaseData will NOT contain cancceled items
		ignoreExpired: true // purchaseData will NOT contain exipired subscription items
	};
	var purchaseData = iap.getPurchaseData(validateData, options);
}

function onError(error) {
	// failed to validate the receipt...
}

Validate Receipts From Multiple Applications

You may feed different Google public key or Apple password etc to validate receipts of different applications with the same code:

Windows is NOT Supported

Google Public Key

NOTE Google subscription is NOT supported.

iap.config(configObject);
iap.setup()
  .then(() => {
    iap.validateOnce(receipt, pubKeyString).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

Apple Subscription

iap.config(configObject);
iap.setup()
  .then(() => {
    iap.validateOnce(receipt, appleSecretString).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

Amazon

iap.config(configObject);
iap.setup()
  .then(() => {
    iap.validateOnce(receipt, amazonSecretString).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

Roku

iap.config(configObject);
iap.setup()
  .then(() => {
    iap.validateOnce(receipt, rokuApiKeyString).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

Google Play Public Key With An Environment Variable

You may not want to keep the public key files on your server(s).

The module also supports environment variables for this.

Instead of using googlePublicKeyPath: 'path/to...' in your configurations, you the following:

export=GOOGLE_IAB_PUBLICKEY_LIVE=PublicKeyHerePlz
export=GOOGLE_IAB_PUBLICKEY_SANDBOX=PublicKeyHerePlz

Google In-app-Billing Set Up

To set up your server-side Android in-app-billing correctly, you must provide the public key string as a file from your Developer Console account.

Reference: Implementing In-app Billing

Once you copy the public key string from the Developer Console account for your application, you simply need to copy and paste it to a file and name it iap-live as shown in the example above.

NOTE: The public key string you copy from the Developer Console account is actually a base64 string. You do NOT have to convert this to anything yourself. The module converts it to the public key automatically for you.

Google Play Store API

To check expiration date or auto renewal status of an Android subscription, you should first setup the access to the Google Play Store API. You should follow these steps:

Part 1 - Get ClientID and ClientSecret
  1. Go to https://play.google.com/apps/publish/
  2. Click on Settings
  3. Click on API Access
  4. There should be a linked project already, if not, create one. If you have it, click it.
  1. Under Mobile API's, make sure "Google Play Developer API is enabled".
  2. Go back, on the left click on Credentials
  3. Click Create Credentials button
  4. Choose OAuth Client ID
  5. Choose Web Application
Part 2 - Get Access and Refresh Tokens
  1. Go to: https://developers.google.com/oauthplayground
  2. On the right, hit the gear/settings.
  3. Check the box: Use your own OAuth credentials
    • Enter in clientID and clientSecret
    • Close
  4. On the left, find "Google Play Developer API v2"
  1. Hit Authorize Api's button
  2. Save Authorization Code
  • This is your: googleAccToken
  1. Hit Exchange Authorization code for token
  2. Grab: Refresh Token
  • This is your: googleRefToken

Now you are able to query for Android subscription status!

Amazon App Store Reference

https://developer.amazon.com/appsandservices/apis/earn/in-app-purchasing/docs/rvs

Windows Signed XML

in-app-purchase module supports the following algorithms:

Canonicalization and Transformation Algorithms

Hashing Algorithms

in-app-purchase's People

Contributors

3mini avatar anshul-kai avatar cemuzunlar avatar chad3814 avatar containmentbreach avatar creynaud avatar dankang avatar electricjesus avatar ember-max avatar ericdube avatar fantoine avatar faust64 avatar froelund avatar gastonelhordoy avatar jgiffard avatar joelfernandes19 avatar kpotehin avatar lfcgomes avatar markshust avatar mathieudutour avatar monder avatar nitewriter avatar raufdean avatar superandrew213 avatar suvitruf avatar timmliu avatar voltrue2 avatar whithang avatar williamchong avatar yanick avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.