1

I have some code on my back end that I am using to try and verify apple purchase receipts with the library node-iap.

I've got all the receipt to verify but I am struggling with something really simple:

Trying to get the code to return out of the iap.verifyPayment method to carry on with the code execution in the subscribe method and return a success. It just hangs within handleAppleSub and doesn't return.

export const subscribe = (req, res, next) => {
    return User.findOne({
        where: {
            ID: req.params.ID
        }
    })
    .then(
        user =>
            user
                ? req.body.OS == "android"
                    ? handleAndroidSub(user, req.body.receipt)
                    : handleAppleSub(user, req.body.receipt)
                : notFound(res)
    )
    .then(success(res))
    .catch(next);
};

const handleAppleSub = (user, receipt) => {
    var platform = "apple";


    var payment = {
        receipt: receipt.transactionReceipt,
        productId: receipt.productId,
        packageName: "com.app.test",
        secret: appleSecret,
        excludeOldTransactions: true
     };

     return iap.verifyPayment(platform, payment, function(error, response) {

        user.dataValues.SubAppleReceipt = receipt.transactionReceipt;

        return User.update(user.dataValues, {
             where: { ID: user.dataValues.ID }
        }).then(user => {
           return user;
     });
});
Softey
  • 1,451
  • 3
  • 21
  • 42
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Yury Tarabanko Jul 24 '18 at 12:23
  • You are returning a Promise from async call. Promisify `iap.verifyPayment`. – Yury Tarabanko Jul 24 '18 at 12:24
  • iap.verifyaPayment comes back as an object not a function when attempting to promisfy? – Softey Jul 24 '18 at 12:53
  • "iap.verifyaPayment comes back as an object not a function " if this is in case how do you expect your current implementation to even work? You are calling it as if it was a function. `iap.verifyPayment(platform, payment, function(error, response) {` – Yury Tarabanko Jul 24 '18 at 13:25
  • I'm following what the documentation is specifying and putting my logic inside the result. Wanting to return out of that function once done. – Softey Jul 24 '18 at 13:35
  • 1
    A promise returned from a nodeback will not percolate upwards as would a promise returned from a `.then()` callback. Promise chains are special in this regard. The standard way to approach this would be to promisify `iap.verifyPayment`, as say `iap.verifyPaymentAsync`, then call the promisified version and chain `.then(...)`. – Roamer-1888 Jul 24 '18 at 19:06
  • This might be going over my head but how I am to promisfy iap.verifyPayment if its part of the library node-iap? – Softey Jul 25 '18 at 07:58
  • See the accepted answer to [How do I convert an existing callback API to promises?](https://stackoverflow.com/a/22519785/3478010). – Roamer-1888 Jul 25 '18 at 12:28

0 Answers0