Skip to content

Commit

Permalink
Created verifyPayment alternative function to return Promise (#69)
Browse files Browse the repository at this point in the history
verifyPayment and cancelSubscription now return promises if no callback is passed
  • Loading branch information
faboyds authored and ronkorving committed Jan 23, 2020
1 parent 16f7e72 commit 4ac0e22
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,28 @@ 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) {
/* your code */
});
```

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.

Expand All @@ -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
Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
};

0 comments on commit 4ac0e22

Please sign in to comment.