Skip to content

Instantly share code, notes, and snippets.

@jukbot
Forked from jeffhuangtw/sample.js
Created August 31, 2021 17:16
Show Gist options
  • Save jukbot/3abab28db17e70182729b7bcdca6284e to your computer and use it in GitHub Desktop.
Save jukbot/3abab28db17e70182729b7bcdca6284e to your computer and use it in GitHub Desktop.
[nodejs] server side check "androidpublisher.purchases.subscriptions.get" with "service account"
// Google Play API Key
// ref: http://stackoverflow.com/questions/35127086/android-inapp-purchase-receipt-validation-google-play
// ref: https://developers.google.com/android-publisher/authorization
// ref: http://google.github.io/google-api-nodejs-client/18.0.0/index.html#toc14__anchor
//
// install npm package
// ref: https://github.com/google/google-api-nodejs-client
// $ npm install googleapis --save
//
const google = require('googleapis');
const publisher = google.androidpublisher('v2');
const OAuth2 = google.auth.OAuth2;
const SERVICE_ACCOUNT_EMAIL = 'your_service_account_email@developer.gserviceaccount.com';
const SERVICE_ACCOUNT_KEY_FILE = require('./path/to/your/service_account.json');
const jwtClient = new google.auth.JWT(
SERVICE_ACCOUNT_EMAIL,
null,
SERVICE_ACCOUNT_KEY_FILE.private_key,
['https://www.googleapis.com/auth/androidpublisher'],
null
);
// setup auth method with service account
google.options({ auth: jwtClient });
// get subscription status by purchaseToken
publisher.purchases.subscriptions.get({
packageName: 'The package name of the android app',
subscriptionId: 'The purchased subscription ID',
token: 'The purchaseToken of user'
}, function (error, response) {
if (error) {
console.log("verify google subscriptions error:" + JSON.stringify(error));
return handleSubscriptionsError(error);
}
return handleUpdateUserExpiration(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment