voltrue2 / in-app-purchase

A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows.
http://iap.gracenode.org
Other
1.04k stars 287 forks source link

Invalid Purchase Token #349

Open desmeit opened 2 years ago

desmeit commented 2 years ago

I try to validate with firebase cloud functions. Apple is working well but with Android I get an error. This is my functions code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const iap = require("in-app-purchase");
admin.initializeApp(functions.config().firebase);

exports.validateReceipt = functions.https.onCall(async (data, context) => {
  const test = data.test;
  iap.config({
    appleExcludeOldTransactions: true,
    applePassword: "XXX",
    googleServiceAccount: {
      clientEmail: "XXX@api-project-XXX.iam.gserviceaccount.com",
      // eslint-disable-next-line max-len
      privateKey: "-----BEGIN PRIVATE KEY-----XXX----END PRIVATE KEY-----\n",
    },
    test: test, // For Apple and Googl Play to force Sandbox
    verbose: true, // Output debug logs to stdout stream
  });
  const receipt = data.receipt;
  await iap.setup();
  const validationResponse = await iap.validate(receipt);
  const purchaseData = iap.getPurchaseData(validationResponse);

  return {response: purchaseData};
});

this is my receipt at flutter:

var receipt = json.encode({
                'purchaseToken': 'XXX',
               'packageName': 'com.XXX.app',
                'productId': 'XXX',
                'subscription': false,
});

I get the following error: Unhandled error

<Amazon> Validation failed: { status: 498, message: 'Invalid Purchase Token

and it seems to check the receipt against Amazon and not against Google. What am I doing wrong? https://github.com/voltrue2/in-app-purchase/issues/219 was not helping.

tgreco commented 2 years ago

I'm having the same issue.

desmeit commented 2 years ago

The issue comes when something is wrong with the JSON.

henriqueArrazao commented 1 year ago

I'm not sure what the type the json.encode returns but I guess It's returning a string. Try to remove it and maintain only the object, without the json.encode()