Skip to content

Commit 4ac0e22

Browse files
faboydsronkorving
authored andcommitted
Created verifyPayment alternative function to return Promise (#69)
verifyPayment and cancelSubscription now return promises if no callback is passed
1 parent 16f7e72 commit 4ac0e22

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,28 @@ var payment = {
3333

3434
### Purchase verification ( all platforms )
3535

36-
A single method is exposed to verify purchase receipts:
36+
A method is exposed to verify purchase receipts:
3737

3838
```javascript
3939
iap.verifyPayment(platform, payment, function (error, response) {
4040
/* your code */
4141
});
4242
```
4343

44+
Or, if you prefer a promise-based alternative:
45+
46+
```javascript
47+
iap.verifyPayment(platform, payment)
48+
.then(
49+
response => {
50+
/* your code */
51+
},
52+
error => {
53+
/* your code */
54+
}
55+
)
56+
```
57+
4458
The receipt you pass must conform to the requirements of the backend you are verifying with. Read
4559
the next chapter for more information on the format.
4660

@@ -57,6 +71,20 @@ iap.cancelSubscription("google", payment, function (error, response) {
5771
});
5872
```
5973

74+
Or, if you prefer a promise-based alternative:
75+
76+
```javascript
77+
iap.cancelSubscription(platform, payment)
78+
.then(
79+
response => {
80+
/* your code */
81+
},
82+
error => {
83+
/* your code */
84+
}
85+
)
86+
```
87+
6088
## Supported platforms
6189

6290
### Amazon

index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ const platforms = {
77
roku: require('./lib/roku')
88
};
99

10+
const promisify = (fn) => {
11+
return (...args) => {
12+
return new Promise((resolve, reject) => {
13+
fn(...args, (err, res) => {
14+
return (err ? reject(err) : resolve(res));
15+
});
16+
});
17+
};
18+
};
1019

11-
exports.verifyPayment = function (platform, payment, cb) {
20+
function verifyPayment(platform, payment, cb) {
1221
function syncError(error) {
1322
process.nextTick(function () {
1423
cb(error);
@@ -34,10 +43,9 @@ exports.verifyPayment = function (platform, payment, cb) {
3443

3544
cb(null, result);
3645
});
37-
};
38-
46+
}
3947

40-
exports.cancelSubscription = function (platform, payment, cb) {
48+
function cancelSubscription(platform, payment, cb) {
4149
function syncError(error) {
4250
process.nextTick(function () {
4351
cb(error);
@@ -66,4 +74,12 @@ exports.cancelSubscription = function (platform, payment, cb) {
6674

6775
cb(null, result);
6876
});
77+
}
78+
79+
exports.verifyPayment = (platform, payment, cb) => {
80+
return (cb ? verifyPayment(platform, payment, cb) : promisify(verifyPayment)(platform, payment));
81+
};
82+
83+
exports.cancelSubscription = (platform, payment, cb) => {
84+
return (cb ? cancelSubscription(platform, payment, cb) : promisify(cancelSubscription)(platform, payment));
6985
};

0 commit comments

Comments
 (0)