in-app-purchase
in-app-purchase copied to clipboard
Error with google play
My code works perfect on Apple but I have troubles with Android:
this is the response <Amazon> Validation failed: { status: 498, message: 'Invalid Purchase Token' }
the code:
const iap = require("in-app-purchase");
const appleConfig = {
appleExcludeOldTransactions: true,
applePassword: process.env["APPLE_SECRECT"]
};
const googleConfig = {
googleServiceAccount: {
clientEmail: process.env["GOOGLE_CLIENT_EMAIL"],
privateKey: process.env["GOOGLE_PRIVATE_KEY"]
},
iap.config({
...appleConfig,
...googleConfig,
test: false,
verbose: true
});
const validatePurchase = async purchaseInfo => {
try {
let isCanceled = false
let isExpired = false
const { receipt } = purchaseInfo;
await iap.setup();
const validationResponse = await iap.validate(receipt);
const in_app = validationResponse.receipt.in_app
const purchaseData = iap.getPurchaseData(validationResponse);
if (iap.isCanceled(purchaseData[0])) {
isCanceled = true
}
if (iap.isExpired(purchaseData[0])) {
isExpired = true
}
return { success: true, data: { purchaseData: in_app, isExpired, isCanceled, receipt }};
} catch (error) {
console.log("Error while validating purchase", error);
return {error}
}
};
module.exports = {
validatePurchase
};
I have this file from Android:
{
"type": "service_account",
"project_id": "project_id",
"private_key_id": "private_key_id",
"private_key": "-----BEGIN PRIVATE KEY-----\n...private_key..,=\n-----END PRIVATE KEY-----\n",
"client_email": "service-account-....gserviceaccount.com",
"client_id": "client_id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "client_x509_cert_url"
}
I used GOOGLE_CLIENT_EMAIL the client_email and GOOGLE_PRIVATE_KEY for private_key.
Any idea?
this is a purchase:
bodyParsed: {
receipt: '{"orderId":"GPA.XXXX-YYYY-NNNN-MMMM","packageName":"com.AAAA.BBBB","productId":"month_0001","purchaseTime":1587723150825,"purchaseState":0,"purchaseToken":"AAAAAAAA.AO-J1Oyp3oRQ9mUWW2TDuhq-2qSJ3NPIsyEjxGfN7zr9g3B1YjaQyGibnVAxMcoKhMQCZOVLFIqSOLEA5wWBdC0kkSS_dWuNY7nu3wG5vsomUU8Ce-U7yRkjcL8ZAPBVe4xgqK89QXAC","autoRenewing":true,"acknowledged":false}',
productId: 'month_0001'
}
Thanks!
I have the same issue. For me, it was because that my Google Play receipt were being interpreted as Amazon receipts due to a missing signature field.
https://github.com/voltrue2/in-app-purchase/blob/e966ee1348bd4f67581779abeec59c4bbc2b2ebc/index.js#L107
Hi! Im having the same issue. How was this resiolved?
Thanks
Sorry, but can someone tell me where I can find the following:
const googleConfig = { googleServiceAccount: { clientEmail: , privateKey: },
did somebody solve it? I tried everything but I still get this issue.
I had the same issue when trying to validate a subscription. The information I had saved from Google didn't indicate it being a subscription. Examples for how I got it working.
Not working:
Receipt object: '{"orderId":"GPA...","packageName":"com....","productId":"ABC...","purchaseTime":158...,"purchaseState":0,"purchaseToken":"AAA...","autoRenewing":true,"acknowledged":false}',
Working (by adding subscription: true) '{"orderId":"GPA...","packageName":"com....","productId":"ABC...","purchaseTime":158...,"purchaseState":0,"purchaseToken":"AAA...","autoRenewing":true,"acknowledged":false,"subscription": true}',
Not sure if this will help someone in the future - but worked for me. Google API v3 requires it but I skimmed over it too fast.
{ packageName: 'The packge name of the item purchased', productId: 'The product ID of the item purchased', purchaseToken: 'PurchaseToken of the receipt from Google', subscription: true/false // if the receipt is a subscription, then true }
For me it was a string that had to be converted to an object first. Then it worked:
In node.js: // Check if receipt is a string and parse it as JSON if (typeof receipt === 'string') { try { receipt = JSON.parse(receipt); } catch (error) { // Handle parsing error console.error("Error parsing receipt JSON:", error); return res.status(400).send("Invalid receipt format"); } }