diff --git a/README.md b/README.md index 6660a5f..7d592ff 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ var payment = { ### Purchase verification ( all platforms ) -A single method is exposed to verify purchase receipts: +A method is exposed to verify purchase receipts: ```javascript iap.verifyPayment(platform, payment, function (error, response) { @@ -41,6 +41,20 @@ iap.verifyPayment(platform, payment, function (error, response) { }); ``` +Or, if you prefer a promise-based alternative: + +```javascript +iap.verifyPayment(platform, payment) +.then( + response => { + /* your code */ + }, + error => { + /* your code */ + } +) +``` + The receipt you pass must conform to the requirements of the backend you are verifying with. Read the next chapter for more information on the format. @@ -57,6 +71,20 @@ iap.cancelSubscription("google", payment, function (error, response) { }); ``` +Or, if you prefer a promise-based alternative: + +```javascript +iap.cancelSubscription(platform, payment) +.then( + response => { + /* your code */ + }, + error => { + /* your code */ + } +) +``` + ## Supported platforms ### Amazon diff --git a/index.js b/index.js index e7220b4..7c51396 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,17 @@ const platforms = { roku: require('./lib/roku') }; +const promisify = (fn) => { + return (...args) => { + return new Promise((resolve, reject) => { + fn(...args, (err, res) => { + return (err ? reject(err) : resolve(res)); + }); + }); + }; +}; -exports.verifyPayment = function (platform, payment, cb) { +function verifyPayment(platform, payment, cb) { function syncError(error) { process.nextTick(function () { cb(error); @@ -34,10 +43,9 @@ exports.verifyPayment = function (platform, payment, cb) { cb(null, result); }); -}; - +} -exports.cancelSubscription = function (platform, payment, cb) { +function cancelSubscription(platform, payment, cb) { function syncError(error) { process.nextTick(function () { cb(error); @@ -66,4 +74,12 @@ exports.cancelSubscription = function (platform, payment, cb) { cb(null, result); }); +} + +exports.verifyPayment = (platform, payment, cb) => { + return (cb ? verifyPayment(platform, payment, cb) : promisify(verifyPayment)(platform, payment)); +}; + +exports.cancelSubscription = (platform, payment, cb) => { + return (cb ? cancelSubscription(platform, payment, cb) : promisify(cancelSubscription)(platform, payment)); };