Failed to validate receiptError: failed to validate purchase using Node.js

家住魔仙堡 提交于 2019-12-24 20:59:45

问题


I am trying to set up a node.js server to validate receipts from AppStore connect In App Purchases I have set up. I have followed this https://github.com/voltrue2/in-app-purchase library but I'm getting an error response saying my receipt is not defined and failed to validate receipt. I'm I doing anything wrong here. I'm still testing on a local server. I want to get it to work before hosting it on Heroku. What would I be doing wrong here?

const iap = require('in-app-purchase');

iap.config({
  applePassword: 'MySecretKey',
  test: true
});

iap.setup()
  .then(() => {
    iap.validateOnce(receipt, appleSecretString).then(onSuccess).catch(onError);
  })
    .catch((error) => {
    if (error) {
        console.log('Validation error' + error);
    }
});

iap.validate(iap.APPLE, function (error, appleResponse) {
  console.log(iap.APPLE);
  if (error) {
    console.log('Failed to validate receipt' + error);
  }
  if (iap.isValidated(appleResponse)) {
    console.log('Validation successful');
  }
});

Here are the logs

iapserver:server Listening on port 3000 +0ms Validation errorReferenceError: receipt is not defined apple Failed to validate receiptError: failed to validate purchase


回答1:


From the error is seems like you're not actually passing the receipt file into the validateOnce function. Somewhere before that code runs you need to set:

const receipt = req.query.receipt;

And invoke your API with something like:

http://localhost:3000/verifyReceipt?receipt=<YOUR_RECEIPT_DATA>

You can verify your receipt beforehand manually to make sure it's valid.

Putting this all together, you'd get something like:

var express = require('express');
var app = express();

const iap = require('in-app-purchase');


app.get('/verifyReceipt', function(req, res){

  const receipt = req.query.receipt;

  iap.config({
    applePassword: 'MySecretKey',
    test: true
  });

  iap.setup()
    .then(() => {
      iap.validateOnce(receipt, appleSecretString).then(onSuccess).catch(onError);
    })
      .catch((error) => {
      if (error) {
          console.log('Validation error' + error);
          res.status(400).send({valid: false});
      }
  });

  iap.validate(iap.APPLE, function (error, appleResponse) {
    console.log(iap.APPLE);
    if (error) {
      console.log('Failed to validate receipt' + error);
      res.status(400).send({valid: false});
    }
    if (iap.isValidated(appleResponse)) {
      console.log('Validation successful');
      res.status(200).send({valid: true});
    }
  });
});

app.listen(3000);

Note that this will only verify the receipt at purchase, if you're implementing subscriptions you also need to periodically refresh the receipt to make sure they user didn't cancel. Here's a good guide on implementing subscriptions: iOS Subscriptions are Hard



来源:https://stackoverflow.com/questions/54250885/failed-to-validate-receipterror-failed-to-validate-purchase-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!