forked from voltrue2/in-app-purchase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (96 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var async = require('./lib/async');
var apple = require('./lib/apple');
var google = require('./lib/google');
var windows = require('./lib/windows');
var amazon = require('./lib/amazon');
var constants = require('./constants');
module.exports.APPLE = constants.SERVICES.APPLE;
module.exports.GOOGLE = constants.SERVICES.GOOGLE;
module.exports.WINDOWS = constants.SERVICES.WINDOWS;
module.exports.AMAZON = constants.SERVICES.AMAZON;
module.exports.config = function (configIn) {
apple.readConfig(configIn);
google.readConfig(configIn);
windows.readConfig(configIn);
amazon.readConfig(configIn);
};
module.exports.setup = function (cb) {
async.parallel([
function (next) {
apple.setup(next);
},
function (next) {
google.setup(next);
},
function (next) {
amazon.setup(next);
}
], cb);
};
module.exports.validate = function (service, receipt, cb) {
var validator;
switch (service) {
case module.exports.APPLE:
validator = apple.validatePurchase;
break;
case module.exports.GOOGLE:
validator = google.validatePurchase;
break;
case module.exports.WINDOWS:
validator = windows.validatePurchase;
break;
case module.exports.AMAZON:
validator = amazon.validatePurchase;
break;
default:
return cb(new Error('invalid service given: ' + service));
}
validator(receipt, function (error, response) {
cb(error, response);
});
};
module.exports.isValidated = function (response) {
if (response && response.status === constants.VALIDATION.SUCCESS) {
return true;
}
return false;
};
module.exports.isExpired = function (purchasedItem) {
if (!purchasedItem || !purchasedItem.transactionId) {
throw new Error('invalid purchased item given:\n' + JSON.stringify(purchasedItem));
}
if (!purchasedItem.expirationDate) {
// there is no exipiration date with this item
return false;
}
if (Date.now() - purchasedItem.expirationDate >= 0) {
return true;
}
// has not exipired yet
return false;
};
module.exports.getPurchaseData = function (purchaseData, options) {
if (!purchaseData || !purchaseData.service) {
return null;
}
switch (purchaseData.service) {
case module.exports.APPLE:
return apple.getPurchaseData(purchaseData, options);
case module.exports.GOOGLE:
return google.getPurchaseData(purchaseData, options);
case module.exports.WINDOWS:
return windows.getPurchaseData(purchaseData, options);
case module.exports.AMAZON:
return amazon.getPurchaseData(purchaseData, options);
default:
return null;
}
};
// test use only
module.exports.reset = function () {
// resets google setup
google.reset();
};
module.exports.refreshGoogleToken = function (cb) {
google.refreshToken(cb);
};