@mathrunet/masamune 1.10.3 → 2.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 (79) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +19 -22
  3. package/dist/functions/agora_cloud_recording.js +48 -38
  4. package/dist/functions/agora_cloud_recording.js.map +1 -1
  5. package/dist/functions/agora_token.js +40 -30
  6. package/dist/functions/agora_token.js.map +1 -1
  7. package/dist/functions/android_auth_code.js +25 -15
  8. package/dist/functions/android_auth_code.js.map +1 -1
  9. package/dist/functions/android_token.js +46 -34
  10. package/dist/functions/android_token.js.map +1 -1
  11. package/dist/functions/consumable_verify_android.js +45 -35
  12. package/dist/functions/consumable_verify_android.js.map +1 -1
  13. package/dist/functions/consumable_verify_ios.js +39 -29
  14. package/dist/functions/consumable_verify_ios.js.map +1 -1
  15. package/dist/functions/geocoding.js +29 -19
  16. package/dist/functions/geocoding.js.map +1 -1
  17. package/dist/functions/gmail.js +36 -26
  18. package/dist/functions/gmail.js.map +1 -1
  19. package/dist/functions/nonconsumable_verify_android.js +44 -34
  20. package/dist/functions/nonconsumable_verify_android.js.map +1 -1
  21. package/dist/functions/nonconsumable_verify_ios.js +38 -28
  22. package/dist/functions/nonconsumable_verify_ios.js.map +1 -1
  23. package/dist/functions/openai_chat_gpt.js +39 -29
  24. package/dist/functions/openai_chat_gpt.js.map +1 -1
  25. package/dist/functions/purchase_webhook_android.js +26 -18
  26. package/dist/functions/purchase_webhook_android.js.map +1 -1
  27. package/dist/functions/purchase_webhook_ios.js +92 -78
  28. package/dist/functions/purchase_webhook_ios.js.map +1 -1
  29. package/dist/functions/scheduler.js +15 -9
  30. package/dist/functions/scheduler.js.map +1 -1
  31. package/dist/functions/send_grid.js +35 -25
  32. package/dist/functions/send_grid.js.map +1 -1
  33. package/dist/functions/send_notification.js +37 -24
  34. package/dist/functions/send_notification.js.map +1 -1
  35. package/dist/functions/stripe.js +866 -858
  36. package/dist/functions/stripe.js.map +1 -1
  37. package/dist/functions/stripe_webhook.js +370 -363
  38. package/dist/functions/stripe_webhook.js.map +1 -1
  39. package/dist/functions/stripe_webhook_connect.js +72 -63
  40. package/dist/functions/stripe_webhook_connect.js.map +1 -1
  41. package/dist/functions/stripe_webhook_secure.js +72 -63
  42. package/dist/functions/stripe_webhook_secure.js.map +1 -1
  43. package/dist/functions/subscription_verify_android.js +61 -52
  44. package/dist/functions/subscription_verify_android.js.map +1 -1
  45. package/dist/functions/subscription_verify_ios.js +59 -50
  46. package/dist/functions/subscription_verify_ios.js.map +1 -1
  47. package/dist/functions.d.ts +22 -68
  48. package/dist/functions.js +22 -22
  49. package/dist/functions.js.map +1 -1
  50. package/dist/index.js +1 -1
  51. package/dist/index.js.map +1 -1
  52. package/dist/lib/call_process_function_base.d.ts +2 -15
  53. package/dist/lib/call_process_function_base.js +13 -13
  54. package/dist/lib/call_process_function_base.js.map +1 -1
  55. package/dist/lib/functions_base.d.ts +97 -9
  56. package/dist/lib/functions_base.js +10 -7
  57. package/dist/lib/functions_base.js.map +1 -1
  58. package/dist/lib/functions_data.d.ts +2 -36
  59. package/dist/lib/functions_data.js +2 -29
  60. package/dist/lib/functions_data.js.map +1 -1
  61. package/dist/lib/gmail.d.ts +2 -2
  62. package/dist/lib/gmail.js +4 -29
  63. package/dist/lib/gmail.js.map +1 -1
  64. package/dist/lib/request_process_function_base.d.ts +4 -16
  65. package/dist/lib/request_process_function_base.js +13 -13
  66. package/dist/lib/request_process_function_base.js.map +1 -1
  67. package/dist/lib/schedule_process_function_base.d.ts +13 -11
  68. package/dist/lib/schedule_process_function_base.js +15 -9
  69. package/dist/lib/schedule_process_function_base.js.map +1 -1
  70. package/dist/lib/send_grid.d.ts +1 -1
  71. package/dist/lib/send_grid.js +3 -27
  72. package/dist/lib/send_grid.js.map +1 -1
  73. package/dist/lib/send_notification.js +1 -1
  74. package/dist/lib/send_notification.js.map +1 -1
  75. package/dist/lib/verify_android.js +1 -1
  76. package/dist/lib/verify_android.js.map +1 -1
  77. package/dist/lib/verify_ios.js +1 -1
  78. package/dist/lib/verify_ios.js.map +1 -1
  79. package/package.json +2 -2
