Wizcorp / node-iap

In-app purchase validation for Apple, Google, Amazon, Roku
262 stars 92 forks source link

Created verifyPayment alternative function to return Promise #69

Closed faboyds closed 4 years ago

faboyds commented 5 years ago

Created an alternative method for verifyPayment that uses promises instead of callbacks.

Should fix #68

ronkorving commented 5 years ago

This removes the callback-style API from the library. In other words, while your new function works, the old one is gone (overwritten).

I would love Promise support, so I think you're on the right track here, but we need a solid solution that we can apply to all APIs, while still supporting callback-style. I think it's possible. Please reconsider a solution.

faboyds commented 5 years ago

Ok, I understand and I agree with you. I will take a deeper look into this when I have more time, and I'll try to deliver a more complete solution.

superandrew213 commented 5 years ago

@faboyds have a look how I did it here: https://github.com/superandrew213/react-native-in-app-utils/blob/listen-for-purchase-event/index.js

You end up with a single method that you can call like this if you want to use callbacks:

method(arg1, arg2, (error, res) => {})

or like this if you want to use Promises:

method(arg1, arg2).then(res => {}).catch(error => {})
justinpage commented 5 years ago

@faboyds An alternative way you could handle this issue is through bluebird promisfy all:

http://bluebirdjs.com/docs/api/promise.promisifyall.html

Simply wrap the existing package:

const iap: any = Bluebird.promisifyAll(require("iap"));

And then you can do something like:

// Verify payment from iap service
let responseFromApple = await iap.verifyPaymentAsync(
    "apple", subscriptionWithSharedSecret
)
.catch(e => {
    logger.error(
        "Receipt.validateApplePurchase: failed to validate apple " +
        "receipt",
        { message: e.message, subscription }
    );

    return Err("ValidationError", e.message);
});

if (isError(responseFromApple)) {
    return responseFromApple;
}
faboyds commented 5 years ago

@faboyds have a look how I did it here: https://github.com/superandrew213/react-native-in-app-utils/blob/listen-for-purchase-event/index.js

You end up with a single method that you can call like this if you want to use callbacks:

method(arg1, arg2, (error, res) => {})

or like this if you want to use Promises:

method(arg1, arg2).then(res => {}).catch(error => {})

I followed this implementation. Please check if the code is now meeting the requirements. And thank you for your suggestions! 😄

artur-ma commented 4 years ago

Can u merge it please? @ronkorving

ronkorving commented 4 years ago

Done, cheers 👍

SeanDunford commented 4 years ago

Could we get this released on npm?

Relevant issue here https://github.com/Wizcorp/node-iap/issues/81