-
Notifications
You must be signed in to change notification settings - Fork 290
/
index.js
387 lines (358 loc) · 12.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict';
var async = require('./lib/async');
var apple = require('./lib/apple');
var google = require('./lib/google');
var windows = require('./lib/windows');
var amazonManager = require('./lib/amazonManager');
var facebook = require('./lib/facebook');
var roku = require('./lib/roku');
var constants = require('./constants');
var verbose = require('./lib/verbose');
var IS_WINDOWS = '<\/Receipt>';
var amazon;
function handlePromisedFunctionCb(resolve, reject) {
return function _handlePromisedCallback(error, response) {
if (error) {
var errorData = { error: error, status: null, message: null };
if (response !== null && typeof response === 'object') {
errorData.status = response.status;
errorData.message = response.message;
}
return reject(JSON.stringify(errorData), response);
}
return resolve(response);
};
}
module.exports.UNITY = constants.SERVICES.UNITY;
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.FACEBOOK = constants.SERVICES.FACEBOOK;
module.exports.ROKU = constants.SERVICES.ROKU;
module.exports.config = function (configIn) {
apple.readConfig(configIn);
google.readConfig(configIn);
windows.readConfig(configIn);
amazon = amazonManager.create(configIn);
facebook.readConfig(configIn);
roku.readConfig(configIn);
verbose.setup(configIn);
};
module.exports.setup = function (cb) {
if (!cb && Promise) {
return new Promise(function (resolve, reject) {
module.exports.setup(handlePromisedFunctionCb(resolve, reject));
});
}
async.series([
function (next) {
apple.setup(next);
},
function (next) {
google.setup(next);
},
function (next) {
amazon.setup(next);
},
function (next) {
facebook.setup(next);
},
], cb);
};
module.exports.getService = function (receipt) {
if (!receipt) {
throw new Error('Receipt was null or undefined');
}
if (receipt.indexOf && receipt.indexOf(IS_WINDOWS) !== -1) {
return module.exports.WINDOWS;
}
if (typeof receipt === 'object') {
// receipt could be either Google, Amazon, or Unity (Apple or Google or Amazon)
if (isUnityReceipt(receipt)) {
return module.exports.UNITY;
}
if (receipt.signature) {
return module.exports.GOOGLE;
} else if (receipt.purchaseToken) {
return module.exports.GOOGLE;
} else {
return module.exports.AMAZON;
}
}
if (typeof receipt === 'string') {
var characters = receipt.match(/\w/g) || '';
var dashes = receipt.match(/-/g) || '';
if (characters.length === 32 && dashes.length === 4) {
return module.exports.ROKU;
}
}
try {
// receipt could be either Google, Amazon, or Unity (Apple or Google or Amazon)
var parsed = JSON.parse(receipt);
if (isUnityReceipt(parsed)) {
return module.exports.UNITY;
}
if (parsed.signature) {
return module.exports.GOOGLE;
} else if (receipt.purchaseToken) {
return module.exports.GOOGLE;
} else {
return module.exports.AMAZON;
}
} catch (error) {
var dotSplitedReceipt = receipt.split('.');
if (dotSplitedReceipt.length === 2) {
return module.exports.FACEBOOK;
}
return module.exports.APPLE;
}
};
module.exports.validate = function (service, receipt, cb) {
if (receipt === undefined && cb === undefined) {
// we are given 1 argument as: const promise = .validate(receipt)
receipt = service;
service = module.exports.getService(receipt);
}
if (cb === undefined && typeof receipt === 'function') {
// we are given 2 arguments as: .validate(receipt, cb)
cb = receipt;
receipt = service;
service = module.exports.getService(receipt);
}
if (!cb && Promise) {
return new Promise(function (resolve, reject) {
module.exports.validate(
service,
receipt,
handlePromisedFunctionCb(resolve, reject)
);
});
}
if (service === module.exports.UNITY) {
service = getServiceFromUnityReceipt(receipt);
receipt = parseUnityReceipt(receipt);
}
switch (service) {
case module.exports.APPLE:
apple.validatePurchase(null, receipt, cb);
break;
case module.exports.GOOGLE:
google.validatePurchase(null, receipt, cb);
break;
case module.exports.WINDOWS:
windows.validatePurchase(receipt, cb);
break;
case module.exports.AMAZON:
amazon.validatePurchase(null, receipt, cb);
break;
case module.exports.FACEBOOK:
facebook.validatePurchase(null, receipt, cb);
break;
case module.exports.ROKU:
roku.validatePurchase(null, receipt, cb);
break;
default:
return cb(new Error('invalid service given: ' + service));
}
};
module.exports.validateOnce = function (service, secretOrPubKey, receipt, cb) {
if (receipt === undefined && cb === undefined) {
// we are given 2 arguments as: const promise = .validateOnce(receipt, secretOrPubKey)
receipt = service;
service = module.exports.getService(receipt);
}
if (cb === undefined && typeof receipt === 'function') {
// we are given 3 arguemnts as: .validateOnce(receipt, secretPubKey, cb)
cb = receipt;
receipt = service;
service = module.exports.getService(receipt);
}
if (!cb && Promise) {
return new Promise(function (resolve, reject) {
module.exports.validateOnce(
service,
secretOrPubKey,
receipt,
handlePromisedFunctionCb(resolve, reject)
);
});
}
if (service === module.exports.UNITY) {
service = getServiceFromUnityReceipt(receipt);
receipt = parseUnityReceipt(receipt);
}
if (!secretOrPubKey && service !== module.exports.APPLE && service !== module.exports.WINDOWS) {
verbose.log('<.validateOnce>', service, receipt);
return cb(new Error('missing secret or public key for dynamic validation:' + service));
}
switch (service) {
case module.exports.APPLE:
apple.validatePurchase(secretOrPubKey, receipt, cb);
break;
case module.exports.GOOGLE:
google.validatePurchase(secretOrPubKey, receipt, cb);
break;
case module.exports.WINDOWS:
windows.validatePurchase(receipt, cb);
break;
case module.exports.AMAZON:
amazon.validatePurchase(secretOrPubKey, receipt, cb);
break;
case module.exports.FACEBOOK:
facebook.validatePurchase(secretOrPubKey, receipt, cb);
break;
case module.exports.ROKU:
roku.validatePurchase(secretOrPubKey, receipt, cb);
break;
default:
verbose.log('<.validateOnce>', secretOrPubKey, receipt);
return cb(new Error('invalid service given: ' + service));
}
};
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.cancellationDate) {
// it has been cancelled
return true;
}
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.isCanceled = function (purchasedItem) {
if (!purchasedItem || !purchasedItem.transactionId) {
throw new Error('invalid purchased item given:\n' + JSON.stringify(purchasedItem));
}
if (purchasedItem.cancellationDate) {
// it has been cancelled
return true;
}
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);
case module.exports.FACEBOOK:
return facebook.getPurchaseData(purchaseData, options);
case module.exports.ROKU:
return roku.getPurchaseData(purchaseData, options);
default:
return null;
}
};
module.exports.refreshGoogleToken = function (cb) {
if (!cb && Promise) {
return new Promise(function (resolve, reject) {
module.exports.refreshGoogleToken(handlePromisedFunctionCb(resolve, reject));
});
}
google.refreshToken(cb);
};
module.exports.setAmazonValidationHost = function (vhost) {
if (amazon.setValidationHost) {
return amazon.setValidationHost(vhost);
}
return false;
};
module.exports.resetAmazonValidationHost = function () {
if (amazon.resetValidationHost) {
return amazon.resetValidationHost();
}
return false;
};
function isUnityReceipt(receipt) {
if (receipt.Store) {
if (
receipt.Store === constants.UNITY.GOOGLE ||
receipt.Store === constants.UNITY.APPLE ||
receipt.Store === constants.UNITY.AMAZON
) {
return true;
}
}
return false;
}
function getServiceFromUnityReceipt(receipt) {
if (typeof receipt !== 'object') {
// at this point we have already established the fact that receipt is a valid JSON string
receipt = JSON.parse(receipt);
}
switch (receipt.Store) {
case constants.UNITY.GOOGLE:
return module.exports.GOOGLE;
case constants.UNITY.APPLE:
return module.exports.APPLE;
case constants.UNITY.AMAZON:
return module.exports.AMAZON;
}
// invalid Store value
return null;
}
function parseUnityReceipt(receipt) {
verbose.log('Parse Unity receipt as ' + receipt.Store);
if (typeof receipt !== 'object') {
// at this point we have already established the fact that receipt is a valid JSON string
receipt = JSON.parse(receipt);
}
switch (receipt.Store) {
case constants.UNITY.GOOGLE:
if (typeof receipt.Payload === 'string') {
try {
receipt.Payload = JSON.parse(receipt.Payload);
} catch (error) {
throw error;
}
}
var payloadContent = typeof receipt.Payload.json !== 'object' ? JSON.parse(receipt.Payload.json) : receipt.Payload.json;
return {
data: receipt.Payload.json,
signature: receipt.Payload.signature,
// add field necessary to use google service account
packageName: payloadContent.packageName,
productId: payloadContent.productId,
purchaseToken: payloadContent.purchaseToken,
subscription: (receipt.Subscription !== undefined && receipt.Subscription)
};
case constants.UNITY.AMAZON:
if (typeof receipt.Payload === 'string') {
try {
receipt.Payload = JSON.parse(receipt.Payload);
} catch (error) {
throw error;
}
}
return receipt.Payload;
case constants.UNITY.APPLE:
return receipt.Payload;
}
}
// test use only
module.exports.reset = function () {
// resets google setup
google.reset();
};