-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
200 lines (168 loc) · 5.76 KB
/
app.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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var IAPVerifier = require('iap_verifier');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.send('hello');
});
var ERROR_CODES_BASE = 6777e3;
var ERR_SETUP = ERROR_CODES_BASE + 1;
var ERR_LOAD = ERROR_CODES_BASE + 2;
var ERR_PURCHASE = ERROR_CODES_BASE + 3;
var ERR_LOAD_RECEIPTS = ERROR_CODES_BASE + 4;
var ERR_CLIENT_INVALID = ERROR_CODES_BASE + 5;
var ERR_PAYMENT_CANCELLED = ERROR_CODES_BASE + 6;
var ERR_PAYMENT_INVALID = ERROR_CODES_BASE + 7;
var ERR_PAYMENT_NOT_ALLOWED = ERROR_CODES_BASE + 8;
var ERR_UNKNOWN = ERROR_CODES_BASE + 10;
var ERR_REFRESH_RECEIPTS = ERROR_CODES_BASE + 11;
var ERR_INVALID_PRODUCT_ID = ERROR_CODES_BASE + 12;
var ERR_FINISH = ERROR_CODES_BASE + 13;
var ERR_COMMUNICATION = ERROR_CODES_BASE + 14;
var ERR_SUBSCRIPTIONS_NOT_AVAILABLE = ERROR_CODES_BASE + 15;
var ERR_MISSING_TOKEN = ERROR_CODES_BASE + 16;
var ERR_VERIFICATION_FAILED = ERROR_CODES_BASE + 17;
var ERR_BAD_RESPONSE = ERROR_CODES_BASE + 18;
var ERR_REFRESH = ERROR_CODES_BASE + 19;
var ERR_PAYMENT_EXPIRED = ERROR_CODES_BASE + 20;
var INVALID_PAYLOAD = 6778001;
var CONNECTION_FAILED = 6778002;
var PURCHASE_EXPIRED = 6778003;
function formatError(iosErrorCode) {
var err = {
code: ERR_VERIFICATION_FAILED,
msg: 'Unknow reason'
};
switch(iosErrorCode) {
case 21000:
err.msg = 'The App Store could not read the JSON object you provided';
break;
case 21002:
err.msg = 'The data in the transaction receipt property was malformed or missing';
break;
case 21003:
err.msg = 'The receipt could not be authenticated';
break;
case 21004:
err.msg = 'The shared secret you provided does not match the shared secret on file for your account';
break;
case 21005:
err.msg = 'The receipt server is not currently available';
break;
case 21006:
err.code = PURCHASE_EXPIRED;
err.msg = 'This receipt is valid but the subscription has expired';
break;
case 21007:
err.msg = 'This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead';
break;
case 21008:
err.msg = 'This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead';
break;
}
return err;
}
/**
* transaction {Object} contains base64 receipt
* isSandBox {String} 'true'|'false'
* password {String}
*/
app.all('/verify', function(req, res) {
console.log(req.body);
var transaction = req.param('transaction');
var password = req.param('password');
var isAutoRenew = req.param('isAutoRenew') === 'true';
var receipt = transaction.transactionReceipt;
var result = {};
var client = new IAPVerifier(password);
var method = isAutoRenew ? client.verifyAutoRenewReceipt : client.verifyReceipt;
method.call(client, receipt, true, function(valid, msg, data) {
result.ok = valid;
if (valid) {
console.log("Valid receipt");
result.data = data;
var latestReceipt = data.latest_receipt_info || receipt;
if (latestReceipt) {
if (latestReceipt.cancellation_date) {
result.ok = false;
result.data.code = ERR_PAYMENT_CANCELLED;
result.data.error = {message: msg || 'Subscription is cancelled'};
} else if (isAutoRenew) {
var expiresDate = parseInt(data.expires_date);
if (expiresDate < new Date().getTime()) {
result.ok = false;
var err = formatError(21006);
result.data.code = err.code;
result.data.error = {message: err.msg};
}
}
}
} else {
console.log("Invalid receipt");
var err = formatError(data.status);
result.data = {
code: err.code,
error: {
message: err.msg || msg
}
};
}
console.log(valid, msg, 'result valid, msg');
console.log(data, '=====result data');
console.log(data.receipt, '====result receipt');
console.log(data.receipt, data.latest_receipt_info, '====result receipt, lastest_receipt_info');
res.send(result);
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var debug = require('debug')('checkpurchase');
app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('host', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
var callback = function() {
debug('Express server listening on port ' + app.get('port'));
};
if (process.env.OPENSHIFT_NODEJS_IP) {
app.listen(app.get('port'), app.get('host'), callback);
} else {
app.listen(app.get('port'), callback);
}