-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (69 loc) · 3.28 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
var request = require('request');
var _ = require('lodash');
var util = require('util');
module.exports = Verifier;
function Verifier(options) {
this.options = options || {};
}
Verifier.prototype.validateConsumable = function(receipt, cb) {
var urlPattern = "https://appstore-sdk.amazon.com/version/2.0/verify/developer/%s/user/%s/purchaseToken/%s";
var finalUrl = util.format(urlPattern, encodeURIComponent(this.options.key), encodeURIComponent(receipt.userId), encodeURIComponent(receipt.purchaseToken));
request({
url: finalUrl,
json: true
}, function(err, res, body) {
if (err) {
return cb(err);
}
if (res.statusCode === 400) {
return cb(new Error('Amazon RVS Error: The transaction represented by this Purchase Token is no longer valid.'));
} else if (res.statusCode === 496) {
return cb(new Error('Amazon RVS Error: Invalid sharedSecret'));
} else if (res.statusCode === 497) {
return cb(new Error('Amazon RVS Error: Invalid User ID'));
} else if (res.statusCode === 498) {
return cb(new Error('Amazon RVS Error: Invalid Purchase Token'));
} else if (res.statusCode === 499) {
return cb(new Error('Amazon RVS Error: The Purchase Token was created with credentials that have expired, use renew to generate a valid purchase token.'));
} else if (res.statusCode === 500) {
return cb(new Error('Amazon RVS Error: There was an Internal Server Error'));
} else if (res.statusCode !== 200) {
return cb(new Error('Amazon RVS Error: Unknown other error'));
}
if (_.every(['productId', 'productType', 'purchaseDate'], Object.prototype.hasOwnProperty, body)) {
cb(null, obj);
} else {
cb(new Error("body did not contain expected json object"));
}
});
};
Verifier.prototype.renewToken = function(receipt, cb) {
var urlPattern = "https://appstore-sdk.amazon.com/version/2.0/renew/developer/%s/user/%s/purchaseToken/%s";
var finalUrl = util.format(urlPattern, encodeURIComponent(this.options.key), encodeURIComponent(receipt.userId), encodeURIComponent(receipt.receiptId));
request({
url: finalUrl,
json: true
}, function(err, res, body) {
if (err) {
return cb(err);
}
if (res.statusCode === 400) {
return cb(new Error('Amazon RVS Error: Bad Request'));
} else if (res.statusCode === 496) {
return cb(new Error('Amazon RVS Error: Invalid sharedSecret'));
} else if (res.statusCode === 497) {
return cb(new Error('Amazon RVS Error: Invalid User ID'));
} else if (res.statusCode === 498) {
return cb(new Error('Amazon RVS Error: Invalid User ID'));
} else if (res.statusCode === 500) {
return cb(new Error('Amazon RVS Error: There is an Internal Server Error'));
} else if (res.statusCode !== 200) {
return cb(new Error('Amazon RVS Error: Unknown other error'));
}
if (_.every(['productId', 'productType', 'purchaseDate'], Object.prototype.hasOwnProperty, body)) {
cb(null, obj);
} else {
cb(new Error("body did not contain expected json object"));
}
});
};