@@ -32,7 +32,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
32
32
  });
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- const functions = __importStar(require("firebase-functions"));
35
+ const functions = __importStar(require("firebase-functions/v2"));
36
36
  const stripe = __importStar(require("stripe"));
37
37
  const admin = __importStar(require("firebase-admin"));
38
38
  /**
@@ -44,7 +44,7 @@ const admin = __importStar(require("firebase-admin"));
44
44
  * こちらをデプロイした際のURLをStripeのWebhook設定に登録してください。
45
45
  * Firestoreとの連携が必須です。Firestoreも利用可能にしてください。
46
46
  *
47
- * @param {string} purchase.stripe.secret_key
47
+ * @param {string} process.env.PURCHASE_STRIPE_SECRETKEY
48
48
  * API key (secret key) to connect to Stripe.
49
49
  * Log in to the following URL and create a project.
50
50
  * After the project is created, the secret key can be copied.
@@ -58,408 +58,416 @@ const admin = __importStar(require("firebase-admin"));
58
58
  * Development enveironment
59
59
  * https://dashboard.stripe.com/test/apikeys
60
60
  *
61
- * @param {string} purchase.stripe.user_path
61
+ * @param {string} process.env.PURCHASE_STRIPE_USERPATH
62
62
  * Stripe user (customer) pass.
63
63
  * Stripeのユーザー(カスタマー)用パス。
64
64
  *
65
- * @param {string} purchase.stripe.payment_path
66
- * Path for payment method information to be placed under [purchase.stripe.user_path].
67
- * [purchase.stripe.user_path]の下に配置する支払い方法の情報用パス。
65
+ * @param {string} process.env.PURCHASE_STRIPE_PAYMENTPATH
66
+ * Path for payment method information to be placed under [process.env.PURCHASE_STRIPE_USERPATH].
67
+ * [process.env.PURCHASE_STRIPE_USERPATH]の下に配置する支払い方法の情報用パス。
68
68
  *
69
- * @param {string} purchase.stripe.purchase_path
70
- * Path for purchase information to be placed under [purchase.stripe.user_path].
71
- * [purchase.stripe.user_path]の下に配置する購入情報用パス。
69
+ * @param {string} process.env.PURCHASE_STRIPE_PURCHASEPATH
70
+ * Path for purchase information to be placed under [process.env.PURCHASE_STRIPE_USERPATH].
71
+ * [process.env.PURCHASE_STRIPE_USERPATH]の下に配置する購入情報用パス。
72
72
  *
73
- * @param {string} purchase.stripe.webhook_secret
73
+ * @param {string} process.env.PURCHASE_STRIPE_WEBHOOKSECRET
74
74
  * Specify the **Signature Secret** after setting it up as a webhook.
75
75
  * Webhookとして設定したあとの**署名シークレット**を指定します。
76
76
  *
77
77
  */
