-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
136 lines (110 loc) · 3.55 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
const request = require('google-oauth-jwt').requestWithJWT();
const util = require('util');
module.exports = Verifier;
// Allow default import syntax from TypeScript
module.exports.default = Verifier;
function Verifier(options) {
this.options = options || {};
}
Verifier.prototype.verifyINAPP = function (receipt) {
this.options.method = 'get';
this.options.body = "";
this.options.json = false;
let urlPattern = "https://www.googleapis.com/androidpublisher/v3/applications/%s/purchases/products/%s/tokens/%s";
if ("developerPayload" in receipt) {
urlPattern += ":acknowledge";
this.options.body = {
"developerPayload": receipt.developerPayload
}
this.options.method = 'post';
this.options.json = true;
}
let finalUrl = util.format(urlPattern, encodeURIComponent(receipt.packageName), encodeURIComponent(receipt.productId), encodeURIComponent(receipt.purchaseToken));
return this.verify(finalUrl)
};
Verifier.prototype.verifySub = function (receipt) {
this.options.method = 'get';
this.options.body = "";
this.options.json = false;
let urlPattern = "https://www.googleapis.com/androidpublisher/v3/applications/%s/purchases/subscriptions/%s/tokens/%s";
if ("developerPayload" in receipt) {
urlPattern += ":acknowledge";
this.options.body = {
"developerPayload": receipt.developerPayload
}
this.options.method = 'post';
this.options.json = true;
}
let finalUrl = util.format(urlPattern, encodeURIComponent(receipt.packageName), encodeURIComponent(receipt.productId), encodeURIComponent(receipt.purchaseToken));
return this.verify(finalUrl)
};
function isValidJson(string) {
try {
JSON.parse(string);
} catch (e) {
return false;
}
return true;
}
Verifier.prototype.verify = function (finalUrl) {
let options = {
uri: finalUrl,
method: this.options.method,
body: this.options.body,
json: this.options.json,
jwt: {
email: this.options.email,
key: this.options.key,
scopes: ['https://www.googleapis.com/auth/androidpublisher']
}
};
return new Promise(function (resolve, reject) {
request(options, function (err, res, body) {
let resultInfo = {};
if (err) {
// Google Auth Errors returns here
let errBody = err.body;
let errorMessage;
if (errBody) {
errorMessage = err.body.error_description;
} else {
errorMessage = err;
}
resultInfo.isSuccessful = false;
resultInfo.errorMessage = errorMessage;
reject(resultInfo);
} else {
let obj = {
"error": {
"code": res.statusCode,
"message": "Invalid response, please check 'Verifier' configuration or the statusCode above"
}
};
if (res.statusCode === 204) {
obj = {
"code": res.statusCode,
"message": "Acknowledged Purchase Successfully"
};
}
if (isValidJson(body)) {
obj = JSON.parse(body);
}
if (res.statusCode === 200 || res.statusCode === 204) {
// All Good
resultInfo.isSuccessful = true;
resultInfo.errorMessage = null;
resultInfo.payload = obj;
resolve(resultInfo);
} else {
// Error
let errorMessage = obj.error.message;
let errorCode = obj.error.code;
resultInfo.isSuccessful = false;
resultInfo.errorCode = errorCode;
resultInfo.errorMessage = errorMessage;
reject(resultInfo);
}
}
});
})
};