@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.
- package/CHANGELOG.md +18 -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/exntensions/string.extension.d.ts +12 -0
- package/dist/lib/exntensions/string.extension.js +15 -0
- package/dist/lib/exntensions/string.extension.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
package/dist/functions/stripe.js
CHANGED
|
@@ -48,6 +48,7 @@ const admin = __importStar(require("firebase-admin"));
|
|
|
48
48
|
const sendgrid = __importStar(require("../lib/functions/send_grid"));
|
|
49
49
|
const gmail = __importStar(require("../lib/functions/gmail"));
|
|
50
50
|
require("../lib/exntensions/string.extension");
|
|
51
|
+
const firebase_loader_1 = require("../lib/src/firebase_loader");
|
|
51
52
|
/**
|
|
52
53
|
* Performs various Stripe processes.
|
|
53
54
|
* Firestore integration is a must; please make Firestore available as well.
|
|
@@ -229,996 +230,1009 @@ module.exports = (regions, options, data) => {
|
|
|
229
230
|
enforceAppCheck: (_c = options.enforceAppCheck) !== null && _c !== void 0 ? _c : undefined,
|
|
230
231
|
consumeAppCheckToken: (_d = options.consumeAppCheckToken) !== null && _d !== void 0 ? _d : undefined,
|
|
231
232
|
}, (query) => __awaiter(void 0, void 0, void 0, function* () {
|
|
232
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
|
|
233
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32;
|
|
233
234
|
try {
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
235
|
+
let error = null;
|
|
236
|
+
const firestoreDatabaseIds = (_a = options.firestoreDatabaseIds) !== null && _a !== void 0 ? _a : [""];
|
|
237
|
+
for (const databaseId of firestoreDatabaseIds) {
|
|
238
|
+
try {
|
|
239
|
+
const apiKey = (_b = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _b !== void 0 ? _b : "";
|
|
240
|
+
const stripeUserPath = (_c = process.env.PURCHASE_STRIPE_USERPATH) !== null && _c !== void 0 ? _c : "plugins/stripe/user";
|
|
241
|
+
const stripePurchasePath = (_d = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _d !== void 0 ? _d : "purchase";
|
|
242
|
+
const stripePaymentPath = (_e = process.env.PURCHASE_STRIPE_PAYMENTPATH) !== null && _e !== void 0 ? _e : "payment";
|
|
243
|
+
const stripeEmailProvider = (_f = process.env.PURCHASE_STRIPE_EMAILPROVIDER) !== null && _f !== void 0 ? _f : "gmail";
|
|
244
|
+
const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId);
|
|
245
|
+
const stripeClient = new stripe.Stripe(apiKey, {
|
|
246
|
+
apiVersion: "2025-01-27.acacia",
|
|
247
|
+
});
|
|
248
|
+
switch (query.data.mode) {
|
|
249
|
+
case "create_account": {
|
|
250
|
+
const userId = query.data.userId;
|
|
251
|
+
const locale = query.data.locale;
|
|
252
|
+
const refreshUrl = query.data.refreshUrl;
|
|
253
|
+
const returnUrl = query.data.returnUrl;
|
|
254
|
+
if (!locale) {
|
|
255
|
+
throw new functions.https.HttpsError("invalid-argument", "The locale is empty.");
|
|
256
|
+
}
|
|
257
|
+
if (!userId) {
|
|
258
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
259
|
+
}
|
|
260
|
+
const country = locale.split("_")[1];
|
|
261
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
262
|
+
const data = doc.data();
|
|
263
|
+
if (!data || !data["account"]) {
|
|
264
|
+
const account = yield stripeClient.accounts.create({
|
|
265
|
+
type: "express",
|
|
266
|
+
country: country !== null && country !== void 0 ? country : "JP",
|
|
267
|
+
});
|
|
268
|
+
const update = {};
|
|
269
|
+
update["@uid"] = userId;
|
|
270
|
+
update["@time"] = new Date();
|
|
271
|
+
update["user"] = userId;
|
|
272
|
+
update["account"] = account.id;
|
|
273
|
+
yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).set(update, {
|
|
274
|
+
merge: true,
|
|
275
|
+
});
|
|
276
|
+
const endpoint = yield stripeClient.accountLinks.create({
|
|
277
|
+
type: "account_onboarding",
|
|
278
|
+
account: account.id,
|
|
279
|
+
refresh_url: refreshUrl,
|
|
280
|
+
return_url: returnUrl,
|
|
281
|
+
});
|
|
282
|
+
return {
|
|
283
|
+
next: "registration",
|
|
284
|
+
endpoint: endpoint.url,
|
|
285
|
+
accountId: account.id,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
if (data["capability"] && data["capability"]["transfers"]) {
|
|
290
|
+
return {
|
|
291
|
+
next: "none",
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
const res = yield stripeClient.accounts.retrieve(data["account"]);
|
|
295
|
+
if (res["capabilities"] && res["capabilities"]["transfers"] === "active") {
|
|
296
|
+
const update = {};
|
|
297
|
+
update["capability"] = {
|
|
298
|
+
transfers: true,
|
|
299
|
+
};
|
|
300
|
+
yield doc.ref.set(update, {
|
|
301
|
+
merge: true,
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
next: "none",
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const endpoint = yield stripeClient.accountLinks.create({
|
|
309
|
+
type: "account_onboarding",
|
|
310
|
+
account: data["account"],
|
|
311
|
+
refresh_url: refreshUrl,
|
|
312
|
+
return_url: returnUrl,
|
|
313
|
+
});
|
|
314
|
+
return {
|
|
315
|
+
next: "registration",
|
|
316
|
+
endpoint: endpoint.url,
|
|
317
|
+
accountId: data["account"],
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
288
321
|
}
|
|
289
|
-
|
|
290
|
-
|
|
322
|
+
case "delete_account": {
|
|
323
|
+
const userId = query.data.userId;
|
|
324
|
+
if (!userId) {
|
|
325
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
326
|
+
}
|
|
327
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
328
|
+
const data = doc.data();
|
|
329
|
+
if (!data || !data["account"]) {
|
|
330
|
+
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
331
|
+
}
|
|
332
|
+
yield stripeClient.accounts.del(data["account"]);
|
|
291
333
|
const update = {};
|
|
292
|
-
update["
|
|
293
|
-
|
|
294
|
-
|
|
334
|
+
update["account"] = admin.firestore.FieldValue.delete();
|
|
335
|
+
update["capability"] = admin.firestore.FieldValue.arrayRemove({
|
|
336
|
+
"transfers": true
|
|
337
|
+
});
|
|
295
338
|
yield doc.ref.set(update, {
|
|
296
339
|
merge: true,
|
|
297
340
|
});
|
|
298
341
|
return {
|
|
299
|
-
|
|
342
|
+
success: true,
|
|
300
343
|
};
|
|
301
344
|
}
|
|
302
|
-
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
345
|
+
case "get_account": {
|
|
346
|
+
const userId = query.data.userId;
|
|
347
|
+
if (!userId) {
|
|
348
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
349
|
+
}
|
|
350
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
351
|
+
const data = doc.data();
|
|
352
|
+
if (!data || !data["account"]) {
|
|
353
|
+
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
354
|
+
}
|
|
355
|
+
const res = yield stripeClient.accounts.retrieve(data["account"]);
|
|
356
|
+
return res;
|
|
357
|
+
}
|
|
358
|
+
case "dashboard_account": {
|
|
359
|
+
const userId = query.data.userId;
|
|
360
|
+
if (!userId) {
|
|
361
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
362
|
+
}
|
|
363
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
364
|
+
const data = doc.data();
|
|
365
|
+
if (!data || !data["account"]) {
|
|
366
|
+
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
367
|
+
}
|
|
368
|
+
const res = yield stripeClient.accounts.createLoginLink(data["account"]);
|
|
309
369
|
return {
|
|
310
|
-
|
|
311
|
-
endpoint: endpoint.url,
|
|
312
|
-
accountId: data["account"],
|
|
370
|
+
endpoint: res["url"],
|
|
313
371
|
};
|
|
314
372
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
323
|
-
const data = doc.data();
|
|
324
|
-
if (!data || !data["account"]) {
|
|
325
|
-
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
326
|
-
}
|
|
327
|
-
yield stripeClient.accounts.del(data["account"]);
|
|
328
|
-
const update = {};
|
|
329
|
-
update["account"] = admin.firestore.FieldValue.delete();
|
|
330
|
-
update["capability"] = admin.firestore.FieldValue.arrayRemove({
|
|
331
|
-
"transfers": true
|
|
332
|
-
});
|
|
333
|
-
yield doc.ref.set(update, {
|
|
334
|
-
merge: true,
|
|
335
|
-
});
|
|
336
|
-
return {
|
|
337
|
-
success: true,
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
case "get_account": {
|
|
341
|
-
const userId = query.data.userId;
|
|
342
|
-
if (!userId) {
|
|
343
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
344
|
-
}
|
|
345
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
346
|
-
const data = doc.data();
|
|
347
|
-
if (!data || !data["account"]) {
|
|
348
|
-
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
349
|
-
}
|
|
350
|
-
const res = yield stripeClient.accounts.retrieve(data["account"]);
|
|
351
|
-
return res;
|
|
352
|
-
}
|
|
353
|
-
case "dashboard_account": {
|
|
354
|
-
const userId = query.data.userId;
|
|
355
|
-
if (!userId) {
|
|
356
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
357
|
-
}
|
|
358
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
359
|
-
const data = doc.data();
|
|
360
|
-
if (!data || !data["account"]) {
|
|
361
|
-
throw new functions.https.HttpsError("not-found", "Account id is not found.");
|
|
362
|
-
}
|
|
363
|
-
const res = yield stripeClient.accounts.createLoginLink(data["account"]);
|
|
364
|
-
return {
|
|
365
|
-
endpoint: res["url"],
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
case "create_customer_and_payment": {
|
|
369
|
-
const userId = query.data.userId;
|
|
370
|
-
const successUrl = query.data.successUrl;
|
|
371
|
-
const cancelUrl = query.data.cancelUrl;
|
|
372
|
-
const authInstance = admin.auth();
|
|
373
|
-
if (!userId) {
|
|
374
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
375
|
-
}
|
|
376
|
-
const user = yield authInstance.getUser(userId);
|
|
377
|
-
if (!user) {
|
|
378
|
-
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
379
|
-
}
|
|
380
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
381
|
-
const data = doc.data();
|
|
382
|
-
if (!data || !data["customer"]) {
|
|
383
|
-
const customer = yield stripeClient.customers.create({
|
|
384
|
-
metadata: {
|
|
385
|
-
"user_id": userId,
|
|
386
|
-
},
|
|
387
|
-
email: (_f = user.email) !== null && _f !== void 0 ? _f : (!data ? null : data["email"]),
|
|
388
|
-
});
|
|
389
|
-
const update = {};
|
|
390
|
-
update["@uid"] = userId;
|
|
391
|
-
update["@time"] = new Date();
|
|
392
|
-
update["user"] = userId;
|
|
393
|
-
update["customer"] = customer.id;
|
|
394
|
-
yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).set(update, {
|
|
395
|
-
merge: true,
|
|
396
|
-
});
|
|
397
|
-
const session = yield stripeClient.checkout.sessions.create({
|
|
398
|
-
payment_method_types: ["card"],
|
|
399
|
-
mode: "setup",
|
|
400
|
-
customer: customer.id,
|
|
401
|
-
success_url: successUrl,
|
|
402
|
-
cancel_url: cancelUrl,
|
|
403
|
-
metadata: {
|
|
404
|
-
"user_id": userId,
|
|
405
|
-
},
|
|
406
|
-
});
|
|
407
|
-
return {
|
|
408
|
-
endpoint: session.url,
|
|
409
|
-
customerId: customer.id,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
else {
|
|
413
|
-
const session = yield stripeClient.checkout.sessions.create({
|
|
414
|
-
payment_method_types: ["card"],
|
|
415
|
-
mode: "setup",
|
|
416
|
-
customer: data["customer"],
|
|
417
|
-
success_url: successUrl,
|
|
418
|
-
cancel_url: cancelUrl,
|
|
419
|
-
});
|
|
420
|
-
return {
|
|
421
|
-
endpoint: session.url,
|
|
422
|
-
customerId: data["customer"],
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
case "set_customer_default_payment": {
|
|
427
|
-
const userId = query.data.userId;
|
|
428
|
-
const paymentId = query.data.paymentId;
|
|
429
|
-
if (!paymentId) {
|
|
430
|
-
throw new functions.https.HttpsError("invalid-argument", "The payment id is empty.");
|
|
431
|
-
}
|
|
432
|
-
if (!userId) {
|
|
433
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
434
|
-
}
|
|
435
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
436
|
-
const data = doc.data();
|
|
437
|
-
if (!data || !data["customer"]) {
|
|
438
|
-
throw new functions.https.HttpsError("not-found", "The customer is empty.");
|
|
439
|
-
}
|
|
440
|
-
const payment = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePaymentPath}/${paymentId}`).get();
|
|
441
|
-
const paymentData = payment.data();
|
|
442
|
-
if (!paymentData || !paymentData["id"]) {
|
|
443
|
-
throw new functions.https.HttpsError("not-found", "The payment method is empty.");
|
|
444
|
-
}
|
|
445
|
-
yield stripeClient.customers.update(data["customer"], {
|
|
446
|
-
invoice_settings: {
|
|
447
|
-
default_payment_method: paymentData["id"],
|
|
448
|
-
},
|
|
449
|
-
});
|
|
450
|
-
if (data["defaultPayment"] !== paymentData["id"]) {
|
|
451
|
-
const update = {};
|
|
452
|
-
update["defaultPayment"] = paymentData["id"];
|
|
453
|
-
yield doc.ref.set(update, {
|
|
454
|
-
merge: true,
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
return {
|
|
458
|
-
success: true,
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
case "delete_payment": {
|
|
462
|
-
const userId = query.data.userId;
|
|
463
|
-
const paymentId = query.data.paymentId;
|
|
464
|
-
if (!paymentId) {
|
|
465
|
-
throw new functions.https.HttpsError("invalid-argument", "The payment id is empty.");
|
|
466
|
-
}
|
|
467
|
-
if (!userId) {
|
|
468
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
469
|
-
}
|
|
470
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
471
|
-
const data = doc.data();
|
|
472
|
-
if (!data || !data["customer"]) {
|
|
473
|
-
throw new functions.https.HttpsError("not-found", "The customer is empty.");
|
|
474
|
-
}
|
|
475
|
-
const payment = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePaymentPath}/${paymentId}`).get();
|
|
476
|
-
const paymentData = payment.data();
|
|
477
|
-
if (!paymentData || !paymentData["id"]) {
|
|
478
|
-
throw new functions.https.HttpsError("not-found", "The payment method is empty.");
|
|
479
|
-
}
|
|
480
|
-
yield stripeClient.paymentMethods.detach(paymentData["id"]);
|
|
481
|
-
if (data["defaultPayment"] === paymentData["id"]) {
|
|
482
|
-
const update = {};
|
|
483
|
-
update["defaultPayment"] = admin.firestore.FieldValue.delete();
|
|
484
|
-
yield doc.ref.set(update, {
|
|
485
|
-
merge: true,
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
return {
|
|
489
|
-
success: true,
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
case "delete_customer": {
|
|
493
|
-
const userId = query.data.userId;
|
|
494
|
-
if (!userId) {
|
|
495
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
496
|
-
}
|
|
497
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
498
|
-
const data = doc.data();
|
|
499
|
-
if (!data || !data["customer"]) {
|
|
500
|
-
throw new functions.https.HttpsError("not-found", "Customer id is not found.");
|
|
501
|
-
}
|
|
502
|
-
yield stripeClient.customers.del(data["customer"]);
|
|
503
|
-
const update = {};
|
|
504
|
-
update["customer"] = admin.firestore.FieldValue.delete();
|
|
505
|
-
yield doc.ref.set(update, {
|
|
506
|
-
merge: true,
|
|
507
|
-
});
|
|
508
|
-
return {
|
|
509
|
-
success: true,
|
|
510
|
-
};
|
|
511
|
-
}
|
|
512
|
-
case "authorization": {
|
|
513
|
-
const authInstance = admin.auth();
|
|
514
|
-
const amount = parseFloat(query.data.amount);
|
|
515
|
-
const currency = (_g = query.data.currency) !== null && _g !== void 0 ? _g : "jpy";
|
|
516
|
-
const returnUrl = query.data.returnUrl;
|
|
517
|
-
const online = query.data.online == "true";
|
|
518
|
-
const emailFrom = query.data.from;
|
|
519
|
-
const emailTitle = query.data.title;
|
|
520
|
-
const emailContent = query.data.content;
|
|
521
|
-
const userId = query.data.userId;
|
|
522
|
-
if (!userId) {
|
|
523
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
524
|
-
}
|
|
525
|
-
const userDoc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
526
|
-
const userData = userDoc.data();
|
|
527
|
-
if (!userData || !userData["customer"]) {
|
|
528
|
-
throw new functions.https.HttpsError("not-found", "The customer id is not found.");
|
|
529
|
-
}
|
|
530
|
-
let defaultPayment = userData["defaultPayment"];
|
|
531
|
-
if (!defaultPayment) {
|
|
532
|
-
const customer = yield stripeClient.customers.retrieve(userData["customer"]);
|
|
533
|
-
defaultPayment = customer.invoice_settings.default_payment_method;
|
|
534
|
-
if (!defaultPayment) {
|
|
535
|
-
const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get();
|
|
536
|
-
if (payments.size <= 0) {
|
|
537
|
-
throw new functions.https.HttpsError("not-found", "The payment method is not found.");
|
|
538
|
-
}
|
|
539
|
-
defaultPayment = payments.docs[0].data()["id"];
|
|
540
|
-
}
|
|
541
|
-
const update = {};
|
|
542
|
-
update["defaultPayment"] = defaultPayment;
|
|
543
|
-
yield userDoc.ref.set(update, {
|
|
544
|
-
merge: true,
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
const user = yield authInstance.getUser(userId);
|
|
548
|
-
if (!user) {
|
|
549
|
-
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
550
|
-
}
|
|
551
|
-
let email = user.email;
|
|
552
|
-
if (!email) {
|
|
553
|
-
const paymentMethod = yield stripeClient.paymentMethods.retrieve(defaultPayment);
|
|
554
|
-
if (paymentMethod && paymentMethod["billing_details"] && paymentMethod["billing_details"]["email"]) {
|
|
555
|
-
email = paymentMethod["billing_details"]["email"];
|
|
556
|
-
}
|
|
557
|
-
if (!email) {
|
|
558
|
-
throw new functions.https.HttpsError("not-found", "The user's email is not found.");
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
562
|
-
payment_method_types: ["card"],
|
|
563
|
-
amount: amount,
|
|
564
|
-
confirm: false,
|
|
565
|
-
capture_method: "manual",
|
|
566
|
-
payment_method: defaultPayment,
|
|
567
|
-
description: "",
|
|
568
|
-
customer: userData["customer"],
|
|
569
|
-
receipt_email: email,
|
|
570
|
-
currency: currency !== null && currency !== void 0 ? currency : "usd",
|
|
571
|
-
setup_future_usage: "off_session",
|
|
572
|
-
});
|
|
573
|
-
if (!paymentIntent) {
|
|
574
|
-
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
575
|
-
}
|
|
576
|
-
const confirmedPaymentIntent = yield stripeClient.paymentIntents.confirm(paymentIntent.id, {
|
|
577
|
-
return_url: returnUrl,
|
|
578
|
-
});
|
|
579
|
-
const nextActionUrl = (_k = (_j = (_h = confirmedPaymentIntent.next_action) === null || _h === void 0 ? void 0 : _h.redirect_to_url) === null || _j === void 0 ? void 0 : _j.url) !== null && _k !== void 0 ? _k : "";
|
|
580
|
-
if (nextActionUrl && !online) {
|
|
581
|
-
if (emailFrom && email && emailTitle && emailContent) {
|
|
582
|
-
switch (stripeEmailProvider) {
|
|
583
|
-
case "gmail": {
|
|
584
|
-
yield gmail.send({
|
|
585
|
-
from: emailFrom,
|
|
586
|
-
to: email,
|
|
587
|
-
title: emailTitle,
|
|
588
|
-
content: emailContent.replace("{url}", nextActionUrl),
|
|
589
|
-
});
|
|
590
|
-
break;
|
|
591
|
-
}
|
|
592
|
-
case "sendgrid": {
|
|
593
|
-
yield sendgrid.send({
|
|
594
|
-
from: emailFrom,
|
|
595
|
-
to: email,
|
|
596
|
-
title: emailTitle,
|
|
597
|
-
content: emailContent.replace("{url}", nextActionUrl),
|
|
598
|
-
});
|
|
599
|
-
break;
|
|
600
|
-
}
|
|
373
|
+
case "create_customer_and_payment": {
|
|
374
|
+
const userId = query.data.userId;
|
|
375
|
+
const successUrl = query.data.successUrl;
|
|
376
|
+
const cancelUrl = query.data.cancelUrl;
|
|
377
|
+
const authInstance = admin.auth();
|
|
378
|
+
if (!userId) {
|
|
379
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
601
380
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
case "create_purchase": {
|
|
624
|
-
const authInstance = admin.auth();
|
|
625
|
-
const amount = parseFloat(query.data.amount);
|
|
626
|
-
const revenue = parseFloat((_p = query.data.revenueRatio) !== null && _p !== void 0 ? _p : 0);
|
|
627
|
-
const currency = (_q = query.data.currency) !== null && _q !== void 0 ? _q : "jpy";
|
|
628
|
-
const userId = query.data.userId;
|
|
629
|
-
const targetUserId = query.data.targetUserId;
|
|
630
|
-
const orderId = query.data.orderId;
|
|
631
|
-
const description = query.data.description;
|
|
632
|
-
const emailFrom = query.data.emailFrom;
|
|
633
|
-
const emailTitle = query.data.emailTitle;
|
|
634
|
-
const emailContent = query.data.emailContent;
|
|
635
|
-
const locale = query.data.locale;
|
|
636
|
-
if (!orderId) {
|
|
637
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
638
|
-
}
|
|
639
|
-
if (!userId) {
|
|
640
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
641
|
-
}
|
|
642
|
-
const userDoc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
643
|
-
const userData = userDoc.data();
|
|
644
|
-
if (!userData || !userData["customer"]) {
|
|
645
|
-
throw new functions.https.HttpsError("not-found", "The customer id is not found.");
|
|
646
|
-
}
|
|
647
|
-
let defaultPayment = userData["defaultPayment"];
|
|
648
|
-
if (!defaultPayment) {
|
|
649
|
-
const customer = yield stripeClient.customers.retrieve(userData["customer"]);
|
|
650
|
-
defaultPayment = customer.invoice_settings.default_payment_method;
|
|
651
|
-
if (!defaultPayment) {
|
|
652
|
-
const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get();
|
|
653
|
-
if (payments.size <= 0) {
|
|
654
|
-
throw new functions.https.HttpsError("not-found", "The payment method is not found.");
|
|
655
|
-
}
|
|
656
|
-
defaultPayment = payments.docs[0].data()["id"];
|
|
657
|
-
}
|
|
658
|
-
const update = {};
|
|
659
|
-
update["defaultPayment"] = defaultPayment;
|
|
660
|
-
yield userDoc.ref.set(update, {
|
|
661
|
-
merge: true,
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
const user = yield authInstance.getUser(userId);
|
|
665
|
-
if (!user) {
|
|
666
|
-
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
667
|
-
}
|
|
668
|
-
let email = user.email;
|
|
669
|
-
if (!email) {
|
|
670
|
-
const paymentMethod = yield stripeClient.paymentMethods.retrieve(defaultPayment);
|
|
671
|
-
if (paymentMethod && paymentMethod["billing_details"] && paymentMethod["billing_details"]["email"]) {
|
|
672
|
-
email = paymentMethod["billing_details"]["email"];
|
|
673
|
-
}
|
|
674
|
-
if (!email) {
|
|
675
|
-
throw new functions.https.HttpsError("not-found", "The user's email is not found.");
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
if (targetUserId) {
|
|
679
|
-
const targetDoc = yield firestoreInstance.doc(`${stripeUserPath}/${targetUserId}`).get();
|
|
680
|
-
const targetData = targetDoc.data();
|
|
681
|
-
if (!targetData || !targetData["account"]) {
|
|
682
|
-
throw new functions.https.HttpsError("not-found", "The target data is not found.");
|
|
683
|
-
}
|
|
684
|
-
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
685
|
-
payment_method_types: ["card"],
|
|
686
|
-
amount: amount,
|
|
687
|
-
confirm: false,
|
|
688
|
-
capture_method: "manual",
|
|
689
|
-
payment_method: defaultPayment,
|
|
690
|
-
description: description,
|
|
691
|
-
customer: userData["customer"],
|
|
692
|
-
metadata: {
|
|
693
|
-
"order_id": orderId,
|
|
694
|
-
},
|
|
695
|
-
receipt_email: email,
|
|
696
|
-
currency: currency,
|
|
697
|
-
setup_future_usage: "off_session",
|
|
698
|
-
application_fee_amount: amount * revenue,
|
|
699
|
-
transfer_data: {
|
|
700
|
-
destination: targetData["account"],
|
|
701
|
-
},
|
|
702
|
-
});
|
|
703
|
-
if (!paymentIntent) {
|
|
704
|
-
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
705
|
-
}
|
|
706
|
-
const update = {};
|
|
707
|
-
update["@uid"] = orderId;
|
|
708
|
-
update["orderId"] = orderId;
|
|
709
|
-
update["purchaseId"] = paymentIntent.id;
|
|
710
|
-
update["paymentMethodId"] = defaultPayment;
|
|
711
|
-
update["confirm"] = false;
|
|
712
|
-
update["verify"] = false;
|
|
713
|
-
update["capture"] = false;
|
|
714
|
-
update["success"] = false;
|
|
715
|
-
update["user"] = userId;
|
|
716
|
-
update["target"] = targetUserId;
|
|
717
|
-
update["nextAction"] = {
|
|
718
|
-
url: (_t = (_s = (_r = paymentIntent.next_action) === null || _r === void 0 ? void 0 : _r.redirect_to_url) === null || _s === void 0 ? void 0 : _s.url) !== null && _t !== void 0 ? _t : "",
|
|
719
|
-
returnUrl: (_w = (_v = (_u = paymentIntent.next_action) === null || _u === void 0 ? void 0 : _u.redirect_to_url) === null || _v === void 0 ? void 0 : _v.return_url) !== null && _w !== void 0 ? _w : "",
|
|
720
|
-
};
|
|
721
|
-
update["targetAccount"] = targetData["account"];
|
|
722
|
-
update["customer"] = userData["customer"];
|
|
723
|
-
update["amount"] = paymentIntent.amount;
|
|
724
|
-
update["application"] = paymentIntent.application;
|
|
725
|
-
update["applicationFeeAmount"] = paymentIntent.application_fee_amount;
|
|
726
|
-
update["transferAmount"] = (_y = (_x = paymentIntent.transfer_data) === null || _x === void 0 ? void 0 : _x.amount) !== null && _y !== void 0 ? _y : 0;
|
|
727
|
-
update["transferDistination"] = (_0 = (_z = paymentIntent.transfer_data) === null || _z === void 0 ? void 0 : _z.destination) !== null && _0 !== void 0 ? _0 : "";
|
|
728
|
-
update["currency"] = paymentIntent.currency;
|
|
729
|
-
update["clientSecret"] = paymentIntent.client_secret;
|
|
730
|
-
update["createdTime"] = new Date(paymentIntent.created * 1000);
|
|
731
|
-
update["updatedTime"] = new Date();
|
|
732
|
-
update["emailFrom"] = emailFrom;
|
|
733
|
-
update["emailTo"] = email;
|
|
734
|
-
update["emailTitle"] = emailTitle;
|
|
735
|
-
update["emailContent"] = emailContent;
|
|
736
|
-
update["locale"] = locale;
|
|
737
|
-
yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).set(update, {
|
|
738
|
-
merge: true,
|
|
739
|
-
});
|
|
740
|
-
return {
|
|
741
|
-
purchaseId: paymentIntent.id,
|
|
742
|
-
};
|
|
743
|
-
}
|
|
744
|
-
else {
|
|
745
|
-
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
746
|
-
payment_method_types: ["card"],
|
|
747
|
-
amount: amount,
|
|
748
|
-
confirm: false,
|
|
749
|
-
capture_method: "manual",
|
|
750
|
-
payment_method: defaultPayment,
|
|
751
|
-
description: description,
|
|
752
|
-
customer: userData["customer"],
|
|
753
|
-
metadata: {
|
|
754
|
-
"order_id": orderId,
|
|
755
|
-
},
|
|
756
|
-
receipt_email: email,
|
|
757
|
-
currency: currency,
|
|
758
|
-
setup_future_usage: "off_session",
|
|
759
|
-
});
|
|
760
|
-
if (!paymentIntent) {
|
|
761
|
-
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
762
|
-
}
|
|
763
|
-
const update = {};
|
|
764
|
-
update["@uid"] = orderId;
|
|
765
|
-
update["orderId"] = orderId;
|
|
766
|
-
update["purchaseId"] = paymentIntent.id;
|
|
767
|
-
update["paymentMethodId"] = defaultPayment;
|
|
768
|
-
update["confirm"] = false;
|
|
769
|
-
update["verify"] = false;
|
|
770
|
-
update["capture"] = false;
|
|
771
|
-
update["success"] = false;
|
|
772
|
-
update["user"] = userId;
|
|
773
|
-
update["nextAction"] = {
|
|
774
|
-
url: (_3 = (_2 = (_1 = paymentIntent.next_action) === null || _1 === void 0 ? void 0 : _1.redirect_to_url) === null || _2 === void 0 ? void 0 : _2.url) !== null && _3 !== void 0 ? _3 : "",
|
|
775
|
-
returnUrl: (_6 = (_5 = (_4 = paymentIntent.next_action) === null || _4 === void 0 ? void 0 : _4.redirect_to_url) === null || _5 === void 0 ? void 0 : _5.return_url) !== null && _6 !== void 0 ? _6 : "",
|
|
776
|
-
};
|
|
777
|
-
update["customer"] = userData["customer"];
|
|
778
|
-
update["amount"] = paymentIntent.amount;
|
|
779
|
-
update["application"] = paymentIntent.application;
|
|
780
|
-
update["applicationFeeAmount"] = paymentIntent.application_fee_amount;
|
|
781
|
-
update["transferAmount"] = (_8 = (_7 = paymentIntent.transfer_data) === null || _7 === void 0 ? void 0 : _7.amount) !== null && _8 !== void 0 ? _8 : 0;
|
|
782
|
-
update["transferDistination"] = (_10 = (_9 = paymentIntent.transfer_data) === null || _9 === void 0 ? void 0 : _9.destination) !== null && _10 !== void 0 ? _10 : "";
|
|
783
|
-
update["currency"] = paymentIntent.currency;
|
|
784
|
-
update["clientSecret"] = paymentIntent.client_secret;
|
|
785
|
-
update["createdTime"] = new Date(paymentIntent.created * 1000);
|
|
786
|
-
update["updatedTime"] = new Date();
|
|
787
|
-
update["emailFrom"] = emailFrom;
|
|
788
|
-
update["emailTo"] = email;
|
|
789
|
-
update["emailTitle"] = emailTitle;
|
|
790
|
-
update["emailContent"] = emailContent;
|
|
791
|
-
update["locale"] = locale;
|
|
792
|
-
yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).set(update, {
|
|
793
|
-
merge: true,
|
|
794
|
-
});
|
|
795
|
-
return {
|
|
796
|
-
purchaseId: paymentIntent.id,
|
|
797
|
-
};
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
case "confirm_purchase": {
|
|
801
|
-
const userId = query.data.userId;
|
|
802
|
-
const orderId = query.data.orderId;
|
|
803
|
-
const successUrl = query.data.successUrl;
|
|
804
|
-
const failureUrl = query.data.failureUrl;
|
|
805
|
-
let returnUrl = query.data.returnUrl;
|
|
806
|
-
const online = query.data.online == "true";
|
|
807
|
-
if (!orderId) {
|
|
808
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
809
|
-
}
|
|
810
|
-
if (!userId) {
|
|
811
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
812
|
-
}
|
|
813
|
-
if (!online) {
|
|
814
|
-
returnUrl = returnUrl + "?token=" + JSON.stringify({
|
|
815
|
-
userId: userId,
|
|
816
|
-
orderId: orderId,
|
|
817
|
-
successUrl: successUrl,
|
|
818
|
-
failureUrl: failureUrl,
|
|
819
|
-
}).encrypt({
|
|
820
|
-
key: apiKey.slice(0, 32),
|
|
821
|
-
ivKey: apiKey.slice(-16),
|
|
822
|
-
});
|
|
823
|
-
}
|
|
824
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
825
|
-
const data = doc.data();
|
|
826
|
-
if (!data || !data["purchaseId"]) {
|
|
827
|
-
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
828
|
-
}
|
|
829
|
-
if (data["error"]) {
|
|
830
|
-
throw new functions.https.HttpsError("aborted", "The purchase data has some errors");
|
|
831
|
-
}
|
|
832
|
-
if (data["cancel"]) {
|
|
833
|
-
throw new functions.https.HttpsError("cancelled", "The purchase data is already canceled.");
|
|
834
|
-
}
|
|
835
|
-
if (data["confirm"]) {
|
|
836
|
-
if (data["verify"]) {
|
|
837
|
-
throw new functions.https.HttpsError("ok", "The purchase data is already confirmed.");
|
|
838
|
-
}
|
|
839
|
-
try {
|
|
840
|
-
const paymentIntent = yield stripeClient.paymentIntents.retrieve(data["purchaseId"]);
|
|
841
|
-
const nextActionUrl = (_13 = (_12 = (_11 = paymentIntent.next_action) === null || _11 === void 0 ? void 0 : _11.redirect_to_url) === null || _12 === void 0 ? void 0 : _12.url) !== null && _13 !== void 0 ? _13 : "";
|
|
842
|
-
const update = {};
|
|
843
|
-
if (nextActionUrl) {
|
|
844
|
-
if (!online) {
|
|
845
|
-
if (data["emailFrom"] && data["emailTo"] && data["emailTitle"] && data["emailContent"]) {
|
|
846
|
-
switch (stripeEmailProvider) {
|
|
847
|
-
case "gmail": {
|
|
848
|
-
yield gmail.send({
|
|
849
|
-
from: data["emailFrom"],
|
|
850
|
-
to: data["emailTo"],
|
|
851
|
-
title: data["emailTitle"],
|
|
852
|
-
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
853
|
-
});
|
|
854
|
-
break;
|
|
855
|
-
}
|
|
856
|
-
case "sendgrid": {
|
|
857
|
-
yield sendgrid.send({
|
|
858
|
-
from: data["emailFrom"],
|
|
859
|
-
to: data["emailTo"],
|
|
860
|
-
title: data["emailTitle"],
|
|
861
|
-
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
862
|
-
});
|
|
863
|
-
break;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
else {
|
|
868
|
-
update["error"] = true;
|
|
869
|
-
update["errorMessage"] = "3D Secure authentication is required, but the user is offline and no email settings have been configured.";
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
update["nextAction"] = {
|
|
873
|
-
url: nextActionUrl,
|
|
874
|
-
returnUrl: (_16 = (_15 = (_14 = paymentIntent.next_action) === null || _14 === void 0 ? void 0 : _14.redirect_to_url) === null || _15 === void 0 ? void 0 : _15.return_url) !== null && _16 !== void 0 ? _16 : "",
|
|
875
|
-
};
|
|
876
|
-
yield doc.ref.set(update, {
|
|
381
|
+
const user = yield authInstance.getUser(userId);
|
|
382
|
+
if (!user) {
|
|
383
|
+
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
384
|
+
}
|
|
385
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
386
|
+
const data = doc.data();
|
|
387
|
+
if (!data || !data["customer"]) {
|
|
388
|
+
const customer = yield stripeClient.customers.create({
|
|
389
|
+
metadata: {
|
|
390
|
+
"user_id": userId,
|
|
391
|
+
},
|
|
392
|
+
email: (_g = user.email) !== null && _g !== void 0 ? _g : (!data ? null : data["email"]),
|
|
393
|
+
});
|
|
394
|
+
const update = {};
|
|
395
|
+
update["@uid"] = userId;
|
|
396
|
+
update["@time"] = new Date();
|
|
397
|
+
update["user"] = userId;
|
|
398
|
+
update["customer"] = customer.id;
|
|
399
|
+
yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).set(update, {
|
|
877
400
|
merge: true,
|
|
878
401
|
});
|
|
402
|
+
const session = yield stripeClient.checkout.sessions.create({
|
|
403
|
+
payment_method_types: ["card"],
|
|
404
|
+
mode: "setup",
|
|
405
|
+
customer: customer.id,
|
|
406
|
+
success_url: successUrl,
|
|
407
|
+
cancel_url: cancelUrl,
|
|
408
|
+
metadata: {
|
|
409
|
+
"user_id": userId,
|
|
410
|
+
},
|
|
411
|
+
});
|
|
879
412
|
return {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
purchaseId: data["purchaseId"],
|
|
413
|
+
endpoint: session.url,
|
|
414
|
+
customerId: customer.id,
|
|
883
415
|
};
|
|
884
416
|
}
|
|
885
417
|
else {
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
418
|
+
const session = yield stripeClient.checkout.sessions.create({
|
|
419
|
+
payment_method_types: ["card"],
|
|
420
|
+
mode: "setup",
|
|
421
|
+
customer: data["customer"],
|
|
422
|
+
success_url: successUrl,
|
|
423
|
+
cancel_url: cancelUrl,
|
|
890
424
|
});
|
|
891
425
|
return {
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
purchaseId: data["purchaseId"],
|
|
426
|
+
endpoint: session.url,
|
|
427
|
+
customerId: data["customer"],
|
|
895
428
|
};
|
|
896
429
|
}
|
|
897
430
|
}
|
|
898
|
-
|
|
431
|
+
case "set_customer_default_payment": {
|
|
432
|
+
const userId = query.data.userId;
|
|
433
|
+
const paymentId = query.data.paymentId;
|
|
434
|
+
if (!paymentId) {
|
|
435
|
+
throw new functions.https.HttpsError("invalid-argument", "The payment id is empty.");
|
|
436
|
+
}
|
|
437
|
+
if (!userId) {
|
|
438
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
439
|
+
}
|
|
440
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
441
|
+
const data = doc.data();
|
|
442
|
+
if (!data || !data["customer"]) {
|
|
443
|
+
throw new functions.https.HttpsError("not-found", "The customer is empty.");
|
|
444
|
+
}
|
|
445
|
+
const payment = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePaymentPath}/${paymentId}`).get();
|
|
446
|
+
const paymentData = payment.data();
|
|
447
|
+
if (!paymentData || !paymentData["id"]) {
|
|
448
|
+
throw new functions.https.HttpsError("not-found", "The payment method is empty.");
|
|
449
|
+
}
|
|
450
|
+
yield stripeClient.customers.update(data["customer"], {
|
|
451
|
+
invoice_settings: {
|
|
452
|
+
default_payment_method: paymentData["id"],
|
|
453
|
+
},
|
|
454
|
+
});
|
|
455
|
+
if (data["defaultPayment"] !== paymentData["id"]) {
|
|
456
|
+
const update = {};
|
|
457
|
+
update["defaultPayment"] = paymentData["id"];
|
|
458
|
+
yield doc.ref.set(update, {
|
|
459
|
+
merge: true,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
success: true,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
case "delete_payment": {
|
|
467
|
+
const userId = query.data.userId;
|
|
468
|
+
const paymentId = query.data.paymentId;
|
|
469
|
+
if (!paymentId) {
|
|
470
|
+
throw new functions.https.HttpsError("invalid-argument", "The payment id is empty.");
|
|
471
|
+
}
|
|
472
|
+
if (!userId) {
|
|
473
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
474
|
+
}
|
|
475
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
476
|
+
const data = doc.data();
|
|
477
|
+
if (!data || !data["customer"]) {
|
|
478
|
+
throw new functions.https.HttpsError("not-found", "The customer is empty.");
|
|
479
|
+
}
|
|
480
|
+
const payment = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePaymentPath}/${paymentId}`).get();
|
|
481
|
+
const paymentData = payment.data();
|
|
482
|
+
if (!paymentData || !paymentData["id"]) {
|
|
483
|
+
throw new functions.https.HttpsError("not-found", "The payment method is empty.");
|
|
484
|
+
}
|
|
485
|
+
yield stripeClient.paymentMethods.detach(paymentData["id"]);
|
|
486
|
+
if (data["defaultPayment"] === paymentData["id"]) {
|
|
487
|
+
const update = {};
|
|
488
|
+
update["defaultPayment"] = admin.firestore.FieldValue.delete();
|
|
489
|
+
yield doc.ref.set(update, {
|
|
490
|
+
merge: true,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
success: true,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
case "delete_customer": {
|
|
498
|
+
const userId = query.data.userId;
|
|
499
|
+
if (!userId) {
|
|
500
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
501
|
+
}
|
|
502
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
503
|
+
const data = doc.data();
|
|
504
|
+
if (!data || !data["customer"]) {
|
|
505
|
+
throw new functions.https.HttpsError("not-found", "Customer id is not found.");
|
|
506
|
+
}
|
|
507
|
+
yield stripeClient.customers.del(data["customer"]);
|
|
899
508
|
const update = {};
|
|
900
|
-
update["
|
|
901
|
-
update["errorMessage"] = "The Purchase confirmation failed. Please replace the billing information and Refresh.";
|
|
509
|
+
update["customer"] = admin.firestore.FieldValue.delete();
|
|
902
510
|
yield doc.ref.set(update, {
|
|
903
511
|
merge: true,
|
|
904
512
|
});
|
|
905
|
-
|
|
513
|
+
return {
|
|
514
|
+
success: true,
|
|
515
|
+
};
|
|
906
516
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const
|
|
517
|
+
case "authorization": {
|
|
518
|
+
const authInstance = admin.auth();
|
|
519
|
+
const amount = parseFloat(query.data.amount);
|
|
520
|
+
const currency = (_h = query.data.currency) !== null && _h !== void 0 ? _h : "jpy";
|
|
521
|
+
const returnUrl = query.data.returnUrl;
|
|
522
|
+
const online = query.data.online == "true";
|
|
523
|
+
const emailFrom = query.data.from;
|
|
524
|
+
const emailTitle = query.data.title;
|
|
525
|
+
const emailContent = query.data.content;
|
|
526
|
+
const userId = query.data.userId;
|
|
527
|
+
if (!userId) {
|
|
528
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
529
|
+
}
|
|
530
|
+
const userDoc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
531
|
+
const userData = userDoc.data();
|
|
532
|
+
if (!userData || !userData["customer"]) {
|
|
533
|
+
throw new functions.https.HttpsError("not-found", "The customer id is not found.");
|
|
534
|
+
}
|
|
535
|
+
let defaultPayment = userData["defaultPayment"];
|
|
536
|
+
if (!defaultPayment) {
|
|
537
|
+
const customer = yield stripeClient.customers.retrieve(userData["customer"]);
|
|
538
|
+
defaultPayment = customer.invoice_settings.default_payment_method;
|
|
539
|
+
if (!defaultPayment) {
|
|
540
|
+
const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get();
|
|
541
|
+
if (payments.size <= 0) {
|
|
542
|
+
throw new functions.https.HttpsError("not-found", "The payment method is not found.");
|
|
543
|
+
}
|
|
544
|
+
defaultPayment = payments.docs[0].data()["id"];
|
|
545
|
+
}
|
|
546
|
+
const update = {};
|
|
547
|
+
update["defaultPayment"] = defaultPayment;
|
|
548
|
+
yield userDoc.ref.set(update, {
|
|
549
|
+
merge: true,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
const user = yield authInstance.getUser(userId);
|
|
553
|
+
if (!user) {
|
|
554
|
+
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
555
|
+
}
|
|
556
|
+
let email = user.email;
|
|
557
|
+
if (!email) {
|
|
558
|
+
const paymentMethod = yield stripeClient.paymentMethods.retrieve(defaultPayment);
|
|
559
|
+
if (paymentMethod && paymentMethod["billing_details"] && paymentMethod["billing_details"]["email"]) {
|
|
560
|
+
email = paymentMethod["billing_details"]["email"];
|
|
561
|
+
}
|
|
562
|
+
if (!email) {
|
|
563
|
+
throw new functions.https.HttpsError("not-found", "The user's email is not found.");
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
567
|
+
payment_method_types: ["card"],
|
|
568
|
+
amount: amount,
|
|
569
|
+
confirm: false,
|
|
570
|
+
capture_method: "manual",
|
|
571
|
+
payment_method: defaultPayment,
|
|
572
|
+
description: "",
|
|
573
|
+
customer: userData["customer"],
|
|
574
|
+
receipt_email: email,
|
|
575
|
+
currency: currency !== null && currency !== void 0 ? currency : "usd",
|
|
576
|
+
setup_future_usage: "off_session",
|
|
577
|
+
});
|
|
578
|
+
if (!paymentIntent) {
|
|
579
|
+
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
580
|
+
}
|
|
581
|
+
const confirmedPaymentIntent = yield stripeClient.paymentIntents.confirm(paymentIntent.id, {
|
|
911
582
|
return_url: returnUrl,
|
|
912
583
|
});
|
|
913
|
-
const nextActionUrl = (
|
|
914
|
-
const update = {};
|
|
584
|
+
const nextActionUrl = (_l = (_k = (_j = confirmedPaymentIntent.next_action) === null || _j === void 0 ? void 0 : _j.redirect_to_url) === null || _k === void 0 ? void 0 : _k.url) !== null && _l !== void 0 ? _l : "";
|
|
915
585
|
if (nextActionUrl && !online) {
|
|
916
|
-
if (
|
|
586
|
+
if (emailFrom && email && emailTitle && emailContent) {
|
|
917
587
|
switch (stripeEmailProvider) {
|
|
918
588
|
case "gmail": {
|
|
919
589
|
yield gmail.send({
|
|
920
|
-
from:
|
|
921
|
-
to:
|
|
922
|
-
title:
|
|
923
|
-
content:
|
|
590
|
+
from: emailFrom,
|
|
591
|
+
to: email,
|
|
592
|
+
title: emailTitle,
|
|
593
|
+
content: emailContent.replace("{url}", nextActionUrl),
|
|
924
594
|
});
|
|
925
595
|
break;
|
|
926
596
|
}
|
|
927
597
|
case "sendgrid": {
|
|
928
598
|
yield sendgrid.send({
|
|
929
|
-
from:
|
|
930
|
-
to:
|
|
931
|
-
title:
|
|
932
|
-
content:
|
|
599
|
+
from: emailFrom,
|
|
600
|
+
to: email,
|
|
601
|
+
title: emailTitle,
|
|
602
|
+
content: emailContent.replace("{url}", nextActionUrl),
|
|
933
603
|
});
|
|
934
604
|
break;
|
|
935
605
|
}
|
|
936
606
|
}
|
|
937
607
|
}
|
|
938
608
|
else {
|
|
939
|
-
|
|
940
|
-
update["errorMessage"] = "3D Secure authentication is required, but the user is offline and no email settings have been configured.";
|
|
609
|
+
throw new functions.https.HttpsError("unavailable", "3D Secure authentication is required, but the user is offline and no email settings have been configured.");
|
|
941
610
|
}
|
|
942
611
|
}
|
|
943
|
-
|
|
944
|
-
url: nextActionUrl,
|
|
945
|
-
returnUrl: (
|
|
612
|
+
return {
|
|
613
|
+
url: online ? nextActionUrl : "",
|
|
614
|
+
returnUrl: online ? (_p = (_o = (_m = confirmedPaymentIntent.next_action) === null || _m === void 0 ? void 0 : _m.redirect_to_url) === null || _o === void 0 ? void 0 : _o.return_url) !== null && _p !== void 0 ? _p : "" : "",
|
|
615
|
+
authorizedId: paymentIntent.id,
|
|
946
616
|
};
|
|
617
|
+
}
|
|
618
|
+
case "confirm_authorization": {
|
|
619
|
+
const authorizedId = query.data.authorizedId;
|
|
620
|
+
if (!authorizedId) {
|
|
621
|
+
throw new functions.https.HttpsError("invalid-argument", "The authorized id is empty.");
|
|
622
|
+
}
|
|
623
|
+
yield stripeClient.paymentIntents.cancel(authorizedId);
|
|
624
|
+
return {
|
|
625
|
+
success: true,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
case "create_purchase": {
|
|
629
|
+
const authInstance = admin.auth();
|
|
630
|
+
const amount = parseFloat(query.data.amount);
|
|
631
|
+
const revenue = parseFloat((_q = query.data.revenueRatio) !== null && _q !== void 0 ? _q : 0);
|
|
632
|
+
const currency = (_r = query.data.currency) !== null && _r !== void 0 ? _r : "jpy";
|
|
633
|
+
const userId = query.data.userId;
|
|
634
|
+
const targetUserId = query.data.targetUserId;
|
|
635
|
+
const orderId = query.data.orderId;
|
|
636
|
+
const description = query.data.description;
|
|
637
|
+
const emailFrom = query.data.emailFrom;
|
|
638
|
+
const emailTitle = query.data.emailTitle;
|
|
639
|
+
const emailContent = query.data.emailContent;
|
|
640
|
+
const locale = query.data.locale;
|
|
641
|
+
if (!orderId) {
|
|
642
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
643
|
+
}
|
|
644
|
+
if (!userId) {
|
|
645
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
646
|
+
}
|
|
647
|
+
const userDoc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
648
|
+
const userData = userDoc.data();
|
|
649
|
+
if (!userData || !userData["customer"]) {
|
|
650
|
+
throw new functions.https.HttpsError("not-found", "The customer id is not found.");
|
|
651
|
+
}
|
|
652
|
+
let defaultPayment = userData["defaultPayment"];
|
|
653
|
+
if (!defaultPayment) {
|
|
654
|
+
const customer = yield stripeClient.customers.retrieve(userData["customer"]);
|
|
655
|
+
defaultPayment = customer.invoice_settings.default_payment_method;
|
|
656
|
+
if (!defaultPayment) {
|
|
657
|
+
const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get();
|
|
658
|
+
if (payments.size <= 0) {
|
|
659
|
+
throw new functions.https.HttpsError("not-found", "The payment method is not found.");
|
|
660
|
+
}
|
|
661
|
+
defaultPayment = payments.docs[0].data()["id"];
|
|
662
|
+
}
|
|
663
|
+
const update = {};
|
|
664
|
+
update["defaultPayment"] = defaultPayment;
|
|
665
|
+
yield userDoc.ref.set(update, {
|
|
666
|
+
merge: true,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
const user = yield authInstance.getUser(userId);
|
|
670
|
+
if (!user) {
|
|
671
|
+
throw new functions.https.HttpsError("not-found", "The user is not found.");
|
|
672
|
+
}
|
|
673
|
+
let email = user.email;
|
|
674
|
+
if (!email) {
|
|
675
|
+
const paymentMethod = yield stripeClient.paymentMethods.retrieve(defaultPayment);
|
|
676
|
+
if (paymentMethod && paymentMethod["billing_details"] && paymentMethod["billing_details"]["email"]) {
|
|
677
|
+
email = paymentMethod["billing_details"]["email"];
|
|
678
|
+
}
|
|
679
|
+
if (!email) {
|
|
680
|
+
throw new functions.https.HttpsError("not-found", "The user's email is not found.");
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (targetUserId) {
|
|
684
|
+
const targetDoc = yield firestoreInstance.doc(`${stripeUserPath}/${targetUserId}`).get();
|
|
685
|
+
const targetData = targetDoc.data();
|
|
686
|
+
if (!targetData || !targetData["account"]) {
|
|
687
|
+
throw new functions.https.HttpsError("not-found", "The target data is not found.");
|
|
688
|
+
}
|
|
689
|
+
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
690
|
+
payment_method_types: ["card"],
|
|
691
|
+
amount: amount,
|
|
692
|
+
confirm: false,
|
|
693
|
+
capture_method: "manual",
|
|
694
|
+
payment_method: defaultPayment,
|
|
695
|
+
description: description,
|
|
696
|
+
customer: userData["customer"],
|
|
697
|
+
metadata: {
|
|
698
|
+
"order_id": orderId,
|
|
699
|
+
},
|
|
700
|
+
receipt_email: email,
|
|
701
|
+
currency: currency,
|
|
702
|
+
setup_future_usage: "off_session",
|
|
703
|
+
application_fee_amount: amount * revenue,
|
|
704
|
+
transfer_data: {
|
|
705
|
+
destination: targetData["account"],
|
|
706
|
+
},
|
|
707
|
+
});
|
|
708
|
+
if (!paymentIntent) {
|
|
709
|
+
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
710
|
+
}
|
|
711
|
+
const update = {};
|
|
712
|
+
update["@uid"] = orderId;
|
|
713
|
+
update["orderId"] = orderId;
|
|
714
|
+
update["purchaseId"] = paymentIntent.id;
|
|
715
|
+
update["paymentMethodId"] = defaultPayment;
|
|
716
|
+
update["confirm"] = false;
|
|
717
|
+
update["verify"] = false;
|
|
718
|
+
update["capture"] = false;
|
|
719
|
+
update["success"] = false;
|
|
720
|
+
update["user"] = userId;
|
|
721
|
+
update["target"] = targetUserId;
|
|
722
|
+
update["nextAction"] = {
|
|
723
|
+
url: (_u = (_t = (_s = paymentIntent.next_action) === null || _s === void 0 ? void 0 : _s.redirect_to_url) === null || _t === void 0 ? void 0 : _t.url) !== null && _u !== void 0 ? _u : "",
|
|
724
|
+
returnUrl: (_x = (_w = (_v = paymentIntent.next_action) === null || _v === void 0 ? void 0 : _v.redirect_to_url) === null || _w === void 0 ? void 0 : _w.return_url) !== null && _x !== void 0 ? _x : "",
|
|
725
|
+
};
|
|
726
|
+
update["targetAccount"] = targetData["account"];
|
|
727
|
+
update["customer"] = userData["customer"];
|
|
728
|
+
update["amount"] = paymentIntent.amount;
|
|
729
|
+
update["application"] = paymentIntent.application;
|
|
730
|
+
update["applicationFeeAmount"] = paymentIntent.application_fee_amount;
|
|
731
|
+
update["transferAmount"] = (_z = (_y = paymentIntent.transfer_data) === null || _y === void 0 ? void 0 : _y.amount) !== null && _z !== void 0 ? _z : 0;
|
|
732
|
+
update["transferDistination"] = (_1 = (_0 = paymentIntent.transfer_data) === null || _0 === void 0 ? void 0 : _0.destination) !== null && _1 !== void 0 ? _1 : "";
|
|
733
|
+
update["currency"] = paymentIntent.currency;
|
|
734
|
+
update["clientSecret"] = paymentIntent.client_secret;
|
|
735
|
+
update["createdTime"] = new Date(paymentIntent.created * 1000);
|
|
736
|
+
update["updatedTime"] = new Date();
|
|
737
|
+
update["emailFrom"] = emailFrom;
|
|
738
|
+
update["emailTo"] = email;
|
|
739
|
+
update["emailTitle"] = emailTitle;
|
|
740
|
+
update["emailContent"] = emailContent;
|
|
741
|
+
update["locale"] = locale;
|
|
742
|
+
yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).set(update, {
|
|
743
|
+
merge: true,
|
|
744
|
+
});
|
|
745
|
+
return {
|
|
746
|
+
purchaseId: paymentIntent.id,
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
const paymentIntent = yield stripeClient.paymentIntents.create({
|
|
751
|
+
payment_method_types: ["card"],
|
|
752
|
+
amount: amount,
|
|
753
|
+
confirm: false,
|
|
754
|
+
capture_method: "manual",
|
|
755
|
+
payment_method: defaultPayment,
|
|
756
|
+
description: description,
|
|
757
|
+
customer: userData["customer"],
|
|
758
|
+
metadata: {
|
|
759
|
+
"order_id": orderId,
|
|
760
|
+
},
|
|
761
|
+
receipt_email: email,
|
|
762
|
+
currency: currency,
|
|
763
|
+
setup_future_usage: "off_session",
|
|
764
|
+
});
|
|
765
|
+
if (!paymentIntent) {
|
|
766
|
+
throw new functions.https.HttpsError("data-loss", "The payment is failed.");
|
|
767
|
+
}
|
|
768
|
+
const update = {};
|
|
769
|
+
update["@uid"] = orderId;
|
|
770
|
+
update["orderId"] = orderId;
|
|
771
|
+
update["purchaseId"] = paymentIntent.id;
|
|
772
|
+
update["paymentMethodId"] = defaultPayment;
|
|
773
|
+
update["confirm"] = false;
|
|
774
|
+
update["verify"] = false;
|
|
775
|
+
update["capture"] = false;
|
|
776
|
+
update["success"] = false;
|
|
777
|
+
update["user"] = userId;
|
|
778
|
+
update["nextAction"] = {
|
|
779
|
+
url: (_4 = (_3 = (_2 = paymentIntent.next_action) === null || _2 === void 0 ? void 0 : _2.redirect_to_url) === null || _3 === void 0 ? void 0 : _3.url) !== null && _4 !== void 0 ? _4 : "",
|
|
780
|
+
returnUrl: (_7 = (_6 = (_5 = paymentIntent.next_action) === null || _5 === void 0 ? void 0 : _5.redirect_to_url) === null || _6 === void 0 ? void 0 : _6.return_url) !== null && _7 !== void 0 ? _7 : "",
|
|
781
|
+
};
|
|
782
|
+
update["customer"] = userData["customer"];
|
|
783
|
+
update["amount"] = paymentIntent.amount;
|
|
784
|
+
update["application"] = paymentIntent.application;
|
|
785
|
+
update["applicationFeeAmount"] = paymentIntent.application_fee_amount;
|
|
786
|
+
update["transferAmount"] = (_9 = (_8 = paymentIntent.transfer_data) === null || _8 === void 0 ? void 0 : _8.amount) !== null && _9 !== void 0 ? _9 : 0;
|
|
787
|
+
update["transferDistination"] = (_11 = (_10 = paymentIntent.transfer_data) === null || _10 === void 0 ? void 0 : _10.destination) !== null && _11 !== void 0 ? _11 : "";
|
|
788
|
+
update["currency"] = paymentIntent.currency;
|
|
789
|
+
update["clientSecret"] = paymentIntent.client_secret;
|
|
790
|
+
update["createdTime"] = new Date(paymentIntent.created * 1000);
|
|
791
|
+
update["updatedTime"] = new Date();
|
|
792
|
+
update["emailFrom"] = emailFrom;
|
|
793
|
+
update["emailTo"] = email;
|
|
794
|
+
update["emailTitle"] = emailTitle;
|
|
795
|
+
update["emailContent"] = emailContent;
|
|
796
|
+
update["locale"] = locale;
|
|
797
|
+
yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).set(update, {
|
|
798
|
+
merge: true,
|
|
799
|
+
});
|
|
800
|
+
return {
|
|
801
|
+
purchaseId: paymentIntent.id,
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
case "confirm_purchase": {
|
|
806
|
+
const userId = query.data.userId;
|
|
807
|
+
const orderId = query.data.orderId;
|
|
808
|
+
const successUrl = query.data.successUrl;
|
|
809
|
+
const failureUrl = query.data.failureUrl;
|
|
810
|
+
let returnUrl = query.data.returnUrl;
|
|
811
|
+
const online = query.data.online == "true";
|
|
812
|
+
if (!orderId) {
|
|
813
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
814
|
+
}
|
|
815
|
+
if (!userId) {
|
|
816
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
817
|
+
}
|
|
818
|
+
if (!online) {
|
|
819
|
+
returnUrl = returnUrl + "?token=" + JSON.stringify({
|
|
820
|
+
userId: userId,
|
|
821
|
+
orderId: orderId,
|
|
822
|
+
successUrl: successUrl,
|
|
823
|
+
failureUrl: failureUrl,
|
|
824
|
+
}).encrypt({
|
|
825
|
+
key: apiKey.slice(0, 32),
|
|
826
|
+
ivKey: apiKey.slice(-16),
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
830
|
+
const data = doc.data();
|
|
831
|
+
if (!data || !data["purchaseId"]) {
|
|
832
|
+
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
833
|
+
}
|
|
834
|
+
if (data["error"]) {
|
|
835
|
+
throw new functions.https.HttpsError("aborted", "The purchase data has some errors");
|
|
836
|
+
}
|
|
837
|
+
if (data["cancel"]) {
|
|
838
|
+
throw new functions.https.HttpsError("cancelled", "The purchase data is already canceled.");
|
|
839
|
+
}
|
|
840
|
+
if (data["confirm"]) {
|
|
841
|
+
if (data["verify"]) {
|
|
842
|
+
throw new functions.https.HttpsError("ok", "The purchase data is already confirmed.");
|
|
843
|
+
}
|
|
844
|
+
try {
|
|
845
|
+
const paymentIntent = yield stripeClient.paymentIntents.retrieve(data["purchaseId"]);
|
|
846
|
+
const nextActionUrl = (_14 = (_13 = (_12 = paymentIntent.next_action) === null || _12 === void 0 ? void 0 : _12.redirect_to_url) === null || _13 === void 0 ? void 0 : _13.url) !== null && _14 !== void 0 ? _14 : "";
|
|
847
|
+
const update = {};
|
|
848
|
+
if (nextActionUrl) {
|
|
849
|
+
if (!online) {
|
|
850
|
+
if (data["emailFrom"] && data["emailTo"] && data["emailTitle"] && data["emailContent"]) {
|
|
851
|
+
switch (stripeEmailProvider) {
|
|
852
|
+
case "gmail": {
|
|
853
|
+
yield gmail.send({
|
|
854
|
+
from: data["emailFrom"],
|
|
855
|
+
to: data["emailTo"],
|
|
856
|
+
title: data["emailTitle"],
|
|
857
|
+
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
858
|
+
});
|
|
859
|
+
break;
|
|
860
|
+
}
|
|
861
|
+
case "sendgrid": {
|
|
862
|
+
yield sendgrid.send({
|
|
863
|
+
from: data["emailFrom"],
|
|
864
|
+
to: data["emailTo"],
|
|
865
|
+
title: data["emailTitle"],
|
|
866
|
+
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
867
|
+
});
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
else {
|
|
873
|
+
update["error"] = true;
|
|
874
|
+
update["errorMessage"] = "3D Secure authentication is required, but the user is offline and no email settings have been configured.";
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
update["nextAction"] = {
|
|
878
|
+
url: nextActionUrl,
|
|
879
|
+
returnUrl: (_17 = (_16 = (_15 = paymentIntent.next_action) === null || _15 === void 0 ? void 0 : _15.redirect_to_url) === null || _16 === void 0 ? void 0 : _16.return_url) !== null && _17 !== void 0 ? _17 : "",
|
|
880
|
+
};
|
|
881
|
+
yield doc.ref.set(update, {
|
|
882
|
+
merge: true,
|
|
883
|
+
});
|
|
884
|
+
return {
|
|
885
|
+
url: online ? nextActionUrl : "",
|
|
886
|
+
returnUrl: online ? (_20 = (_19 = (_18 = paymentIntent.next_action) === null || _18 === void 0 ? void 0 : _18.redirect_to_url) === null || _19 === void 0 ? void 0 : _19.return_url) !== null && _20 !== void 0 ? _20 : "" : "",
|
|
887
|
+
purchaseId: data["purchaseId"],
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
else {
|
|
891
|
+
update["verify"] = true;
|
|
892
|
+
update["nextAction"] = admin.firestore.FieldValue.delete();
|
|
893
|
+
yield doc.ref.set(update, {
|
|
894
|
+
merge: true,
|
|
895
|
+
});
|
|
896
|
+
return {
|
|
897
|
+
url: "",
|
|
898
|
+
returnUrl: "",
|
|
899
|
+
purchaseId: data["purchaseId"],
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
catch (err) {
|
|
904
|
+
const update = {};
|
|
905
|
+
update["error"] = true;
|
|
906
|
+
update["errorMessage"] = "The Purchase confirmation failed. Please replace the billing information and Refresh.";
|
|
907
|
+
yield doc.ref.set(update, {
|
|
908
|
+
merge: true,
|
|
909
|
+
});
|
|
910
|
+
throw err;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
try {
|
|
915
|
+
const paymentIntent = yield stripeClient.paymentIntents.confirm(data["purchaseId"], {
|
|
916
|
+
return_url: returnUrl,
|
|
917
|
+
});
|
|
918
|
+
const nextActionUrl = (_23 = (_22 = (_21 = paymentIntent.next_action) === null || _21 === void 0 ? void 0 : _21.redirect_to_url) === null || _22 === void 0 ? void 0 : _22.url) !== null && _23 !== void 0 ? _23 : "";
|
|
919
|
+
const update = {};
|
|
920
|
+
if (nextActionUrl && !online) {
|
|
921
|
+
if (data["emailFrom"] && data["emailTo"] && data["emailTitle"] && data["emailContent"]) {
|
|
922
|
+
switch (stripeEmailProvider) {
|
|
923
|
+
case "gmail": {
|
|
924
|
+
yield gmail.send({
|
|
925
|
+
from: data["emailFrom"],
|
|
926
|
+
to: data["emailTo"],
|
|
927
|
+
title: data["emailTitle"],
|
|
928
|
+
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
929
|
+
});
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
case "sendgrid": {
|
|
933
|
+
yield sendgrid.send({
|
|
934
|
+
from: data["emailFrom"],
|
|
935
|
+
to: data["emailTo"],
|
|
936
|
+
title: data["emailTitle"],
|
|
937
|
+
content: data["emailContent"].replace("{url}", nextActionUrl),
|
|
938
|
+
});
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
update["error"] = true;
|
|
945
|
+
update["errorMessage"] = "3D Secure authentication is required, but the user is offline and no email settings have been configured.";
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
update["nextAction"] = {
|
|
949
|
+
url: nextActionUrl,
|
|
950
|
+
returnUrl: (_26 = (_25 = (_24 = paymentIntent.next_action) === null || _24 === void 0 ? void 0 : _24.redirect_to_url) === null || _25 === void 0 ? void 0 : _25.return_url) !== null && _26 !== void 0 ? _26 : "",
|
|
951
|
+
};
|
|
952
|
+
yield doc.ref.set(update, {
|
|
953
|
+
merge: true,
|
|
954
|
+
});
|
|
955
|
+
return {
|
|
956
|
+
url: online ? nextActionUrl : "",
|
|
957
|
+
returnUrl: online ? (_29 = (_28 = (_27 = paymentIntent.next_action) === null || _27 === void 0 ? void 0 : _27.redirect_to_url) === null || _28 === void 0 ? void 0 : _28.return_url) !== null && _29 !== void 0 ? _29 : "" : "",
|
|
958
|
+
purchaseId: data["purchaseId"],
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
catch (err) {
|
|
962
|
+
const update = {};
|
|
963
|
+
update["error"] = true;
|
|
964
|
+
update["errorMessage"] = "The Purchase confirmation failed. Please replace the billing information and Refresh.";
|
|
965
|
+
yield doc.ref.set(update, {
|
|
966
|
+
merge: true,
|
|
967
|
+
});
|
|
968
|
+
throw err;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
case "capture_purchase": {
|
|
973
|
+
const userId = query.data.userId;
|
|
974
|
+
const orderId = query.data.orderId;
|
|
975
|
+
const amount = parseFloat((_30 = query.data.amount) !== null && _30 !== void 0 ? _30 : 0.0);
|
|
976
|
+
if (!orderId) {
|
|
977
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
978
|
+
}
|
|
979
|
+
if (!userId) {
|
|
980
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
981
|
+
}
|
|
982
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
983
|
+
const data = doc.data();
|
|
984
|
+
if (!data || !data["purchaseId"]) {
|
|
985
|
+
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
986
|
+
}
|
|
987
|
+
if (data["error"]) {
|
|
988
|
+
throw new functions.https.HttpsError("aborted", "The purchase data has some errors");
|
|
989
|
+
}
|
|
990
|
+
if (data["cancel"]) {
|
|
991
|
+
throw new functions.https.HttpsError("cancelled", "This purchase data has already been cancelled.");
|
|
992
|
+
}
|
|
993
|
+
if (!data["confirm"] || !data["verify"]) {
|
|
994
|
+
throw new functions.https.HttpsError("failed-precondition", "The purchase data is not confirmed.");
|
|
995
|
+
}
|
|
996
|
+
if (data["capture"]) {
|
|
997
|
+
throw new functions.https.HttpsError("ok", "The purchase data is already captured.");
|
|
998
|
+
}
|
|
999
|
+
if (data["amount"] < amount) {
|
|
1000
|
+
throw new functions.https.HttpsError("invalid-argument", "You cannot capture an amount higher than the billing amount already saved.");
|
|
1001
|
+
}
|
|
1002
|
+
try {
|
|
1003
|
+
const paymentIntent = yield (amount > 0 ? stripeClient.paymentIntents.capture(data["purchaseId"], {
|
|
1004
|
+
amount_to_capture: amount,
|
|
1005
|
+
}) : stripeClient.paymentIntents.capture(data["purchaseId"]));
|
|
1006
|
+
if (paymentIntent.status !== "succeeded") {
|
|
1007
|
+
throw new functions.https.HttpsError("aborted", "The Payment capture failed.");
|
|
1008
|
+
}
|
|
1009
|
+
return {
|
|
1010
|
+
purchaseId: data["purchaseId"],
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
catch (err) {
|
|
1014
|
+
const update = {};
|
|
1015
|
+
update["error"] = true;
|
|
1016
|
+
update["errorMessage"] = "The Purchase capture failed. Please replace the billing information and Refresh.";
|
|
1017
|
+
yield doc.ref.set(update, {
|
|
1018
|
+
merge: true,
|
|
1019
|
+
});
|
|
1020
|
+
throw err;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
case "refresh_purchase": {
|
|
1024
|
+
const orderId = query.data.orderId;
|
|
1025
|
+
const userId = query.data.userId;
|
|
1026
|
+
if (!orderId) {
|
|
1027
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1028
|
+
}
|
|
1029
|
+
if (!userId) {
|
|
1030
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1031
|
+
}
|
|
1032
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
1033
|
+
const data = doc.data();
|
|
1034
|
+
if (!data || !data["purchaseId"]) {
|
|
1035
|
+
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
1036
|
+
}
|
|
1037
|
+
if (data["success"]) {
|
|
1038
|
+
throw new functions.https.HttpsError("already-exists", "The payment has already been succeed.");
|
|
1039
|
+
}
|
|
1040
|
+
if (!data["error"]) {
|
|
1041
|
+
return {
|
|
1042
|
+
success: true,
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
const userDoc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}`).get();
|
|
1046
|
+
const userData = userDoc.data();
|
|
1047
|
+
if (!userData || !userData["customer"]) {
|
|
1048
|
+
throw new functions.https.HttpsError("not-found", "The customer id is not found.");
|
|
1049
|
+
}
|
|
1050
|
+
let defaultPayment = userData["defaultPayment"];
|
|
1051
|
+
if (!defaultPayment) {
|
|
1052
|
+
const customer = yield stripeClient.customers.retrieve(userData["customer"]);
|
|
1053
|
+
defaultPayment = customer.invoice_settings.default_payment_method;
|
|
1054
|
+
if (!defaultPayment) {
|
|
1055
|
+
const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get();
|
|
1056
|
+
if (payments.size <= 0) {
|
|
1057
|
+
throw new functions.https.HttpsError("not-found", "The payment method is not found.");
|
|
1058
|
+
}
|
|
1059
|
+
defaultPayment = payments.docs[0].data()["id"];
|
|
1060
|
+
}
|
|
1061
|
+
const update = {};
|
|
1062
|
+
update["defaultPayment"] = defaultPayment;
|
|
1063
|
+
yield userDoc.ref.set(update, {
|
|
1064
|
+
merge: true,
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
if (defaultPayment === data["payment_method"]) {
|
|
1068
|
+
throw new functions.https.HttpsError("failed-precondition", "There was no change in the Payment method.");
|
|
1069
|
+
}
|
|
1070
|
+
yield stripeClient.paymentIntents.update(data["purchaseId"], {
|
|
1071
|
+
payment_method: defaultPayment,
|
|
1072
|
+
});
|
|
1073
|
+
const update = {};
|
|
1074
|
+
update["paymentMethodId"] = defaultPayment;
|
|
1075
|
+
update["error"] = admin.firestore.FieldValue.delete();
|
|
1076
|
+
update["errorMessage"] = admin.firestore.FieldValue.delete();
|
|
947
1077
|
yield doc.ref.set(update, {
|
|
948
1078
|
merge: true,
|
|
949
1079
|
});
|
|
950
1080
|
return {
|
|
951
|
-
|
|
952
|
-
returnUrl: online ? (_28 = (_27 = (_26 = paymentIntent.next_action) === null || _26 === void 0 ? void 0 : _26.redirect_to_url) === null || _27 === void 0 ? void 0 : _27.return_url) !== null && _28 !== void 0 ? _28 : "" : "",
|
|
953
|
-
purchaseId: data["purchaseId"],
|
|
1081
|
+
success: true,
|
|
954
1082
|
};
|
|
955
1083
|
}
|
|
956
|
-
|
|
1084
|
+
case "cancel_purchase": {
|
|
1085
|
+
const orderId = query.data.orderId;
|
|
1086
|
+
const userId = query.data.userId;
|
|
1087
|
+
if (!orderId) {
|
|
1088
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1089
|
+
}
|
|
1090
|
+
if (!userId) {
|
|
1091
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1092
|
+
}
|
|
1093
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
1094
|
+
const data = doc.data();
|
|
1095
|
+
if (!data || !data["purchaseId"]) {
|
|
1096
|
+
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
1097
|
+
}
|
|
1098
|
+
if (data["cancel"]) {
|
|
1099
|
+
throw new functions.https.HttpsError("ok", "The purchase data is already canceled.");
|
|
1100
|
+
}
|
|
1101
|
+
if (data["capture"] || data["success"]) {
|
|
1102
|
+
throw new functions.https.HttpsError("failed-precondition", "The payment has already been completed.");
|
|
1103
|
+
}
|
|
1104
|
+
yield stripeClient.paymentIntents.cancel(data["purchaseId"]);
|
|
957
1105
|
const update = {};
|
|
958
|
-
update["
|
|
959
|
-
update["
|
|
1106
|
+
update["cancel"] = true;
|
|
1107
|
+
update["error"] = admin.firestore.FieldValue.delete();
|
|
1108
|
+
update["errorMessage"] = admin.firestore.FieldValue.delete();
|
|
960
1109
|
yield doc.ref.set(update, {
|
|
961
1110
|
merge: true,
|
|
962
1111
|
});
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
}
|
|
967
|
-
case "capture_purchase": {
|
|
968
|
-
const userId = query.data.userId;
|
|
969
|
-
const orderId = query.data.orderId;
|
|
970
|
-
const amount = parseFloat((_29 = query.data.amount) !== null && _29 !== void 0 ? _29 : 0.0);
|
|
971
|
-
if (!orderId) {
|
|
972
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
973
|
-
}
|
|
974
|
-
if (!userId) {
|
|
975
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
976
|
-
}
|
|
977
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
978
|
-
const data = doc.data();
|
|
979
|
-
if (!data || !data["purchaseId"]) {
|
|
980
|
-
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
981
|
-
}
|
|
982
|
-
if (data["error"]) {
|
|
983
|
-
throw new functions.https.HttpsError("aborted", "The purchase data has some errors");
|
|
984
|
-
}
|
|
985
|
-
if (data["cancel"]) {
|
|
986
|
-
throw new functions.https.HttpsError("cancelled", "This purchase data has already been cancelled.");
|
|
987
|
-
}
|
|
988
|
-
if (!data["confirm"] || !data["verify"]) {
|
|
989
|
-
throw new functions.https.HttpsError("failed-precondition", "The purchase data is not confirmed.");
|
|
990
|
-
}
|
|
991
|
-
if (data["capture"]) {
|
|
992
|
-
throw new functions.https.HttpsError("ok", "The purchase data is already captured.");
|
|
993
|
-
}
|
|
994
|
-
if (data["amount"] < amount) {
|
|
995
|
-
throw new functions.https.HttpsError("invalid-argument", "You cannot capture an amount higher than the billing amount already saved.");
|
|
996
|
-
}
|
|
997
|
-
try {
|
|
998
|
-
const paymentIntent = yield (amount > 0 ? stripeClient.paymentIntents.capture(data["purchaseId"], {
|
|
999
|
-
amount_to_capture: amount,
|
|
1000
|
-
}) : stripeClient.paymentIntents.capture(data["purchaseId"]));
|
|
1001
|
-
if (paymentIntent.status !== "succeeded") {
|
|
1002
|
-
throw new functions.https.HttpsError("aborted", "The Payment capture failed.");
|
|
1112
|
+
return {
|
|
1113
|
+
success: true,
|
|
1114
|
+
};
|
|
1003
1115
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1116
|
+
case "refund_purchase": {
|
|
1117
|
+
const orderId = query.data.orderId;
|
|
1118
|
+
const userId = query.data.userId;
|
|
1119
|
+
const amount = parseFloat((_31 = query.data.amount) !== null && _31 !== void 0 ? _31 : 0.0);
|
|
1120
|
+
if (!orderId) {
|
|
1121
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1122
|
+
}
|
|
1123
|
+
if (!userId) {
|
|
1124
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1125
|
+
}
|
|
1126
|
+
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
1127
|
+
const data = doc.data();
|
|
1128
|
+
if (!data || !data["purchaseId"]) {
|
|
1129
|
+
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
1130
|
+
}
|
|
1131
|
+
if (!data["capture"] || !data["success"]) {
|
|
1132
|
+
throw new functions.https.HttpsError("failed-precondition", "The payment is not yet in your jurisdiction.");
|
|
1133
|
+
}
|
|
1134
|
+
if (data["amount"] < amount) {
|
|
1135
|
+
throw new functions.https.HttpsError("invalid-argument", "The amount to be refunded exceeds the original amount.");
|
|
1136
|
+
}
|
|
1137
|
+
try {
|
|
1138
|
+
if (amount > 0) {
|
|
1139
|
+
yield stripeClient.refunds.create({
|
|
1140
|
+
payment_intent: data["purchaseId"],
|
|
1141
|
+
amount: amount,
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
yield stripeClient.refunds.create({
|
|
1146
|
+
payment_intent: data["purchaseId"],
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
const update = {};
|
|
1150
|
+
update["refund"] = true;
|
|
1151
|
+
update["cancel"] = true;
|
|
1152
|
+
yield doc.ref.set(update, {
|
|
1153
|
+
merge: true,
|
|
1154
|
+
});
|
|
1155
|
+
return {
|
|
1156
|
+
success: true,
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
catch (err) {
|
|
1160
|
+
const update = {};
|
|
1161
|
+
update["error"] = true;
|
|
1162
|
+
update["errorMessage"] = "The Purchase confirmation failed. Please replace the billing information and Refresh.";
|
|
1163
|
+
yield doc.ref.set(update, {
|
|
1164
|
+
merge: true,
|
|
1165
|
+
});
|
|
1166
|
+
throw err;
|
|
1167
|
+
}
|
|
1055
1168
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
const data = doc.data();
|
|
1090
|
-
if (!data || !data["purchaseId"]) {
|
|
1091
|
-
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
1092
|
-
}
|
|
1093
|
-
if (data["cancel"]) {
|
|
1094
|
-
throw new functions.https.HttpsError("ok", "The purchase data is already canceled.");
|
|
1095
|
-
}
|
|
1096
|
-
if (data["capture"] || data["success"]) {
|
|
1097
|
-
throw new functions.https.HttpsError("failed-precondition", "The payment has already been completed.");
|
|
1098
|
-
}
|
|
1099
|
-
yield stripeClient.paymentIntents.cancel(data["purchaseId"]);
|
|
1100
|
-
const update = {};
|
|
1101
|
-
update["cancel"] = true;
|
|
1102
|
-
update["error"] = admin.firestore.FieldValue.delete();
|
|
1103
|
-
update["errorMessage"] = admin.firestore.FieldValue.delete();
|
|
1104
|
-
yield doc.ref.set(update, {
|
|
1105
|
-
merge: true,
|
|
1106
|
-
});
|
|
1107
|
-
return {
|
|
1108
|
-
success: true,
|
|
1109
|
-
};
|
|
1110
|
-
}
|
|
1111
|
-
case "refund_purchase": {
|
|
1112
|
-
const orderId = query.data.orderId;
|
|
1113
|
-
const userId = query.data.userId;
|
|
1114
|
-
const amount = parseFloat((_30 = query.data.amount) !== null && _30 !== void 0 ? _30 : 0.0);
|
|
1115
|
-
if (!orderId) {
|
|
1116
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1117
|
-
}
|
|
1118
|
-
if (!userId) {
|
|
1119
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1120
|
-
}
|
|
1121
|
-
const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get();
|
|
1122
|
-
const data = doc.data();
|
|
1123
|
-
if (!data || !data["purchaseId"]) {
|
|
1124
|
-
throw new functions.https.HttpsError("not-found", "The purchase data is invalid.");
|
|
1125
|
-
}
|
|
1126
|
-
if (!data["capture"] || !data["success"]) {
|
|
1127
|
-
throw new functions.https.HttpsError("failed-precondition", "The payment is not yet in your jurisdiction.");
|
|
1128
|
-
}
|
|
1129
|
-
if (data["amount"] < amount) {
|
|
1130
|
-
throw new functions.https.HttpsError("invalid-argument", "The amount to be refunded exceeds the original amount.");
|
|
1131
|
-
}
|
|
1132
|
-
try {
|
|
1133
|
-
if (amount > 0) {
|
|
1134
|
-
yield stripeClient.refunds.create({
|
|
1135
|
-
payment_intent: data["purchaseId"],
|
|
1136
|
-
amount: amount,
|
|
1169
|
+
case "create_subscription": {
|
|
1170
|
+
const productId = query.data.productId;
|
|
1171
|
+
const orderId = query.data.orderId;
|
|
1172
|
+
const userId = query.data.userId;
|
|
1173
|
+
const count = (_32 = query.data.count) !== null && _32 !== void 0 ? _32 : 1;
|
|
1174
|
+
const successUrl = query.data.successUrl;
|
|
1175
|
+
const cancelUrl = query.data.cancelUrl;
|
|
1176
|
+
if (!orderId) {
|
|
1177
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1178
|
+
}
|
|
1179
|
+
if (!userId) {
|
|
1180
|
+
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1181
|
+
}
|
|
1182
|
+
if (!productId) {
|
|
1183
|
+
throw new functions.https.HttpsError("invalid-argument", "The product id is empty.");
|
|
1184
|
+
}
|
|
1185
|
+
const res = yield stripeClient.checkout.sessions.create({
|
|
1186
|
+
billing_address_collection: "auto",
|
|
1187
|
+
subscription_data: {
|
|
1188
|
+
metadata: {
|
|
1189
|
+
"userId": userId,
|
|
1190
|
+
"orderId": orderId,
|
|
1191
|
+
},
|
|
1192
|
+
},
|
|
1193
|
+
line_items: [
|
|
1194
|
+
{
|
|
1195
|
+
price: productId,
|
|
1196
|
+
quantity: count,
|
|
1197
|
+
},
|
|
1198
|
+
],
|
|
1199
|
+
mode: "subscription",
|
|
1200
|
+
success_url: successUrl,
|
|
1201
|
+
cancel_url: cancelUrl,
|
|
1137
1202
|
});
|
|
1203
|
+
return {
|
|
1204
|
+
endpoint: res["url"],
|
|
1205
|
+
};
|
|
1138
1206
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1207
|
+
case "delete_subscription": {
|
|
1208
|
+
const orderId = query.data.orderId;
|
|
1209
|
+
if (!orderId) {
|
|
1210
|
+
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1211
|
+
}
|
|
1212
|
+
const doc = yield firestoreInstance.doc(`${stripePurchasePath}/${orderId}`).get();
|
|
1213
|
+
if (!doc.exists || !doc.get("subscription")) {
|
|
1214
|
+
throw new functions.https.HttpsError("not-found", "The orderId data is not found");
|
|
1215
|
+
}
|
|
1216
|
+
const res = yield stripeClient.subscriptions.update(doc.get("subscription"), {
|
|
1217
|
+
cancel_at_period_end: true,
|
|
1142
1218
|
});
|
|
1219
|
+
return {
|
|
1220
|
+
success: res["cancel_at_period_end"],
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
default: {
|
|
1224
|
+
throw new functions.https.HttpsError("not-found", "There is no mode:" + query.data.mode);
|
|
1143
1225
|
}
|
|
1144
|
-
const update = {};
|
|
1145
|
-
update["refund"] = true;
|
|
1146
|
-
update["cancel"] = true;
|
|
1147
|
-
yield doc.ref.set(update, {
|
|
1148
|
-
merge: true,
|
|
1149
|
-
});
|
|
1150
|
-
return {
|
|
1151
|
-
success: true,
|
|
1152
|
-
};
|
|
1153
|
-
}
|
|
1154
|
-
catch (err) {
|
|
1155
|
-
const update = {};
|
|
1156
|
-
update["error"] = true;
|
|
1157
|
-
update["errorMessage"] = "The Purchase confirmation failed. Please replace the billing information and Refresh.";
|
|
1158
|
-
yield doc.ref.set(update, {
|
|
1159
|
-
merge: true,
|
|
1160
|
-
});
|
|
1161
|
-
throw err;
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
case "create_subscription": {
|
|
1165
|
-
const productId = query.data.productId;
|
|
1166
|
-
const orderId = query.data.orderId;
|
|
1167
|
-
const userId = query.data.userId;
|
|
1168
|
-
const count = (_31 = query.data.count) !== null && _31 !== void 0 ? _31 : 1;
|
|
1169
|
-
const successUrl = query.data.successUrl;
|
|
1170
|
-
const cancelUrl = query.data.cancelUrl;
|
|
1171
|
-
if (!orderId) {
|
|
1172
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1173
|
-
}
|
|
1174
|
-
if (!userId) {
|
|
1175
|
-
throw new functions.https.HttpsError("invalid-argument", "The user id is empty.");
|
|
1176
|
-
}
|
|
1177
|
-
if (!productId) {
|
|
1178
|
-
throw new functions.https.HttpsError("invalid-argument", "The product id is empty.");
|
|
1179
|
-
}
|
|
1180
|
-
const res = yield stripeClient.checkout.sessions.create({
|
|
1181
|
-
billing_address_collection: "auto",
|
|
1182
|
-
subscription_data: {
|
|
1183
|
-
metadata: {
|
|
1184
|
-
"userId": userId,
|
|
1185
|
-
"orderId": orderId,
|
|
1186
|
-
},
|
|
1187
|
-
},
|
|
1188
|
-
line_items: [
|
|
1189
|
-
{
|
|
1190
|
-
price: productId,
|
|
1191
|
-
quantity: count,
|
|
1192
|
-
},
|
|
1193
|
-
],
|
|
1194
|
-
mode: "subscription",
|
|
1195
|
-
success_url: successUrl,
|
|
1196
|
-
cancel_url: cancelUrl,
|
|
1197
|
-
});
|
|
1198
|
-
return {
|
|
1199
|
-
endpoint: res["url"],
|
|
1200
|
-
};
|
|
1201
|
-
}
|
|
1202
|
-
case "delete_subscription": {
|
|
1203
|
-
const orderId = query.data.orderId;
|
|
1204
|
-
if (!orderId) {
|
|
1205
|
-
throw new functions.https.HttpsError("invalid-argument", "The order id is empty.");
|
|
1206
|
-
}
|
|
1207
|
-
const doc = yield firestoreInstance.doc(`${stripePurchasePath}/${orderId}`).get();
|
|
1208
|
-
if (!doc.exists || !doc.get("subscription")) {
|
|
1209
|
-
throw new functions.https.HttpsError("not-found", "The orderId data is not found");
|
|
1210
1226
|
}
|
|
1211
|
-
const res = yield stripeClient.subscriptions.update(doc.get("subscription"), {
|
|
1212
|
-
cancel_at_period_end: true,
|
|
1213
|
-
});
|
|
1214
|
-
return {
|
|
1215
|
-
success: res["cancel_at_period_end"],
|
|
1216
|
-
};
|
|
1217
1227
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1228
|
+
catch (err) {
|
|
1229
|
+
error = err;
|
|
1220
1230
|
}
|
|
1221
1231
|
}
|
|
1232
|
+
if (error) {
|
|
1233
|
+
console.error(error);
|
|
1234
|
+
throw new functions.https.HttpsError("unknown", "Unknown error.");
|
|
1235
|
+
}
|
|
1222
1236
|
}
|
|
1223
1237
|
catch (err) {
|
|
1224
1238
|
console.error(err);
|