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