78
- module.exports = (regions, timeoutSeconds, data) => functions.runWith({ timeoutSeconds: timeoutSeconds }).region(...regions).https.onRequest((req, res) => __awaiter(void 0, void 0, void 0, function* () {
78
+ module.exports = (regions, options, data) => {
79
79
  var _a;
80
- try {
81
- const config = functions.config().purchase;
82
- const stripeConfig = config.stripe;
83
- const apiKey = stripeConfig.secret_key;
84
- const stripeUserPath = stripeConfig.user_path;
85
- const stripePurchasePath = stripeConfig.purchase_path;
86
- const stripeWebhookSecret = stripeConfig.webhook_secret;
87
- const firestoreInstance = admin.firestore();
88
- const stripeClient = new stripe.Stripe(apiKey, {
89
- apiVersion: "2022-11-15",
90
- });
91
- const signature = req.headers["stripe-signature"];
92
- if (!signature) {
93
- res.status(403).send(JSON.stringify({
94
- "error": "Access denied.",
95
- }));
96
- return;
97
- }
98
- const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret);
99
- switch (event.type) {
100
- case "payment_intent.requires_action":
101
- case "payment_intent.amount_capturable_updated": {
102
- const payment = event.data.object;
103
- const purchaseId = payment["id"];
104
- const customerId = payment["customer"];
105
- const status = payment["status"];
106
- if (!customerId) {
107
- res.status(404).send(JSON.stringify({
108
- "error": "The customer id is not found.",
109
- }));
110
- return;
111
- }
112
- if (!status) {
113
- res.status(404).send(JSON.stringify({
114
- "error": "The status is not found.",
115
- }));
116
- return;
117
- }
118
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
119
- if (userCol.empty) {
120
- res.status(404).send(JSON.stringify({
121
- "error": "The account data is not found.",
122
- }));
123
- return;
124
- }
125
- const user = userCol.docs[0];
126
- const userId = user.id;
127
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
128
- if (purchaseCol.empty) {
129
- res.status(404).send(JSON.stringify({
130
- "error": "The purchase data is not found.",
131
- }));
132
- return;
133
- }
134
- const purchase = purchaseCol.docs[0];
135
- const update = {};
136
- switch (status) {
137
- case "requires_payment_method":
138
- case "requires_confirmation": {
139
- update["confirm"] = false;
140
- update["verify"] = false;
141
- update["capture"] = false;
142
- update["success"] = false;
143
- break;
144
- }
145
- case "requires_action": {
146
- update["confirm"] = true;
147
- update["verify"] = false;
148
- update["capture"] = false;
149
- update["success"] = false;
150
- break;
151
- }
152
- case "requires_capture": {
153
- update["confirm"] = true;
154
- update["verify"] = true;
155
- update["capture"] = false;
156
- update["success"] = false;
157
- break;
158
- }
159
- }
160
- update["updatedTime"] = new Date();
161
- yield purchase.ref.set(update, {
162
- merge: true,
163
- });
164
- res.status(200).send(JSON.stringify({
165
- "success": true,
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.",
166
102
  }));
167
103
  return;
168
104
  }
169
- case "payment_intent.succeeded": {
170
- const payment = event.data.object;
171
- const purchaseId = payment["id"];
172
- const customerId = payment["customer"];
173
- if (!customerId) {
174
- res.status(404).send(JSON.stringify({
175
- "error": "The customer id is not found.",
176
- }));
177
- return;
178
- }
179
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
180
- if (userCol.empty) {
181
- res.status(404).send(JSON.stringify({
182
- "error": "The account data is not found.",
183
- }));
184
- return;
185
- }
186
- const user = userCol.docs[0];
187
- const userId = user.id;
188
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
189
- if (purchaseCol.empty) {
190
- res.status(404).send(JSON.stringify({
191
- "error": "The purchase data is not found.",
192
- }));
193
- return;
194
- }
195
- const purchase = purchaseCol.docs[0];
196
- const update = {};
197
- update["confirm"] = true;
198
- update["verify"] = true;
199
- update["capture"] = true;
200
- update["success"] = true;
201
- update["error"] = admin.firestore.FieldValue.delete();
202
- update["errorMessage"] = admin.firestore.FieldValue.delete();
203
- update["updatedTime"] = new Date();
204
- if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) {
205
- if (payment["charges"]["data"][0]["receipt_url"]) {
206
- 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;
207
118
  }
208
- if (payment["charges"]["data"][0]["amount_captured"]) {
209
- update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"];
119
+ if (!status) {
120
+ res.status(404).send(JSON.stringify({
121
+ "error": "The status is not found.",
122
+ }));
123
+ return;
210
124
  }
211
- }
212
- yield purchase.ref.set(update, {
213
- merge: true,
214
- });
215
- res.status(200).send(JSON.stringify({
216
- "success": true,
217
- }));
218
- return;
219
- }
220
- case "payment_intent.payment_failed": {
221
- const payment = event.data.object;
222
- const purchaseId = payment["id"];
223
- const customerId = payment["customer"];
224
- const status = payment["status"];
225
- if (!customerId) {
226
- res.status(404).send(JSON.stringify({
227
- "error": "The customer id is not found.",
228
- }));
229
- return;
230
- }
231
- const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
232
- if (userCol.empty) {
233
- res.status(404).send(JSON.stringify({
234
- "error": "The account data is not found.",
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,
235
173
  }));
236
174
  return;
237
175
  }
238
- const user = userCol.docs[0];
239
- const userId = user.id;
240
- const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get();
241
- if (purchaseCol.empty) {
242
- res.status(404).send(JSON.stringify({
243
- "error": "The purchase data is not found.",
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,
244
224
  }));
245
225
  return;
246
226
  }
247
- const errorMessage = payment["last_payment_error"]["message"];
248
- const purchase = purchaseCol.docs[0];
249
- const update = {};
250
- switch (status) {
251
- case "requires_payment_method":
252
- case "requires_confirmation": {
253
- update["confirm"] = false;
254
- update["verify"] = false;
255
- update["capture"] = false;
256
- update["success"] = false;
257
- 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;
258
237
  }
259
- case "requires_action": {
260
- update["confirm"] = true;
261
- update["verify"] = false;
262
- update["capture"] = false;
263
- update["success"] = false;
264
- break;
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;
265
244
  }
266
- case "requires_capture": {
267
- update["confirm"] = true;
268
- update["verify"] = true;
269
- update["capture"] = false;
270
- update["success"] = false;
271
- break;
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;
272
253
  }
273
- }
274
- update["updatedTime"] = new Date();
275
- update["error"] = true;
276
- update["errorMessage"] = errorMessage;
277
- yield purchase.ref.set(update, {
278
- merge: true,
279
- });
280
- res.status(200).send(JSON.stringify({
281
- "success": true,
282
- }));
283
- return;
284
- }
285
- case "payment_method.detached":
286
- case "payment_method.updated": {
287
- const payment = event.data.object;
288
- const previous = event.data.previous_attributes;
289
- let customerId = payment["customer"];
290
- if (!customerId) {
291
- customerId = previous["customer"];
292
- }
293
- if (!customerId) {
294
- res.status(404).send(JSON.stringify({
295
- "error": "The customer id is not found.",
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,
296
289
  }));
297
290
  return;
298
291
  }
299
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
300
- if (col.empty) {
301
- res.status(404).send(JSON.stringify({
302
- "error": "The account data is not found.",
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,
303
318
  }));
304
319
  return;
305
320
  }
306
- const user = col.docs[0];
307
- const userId = user.id;
308
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
309
- res.status(200).send(JSON.stringify({
310
- "success": true,
311
- }));
312
- return;
313
- }
314
- case "customer.updated": {
315
- const customer = event.data.object;
316
- const customerId = customer["id"];
317
- if (!customerId) {
318
- res.status(404).send(JSON.stringify({
319
- "error": "The customer id is not found.",
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,
320
342
  }));
321
343
  return;
322
344
  }
323
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
324
- if (col.empty) {
325
- res.status(404).send(JSON.stringify({
326
- "error": "The account data is not found.",
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,
327
380
  }));
328
381
  return;
329
382
  }
330
- const user = col.docs[0];
331
- const userId = user.id;
332
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
333
- res.status(200).send(JSON.stringify({
334
- "success": true,
335
- }));
336
- return;
337
- }
338
- case "checkout.session.completed": {
339
- const session = event.data.object;
340
- const customerId = session["customer"];
341
- if (!customerId) {
342
- res.status(404).send(JSON.stringify({
343
- "error": "The customer id is not found.",
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.",
344
446
  }));
345
- return;
447
+ break;
346
448
  }
347
- const setupIntent = session["setup_intent"];
348
- if (!setupIntent) {
349
- res.status(404).send(JSON.stringify({
350
- "error": "The setup intent is not found.",
351
- }));
352
- return;
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;
353
456
  }
354
- const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get();
355
- if (col.empty) {
457
+ default: {
356
458
  res.status(404).send(JSON.stringify({
357
- "error": "The account data is not found.",
459
+ "error": "Event is not found.",
358
460
  }));
359
461
  return;
360
462
  }
361
- const user = col.docs[0];
362
- const userId = user.id;
363
- const update = {};
364
- if (session["setup_intent"]) {
365
- update["setupIntent"] = session["setup_intent"];
366
- }
367
- yield user.ref.set(update, {
368
- merge: true,
369
- });
370
- yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId);
371
- res.status(200).send(JSON.stringify({
372
- "success": true,
373
- }));
374
- return;
375
- }
376
- case "customer.subscription.trial_will_end":
377
- case "customer.subscription.updated":
378
- case "customer.subscription.created": {
379
- const now = new Date();
380
- const update = {};
381
- const subscription = event.data.object;
382
- const status = subscription["status"];
383
- if (status != "active") {
384
- res.status(200).send(JSON.stringify({
385
- "success": "Subscription is not active.",
386
- }));
387
- return;
388
- }
389
- const endDate = new Date(subscription["current_period_end"] * 1000);
390
- const id = subscription["id"];
391
- const userId = subscription.metadata.userId;
392
- const orderId = (_a = subscription.metadata.orderId) !== null && _a !== void 0 ? _a : id;
393
- const targetPath = `${stripePurchasePath}/${orderId}`;
394
- const plan = subscription.plan;
395
- let doc;
396
- const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get();
397
- if (subscriptionCol.empty) {
398
- doc = yield firestoreInstance.doc(targetPath);
399
- }
400
- else {
401
- doc = subscriptionCol.docs[0].ref;
402
- }
403
- update["expired"] = now >= endDate;
404
- if (userId) {
405
- update["user"] = userId;
406
- }
407
- update["@uid"] = orderId;
408
- update["@time"] = new Date();
409
- update["subscription"] = id;
410
- update["application"] = subscription["application"];
411
- update["application_fee_percent"] = subscription["application_fee_percent"];
412
- update["cancel_at"] = subscription["cancel_at"];
413
- update["cancel_at_period_end"] = subscription["cancel_at_period_end"];
414
- update["canceled_at"] = subscription["canceled_at"];
415
- update["collection_method"] = subscription["collection_method"];
416
- update["currency"] = subscription["currency"];
417
- update["current_period_start"] = subscription["current_period_start"];
418
- update["current_period_end"] = subscription["current_period_end"];
419
- update["customer"] = subscription["customer"];
420
- update["default_payment_method"] = subscription["default_payment_method"];
421
- update["ended_at"] = subscription["ended_at"];
422
- update["latest_invoice"] = subscription["latest_invoice"];
423
- update["price_id"] = plan["id"];
424
- update["active"] = plan["active"];
425
- update["amount"] = plan["amount"];
426
- update["billing_scheme"] = plan["billing_scheme"];
427
- update["interval"] = plan["interval"];
428
- update["interval_count"] = plan["interval_count"];
429
- update["usage_type"] = plan["usage_type"];
430
- update["quantity"] = subscription["quantity"];
431
- update["start_date"] = subscription["start_date"];
432
- update["start_date"] = subscription["start_date"];
433
- console.log(`Subscription status is ${status} ${stripePurchasePath}.`);
434
- yield doc.set(update, {
435
- merge: true,
436
- });
437
- res.status(200).send(JSON.stringify({
438
- "success": "Subscription is active.",
439
- }));
440
- break;
441
- }
442
- case "customer.subscription.deleted": {
443
- const subscription = event.data.object;
444
- const status = subscription["status"];
445
- console.log(`Subscription status is ${status} ${"expired"}.`);
446
- // Then define and call a method to handle the subscription deleted.
447
- // handleSubscriptionDeleted(subscriptionDeleted);
448
- break;
449
- }
450
- default: {
451
- res.status(404).send(JSON.stringify({
452
- "error": "Event is not found.",
453
- }));
454
- return;
455
463
  }
456
464
  }
457
- }
458
- catch (err) {
459
- console.error(err);
460
- throw err;
461
- }
462
- }));
465
+ catch (err) {
466
+ console.error(err);
467
+ throw err;
468
+ }
469
+ }));
470
+ };
463
471
  /**
464
472
  * Synchronize Stripe acquisition results to Firestore.
465
473
  *
@@ -485,11 +493,10 @@ module.exports = (regions, timeoutSeconds, data) => functions.runWith({ timeoutS
485
493
  *
486
494
  */
487
495
  function syncStripePayment(stripeClient, firestoreInstance, userId, customerId) {
496
+ var _a, _b;
488
497
  return __awaiter(this, void 0, void 0, function* () {
489
- const config = functions.config().purchase;
490
- const stripeConfig = config.stripe;
491
- const stripeUserPath = stripeConfig.user_path;
492
- const stripePaymentPath = stripeConfig.payment_path;
498
+ const stripeUserPath = (_a = process.env.PURCHASE_STRIPE_USERPATH) !== null && _a !== void 0 ? _a : "plugins/stripe/user";
499
+ const stripePaymentPath = (_b = process.env.PURCHASE_STRIPE_PAYMENTPATH) !== null && _b !== void 0 ? _b : "payment";
493
500
  const customer = yield stripeClient.customers.retrieve(customerId);
494
501
  let defaultSource = customer.invoice_settings.default_payment_method;
495
502
  const paymentMethods = yield stripeClient.customers.listPaymentMethods(customerId, {