@mathrunet/masamune 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/functions/agora_cloud_recording.js +47 -44
- package/dist/functions/agora_cloud_recording.js.map +1 -1
- package/dist/functions/agora_token.js +37 -34
- package/dist/functions/agora_token.js.map +1 -1
- package/dist/functions/algolia.js +16 -16
- package/dist/functions/algolia.js.map +1 -1
- package/dist/functions/android_auth_code.js +22 -19
- package/dist/functions/android_auth_code.js.map +1 -1
- package/dist/functions/android_token.js +42 -39
- package/dist/functions/android_token.js.map +1 -1
- package/dist/functions/consumable_verify_android.js +41 -38
- package/dist/functions/consumable_verify_android.js.map +1 -1
- package/dist/functions/consumable_verify_ios.js +36 -33
- package/dist/functions/consumable_verify_ios.js.map +1 -1
- package/dist/functions/geocoding.js +26 -23
- package/dist/functions/geocoding.js.map +1 -1
- package/dist/functions/gmail.js +33 -30
- package/dist/functions/gmail.js.map +1 -1
- package/dist/functions/nonconsumable_verify_android.js +40 -37
- package/dist/functions/nonconsumable_verify_android.js.map +1 -1
- package/dist/functions/nonconsumable_verify_ios.js +35 -32
- package/dist/functions/nonconsumable_verify_ios.js.map +1 -1
- package/dist/functions/openai_chat_gpt.js +37 -34
- package/dist/functions/openai_chat_gpt.js.map +1 -1
- package/dist/functions/purchase_webhook_android.js +2 -2
- package/dist/functions/purchase_webhook_android.js.map +1 -1
- package/dist/functions/purchase_webhook_ios.js +85 -82
- package/dist/functions/purchase_webhook_ios.js.map +1 -1
- package/dist/functions/scheduler.js +4 -4
- package/dist/functions/scheduler.js.map +1 -1
- package/dist/functions/send_grid.js +33 -30
- package/dist/functions/send_grid.js.map +1 -1
- package/dist/functions/send_notification.js +35 -32
- package/dist/functions/send_notification.js.map +1 -1
- package/dist/functions/stripe.js +856 -853
- package/dist/functions/stripe.js.map +1 -1
- package/dist/functions/stripe_webhook.js +358 -355
- package/dist/functions/stripe_webhook.js.map +1 -1
- package/dist/functions/stripe_webhook_connect.js +68 -65
- package/dist/functions/stripe_webhook_connect.js.map +1 -1
- package/dist/functions/stripe_webhook_secure.js +66 -63
- package/dist/functions/stripe_webhook_secure.js.map +1 -1
- package/dist/functions/subscription_verify_android.js +57 -54
- package/dist/functions/subscription_verify_android.js.map +1 -1
- package/dist/functions/subscription_verify_ios.js +57 -54
- package/dist/functions/subscription_verify_ios.js.map +1 -1
- package/dist/lib/call_process_function_base.d.ts +16 -1
- package/dist/lib/call_process_function_base.js +15 -8
- package/dist/lib/call_process_function_base.js.map +1 -1
- package/dist/lib/functions_base.d.ts +44 -0
- package/dist/lib/request_process_function_base.d.ts +16 -1
- package/dist/lib/request_process_function_base.js +15 -8
- package/dist/lib/request_process_function_base.js.map +1 -1
- package/dist/lib/schedule_process_function_base.js +8 -7
- package/dist/lib/schedule_process_function_base.js.map +1 -1
- package/package.json +3 -3
|
@@ -75,396 +75,399 @@ const admin = __importStar(require("firebase-admin"));
|
|
|
75
75
|
* Webhookとして設定したあとの**署名シークレット**を指定します。
|
|
76
76
|
*
|
|
77
77
|
*/
|
|
78
|
-
module.exports = (regions, options, data) =>
|
|
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
|
-
const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret);
|
|
104
|
-
switch (event.type) {
|
|
105
|
-
case "payment_intent.requires_action":
|
|
106
|
-
case "payment_intent.amount_capturable_updated": {
|
|
107
|
-
const payment = event.data.object;
|
|
108
|
-
const purchaseId = payment["id"];
|
|
109
|
-
const customerId = payment["customer"];
|
|
110
|
-
const status = payment["status"];
|
|
111
|
-
if (!customerId) {
|
|
112
|
-
res.status(404).send(JSON.stringify({
|
|
113
|
-
"error": "The customer id is not found.",
|
|
114
|
-
}));
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
if (!status) {
|
|
118
|
-
res.status(404).send(JSON.stringify({
|
|
119
|
-
"error": "The status is not found.",
|
|
120
|
-
}));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
124
|
-
if (userCol.empty) {
|
|
125
|
-
res.status(404).send(JSON.stringify({
|
|
126
|
-
"error": "The account data is not found.",
|
|
127
|
-
}));
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const user = userCol.docs[0];
|
|
131
|
-
const userId = user.id;
|
|
132
|
-
const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
|
|
133
|
-
if (purchaseCol.empty) {
|
|
134
|
-
res.status(404).send(JSON.stringify({
|
|
135
|
-
"error": "The purchase data is not found.",
|
|
136
|
-
}));
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
const purchase = purchaseCol.docs[0];
|
|
140
|
-
const update = {};
|
|
141
|
-
switch (status) {
|
|
142
|
-
case "requires_payment_method":
|
|
143
|
-
case "requires_confirmation": {
|
|
144
|
-
update["confirm"] = false;
|
|
145
|
-
update["verify"] = false;
|
|
146
|
-
update["capture"] = false;
|
|
147
|
-
update["success"] = false;
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
case "requires_action": {
|
|
151
|
-
update["confirm"] = true;
|
|
152
|
-
update["verify"] = false;
|
|
153
|
-
update["capture"] = false;
|
|
154
|
-
update["success"] = false;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
case "requires_capture": {
|
|
158
|
-
update["confirm"] = true;
|
|
159
|
-
update["verify"] = true;
|
|
160
|
-
update["capture"] = false;
|
|
161
|
-
update["success"] = false;
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
update["updatedTime"] = new Date();
|
|
166
|
-
yield purchase.ref.set(update, {
|
|
167
|
-
merge: true,
|
|
168
|
-
});
|
|
169
|
-
res.status(200).send(JSON.stringify({
|
|
170
|
-
"success": true,
|
|
78
|
+
module.exports = (regions, options, data) => {
|
|
79
|
+
var _a;
|
|
80
|
+
return functions.https.onRequest({
|
|
81
|
+
region: (_a = options.region) !== null && _a !== void 0 ? _a : regions,
|
|
82
|
+
timeoutSeconds: options.timeoutSeconds,
|
|
83
|
+
memory: options.memory,
|
|
84
|
+
minInstances: options.minInstances,
|
|
85
|
+
concurrency: options.concurrency,
|
|
86
|
+
maxInstances: options.maxInstances,
|
|
87
|
+
}, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
var _b, _c, _d, _e, _f;
|
|
89
|
+
try {
|
|
90
|
+
const apiKey = (_b = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _b !== void 0 ? _b : "";
|
|
91
|
+
const stripeUserPath = (_c = process.env.PURCHASE_STRIPE_USERPATH) !== null && _c !== void 0 ? _c : "plugins/stripe/user";
|
|
92
|
+
const stripePurchasePath = (_d = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _d !== void 0 ? _d : "purchase";
|
|
93
|
+
const stripeWebhookSecret = (_e = process.env.PURCHASE_STRIPE_WEBHOOKSECRET) !== null && _e !== void 0 ? _e : "";
|
|
94
|
+
const firestoreInstance = admin.firestore();
|
|
95
|
+
const stripeClient = new stripe.Stripe(apiKey, {
|
|
96
|
+
apiVersion: "2022-11-15",
|
|
97
|
+
});
|
|
98
|
+
const signature = req.headers["stripe-signature"];
|
|
99
|
+
if (!signature) {
|
|
100
|
+
res.status(403).send(JSON.stringify({
|
|
101
|
+
"error": "Access denied.",
|
|
171
102
|
}));
|
|
172
103
|
return;
|
|
173
104
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
"error": "The account data is not found.",
|
|
188
|
-
}));
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const user = userCol.docs[0];
|
|
192
|
-
const userId = user.id;
|
|
193
|
-
const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
|
|
194
|
-
if (purchaseCol.empty) {
|
|
195
|
-
res.status(404).send(JSON.stringify({
|
|
196
|
-
"error": "The purchase data is not found.",
|
|
197
|
-
}));
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
const purchase = purchaseCol.docs[0];
|
|
201
|
-
const update = {};
|
|
202
|
-
update["confirm"] = true;
|
|
203
|
-
update["verify"] = true;
|
|
204
|
-
update["capture"] = true;
|
|
205
|
-
update["success"] = true;
|
|
206
|
-
update["error"] = admin.firestore.FieldValue.delete();
|
|
207
|
-
update["errorMessage"] = admin.firestore.FieldValue.delete();
|
|
208
|
-
update["updatedTime"] = new Date();
|
|
209
|
-
if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) {
|
|
210
|
-
if (payment["charges"]["data"][0]["receipt_url"]) {
|
|
211
|
-
update["receiptUrl"] = payment["charges"]["data"][0]["receipt_url"];
|
|
105
|
+
const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret);
|
|
106
|
+
switch (event.type) {
|
|
107
|
+
case "payment_intent.requires_action":
|
|
108
|
+
case "payment_intent.amount_capturable_updated": {
|
|
109
|
+
const payment = event.data.object;
|
|
110
|
+
const purchaseId = payment["id"];
|
|
111
|
+
const customerId = payment["customer"];
|
|
112
|
+
const status = payment["status"];
|
|
113
|
+
if (!customerId) {
|
|
114
|
+
res.status(404).send(JSON.stringify({
|
|
115
|
+
"error": "The customer id is not found.",
|
|
116
|
+
}));
|
|
117
|
+
return;
|
|
212
118
|
}
|
|
213
|
-
if (
|
|
214
|
-
|
|
119
|
+
if (!status) {
|
|
120
|
+
res.status(404).send(JSON.stringify({
|
|
121
|
+
"error": "The status is not found.",
|
|
122
|
+
}));
|
|
123
|
+
return;
|
|
215
124
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
125
|
+
const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
126
|
+
if (userCol.empty) {
|
|
127
|
+
res.status(404).send(JSON.stringify({
|
|
128
|
+
"error": "The account data is not found.",
|
|
129
|
+
}));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const user = userCol.docs[0];
|
|
133
|
+
const userId = user.id;
|
|
134
|
+
const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
|
|
135
|
+
if (purchaseCol.empty) {
|
|
136
|
+
res.status(404).send(JSON.stringify({
|
|
137
|
+
"error": "The purchase data is not found.",
|
|
138
|
+
}));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const purchase = purchaseCol.docs[0];
|
|
142
|
+
const update = {};
|
|
143
|
+
switch (status) {
|
|
144
|
+
case "requires_payment_method":
|
|
145
|
+
case "requires_confirmation": {
|
|
146
|
+
update["confirm"] = false;
|
|
147
|
+
update["verify"] = false;
|
|
148
|
+
update["capture"] = false;
|
|
149
|
+
update["success"] = false;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case "requires_action": {
|
|
153
|
+
update["confirm"] = true;
|
|
154
|
+
update["verify"] = false;
|
|
155
|
+
update["capture"] = false;
|
|
156
|
+
update["success"] = false;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
case "requires_capture": {
|
|
160
|
+
update["confirm"] = true;
|
|
161
|
+
update["verify"] = true;
|
|
162
|
+
update["capture"] = false;
|
|
163
|
+
update["success"] = false;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
update["updatedTime"] = new Date();
|
|
168
|
+
yield purchase.ref.set(update, {
|
|
169
|
+
merge: true,
|
|
170
|
+
});
|
|
171
|
+
res.status(200).send(JSON.stringify({
|
|
172
|
+
"success": true,
|
|
240
173
|
}));
|
|
241
174
|
return;
|
|
242
175
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
176
|
+
case "payment_intent.succeeded": {
|
|
177
|
+
const payment = event.data.object;
|
|
178
|
+
const purchaseId = payment["id"];
|
|
179
|
+
const customerId = payment["customer"];
|
|
180
|
+
if (!customerId) {
|
|
181
|
+
res.status(404).send(JSON.stringify({
|
|
182
|
+
"error": "The customer id is not found.",
|
|
183
|
+
}));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
187
|
+
if (userCol.empty) {
|
|
188
|
+
res.status(404).send(JSON.stringify({
|
|
189
|
+
"error": "The account data is not found.",
|
|
190
|
+
}));
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const user = userCol.docs[0];
|
|
194
|
+
const userId = user.id;
|
|
195
|
+
const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
|
|
196
|
+
if (purchaseCol.empty) {
|
|
197
|
+
res.status(404).send(JSON.stringify({
|
|
198
|
+
"error": "The purchase data is not found.",
|
|
199
|
+
}));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const purchase = purchaseCol.docs[0];
|
|
203
|
+
const update = {};
|
|
204
|
+
update["confirm"] = true;
|
|
205
|
+
update["verify"] = true;
|
|
206
|
+
update["capture"] = true;
|
|
207
|
+
update["success"] = true;
|
|
208
|
+
update["error"] = admin.firestore.FieldValue.delete();
|
|
209
|
+
update["errorMessage"] = admin.firestore.FieldValue.delete();
|
|
210
|
+
update["updatedTime"] = new Date();
|
|
211
|
+
if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) {
|
|
212
|
+
if (payment["charges"]["data"][0]["receipt_url"]) {
|
|
213
|
+
update["receiptUrl"] = payment["charges"]["data"][0]["receipt_url"];
|
|
214
|
+
}
|
|
215
|
+
if (payment["charges"]["data"][0]["amount_captured"]) {
|
|
216
|
+
update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
yield purchase.ref.set(update, {
|
|
220
|
+
merge: true,
|
|
221
|
+
});
|
|
222
|
+
res.status(200).send(JSON.stringify({
|
|
223
|
+
"success": true,
|
|
249
224
|
}));
|
|
250
225
|
return;
|
|
251
226
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
break;
|
|
227
|
+
case "payment_intent.payment_failed": {
|
|
228
|
+
const payment = event.data.object;
|
|
229
|
+
const purchaseId = payment["id"];
|
|
230
|
+
const customerId = payment["customer"];
|
|
231
|
+
const status = payment["status"];
|
|
232
|
+
if (!customerId) {
|
|
233
|
+
res.status(404).send(JSON.stringify({
|
|
234
|
+
"error": "The customer id is not found.",
|
|
235
|
+
}));
|
|
236
|
+
return;
|
|
263
237
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
238
|
+
const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
239
|
+
if (userCol.empty) {
|
|
240
|
+
res.status(404).send(JSON.stringify({
|
|
241
|
+
"error": "The account data is not found.",
|
|
242
|
+
}));
|
|
243
|
+
return;
|
|
270
244
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
245
|
+
const user = userCol.docs[0];
|
|
246
|
+
const userId = user.id;
|
|
247
|
+
const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
|
|
248
|
+
if (purchaseCol.empty) {
|
|
249
|
+
res.status(404).send(JSON.stringify({
|
|
250
|
+
"error": "The purchase data is not found.",
|
|
251
|
+
}));
|
|
252
|
+
return;
|
|
277
253
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
254
|
+
const errorMessage = payment["last_payment_error"]["message"];
|
|
255
|
+
const purchase = purchaseCol.docs[0];
|
|
256
|
+
const update = {};
|
|
257
|
+
switch (status) {
|
|
258
|
+
case "requires_payment_method":
|
|
259
|
+
case "requires_confirmation": {
|
|
260
|
+
update["confirm"] = false;
|
|
261
|
+
update["verify"] = false;
|
|
262
|
+
update["capture"] = false;
|
|
263
|
+
update["success"] = false;
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case "requires_action": {
|
|
267
|
+
update["confirm"] = true;
|
|
268
|
+
update["verify"] = false;
|
|
269
|
+
update["capture"] = false;
|
|
270
|
+
update["success"] = false;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case "requires_capture": {
|
|
274
|
+
update["confirm"] = true;
|
|
275
|
+
update["verify"] = true;
|
|
276
|
+
update["capture"] = false;
|
|
277
|
+
update["success"] = false;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
update["updatedTime"] = new Date();
|
|
282
|
+
update["error"] = true;
|
|
283
|
+
update["errorMessage"] = errorMessage;
|
|
284
|
+
yield purchase.ref.set(update, {
|
|
285
|
+
merge: true,
|
|
286
|
+
});
|
|
287
|
+
res.status(200).send(JSON.stringify({
|
|
288
|
+
"success": true,
|
|
301
289
|
}));
|
|
302
290
|
return;
|
|
303
291
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
292
|
+
case "payment_method.detached":
|
|
293
|
+
case "payment_method.updated": {
|
|
294
|
+
const payment = event.data.object;
|
|
295
|
+
const previous = event.data.previous_attributes;
|
|
296
|
+
let customerId = payment["customer"];
|
|
297
|
+
if (!customerId) {
|
|
298
|
+
customerId = previous["customer"];
|
|
299
|
+
}
|
|
300
|
+
if (!customerId) {
|
|
301
|
+
res.status(404).send(JSON.stringify({
|
|
302
|
+
"error": "The customer id is not found.",
|
|
303
|
+
}));
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
307
|
+
if (col.empty) {
|
|
308
|
+
res.status(404).send(JSON.stringify({
|
|
309
|
+
"error": "The account data is not found.",
|
|
310
|
+
}));
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const user = col.docs[0];
|
|
314
|
+
const userId = user.id;
|
|
315
|
+
yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
|
|
316
|
+
res.status(200).send(JSON.stringify({
|
|
317
|
+
"success": true,
|
|
308
318
|
}));
|
|
309
319
|
return;
|
|
310
320
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
321
|
+
case "customer.updated": {
|
|
322
|
+
const customer = event.data.object;
|
|
323
|
+
const customerId = customer["id"];
|
|
324
|
+
if (!customerId) {
|
|
325
|
+
res.status(404).send(JSON.stringify({
|
|
326
|
+
"error": "The customer id is not found.",
|
|
327
|
+
}));
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
331
|
+
if (col.empty) {
|
|
332
|
+
res.status(404).send(JSON.stringify({
|
|
333
|
+
"error": "The account data is not found.",
|
|
334
|
+
}));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const user = col.docs[0];
|
|
338
|
+
const userId = user.id;
|
|
339
|
+
yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
|
|
340
|
+
res.status(200).send(JSON.stringify({
|
|
341
|
+
"success": true,
|
|
325
342
|
}));
|
|
326
343
|
return;
|
|
327
344
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
345
|
+
case "checkout.session.completed": {
|
|
346
|
+
const session = event.data.object;
|
|
347
|
+
const customerId = session["customer"];
|
|
348
|
+
if (!customerId) {
|
|
349
|
+
res.status(404).send(JSON.stringify({
|
|
350
|
+
"error": "The customer id is not found.",
|
|
351
|
+
}));
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const setupIntent = session["setup_intent"];
|
|
355
|
+
if (!setupIntent) {
|
|
356
|
+
res.status(404).send(JSON.stringify({
|
|
357
|
+
"error": "The setup intent is not found.",
|
|
358
|
+
}));
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
|
|
362
|
+
if (col.empty) {
|
|
363
|
+
res.status(404).send(JSON.stringify({
|
|
364
|
+
"error": "The account data is not found.",
|
|
365
|
+
}));
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const user = col.docs[0];
|
|
369
|
+
const userId = user.id;
|
|
370
|
+
const update = {};
|
|
371
|
+
if (session["setup_intent"]) {
|
|
372
|
+
update["setupIntent"] = session["setup_intent"];
|
|
373
|
+
}
|
|
374
|
+
yield user.ref.set(update, {
|
|
375
|
+
merge: true,
|
|
376
|
+
});
|
|
377
|
+
yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
|
|
378
|
+
res.status(200).send(JSON.stringify({
|
|
379
|
+
"success": true,
|
|
332
380
|
}));
|
|
333
381
|
return;
|
|
334
382
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
383
|
+
case "customer.subscription.trial_will_end":
|
|
384
|
+
case "customer.subscription.updated":
|
|
385
|
+
case "customer.subscription.created": {
|
|
386
|
+
const now = new Date();
|
|
387
|
+
const update = {};
|
|
388
|
+
const subscription = event.data.object;
|
|
389
|
+
const status = subscription["status"];
|
|
390
|
+
if (status != "active") {
|
|
391
|
+
res.status(200).send(JSON.stringify({
|
|
392
|
+
"success": "Subscription is not active.",
|
|
393
|
+
}));
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const endDate = new Date(subscription["current_period_end"] * 1000);
|
|
397
|
+
const id = subscription["id"];
|
|
398
|
+
const userId = subscription.metadata.userId;
|
|
399
|
+
const orderId = (_f = subscription.metadata.orderId) !== null && _f !== void 0 ? _f : id;
|
|
400
|
+
const targetPath = `${stripePurchasePath}/${orderId}`;
|
|
401
|
+
const plan = subscription.plan;
|
|
402
|
+
let doc;
|
|
403
|
+
const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get();
|
|
404
|
+
if (subscriptionCol.empty) {
|
|
405
|
+
doc = yield firestoreInstance.doc(targetPath);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
doc = subscriptionCol.docs[0].ref;
|
|
409
|
+
}
|
|
410
|
+
update["expired"] = now >= endDate;
|
|
411
|
+
if (userId) {
|
|
412
|
+
update["user"] = userId;
|
|
413
|
+
}
|
|
414
|
+
update["@uid"] = orderId;
|
|
415
|
+
update["@time"] = new Date();
|
|
416
|
+
update["subscription"] = id;
|
|
417
|
+
update["application"] = subscription["application"];
|
|
418
|
+
update["application_fee_percent"] = subscription["application_fee_percent"];
|
|
419
|
+
update["cancel_at"] = subscription["cancel_at"];
|
|
420
|
+
update["cancel_at_period_end"] = subscription["cancel_at_period_end"];
|
|
421
|
+
update["canceled_at"] = subscription["canceled_at"];
|
|
422
|
+
update["collection_method"] = subscription["collection_method"];
|
|
423
|
+
update["currency"] = subscription["currency"];
|
|
424
|
+
update["current_period_start"] = subscription["current_period_start"];
|
|
425
|
+
update["current_period_end"] = subscription["current_period_end"];
|
|
426
|
+
update["customer"] = subscription["customer"];
|
|
427
|
+
update["default_payment_method"] = subscription["default_payment_method"];
|
|
428
|
+
update["ended_at"] = subscription["ended_at"];
|
|
429
|
+
update["latest_invoice"] = subscription["latest_invoice"];
|
|
430
|
+
update["price_id"] = plan["id"];
|
|
431
|
+
update["active"] = plan["active"];
|
|
432
|
+
update["amount"] = plan["amount"];
|
|
433
|
+
update["billing_scheme"] = plan["billing_scheme"];
|
|
434
|
+
update["interval"] = plan["interval"];
|
|
435
|
+
update["interval_count"] = plan["interval_count"];
|
|
436
|
+
update["usage_type"] = plan["usage_type"];
|
|
437
|
+
update["quantity"] = subscription["quantity"];
|
|
438
|
+
update["start_date"] = subscription["start_date"];
|
|
439
|
+
update["start_date"] = subscription["start_date"];
|
|
440
|
+
console.log(`Subscription status is ${status} ${stripePurchasePath}.`);
|
|
441
|
+
yield doc.set(update, {
|
|
442
|
+
merge: true,
|
|
443
|
+
});
|
|
444
|
+
res.status(200).send(JSON.stringify({
|
|
445
|
+
"success": "Subscription is active.",
|
|
349
446
|
}));
|
|
350
|
-
|
|
447
|
+
break;
|
|
351
448
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
449
|
+
case "customer.subscription.deleted": {
|
|
450
|
+
const subscription = event.data.object;
|
|
451
|
+
const status = subscription["status"];
|
|
452
|
+
console.log(`Subscription status is ${status} ${"expired"}.`);
|
|
453
|
+
// Then define and call a method to handle the subscription deleted.
|
|
454
|
+
// handleSubscriptionDeleted(subscriptionDeleted);
|
|
455
|
+
break;
|
|
358
456
|
}
|
|
359
|
-
|
|
360
|
-
if (col.empty) {
|
|
457
|
+
default: {
|
|
361
458
|
res.status(404).send(JSON.stringify({
|
|
362
|
-
"error": "
|
|
459
|
+
"error": "Event is not found.",
|
|
363
460
|
}));
|
|
364
461
|
return;
|
|
365
462
|
}
|
|
366
|
-
const user = col.docs[0];
|
|
367
|
-
const userId = user.id;
|
|
368
|
-
const update = {};
|
|
369
|
-
if (session["setup_intent"]) {
|
|
370
|
-
update["setupIntent"] = session["setup_intent"];
|
|
371
|
-
}
|
|
372
|
-
yield user.ref.set(update, {
|
|
373
|
-
merge: true,
|
|
374
|
-
});
|
|
375
|
-
yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
|
|
376
|
-
res.status(200).send(JSON.stringify({
|
|
377
|
-
"success": true,
|
|
378
|
-
}));
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
case "customer.subscription.trial_will_end":
|
|
382
|
-
case "customer.subscription.updated":
|
|
383
|
-
case "customer.subscription.created": {
|
|
384
|
-
const now = new Date();
|
|
385
|
-
const update = {};
|
|
386
|
-
const subscription = event.data.object;
|
|
387
|
-
const status = subscription["status"];
|
|
388
|
-
if (status != "active") {
|
|
389
|
-
res.status(200).send(JSON.stringify({
|
|
390
|
-
"success": "Subscription is not active.",
|
|
391
|
-
}));
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
const endDate = new Date(subscription["current_period_end"] * 1000);
|
|
395
|
-
const id = subscription["id"];
|
|
396
|
-
const userId = subscription.metadata.userId;
|
|
397
|
-
const orderId = (_e = subscription.metadata.orderId) !== null && _e !== void 0 ? _e : id;
|
|
398
|
-
const targetPath = `${stripePurchasePath}/${orderId}`;
|
|
399
|
-
const plan = subscription.plan;
|
|
400
|
-
let doc;
|
|
401
|
-
const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get();
|
|
402
|
-
if (subscriptionCol.empty) {
|
|
403
|
-
doc = yield firestoreInstance.doc(targetPath);
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
doc = subscriptionCol.docs[0].ref;
|
|
407
|
-
}
|
|
408
|
-
update["expired"] = now >= endDate;
|
|
409
|
-
if (userId) {
|
|
410
|
-
update["user"] = userId;
|
|
411
|
-
}
|
|
412
|
-
update["@uid"] = orderId;
|
|
413
|
-
update["@time"] = new Date();
|
|
414
|
-
update["subscription"] = id;
|
|
415
|
-
update["application"] = subscription["application"];
|
|
416
|
-
update["application_fee_percent"] = subscription["application_fee_percent"];
|
|
417
|
-
update["cancel_at"] = subscription["cancel_at"];
|
|
418
|
-
update["cancel_at_period_end"] = subscription["cancel_at_period_end"];
|
|
419
|
-
update["canceled_at"] = subscription["canceled_at"];
|
|
420
|
-
update["collection_method"] = subscription["collection_method"];
|
|
421
|
-
update["currency"] = subscription["currency"];
|
|
422
|
-
update["current_period_start"] = subscription["current_period_start"];
|
|
423
|
-
update["current_period_end"] = subscription["current_period_end"];
|
|
424
|
-
update["customer"] = subscription["customer"];
|
|
425
|
-
update["default_payment_method"] = subscription["default_payment_method"];
|
|
426
|
-
update["ended_at"] = subscription["ended_at"];
|
|
427
|
-
update["latest_invoice"] = subscription["latest_invoice"];
|
|
428
|
-
update["price_id"] = plan["id"];
|
|
429
|
-
update["active"] = plan["active"];
|
|
430
|
-
update["amount"] = plan["amount"];
|
|
431
|
-
update["billing_scheme"] = plan["billing_scheme"];
|
|
432
|
-
update["interval"] = plan["interval"];
|
|
433
|
-
update["interval_count"] = plan["interval_count"];
|
|
434
|
-
update["usage_type"] = plan["usage_type"];
|
|
435
|
-
update["quantity"] = subscription["quantity"];
|
|
436
|
-
update["start_date"] = subscription["start_date"];
|
|
437
|
-
update["start_date"] = subscription["start_date"];
|
|
438
|
-
console.log(`Subscription status is ${status} ${stripePurchasePath}.`);
|
|
439
|
-
yield doc.set(update, {
|
|
440
|
-
merge: true,
|
|
441
|
-
});
|
|
442
|
-
res.status(200).send(JSON.stringify({
|
|
443
|
-
"success": "Subscription is active.",
|
|
444
|
-
}));
|
|
445
|
-
break;
|
|
446
|
-
}
|
|
447
|
-
case "customer.subscription.deleted": {
|
|
448
|
-
const subscription = event.data.object;
|
|
449
|
-
const status = subscription["status"];
|
|
450
|
-
console.log(`Subscription status is ${status} ${"expired"}.`);
|
|
451
|
-
// Then define and call a method to handle the subscription deleted.
|
|
452
|
-
// handleSubscriptionDeleted(subscriptionDeleted);
|
|
453
|
-
break;
|
|
454
|
-
}
|
|
455
|
-
default: {
|
|
456
|
-
res.status(404).send(JSON.stringify({
|
|
457
|
-
"error": "Event is not found.",
|
|
458
|
-
}));
|
|
459
|
-
return;
|
|
460
463
|
}
|
|
461
464
|
}
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
}
|
|
465
|
+
catch (err) {
|
|
466
|
+
console.error(err);
|
|
467
|
+
throw err;
|
|
468
|
+
}
|
|
469
|
+
}));
|
|
470
|
+
};
|
|
468
471
|
/**
|
|
469
472
|
* Synchronize Stripe acquisition results to Firestore.
|
|
470
473
|
*
|