Skip to content Skip to sidebar Skip to footer

Failed To Validate Receipterror: Failed To Validate Purchase Using Node.js

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

Solution 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

Post a Comment for "Failed To Validate Receipterror: Failed To Validate Purchase Using Node.js"