@mathrunet/masamune 2.14.1 → 3.0.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/functions/consumable_verify_android.js +23 -7
  3. package/dist/functions/consumable_verify_android.js.map +1 -1
  4. package/dist/functions/consumable_verify_ios.js +23 -7
  5. package/dist/functions/consumable_verify_ios.js.map +1 -1
  6. package/dist/functions/delete_documents.js +6 -1
  7. package/dist/functions/delete_documents.js.map +1 -1
  8. package/dist/functions/nonconsumable_verify_android.js +22 -6
  9. package/dist/functions/nonconsumable_verify_android.js.map +1 -1
  10. package/dist/functions/nonconsumable_verify_ios.js +22 -6
  11. package/dist/functions/nonconsumable_verify_ios.js.map +1 -1
  12. package/dist/functions/purchase_webhook_android.js +143 -129
  13. package/dist/functions/purchase_webhook_android.js.map +1 -1
  14. package/dist/functions/purchase_webhook_ios.js +81 -67
  15. package/dist/functions/purchase_webhook_ios.js.map +1 -1
  16. package/dist/functions/scheduler.js +61 -48
  17. package/dist/functions/scheduler.js.map +1 -1
  18. package/dist/functions/send_notification.js +42 -16
  19. package/dist/functions/send_notification.js.map +1 -1
  20. package/dist/functions/stripe.js +935 -921
  21. package/dist/functions/stripe.js.map +1 -1
  22. package/dist/functions/stripe_webhook.js +371 -357
  23. package/dist/functions/stripe_webhook.js.map +1 -1
  24. package/dist/functions/stripe_webhook_connect.js +64 -51
  25. package/dist/functions/stripe_webhook_connect.js.map +1 -1
  26. package/dist/functions/stripe_webhook_secure.js +63 -50
  27. package/dist/functions/stripe_webhook_secure.js.map +1 -1
  28. package/dist/functions/subscription_verify_android.js +31 -15
  29. package/dist/functions/subscription_verify_android.js.map +1 -1
  30. package/dist/functions/subscription_verify_ios.js +31 -15
  31. package/dist/functions/subscription_verify_ios.js.map +1 -1
  32. package/dist/functions/test.js +8 -4
  33. package/dist/functions/test.js.map +1 -1
  34. package/dist/lib/exntensions/string.extension.d.ts +12 -0
  35. package/dist/lib/exntensions/string.extension.js +15 -0
  36. package/dist/lib/exntensions/string.extension.js.map +1 -1
  37. package/dist/lib/functions/delete_documents.d.ts +2 -1
  38. package/dist/lib/functions/delete_documents.js +1 -3
  39. package/dist/lib/functions/delete_documents.js.map +1 -1
  40. package/dist/lib/functions/send_notification.d.ts +2 -1
  41. package/dist/lib/functions/send_notification.js +3 -3
  42. package/dist/lib/functions/send_notification.js.map +1 -1
  43. package/dist/lib/functions/update_subscription.d.ts +2 -1
  44. package/dist/lib/functions/update_subscription.js +1 -3
  45. package/dist/lib/functions/update_subscription.js.map +1 -1
  46. package/dist/lib/functions/update_unlock.d.ts +2 -1
  47. package/dist/lib/functions/update_unlock.js +1 -3
  48. package/dist/lib/functions/update_unlock.js.map +1 -1
  49. package/dist/lib/functions/update_wallet.d.ts +2 -1
  50. package/dist/lib/functions/update_wallet.js +1 -2
  51. package/dist/lib/functions/update_wallet.js.map +1 -1
  52. package/dist/lib/schedulers/copy_document.d.ts +3 -3
  53. package/dist/lib/schedulers/copy_document.js +3 -3
  54. package/dist/lib/schedulers/copy_document.js.map +1 -1
  55. package/dist/lib/schedulers/delete_documents.d.ts +3 -3
  56. package/dist/lib/schedulers/delete_documents.js +3 -2
  57. package/dist/lib/schedulers/delete_documents.js.map +1 -1
  58. package/dist/lib/schedulers/notification.d.ts +3 -3
  59. package/dist/lib/schedulers/notification.js +3 -2
  60. package/dist/lib/schedulers/notification.js.map +1 -1
  61. package/dist/lib/src/firebase_loader.d.ts +30 -0
  62. package/dist/lib/src/firebase_loader.js +77 -0
  63. package/dist/lib/src/firebase_loader.js.map +1 -0
  64. package/dist/lib/src/functions_base.d.ts +12 -0
  65. package/package.json +1 -1
