UNPKG

@mathrunet/masamune

Version:

Manages packages for the server portion (NodeJS) of the Masamune framework.

182 lines 8.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const functions = __importStar(require("firebase-functions/v2")); const stripe = __importStar(require("stripe")); require("../lib/exntensions/string.extension"); const firebase_loader_1 = require("../lib/src/firebase_loader"); /** * Webhook for proper redirection when 3D Secure authentication is required. * Please set here for [returnUrl]. * * 3Dセキュア認証が必要な場合、適切なリダイレクトを行うためのWebhookです。 * [returnUrl]にこちらを設定してください。 * * @param {string} process.env.PURCHASE_STRIPE_SECRETKEY * API key (secret key) to connect to Stripe. * Log in to the following URL and create a project. * After the project is created, the secret key can be copied. * * Stripeへ接続するためのAPIキー(シークレットキー)。 * 下記URLにログインし、プロジェクトを作成します。 * プロジェクト作成後、シークレットキーをコピーすることができます。 * * Production environment * https://dashboard.stripe.com/apikeys * Development enveironment * https://dashboard.stripe.com/test/apikeys * * @param {string} process.env.PURCHASE_STRIPE_USERPATH * Stripe user (customer) pass. * Stripeのユーザー(カスタマー)用パス。 * * @param {string} process.env.PURCHASE_STRIPE_PURCHASEPATH * Path for purchase information to be placed under [process.env.PURCHASE_STRIPE_USERPATH]. * [process.env.PURCHASE_STRIPE_USERPATH]の下に配置する購入情報用パス。 * * @param {string} userId * [required] * Specify the purchaser's user ID. * 購入者のユーザーIDを指定します。 * * @param {string} orderId * [required] * Specify the order ID. This will be the database key. * 注文IDを指定します。これがデータベースのキーとなります。 * * @param {string} successUrl * [required] * Specify the URL to redirect to upon success due to browser settings, etc. * ブラウザの設定等による成功時のリダイレクト先URLを指定します。 * * @param {string} failureUrl * [required] * Specify the URL to redirect to upon failure due to browser settings, etc. * ブラウザの設定等による失敗時のリダイレクト先URLを指定します。 * */ module.exports = (regions, options, data) => { var _a, _b; return functions.https.onRequest({ region: (_a = options.region) !== null && _a !== void 0 ? _a : regions, timeoutSeconds: options.timeoutSeconds, memory: options.memory, minInstances: options.minInstances, concurrency: options.concurrency, maxInstances: options.maxInstances, serviceAccount: (_b = options.serviceAccount) !== null && _b !== void 0 ? _b : undefined, }, (req, res) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d; try { let error = null; const firestoreDatabaseIds = (_a = options.firestoreDatabaseIds) !== null && _a !== void 0 ? _a : [""]; for (const databaseId of firestoreDatabaseIds) { try { const apiKey = (_b = process.env.PURCHASE_STRIPE_SECRETKEY) !== null && _b !== void 0 ? _b : ""; const stripeUserPath = (_c = process.env.PURCHASE_STRIPE_USERPATH) !== null && _c !== void 0 ? _c : "plugins/stripe/user"; const stripePurchasePath = (_d = process.env.PURCHASE_STRIPE_PURCHASEPATH) !== null && _d !== void 0 ? _d : "purchase"; const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId); const stripeClient = new stripe.Stripe(apiKey, { apiVersion: "2025-01-27.acacia", }); const token = req.query.token; if (!token || typeof token !== "string") { res.status(403).send(JSON.stringify({ "error": "Invalid parameters", })); return; } const param = JSON.parse(token.decrypt({ key: apiKey.slice(0, 32), ivKey: apiKey.slice(-16), })); if (!param["userId"] || !param["orderId"] || !param["successUrl"] || !param["failureUrl"]) { res.status(403).send(JSON.stringify({ "error": "Invalid parameters", })); return; } const userId = param["userId"]; const orderId = param["orderId"]; const successUrl = param["successUrl"]; const failureUrl = param["failureUrl"]; const doc = yield firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePurchasePath}/${orderId}`).get(); const data = doc.data(); if (!data || !data["paymentId"]) { res.status(404).send(JSON.stringify({ "error": "The purchase data is not found.", })); return; } const purchase = yield stripeClient.paymentIntents.retrieve(data["paymentId"]); if (!purchase) { res.status(404).send(JSON.stringify({ "error": "The purchase data is not found.", })); return; } const status = purchase.status; if (status === "requires_capture" || status === "succeeded" || status === "processing") { res.redirect(successUrl); } else { res.redirect(failureUrl); } } catch (err) { error = err; } } if (error) { console.error(error); throw new functions.https.HttpsError("unknown", "Unknown error."); } } catch (err) { console.error(err); throw err; } })); }; //# sourceMappingURL=stripe_webhook_secure.js.map