Last active
October 1, 2021 03:36
-
-
Save jhonywalkeer/b66183a7e3b82ae6af7546c283bb2013 to your computer and use it in GitHub Desktop.
Integrating Google Play Billing Validator with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GoogleReceiptVerify = require('google-play-billing-validator') | |
// some module that handles management of subscription data | |
var Subscriptions = require('../data/Subscriptions') | |
// instantiate google receipt verify | |
var googleReceiptVerify = new GoogleReceiptVerify({ | |
email: ServiceAccount.client_email, | |
key: ServiceAccount.private_key | |
}); | |
// function that handles validation | |
const init = async (productId, purchaseToken) => { | |
let response = await googleReceiptVerify.verifySub({ | |
packageName: "<YOUR_APP_ID>", | |
productId: productId, | |
purchaseToken: purchaseToken, | |
}); | |
if (response.isSuccessful === true) { | |
// insert receipt and products record | |
await Plans.insertPurchaseReceiptRecord({ | |
purchaseToken: purchaseToken, | |
verified: true, | |
products: response['payload'] | |
}); | |
// convert the returned expiry timestamp from milliseconds to seconds | |
let expirationUnix = Math.round(response['payload'].expiryTimeMillis / 1000); | |
// further processing. | |
let result = await Subscriptions.processSuccessfulSubscription(productId, expirationUnix); | |
// log final processing result | |
console.log(result); | |
} else { | |
// error: validation not successful | |
console.log(response.errorMessage); | |
} | |
} | |
// --- initiate function --- | |
const purchaseToken = '<PURCHASE_TOKEN>'; | |
const productId = '<PRODUCT_ID>'; | |
init(productId, purchaseToken); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment