@mathrunet/masamune 2.0.0 → 2.0.1

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/functions/agora_cloud_recording.js +44 -47
  3. package/dist/functions/agora_cloud_recording.js.map +1 -1
  4. package/dist/functions/agora_token.js +34 -37
  5. package/dist/functions/agora_token.js.map +1 -1
  6. package/dist/functions/android_auth_code.js +19 -22
  7. package/dist/functions/android_auth_code.js.map +1 -1
  8. package/dist/functions/android_token.js +39 -42
  9. package/dist/functions/android_token.js.map +1 -1
  10. package/dist/functions/consumable_verify_android.js +38 -41
  11. package/dist/functions/consumable_verify_android.js.map +1 -1
  12. package/dist/functions/consumable_verify_ios.js +33 -36
  13. package/dist/functions/consumable_verify_ios.js.map +1 -1
  14. package/dist/functions/geocoding.js +23 -26
  15. package/dist/functions/geocoding.js.map +1 -1
  16. package/dist/functions/gmail.js +30 -33
  17. package/dist/functions/gmail.js.map +1 -1
  18. package/dist/functions/nonconsumable_verify_android.js +37 -40
  19. package/dist/functions/nonconsumable_verify_android.js.map +1 -1
  20. package/dist/functions/nonconsumable_verify_ios.js +32 -35
  21. package/dist/functions/nonconsumable_verify_ios.js.map +1 -1
  22. package/dist/functions/openai_chat_gpt.js +34 -37
  23. package/dist/functions/openai_chat_gpt.js.map +1 -1
  24. package/dist/functions/purchase_webhook_android.js +2 -2
  25. package/dist/functions/purchase_webhook_android.js.map +1 -1
  26. package/dist/functions/purchase_webhook_ios.js +82 -85
  27. package/dist/functions/purchase_webhook_ios.js.map +1 -1
  28. package/dist/functions/scheduler.js +4 -4
  29. package/dist/functions/scheduler.js.map +1 -1
  30. package/dist/functions/send_grid.js +30 -33
  31. package/dist/functions/send_grid.js.map +1 -1
  32. package/dist/functions/send_notification.js +32 -35
  33. package/dist/functions/send_notification.js.map +1 -1
  34. package/dist/functions/stripe.js +853 -856
  35. package/dist/functions/stripe.js.map +1 -1
  36. package/dist/functions/stripe_webhook.js +355 -358
  37. package/dist/functions/stripe_webhook.js.map +1 -1
  38. package/dist/functions/stripe_webhook_connect.js +65 -68
  39. package/dist/functions/stripe_webhook_connect.js.map +1 -1
  40. package/dist/functions/stripe_webhook_secure.js +63 -66
  41. package/dist/functions/stripe_webhook_secure.js.map +1 -1
  42. package/dist/functions/subscription_verify_android.js +54 -57
  43. package/dist/functions/subscription_verify_android.js.map +1 -1
  44. package/dist/functions/subscription_verify_ios.js +54 -57
  45. package/dist/functions/subscription_verify_ios.js.map +1 -1
  46. package/dist/lib/call_process_function_base.d.ts +3 -1
  47. package/dist/lib/call_process_function_base.js +1 -2
  48. package/dist/lib/call_process_function_base.js.map +1 -1
  49. package/dist/lib/functions_base.d.ts +1 -1
  50. package/dist/lib/functions_base.js +1 -0
  51. package/dist/lib/functions_base.js.map +1 -1
  52. package/dist/lib/request_process_function_base.d.ts +2 -0
  53. package/dist/lib/request_process_function_base.js +1 -2
  54. package/dist/lib/request_process_function_base.js.map +1 -1
  55. package/dist/lib/schedule_process_function_base.js +2 -2
  56. package/dist/lib/schedule_process_function_base.js.map +1 -1
  57. package/package.json +1 -1
@@ -75,399 +75,396 @@ const admin = __importStar(require("firebase-admin"));
75
75
  * Webhookとして設定したあとの**署名シークレット**を指定します。
76
76
  *
77
77
  */
78
- module.exports = (regions, options, data) => {
79
- var _a;
80
- return functions.https.onRequest({
81
- region: regions,
82
- timeoutSeconds: options.timeoutSeconds,
83
- memory: options.memory,
84
- minInstances: options.minInstances,
85
- concurrency: options.concurrency,
86
- maxInstances: (_a = options.maxInstances) !== null && _a !== void 0 ? _a : undefined,
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.",
102
- }));
103
- return;
104
- }
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;
118
- }
119
- if (!status) {
120
- res.status(404).send(JSON.stringify({
121
- "error": "The status is not found.",
122
- }));
123
- return;
124
- }
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,
78
+ module.exports = (regions, options, data) => functions.https.onRequest({
79
+ region: regions,
80
+ timeoutSeconds: options.timeoutSeconds,
81
+ memory: options.memory,
82
+ minInstances: options.minInstances,
83
+ concurrency: options.concurrency,
84
+ maxInstances: options.maxInstances,
85
+ }, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
86
+ var _a, _b, _c, _d, _e;
87
+ try {
88
+ const apiKey = (_a = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _a !== void 0 ? _a : "";
89
+ const stripeUserPath = (_b = process.env.PURCHASE_STRIPE_USERPATH) !== null && _b !== void 0 ? _b : "plugins/stripe/user";
90
+ const stripePurchasePath = (_c = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _c !== void 0 ? _c : "purchase";
91
+ const stripeWebhookSecret = (_d = process.env.PURCHASE_STRIPE_WEBHOOKSECRET) !== null && _d !== void 0 ? _d : "";
92
+ const firestoreInstance = admin.firestore();
93
+ const stripeClient = new stripe.Stripe(apiKey, {
94
+ apiVersion: "2022-11-15",
95
+ });
96
+ const signature = req.headers["stripe-signature"];
97
+ if (!signature) {
98
+ res.status(403).send(JSON.stringify({
99
+ "error": "Access denied.",
100
+ }));
101
+ return;
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.",
173
114
  }));
174
115
  return;
175
116
  }
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,
117
+ if (!status) {
118
+ res.status(404).send(JSON.stringify({
119
+ "error": "The status is not found.",
224
120
  }));
225
121
  return;
226
122
  }
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;
237
- }
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;
244
- }
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;
253
- }
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,
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.",
289
127
  }));
290
128
  return;
291
129
  }
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"];
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;
299
149
  }
300
- if (!customerId) {
301
- res.status(404).send(JSON.stringify({
302
- "error": "The customer id is not found.",
303
- }));
304
- return;
150
+ case "requires_action": {
151
+ update["confirm"] = true;
152
+ update["verify"] = false;
153
+ update["capture"] = false;
154
+ update["success"] = false;
155
+ break;
305
156
  }
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;
157
+ case "requires_capture": {
158
+ update["confirm"] = true;
159
+ update["verify"] = true;
160
+ update["capture"] = false;
161
+ update["success"] = false;
162
+ break;
312
163
  }
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,
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,
171
+ }));
172
+ return;
173
+ }
174
+ case "payment_intent.succeeded": {
175
+ const payment = event.data.object;
176
+ const purchaseId = payment["id"];
177
+ const customerId = payment["customer"];
178
+ if (!customerId) {
179
+ res.status(404).send(JSON.stringify({
180
+ "error": "The customer id is not found.",
318
181
  }));
319
182
  return;
320
183
  }
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,
184
+ const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
185
+ if (userCol.empty) {
186
+ res.status(404).send(JSON.stringify({
187
+ "error": "The account data is not found.",
342
188
  }));
343
189
  return;
344
190
  }
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;
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"];
367
212
  }
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"];
213
+ if (payment["charges"]["data"][0]["amount_captured"]) {
214
+ update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"];
373
215
  }
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,
216
+ }
217
+ yield purchase.ref.set(update, {
218
+ merge: true,
219
+ });
220
+ res.status(200).send(JSON.stringify({
221
+ "success": true,
222
+ }));
223
+ return;
224
+ }
225
+ case "payment_intent.payment_failed": {
226
+ const payment = event.data.object;
227
+ const purchaseId = payment["id"];
228
+ const customerId = payment["customer"];
229
+ const status = payment["status"];
230
+ if (!customerId) {
231
+ res.status(404).send(JSON.stringify({
232
+ "error": "The customer id is not found.",
380
233
  }));
381
234
  return;
382
235
  }
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);
236
+ const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
237
+ if (userCol.empty) {
238
+ res.status(404).send(JSON.stringify({
239
+ "error": "The account data is not found.",
240
+ }));
241
+ return;
242
+ }
243
+ const user = userCol.docs[0];
244
+ const userId = user.id;
245
+ const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
246
+ if (purchaseCol.empty) {
247
+ res.status(404).send(JSON.stringify({
248
+ "error": "The purchase data is not found.",
249
+ }));
250
+ return;
251
+ }
252
+ const errorMessage = payment["last_payment_error"]["message"];
253
+ const purchase = purchaseCol.docs[0];
254
+ const update = {};
255
+ switch (status) {
256
+ case "requires_payment_method":
257
+ case "requires_confirmation": {
258
+ update["confirm"] = false;
259
+ update["verify"] = false;
260
+ update["capture"] = false;
261
+ update["success"] = false;
262
+ break;
406
263
  }
407
- else {
408
- doc = subscriptionCol.docs[0].ref;
264
+ case "requires_action": {
265
+ update["confirm"] = true;
266
+ update["verify"] = false;
267
+ update["capture"] = false;
268
+ update["success"] = false;
269
+ break;
409
270
  }
410
- update["expired"] = now >= endDate;
411
- if (userId) {
412
- update["user"] = userId;
271
+ case "requires_capture": {
272
+ update["confirm"] = true;
273
+ update["verify"] = true;
274
+ update["capture"] = false;
275
+ update["success"] = false;
276
+ break;
413
277
  }
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.",
278
+ }
279
+ update["updatedTime"] = new Date();
280
+ update["error"] = true;
281
+ update["errorMessage"] = errorMessage;
282
+ yield purchase.ref.set(update, {
283
+ merge: true,
284
+ });
285
+ res.status(200).send(JSON.stringify({
286
+ "success": true,
287
+ }));
288
+ return;
289
+ }
290
+ case "payment_method.detached":
291
+ case "payment_method.updated": {
292
+ const payment = event.data.object;
293
+ const previous = event.data.previous_attributes;
294
+ let customerId = payment["customer"];
295
+ if (!customerId) {
296
+ customerId = previous["customer"];
297
+ }
298
+ if (!customerId) {
299
+ res.status(404).send(JSON.stringify({
300
+ "error": "The customer id is not found.",
301
+ }));
302
+ return;
303
+ }
304
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
305
+ if (col.empty) {
306
+ res.status(404).send(JSON.stringify({
307
+ "error": "The account data is not found.",
446
308
  }));
447
- break;
309
+ return;
448
310
  }
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;
311
+ const user = col.docs[0];
312
+ const userId = user.id;
313
+ yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
314
+ res.status(200).send(JSON.stringify({
315
+ "success": true,
316
+ }));
317
+ return;
318
+ }
319
+ case "customer.updated": {
320
+ const customer = event.data.object;
321
+ const customerId = customer["id"];
322
+ if (!customerId) {
323
+ res.status(404).send(JSON.stringify({
324
+ "error": "The customer id is not found.",
325
+ }));
326
+ return;
456
327
  }
457
- default: {
328
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
329
+ if (col.empty) {
458
330
  res.status(404).send(JSON.stringify({
459
- "error": "Event is not found.",
331
+ "error": "The account data is not found.",
460
332
  }));
461
333
  return;
462
334
  }
335
+ const user = col.docs[0];
336
+ const userId = user.id;
337
+ yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
338
+ res.status(200).send(JSON.stringify({
339
+ "success": true,
340
+ }));
341
+ return;
342
+ }
343
+ case "checkout.session.completed": {
344
+ const session = event.data.object;
345
+ const customerId = session["customer"];
346
+ if (!customerId) {
347
+ res.status(404).send(JSON.stringify({
348
+ "error": "The customer id is not found.",
349
+ }));
350
+ return;
351
+ }
352
+ const setupIntent = session["setup_intent"];
353
+ if (!setupIntent) {
354
+ res.status(404).send(JSON.stringify({
355
+ "error": "The setup intent is not found.",
356
+ }));
357
+ return;
358
+ }
359
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
360
+ if (col.empty) {
361
+ res.status(404).send(JSON.stringify({
362
+ "error": "The account data is not found.",
363
+ }));
364
+ return;
365
+ }
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;
463
460
  }
464
461
  }
465
- catch (err) {
466
- console.error(err);
467
- throw err;
468
- }
469
- }));
470
- };
462
+ }
463
+ catch (err) {
464
+ console.error(err);
465
+ throw err;
466
+ }
467
+ }));
471
468
  /**
472
469
  * Synchronize Stripe acquisition results to Firestore.
473
470
  *