@mathrunet/masamune 2.15.0 → 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.
- package/CHANGELOG.md +9 -0
- package/dist/functions/consumable_verify_android.js +23 -7
- package/dist/functions/consumable_verify_android.js.map +1 -1
- package/dist/functions/consumable_verify_ios.js +23 -7
- package/dist/functions/consumable_verify_ios.js.map +1 -1
- package/dist/functions/delete_documents.js +6 -1
- package/dist/functions/delete_documents.js.map +1 -1
- package/dist/functions/nonconsumable_verify_android.js +22 -6
- package/dist/functions/nonconsumable_verify_android.js.map +1 -1
- package/dist/functions/nonconsumable_verify_ios.js +22 -6
- package/dist/functions/nonconsumable_verify_ios.js.map +1 -1
- package/dist/functions/purchase_webhook_android.js +143 -129
- package/dist/functions/purchase_webhook_android.js.map +1 -1
- package/dist/functions/purchase_webhook_ios.js +81 -67
- package/dist/functions/purchase_webhook_ios.js.map +1 -1
- package/dist/functions/scheduler.js +61 -48
- package/dist/functions/scheduler.js.map +1 -1
- package/dist/functions/send_notification.js +42 -16
- package/dist/functions/send_notification.js.map +1 -1
- package/dist/functions/stripe.js +935 -921
- package/dist/functions/stripe.js.map +1 -1
- package/dist/functions/stripe_webhook.js +371 -357
- package/dist/functions/stripe_webhook.js.map +1 -1
- package/dist/functions/stripe_webhook_connect.js +64 -51
- package/dist/functions/stripe_webhook_connect.js.map +1 -1
- package/dist/functions/stripe_webhook_secure.js +63 -50
- package/dist/functions/stripe_webhook_secure.js.map +1 -1
- package/dist/functions/subscription_verify_android.js +31 -15
- package/dist/functions/subscription_verify_android.js.map +1 -1
- package/dist/functions/subscription_verify_ios.js +31 -15
- package/dist/functions/subscription_verify_ios.js.map +1 -1
- package/dist/functions/test.js +8 -4
- package/dist/functions/test.js.map +1 -1
- package/dist/lib/functions/delete_documents.d.ts +2 -1
- package/dist/lib/functions/delete_documents.js +1 -3
- package/dist/lib/functions/delete_documents.js.map +1 -1
- package/dist/lib/functions/send_notification.d.ts +2 -1
- package/dist/lib/functions/send_notification.js +3 -3
- package/dist/lib/functions/send_notification.js.map +1 -1
- package/dist/lib/functions/update_subscription.d.ts +2 -1
- package/dist/lib/functions/update_subscription.js +1 -3
- package/dist/lib/functions/update_subscription.js.map +1 -1
- package/dist/lib/functions/update_unlock.d.ts +2 -1
- package/dist/lib/functions/update_unlock.js +1 -3
- package/dist/lib/functions/update_unlock.js.map +1 -1
- package/dist/lib/functions/update_wallet.d.ts +2 -1
- package/dist/lib/functions/update_wallet.js +1 -2
- package/dist/lib/functions/update_wallet.js.map +1 -1
- package/dist/lib/schedulers/copy_document.d.ts +3 -3
- package/dist/lib/schedulers/copy_document.js +3 -3
- package/dist/lib/schedulers/copy_document.js.map +1 -1
- package/dist/lib/schedulers/delete_documents.d.ts +3 -3
- package/dist/lib/schedulers/delete_documents.js +3 -2
- package/dist/lib/schedulers/delete_documents.js.map +1 -1
- package/dist/lib/schedulers/notification.d.ts +3 -3
- package/dist/lib/schedulers/notification.js +3 -2
- package/dist/lib/schedulers/notification.js.map +1 -1
- package/dist/lib/src/firebase_loader.d.ts +30 -0
- package/dist/lib/src/firebase_loader.js +77 -0
- package/dist/lib/src/firebase_loader.js.map +1 -0
- package/dist/lib/src/functions_base.d.ts +12 -0
- 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
|
-
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
case "
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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 "
|
|
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"] =
|
|
174
|
-
update["success"] =
|
|
175
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
227
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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 "
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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 "
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
|
|
461
|
-
|
|
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);
|