-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
229 lines (187 loc) · 6.83 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
'use strict';
require('dotenv').config();
const http = require('http');
const express = require('express');
const iap = require('in-app-purchase');
const assert = require('assert');
const Knex = require('knex');
const moment = require('moment');
const asyncHandler = require('express-async-handler');
const { JWT } = require('google-auth-library');
const { google } = require('googleapis');
google.options({ auth: new JWT(
process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
null,
process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY,
['https://www.googleapis.com/auth/androidpublisher'],
) });
const androidGoogleApi = google.androidpublisher({ version: 'v3' });
const knex = Knex({
client: 'mysql',
connection: {
timezone: 'UTC',
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB_NAME,
charset: 'utf8mb4',
},
});
async function getActiveSubscriptions() {
return knex('subscriptions')
.where('end_date', '>=', new Date())
.where('fake', false)
.select(['id', 'latest_receipt', 'user_id', 'app']);
}
async function updateSubscription({
app, environment, origTxId, userId, validationResponse, latestReceipt, startDate, endDate, productId, isCancelled,
}) {
const data = {
app,
environment,
user_id: userId,
orig_tx_id: origTxId,
validation_response: JSON.stringify(validationResponse),
latest_receipt: latestReceipt,
start_date: startDate,
end_date: endDate,
product_id: productId,
is_cancelled: isCancelled,
};
try {
await knex('subscriptions').insert(data);
} catch (err) {
if (err.code !== 'ER_DUP_ENTRY') throw err;
await knex('subscriptions').where('orig_tx_id', origTxId).update(data);
}
}
async function getUserSubscription(userId, appType) {
if (!appType) return undefined;
const row = await knex('subscriptions')
.where({ user_id: userId, app: appType })
.select(['start_date', 'end_date', 'product_id', 'is_cancelled'])
.orderBy('start_date', 'desc')
.first();
if (!row) return undefined;
return {
startDate: row.start_date,
endDate: row.end_date,
productId: row.product_id,
isCancelled: !!row.is_cancelled,
type: 'iap',
};
}
async function processPurchase(app, userId, receipt) {
await iap.setup();
const validationResponse = await iap.validate(receipt);
// Sanity check
assert((app === 'android' && validationResponse.service === 'google')
|| (app === 'ios' && validationResponse.service === 'apple'));
const purchaseData = iap.getPurchaseData(validationResponse);
const firstPurchaseItem = purchaseData[0];
const isCancelled = iap.isCanceled(firstPurchaseItem);
// const isExpired = iap.isExpired(firstPurchaseItem);
const { productId } = firstPurchaseItem;
const origTxId = app === 'ios' ? firstPurchaseItem.originalTransactionId : firstPurchaseItem.transactionId;
const latestReceipt = app === 'ios' ? validationResponse.latest_receipt : JSON.stringify(receipt);
const startDate = app === 'ios' ? new Date(firstPurchaseItem.originalPurchaseDateMs) : new Date(parseInt(firstPurchaseItem.startTimeMillis, 10));
const endDate = app === 'ios' ? new Date(firstPurchaseItem.expiresDateMs) : new Date(parseInt(firstPurchaseItem.expiryTimeMillis, 10));
let environment = '';
// validationResponse contains sandbox: true/false for Apple and Amazon
// Android we don't know if it was a sandbox account
if (app === 'ios') {
environment = validationResponse.sandbox ? 'sandbox' : 'production';
}
await updateSubscription({
userId,
app,
environment,
productId,
origTxId,
latestReceipt,
validationResponse,
startDate,
endDate,
isCancelled,
});
// From https://developer.android.com/google/play/billing/billing_library_overview:
// You must acknowledge all purchases within three days.
// Failure to properly acknowledge purchases results in those purchases being refunded.
if (app === 'android' && validationResponse.acknowledgementState === 0) {
await androidGoogleApi.purchases.subscriptions.acknowledge({
packageName: androidPackageName,
subscriptionId: productId,
token: receipt.purchaseToken,
});
}
}
async function validateAllSubscriptions() {
const subscriptions = await getActiveSubscriptions();
for (const subscription of subscriptions) {
try {
if (subscription.app === 'ios') {
await processPurchase(subscription.app, subscription.user_id, subscription.latest_receipt);
} else {
await processPurchase(subscription.app, subscription.user_id, JSON.parse(subscription.latest_receipt));
}
} catch (err) {
console.error('Failed to validate subscription', subscription.id);
}
}
}
function checkIfHasSubscription(subscription) {
if (!subscription) return false;
if (subscription.isCancelled) return false;
const nowMs = new Date().getTime();
return moment(subscription.startDate).valueOf() <= nowMs
&& moment(subscription.endDate).valueOf() >= nowMs; // TODO grace period?
}
const iapTestMode = process.env.IAP_TEST_MODE === 'true';
const androidPackageName = process.env.ANDROID_PACKAGE_NAME;
// https://www.appypie.com/faqs/how-can-i-get-shared-secret-key-for-in-app-purchase
iap.config({
// If you want to exclude old transaction, set this to true. Default is false:
appleExcludeOldTransactions: true,
// this comes from iTunes Connect (You need this to valiate subscriptions):
applePassword: process.env.APPLE_SHARED_SECRET,
googleServiceAccount: {
clientEmail: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
privateKey: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY,
},
/* Configurations all platforms */
test: iapTestMode, // For Apple and Google Play to force Sandbox validation only
// verbose: true, // Output debug logs to stdout stream
});
const app = express();
const server = http.Server(app);
app.use((req, res, next) => {
// TODO Your auth middleware
req.userId = 123;
next();
});
server.listen(8080);
app.post('/iap/save-receipt', asyncHandler(async (req, res) => {
const { userId } = req;
const { appType, purchase } = req.body;
assert(['ios', 'android'].includes(appType));
const receipt = appType === 'ios' ? purchase.transactionReceipt : {
packageName: androidPackageName,
productId: purchase.productId,
purchaseToken: purchase.purchaseToken,
subscription: true,
};
await processPurchase(appType, userId, receipt);
res.end();
}));
app.get('/iap/user-subscription/:appType', asyncHandler(async (req, res) => {
const { userId } = req;
const { appType } = req.params;
const subscription = await getUserSubscription(userId, appType);
res.send({
subscription,
hasSubscription: checkIfHasSubscription(subscription),
})
res.end();
}));
setInterval(validateAllSubscriptions, 24 * 60 * 60 * 1000);