@@ -45,6 +45,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
45
45
  const functions = __importStar(require("firebase-functions/v2"));
46
46
  const stripe = __importStar(require("stripe"));
47
47
  const admin = __importStar(require("firebase-admin"));
48
+ const firebase_loader_1 = require("../lib/src/firebase_loader");
48
49
  /**
49
50
  * Receives and processes webhooks from Stripe.
50
51
  * Please register the URL when you deploy this in your Stripe webhook settings.
@@ -96,382 +97,395 @@ module.exports = (regions, options, data) => {
96
97
  maxInstances: options.maxInstances,
97
98
  serviceAccount: (_b = options.serviceAccount) !== null && _b !== void 0 ? _b : undefined,
98
99
  }, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
99
- var _a, _b, _c, _d, _e;
100
+ var _a, _b, _c, _d, _e, _f;
100
101
  try {
101
- const apiKey = (_a = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _a !== void 0 ? _a : "";
102
- const stripeUserPath = (_b = process.env.PURCHASE_STRIPE_USERPATH) !== null && _b !== void 0 ? _b : "plugins/stripe/user";
103
- const stripePurchasePath = (_c = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _c !== void 0 ? _c : "purchase";
104
- const stripeWebhookSecret = (_d = process.env.PURCHASE_STRIPE_WEBHOOKSECRET) !== null && _d !== void 0 ? _d : "";
105
- const firestoreInstance = admin.firestore();
106
- const stripeClient = new stripe.Stripe(apiKey, {
107
- apiVersion: "2025-01-27.acacia",
108
- });
109
- const signature = req.headers["stripe-signature"];
110
- if (!signature) {
111
- res.status(403).send(JSON.stringify({
112
- "error": "Access denied.",
113
- }));
114
- return;
115
- }
116
- const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret);
117
- switch (event.type) {
118
- case "payment_intent.requires_action":
119
- case "payment_intent.amount_capturable_updated": {
120
- const payment = event.data.object;
121
- const purchaseId = payment["id"];
122
- const customerId = payment["customer"];
123
- const status = payment["status"];
124
- if (!customerId) {
125
- res.status(404).send(JSON.stringify({
126
- "error": "The customer id is not found.",
127
- }));
128
- return;
129
- }
130
- if (!status) {
131
- res.status(404).send(JSON.stringify({
132
- "error": "The status is not found.",
133
- }));
134
- return;
135
- }
136
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
137
- if (userCol.empty) {
138
- res.status(404).send(JSON.stringify({
139
- "error": "The account data is not found.",
140
- }));
141
- return;
142
- }
143
- const user = userCol.docs[0];
144
- const userId = user.id;
145
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
146
- if (purchaseCol.empty) {
147
- res.status(404).send(JSON.stringify({
148
- "error": "The purchase data is not found.",
102
+ let error = null;
103
+ const firestoreDatabaseIds = (_a = options.firestoreDatabaseIds) !== null && _a !== void 0 ? _a : [""];
104
+ for (const databaseId of firestoreDatabaseIds) {
105
+ try {
106
+ const apiKey = (_b = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _b !== void 0 ? _b : "";
107
+ const stripeUserPath = (_c = process.env.PURCHASE_STRIPE_USERPATH) !== null && _c !== void 0 ? _c : "plugins/stripe/user";
108
+ const stripePurchasePath = (_d = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _d !== void 0 ? _d : "purchase";
109
+ const stripeWebhookSecret = (_e = process.env.PURCHASE_STRIPE_WEBHOOKSECRET) !== null && _e !== void 0 ? _e : "";
110
+ const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId);
111
+ const stripeClient = new stripe.Stripe(apiKey, {
112
+ apiVersion: "2025-01-27.acacia",
113
+ });
114
+ const signature = req.headers["stripe-signature"];
115
+ if (!signature) {
116
+ res.status(403).send(JSON.stringify({
117
+ "error": "Access denied.",
149
118
  }));
150
119
  return;
151
120
  }
152
- const purchase = purchaseCol.docs[0];
153
- const update = {};
154
- switch (status) {
155
- case "requires_payment_method":
156
- case "requires_confirmation": {
157
- update["confirm"] = false;
158
- update["verify"] = false;
159
- update["capture"] = false;
160
- update["success"] = false;
161
- break;
162
- }
163
- case "requires_action": {
164
- update["confirm"] = true;
165
- update["verify"] = false;
166
- update["capture"] = false;
167
- update["success"] = false;
168
- break;
121
+ const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret);
122
+ switch (event.type) {
123
+ case "payment_intent.requires_action":
124
+ case "payment_intent.amount_capturable_updated": {
125
+ const payment = event.data.object;
126
+ const purchaseId = payment["id"];
127
+ const customerId = payment["customer"];
128
+ const status = payment["status"];
129
+ if (!customerId) {
130
+ res.status(404).send(JSON.stringify({
131
+ "error": "The customer id is not found.",
132
+ }));
133
+ return;
134
+ }
135
+ if (!status) {
136
+ res.status(404).send(JSON.stringify({
137
+ "error": "The status is not found.",
138
+ }));
139
+ return;
140
+ }
141
+ const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
142
+ if (userCol.empty) {
143
+ res.status(404).send(JSON.stringify({
144
+ "error": "The account data is not found.",
145
+ }));
146
+ return;
147
+ }
148
+ const user = userCol.docs[0];
149
+ const userId = user.id;
150
+ const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
151
+ if (purchaseCol.empty) {
152
+ res.status(404).send(JSON.stringify({
153
+ "error": "The purchase data is not found.",
154
+ }));
155
+ return;
156
+ }
157
+ const purchase = purchaseCol.docs[0];
158
+ const update = {};
159
+ switch (status) {
160
+ case "requires_payment_method":
161
+ case "requires_confirmation": {
162
+ update["confirm"] = false;
163
+ update["verify"] = false;
164
+ update["capture"] = false;
165
+ update["success"] = false;
166
+ break;
167
+ }
168
+ case "requires_action": {
169
+ update["confirm"] = true;
170
+ update["verify"] = false;
171
+ update["capture"] = false;
172
+ update["success"] = false;
173
+ break;
174
+ }
175
+ case "requires_capture": {
176
+ update["confirm"] = true;
177
+ update["verify"] = true;
178
+ update["capture"] = false;
179
+ update["success"] = false;
180
+ break;
181
+ }
182
+ }
183
+ update["updatedTime"] = new Date();
184
+ yield purchase.ref.set(update, {
185
+ merge: true,
186
+ });
187
+ res.status(200).send(JSON.stringify({
188
+ "success": true,
189
+ }));
190
+ return;
169
191
  }
170
- case "requires_capture": {
192
+ case "payment_intent.succeeded": {
193
+ const payment = event.data.object;
194
+ const purchaseId = payment["id"];
195
+ const customerId = payment["customer"];
196
+ if (!customerId) {
197
+ res.status(404).send(JSON.stringify({
198
+ "error": "The customer id is not found.",
199
+ }));
200
+ return;
201
+ }
202
+ const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
203
+ if (userCol.empty) {
204
+ res.status(404).send(JSON.stringify({
205
+ "error": "The account data is not found.",
206
+ }));
207
+ return;
208
+ }
209
+ const user = userCol.docs[0];
210
+ const userId = user.id;
211
+ const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
212
+ if (purchaseCol.empty) {
213
+ res.status(404).send(JSON.stringify({
214
+ "error": "The purchase data is not found.",
215
+ }));
216
+ return;
217
+ }
218
+ const purchase = purchaseCol.docs[0];
219
+ const update = {};
171
220
  update["confirm"] = true;
172
221
  update["verify"] = true;
173
- update["capture"] = false;
174
- update["success"] = false;
175
- break;
222
+ update["capture"] = true;
223
+ update["success"] = true;
224
+ update["error"] = admin.firestore.FieldValue.delete();
225
+ update["errorMessage"] = admin.firestore.FieldValue.delete();
226
+ update["updatedTime"] = new Date();
227
+ if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) {
228
+ if (payment["charges"]["data"][0]["receipt_url"]) {
229
+ update["receiptUrl"] = payment["charges"]["data"][0]["receipt_url"];
230
+ }
231
+ if (payment["charges"]["data"][0]["amount_captured"]) {
232
+ update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"];
233
+ }
234
+ }
235
+ yield purchase.ref.set(update, {
236
+ merge: true,
237
+ });
238
+ res.status(200).send(JSON.stringify({
239
+ "success": true,
240
+ }));
241
+ return;
176
242
  }
177
- }
178
- update["updatedTime"] = new Date();
179
- yield purchase.ref.set(update, {
180
- merge: true,
181
- });
182
- res.status(200).send(JSON.stringify({
183
- "success": true,
184
- }));
185
- return;
186
- }
187
- case "payment_intent.succeeded": {
188
- const payment = event.data.object;
189
- const purchaseId = payment["id"];
190
- const customerId = payment["customer"];
191
- if (!customerId) {
192
- res.status(404).send(JSON.stringify({
193
- "error": "The customer id is not found.",
194
- }));
195
- return;
196
- }
197
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
198
- if (userCol.empty) {
199
- res.status(404).send(JSON.stringify({
200
- "error": "The account data is not found.",
201
- }));
202
- return;
203
- }
204
- const user = userCol.docs[0];
205
- const userId = user.id;
206
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
207
- if (purchaseCol.empty) {
208
- res.status(404).send(JSON.stringify({
209
- "error": "The purchase data is not found.",
210
- }));
211
- return;
212
- }
213
- const purchase = purchaseCol.docs[0];
214
- const update = {};
215
- update["confirm"] = true;
216
- update["verify"] = true;
217
- update["capture"] = true;
218
- update["success"] = true;
219
- update["error"] = admin.firestore.FieldValue.delete();
220
- update["errorMessage"] = admin.firestore.FieldValue.delete();
221
- update["updatedTime"] = new Date();
222
- if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) {
223
- if (payment["charges"]["data"][0]["receipt_url"]) {
224
- update["receiptUrl"] = payment["charges"]["data"][0]["receipt_url"];
243
+ case "payment_intent.payment_failed": {
244
+ const payment = event.data.object;
245
+ const purchaseId = payment["id"];
246
+ const customerId = payment["customer"];
247
+ const status = payment["status"];
248
+ if (!customerId) {
249
+ res.status(404).send(JSON.stringify({
250
+ "error": "The customer id is not found.",
251
+ }));
252
+ return;
253
+ }
254
+ const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
255
+ if (userCol.empty) {
256
+ res.status(404).send(JSON.stringify({
257
+ "error": "The account data is not found.",
258
+ }));
259
+ return;
260
+ }
261
+ const user = userCol.docs[0];
262
+ const userId = user.id;
263
+ const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
264
+ if (purchaseCol.empty) {
265
+ res.status(404).send(JSON.stringify({
266
+ "error": "The purchase data is not found.",
267
+ }));
268
+ return;
269
+ }
270
+ const errorMessage = payment["last_payment_error"]["message"];
271
+ const purchase = purchaseCol.docs[0];
272
+ const update = {};
273
+ switch (status) {
274
+ case "requires_payment_method":
275
+ case "requires_confirmation": {
276
+ update["confirm"] = false;
277
+ update["verify"] = false;
278
+ update["capture"] = false;
279
+ update["success"] = false;
280
+ break;
281
+ }
282
+ case "requires_action": {
283
+ update["confirm"] = true;
284
+ update["verify"] = false;
285
+ update["capture"] = false;
286
+ update["success"] = false;
287
+ break;
288
+ }
289
+ case "requires_capture": {
290
+ update["confirm"] = true;
291
+ update["verify"] = true;
292
+ update["capture"] = false;
293
+ update["success"] = false;
294
+ break;
295
+ }
296
+ }
297
+ update["updatedTime"] = new Date();
298
+ update["error"] = true;
299
+ update["errorMessage"] = errorMessage;
300
+ yield purchase.ref.set(update, {
301
+ merge: true,
302
+ });
303
+ res.status(200).send(JSON.stringify({
304
+ "success": true,
305
+ }));
306
+ return;
225
307
  }
226
- if (payment["charges"]["data"][0]["amount_captured"]) {
227
- update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"];
308
+ case "payment_method.detached":
309
+ case "payment_method.updated": {
310
+ const payment = event.data.object;
311
+ const previous = event.data.previous_attributes;
312
+ let customerId = payment["customer"];
313
+ if (!customerId) {
314
+ customerId = previous["customer"];
315
+ }
316
+ if (!customerId) {
317
+ res.status(404).send(JSON.stringify({
318
+ "error": "The customer id is not found.",
319
+ }));
320
+ return;
321
+ }
322
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
323
+ if (col.empty) {
324
+ res.status(404).send(JSON.stringify({
325
+ "error": "The account data is not found.",
326
+ }));
327
+ return;
328
+ }
329
+ const user = col.docs[0];
330
+ const userId = user.id;
331
+ yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
332
+ res.status(200).send(JSON.stringify({
333
+ "success": true,
334
+ }));
335
+ return;
228
336
  }
229
- }
230
- yield purchase.ref.set(update, {
231
- merge: true,
232
- });
233
- res.status(200).send(JSON.stringify({
234
- "success": true,
235
- }));
236
- return;
237
- }
238
- case "payment_intent.payment_failed": {
239
- const payment = event.data.object;
240
- const purchaseId = payment["id"];
241
- const customerId = payment["customer"];
242
- const status = payment["status"];
243
- if (!customerId) {
244
- res.status(404).send(JSON.stringify({
245
- "error": "The customer id is not found.",
246
- }));
247
- return;
248
- }
249
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
250
- if (userCol.empty) {
251
- res.status(404).send(JSON.stringify({
252
- "error": "The account data is not found.",
253
- }));
254
- return;
255
- }
256
- const user = userCol.docs[0];
257
- const userId = user.id;
258
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
259
- if (purchaseCol.empty) {
260
- res.status(404).send(JSON.stringify({
261
- "error": "The purchase data is not found.",
262
- }));
263
- return;
264
- }
265
- const errorMessage = payment["last_payment_error"]["message"];
266
- const purchase = purchaseCol.docs[0];
267
- const update = {};
268
- switch (status) {
269
- case "requires_payment_method":
270
- case "requires_confirmation": {
271
- update["confirm"] = false;
272
- update["verify"] = false;
273
- update["capture"] = false;
274
- update["success"] = false;
275
- break;
337
+ case "customer.updated": {
338
+ const customer = event.data.object;
339
+ const customerId = customer["id"];
340
+ if (!customerId) {
341
+ res.status(404).send(JSON.stringify({
342
+ "error": "The customer id is not found.",
343
+ }));
344
+ return;
345
+ }
346
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
347
+ if (col.empty) {
348
+ res.status(404).send(JSON.stringify({
349
+ "error": "The account data is not found.",
350
+ }));
351
+ return;
352
+ }
353
+ const user = col.docs[0];
354
+ const userId = user.id;
355
+ yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
356
+ res.status(200).send(JSON.stringify({
357
+ "success": true,
358
+ }));
359
+ return;
276
360
  }
277
- case "requires_action": {
278
- update["confirm"] = true;
279
- update["verify"] = false;
280
- update["capture"] = false;
281
- update["success"] = false;
361
+ case "checkout.session.completed": {
362
+ const session = event.data.object;
363
+ const customerId = session["customer"];
364
+ if (!customerId) {
365
+ res.status(404).send(JSON.stringify({
366
+ "error": "The customer id is not found.",
367
+ }));
368
+ return;
369
+ }
370
+ const setupIntent = session["setup_intent"];
371
+ if (!setupIntent) {
372
+ res.status(404).send(JSON.stringify({
373
+ "error": "The setup intent is not found.",
374
+ }));
375
+ return;
376
+ }
377
+ const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
378
+ if (col.empty) {
379
+ res.status(404).send(JSON.stringify({
380
+ "error": "The account data is not found.",
381
+ }));
382
+ return;
383
+ }
384
+ const user = col.docs[0];
385
+ const userId = user.id;
386
+ const update = {};
387
+ if (session["setup_intent"]) {
388
+ update["setupIntent"] = session["setup_intent"];
389
+ }
390
+ yield user.ref.set(update, {
391
+ merge: true,
392
+ });
393
+ yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
394
+ res.status(200).send(JSON.stringify({
395
+ "success": true,
396
+ }));
397
+ return;
398
+ }
399
+ case "customer.subscription.trial_will_end":
400
+ case "customer.subscription.updated":
401
+ case "customer.subscription.created": {
402
+ const now = new Date();
403
+ const update = {};
404
+ const subscription = event.data.object;
405
+ const status = subscription["status"];
406
+ if (status != "active") {
407
+ res.status(200).send(JSON.stringify({
408
+ "success": "Subscription is not active.",
409
+ }));
410
+ return;
411
+ }
412
+ const endDate = new Date(subscription["current_period_end"] * 1000);
413
+ const id = subscription["id"];
414
+ const userId = subscription.metadata.userId;
415
+ const orderId = (_f = subscription.metadata.orderId) !== null && _f !== void 0 ? _f : id;
416
+ const targetPath = `${stripePurchasePath}/${orderId}`;
417
+ const plan = subscription.plan;
418
+ let doc;
419
+ const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get();
420
+ if (subscriptionCol.empty) {
421
+ doc = yield firestoreInstance.doc(targetPath);
422
+ }
423
+ else {
424
+ doc = subscriptionCol.docs[0].ref;
425
+ }
426
+ update["expired"] = now >= endDate;
427
+ if (userId) {
428
+ update["user"] = userId;
429
+ }
430
+ update["@uid"] = orderId;
431
+ update["@time"] = new Date();
432
+ update["subscription"] = id;
433
+ update["application"] = subscription["application"];
434
+ update["application_fee_percent"] = subscription["application_fee_percent"];
435
+ update["cancel_at"] = subscription["cancel_at"];
436
+ update["cancel_at_period_end"] = subscription["cancel_at_period_end"];
437
+ update["canceled_at"] = subscription["canceled_at"];
438
+ update["collection_method"] = subscription["collection_method"];
439
+ update["currency"] = subscription["currency"];
440
+ update["current_period_start"] = subscription["current_period_start"];
441
+ update["current_period_end"] = subscription["current_period_end"];
442
+ update["customer"] = subscription["customer"];
443
+ update["default_payment_method"] = subscription["default_payment_method"];
444
+ update["ended_at"] = subscription["ended_at"];
445
+ update["latest_invoice"] = subscription["latest_invoice"];
446
+ update["price_id"] = plan["id"];
447
+ update["active"] = plan["active"];
448
+ update["amount"] = plan["amount"];
449
+ update["billing_scheme"] = plan["billing_scheme"];
450
+ update["interval"] = plan["interval"];
451
+ update["interval_count"] = plan["interval_count"];
452
+ update["usage_type"] = plan["usage_type"];
453
+ update["quantity"] = subscription["quantity"];
454
+ update["start_date"] = subscription["start_date"];
455
+ update["start_date"] = subscription["start_date"];
456
+ console.log(`Subscription status is ${status} ${stripePurchasePath}.`);
457
+ yield doc.set(update, {
458
+ merge: true,
459
+ });
460
+ res.status(200).send(JSON.stringify({
461
+ "success": "Subscription is active.",
462
+ }));
282
463
  break;
283
464
  }
284
- case "requires_capture": {
285
- update["confirm"] = true;
286
- update["verify"] = true;
287
- update["capture"] = false;
288
- update["success"] = false;
465
+ case "customer.subscription.deleted": {
466
+ const subscription = event.data.object;
467
+ const status = subscription["status"];
468
+ console.log(`Subscription status is ${status} ${"expired"}.`);
469
+ // Then define and call a method to handle the subscription deleted.
470
+ // handleSubscriptionDeleted(subscriptionDeleted);
289
471
  break;
290
472
  }
473
+ default: {
474
+ res.status(404).send(JSON.stringify({
475
+ "error": "Event is not found.",
476
+ }));
477
+ return;
478
+ }
291
479
  }
292
- update["updatedTime"] = new Date();
293
- update["error"] = true;
294
- update["errorMessage"] = errorMessage;
295
- yield purchase.ref.set(update, {
296
- merge: true,
297
- });
298
- res.status(200).send(JSON.stringify({
299
- "success": true,
300
- }));
301
- return;
302
- }
303
- case "payment_method.detached":
304
- case "payment_method.updated": {
305
- const payment = event.data.object;
306
- const previous = event.data.previous_attributes;
307
- let customerId = payment["customer"];
308
- if (!customerId) {
309
- customerId = previous["customer"];
310
- }
311
- if (!customerId) {
312
- res.status(404).send(JSON.stringify({
313
- "error": "The customer id is not found.",
314
- }));
315
- return;
316
- }
317
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
318
- if (col.empty) {
319
- res.status(404).send(JSON.stringify({
320
- "error": "The account data is not found.",
321
- }));
322
- return;
323
- }
324
- const user = col.docs[0];
325
- const userId = user.id;
326
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
327
- res.status(200).send(JSON.stringify({
328
- "success": true,
329
- }));
330
- return;
331
- }
332
- case "customer.updated": {
333
- const customer = event.data.object;
334
- const customerId = customer["id"];
335
- if (!customerId) {
336
- res.status(404).send(JSON.stringify({
337
- "error": "The customer id is not found.",
338
- }));
339
- return;
340
- }
341
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
342
- if (col.empty) {
343
- res.status(404).send(JSON.stringify({
344
- "error": "The account data is not found.",
345
- }));
346
- return;
347
- }
348
- const user = col.docs[0];
349
- const userId = user.id;
350
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
351
- res.status(200).send(JSON.stringify({
352
- "success": true,
353
- }));
354
- return;
355
- }
356
- case "checkout.session.completed": {
357
- const session = event.data.object;
358
- const customerId = session["customer"];
359
- if (!customerId) {
360
- res.status(404).send(JSON.stringify({
361
- "error": "The customer id is not found.",
362
- }));
363
- return;
364
- }
365
- const setupIntent = session["setup_intent"];
366
- if (!setupIntent) {
367
- res.status(404).send(JSON.stringify({
368
- "error": "The setup intent is not found.",
369
- }));
370
- return;
371
- }
372
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
373
- if (col.empty) {
374
- res.status(404).send(JSON.stringify({
375
- "error": "The account data is not found.",
376
- }));
377
- return;
378
- }
379
- const user = col.docs[0];
380
- const userId = user.id;
381
- const update = {};
382
- if (session["setup_intent"]) {
383
- update["setupIntent"] = session["setup_intent"];
384
- }
385
- yield user.ref.set(update, {
386
- merge: true,
387
- });
388
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
389
- res.status(200).send(JSON.stringify({
390
- "success": true,
391
- }));
392
- return;
393
- }
394
- case "customer.subscription.trial_will_end":
395
- case "customer.subscription.updated":
396
- case "customer.subscription.created": {
397
- const now = new Date();
398
- const update = {};
399
- const subscription = event.data.object;
400
- const status = subscription["status"];
401
- if (status != "active") {
402
- res.status(200).send(JSON.stringify({
403
- "success": "Subscription is not active.",
404
- }));
405
- return;
406
- }
407
- const endDate = new Date(subscription["current_period_end"] * 1000);
408
- const id = subscription["id"];
409
- const userId = subscription.metadata.userId;
410
- const orderId = (_e = subscription.metadata.orderId) !== null && _e !== void 0 ? _e : id;
411
- const targetPath = `${stripePurchasePath}/${orderId}`;
412
- const plan = subscription.plan;
413
- let doc;
414
- const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get();
415
- if (subscriptionCol.empty) {
416
- doc = yield firestoreInstance.doc(targetPath);
417
- }
418
- else {
419
- doc = subscriptionCol.docs[0].ref;
420
- }
421
- update["expired"] = now >= endDate;
422
- if (userId) {
423
- update["user"] = userId;
424
- }
425
- update["@uid"] = orderId;
426
- update["@time"] = new Date();
427
- update["subscription"] = id;
428
- update["application"] = subscription["application"];
429
- update["application_fee_percent"] = subscription["application_fee_percent"];
430
- update["cancel_at"] = subscription["cancel_at"];
431
- update["cancel_at_period_end"] = subscription["cancel_at_period_end"];
432
- update["canceled_at"] = subscription["canceled_at"];
433
- update["collection_method"] = subscription["collection_method"];
434
- update["currency"] = subscription["currency"];
435
- update["current_period_start"] = subscription["current_period_start"];
436
- update["current_period_end"] = subscription["current_period_end"];
437
- update["customer"] = subscription["customer"];
438
- update["default_payment_method"] = subscription["default_payment_method"];
439
- update["ended_at"] = subscription["ended_at"];
440
- update["latest_invoice"] = subscription["latest_invoice"];
441
- update["price_id"] = plan["id"];
442
- update["active"] = plan["active"];
443
- update["amount"] = plan["amount"];
444
- update["billing_scheme"] = plan["billing_scheme"];
445
- update["interval"] = plan["interval"];
446
- update["interval_count"] = plan["interval_count"];
447
- update["usage_type"] = plan["usage_type"];
448
- update["quantity"] = subscription["quantity"];
449
- update["start_date"] = subscription["start_date"];
450
- update["start_date"] = subscription["start_date"];
451
- console.log(`Subscription status is ${status} ${stripePurchasePath}.`);
452
- yield doc.set(update, {
453
- merge: true,
454
- });
455
- res.status(200).send(JSON.stringify({
456
- "success": "Subscription is active.",
457
- }));
458
- break;
459
480
  }
460
- case "customer.subscription.deleted": {
461
- const subscription = event.data.object;
462
- const status = subscription["status"];
463
- console.log(`Subscription status is ${status} ${"expired"}.`);
464
- // Then define and call a method to handle the subscription deleted.
465
- // handleSubscriptionDeleted(subscriptionDeleted);
466
- break;
467
- }
468
- default: {
469
- res.status(404).send(JSON.stringify({
470
- "error": "Event is not found.",
471
- }));
472
- return;
481
+ catch (err) {
482
+ error = err;
473
483
  }
474
484
  }
485
+ if (error) {
486
+ console.error(error);
487
+ throw new functions.https.HttpsError("unknown", "Unknown error.");
488
+ }
475
489
  }
476
490
  catch (err) {
477
491
  console.error(err);