UNPKG

@mathrunet/masamune

Version:

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

605 lines 32.5 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")); const admin = __importStar(require("firebase-admin")); const firebase_loader_1 = require("../lib/src/firebase_loader"); /** * Receives and processes webhooks from Stripe. * Please register the URL when you deploy this in your Stripe webhook settings. * Firestore integration is a must; please make Firestore available as well. * * StripeからのWebhookを受け取り処理を行います。 * こちらをデプロイした際のURLをStripeのWebhook設定に登録してください。 * Firestoreとの連携が必須です。Firestoreも利用可能にしてください。 * * @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_PAYMENTPATH * Path for payment method information to be placed under [process.env.PURCHASE_STRIPE_USERPATH]. * [process.env.PURCHASE_STRIPE_USERPATH]の下に配置する支払い方法の情報用パス。 * * @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} process.env.PURCHASE_STRIPE_WEBHOOKSECRET * Specify the **Signature Secret** after setting it up as a webhook. * Webhookとして設定したあとの**署名シークレット**を指定します。 * */ 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, _e, _f; 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 stripeWebhookSecret = (_e = process.env.PURCHASE_STRIPE_WEBHOOKSECRET) !== null && _e !== void 0 ? _e : ""; const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId); const stripeClient = new stripe.Stripe(apiKey, { apiVersion: "2025-01-27.acacia", }); const signature = req.headers["stripe-signature"]; if (!signature) { res.status(403).send(JSON.stringify({ "error": "Access denied.", })); return; } const event = stripeClient.webhooks.constructEvent(req.rawBody, signature, stripeWebhookSecret); switch (event.type) { case "payment_intent.requires_action": case "payment_intent.amount_capturable_updated": { const payment = event.data.object; const purchaseId = payment["id"]; const customerId = payment["customer"]; const status = payment["status"]; if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } if (!status) { res.status(404).send(JSON.stringify({ "error": "The status is not found.", })); return; } const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (userCol.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = userCol.docs[0]; const userId = user.id; const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get(); if (purchaseCol.empty) { res.status(404).send(JSON.stringify({ "error": "The purchase data is not found.", })); return; } const purchase = purchaseCol.docs[0]; const update = {}; switch (status) { case "requires_payment_method": case "requires_confirmation": { update["confirm"] = false; update["verify"] = false; update["capture"] = false; update["success"] = false; break; } case "requires_action": { update["confirm"] = true; update["verify"] = false; update["capture"] = false; update["success"] = false; break; } case "requires_capture": { update["confirm"] = true; update["verify"] = true; update["capture"] = false; update["success"] = false; break; } } update["updatedTime"] = new Date(); yield purchase.ref.set(update, { merge: true, }); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "payment_intent.succeeded": { const payment = event.data.object; const purchaseId = payment["id"]; const customerId = payment["customer"]; if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (userCol.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = userCol.docs[0]; const userId = user.id; const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get(); if (purchaseCol.empty) { res.status(404).send(JSON.stringify({ "error": "The purchase data is not found.", })); return; } const purchase = purchaseCol.docs[0]; const update = {}; update["confirm"] = true; update["verify"] = true; update["capture"] = true; update["success"] = true; update["error"] = admin.firestore.FieldValue.delete(); update["errorMessage"] = admin.firestore.FieldValue.delete(); update["updatedTime"] = new Date(); if (payment["charges"] && payment["charges"]["data"] && payment["charges"]["data"].length > 0 && payment["charges"]["data"][0]) { if (payment["charges"]["data"][0]["receipt_url"]) { update["receiptUrl"] = payment["charges"]["data"][0]["receipt_url"]; } if (payment["charges"]["data"][0]["amount_captured"]) { update["capturedAmount"] = payment["charges"]["data"][0]["amount_captured"]; } } yield purchase.ref.set(update, { merge: true, }); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "payment_intent.payment_failed": { const payment = event.data.object; const purchaseId = payment["id"]; const customerId = payment["customer"]; const status = payment["status"]; if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } const userCol = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (userCol.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = userCol.docs[0]; const userId = user.id; const purchaseCol = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePurchasePath}`).where("purchaseId", "==", purchaseId).get(); if (purchaseCol.empty) { res.status(404).send(JSON.stringify({ "error": "The purchase data is not found.", })); return; } const errorMessage = payment["last_payment_error"]["message"]; const purchase = purchaseCol.docs[0]; const update = {}; switch (status) { case "requires_payment_method": case "requires_confirmation": { update["confirm"] = false; update["verify"] = false; update["capture"] = false; update["success"] = false; break; } case "requires_action": { update["confirm"] = true; update["verify"] = false; update["capture"] = false; update["success"] = false; break; } case "requires_capture": { update["confirm"] = true; update["verify"] = true; update["capture"] = false; update["success"] = false; break; } } update["updatedTime"] = new Date(); update["error"] = true; update["errorMessage"] = errorMessage; yield purchase.ref.set(update, { merge: true, }); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "payment_method.detached": case "payment_method.updated": { const payment = event.data.object; const previous = event.data.previous_attributes; let customerId = payment["customer"]; if (!customerId) { customerId = previous["customer"]; } if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (col.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = col.docs[0]; const userId = user.id; yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "customer.updated": { const customer = event.data.object; const customerId = customer["id"]; if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (col.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = col.docs[0]; const userId = user.id; yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "checkout.session.completed": { const session = event.data.object; const customerId = session["customer"]; if (!customerId) { res.status(404).send(JSON.stringify({ "error": "The customer id is not found.", })); return; } const setupIntent = session["setup_intent"]; if (!setupIntent) { res.status(404).send(JSON.stringify({ "error": "The setup intent is not found.", })); return; } const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("customer", "==", customerId).get(); if (col.empty) { res.status(404).send(JSON.stringify({ "error": "The account data is not found.", })); return; } const user = col.docs[0]; const userId = user.id; const update = {}; if (session["setup_intent"]) { update["setupIntent"] = session["setup_intent"]; } yield user.ref.set(update, { merge: true, }); yield syncStripePayment(stripeClient, firestoreInstance, userId, customerId); res.status(200).send(JSON.stringify({ "success": true, })); return; } case "customer.subscription.trial_will_end": case "customer.subscription.updated": case "customer.subscription.created": { const now = new Date(); const update = {}; const subscription = event.data.object; const status = subscription["status"]; if (status != "active") { res.status(200).send(JSON.stringify({ "success": "Subscription is not active.", })); return; } const endDate = new Date(subscription["current_period_end"] * 1000); const id = subscription["id"]; const userId = subscription.metadata.userId; const orderId = (_f = subscription.metadata.orderId) !== null && _f !== void 0 ? _f : id; const targetPath = `${stripePurchasePath}/${orderId}`; const plan = subscription.plan; let doc; const subscriptionCol = yield firestoreInstance.collection(`${stripePurchasePath}`).where("subscription", "==", id).get(); if (subscriptionCol.empty) { doc = yield firestoreInstance.doc(targetPath); } else { doc = subscriptionCol.docs[0].ref; } update["expired"] = now >= endDate; if (userId) { update["user"] = userId; } update["@uid"] = orderId; update["@time"] = new Date(); update["subscription"] = id; update["application"] = subscription["application"]; update["application_fee_percent"] = subscription["application_fee_percent"]; update["cancel_at"] = subscription["cancel_at"]; update["cancel_at_period_end"] = subscription["cancel_at_period_end"]; update["canceled_at"] = subscription["canceled_at"]; update["collection_method"] = subscription["collection_method"]; update["currency"] = subscription["currency"]; update["current_period_start"] = subscription["current_period_start"]; update["current_period_end"] = subscription["current_period_end"]; update["customer"] = subscription["customer"]; update["default_payment_method"] = subscription["default_payment_method"]; update["ended_at"] = subscription["ended_at"]; update["latest_invoice"] = subscription["latest_invoice"]; update["price_id"] = plan["id"]; update["active"] = plan["active"]; update["amount"] = plan["amount"]; update["billing_scheme"] = plan["billing_scheme"]; update["interval"] = plan["interval"]; update["interval_count"] = plan["interval_count"]; update["usage_type"] = plan["usage_type"]; update["quantity"] = subscription["quantity"]; update["start_date"] = subscription["start_date"]; update["start_date"] = subscription["start_date"]; console.log(`Subscription status is ${status} ${stripePurchasePath}.`); yield doc.set(update, { merge: true, }); res.status(200).send(JSON.stringify({ "success": "Subscription is active.", })); break; } case "customer.subscription.deleted": { const subscription = event.data.object; const status = subscription["status"]; console.log(`Subscription status is ${status} ${"expired"}.`); // Then define and call a method to handle the subscription deleted. // handleSubscriptionDeleted(subscriptionDeleted); break; } default: { res.status(404).send(JSON.stringify({ "error": "Event is not found.", })); return; } } } catch (err) { error = err; } } if (error) { console.error(error); throw new functions.https.HttpsError("unknown", "Unknown error."); } } catch (err) { console.error(err); throw err; } })); }; /** * Synchronize Stripe acquisition results to Firestore. * * Stripeの取得結果をFirestoreに同期します。 * * @param {stripe.Stripe} stripeClient * Stripe client instances. * ストライプのクライアントインスタンス。 * * @param {FirebaseFirestore.Firestore} firestoreInstance * Firestore client instances. * Firestoreのクライアントインスタンス。 * * @param {string} userId * User ID. * ユーザーID。 * * @param {string} customerId * Customer ID. * 顧客ID。 * * @returns {Promise<void>} * */ function syncStripePayment(stripeClient, firestoreInstance, userId, customerId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const stripeUserPath = (_a = process.env.PURCHASE_STRIPE_USERPATH) !== null && _a !== void 0 ? _a : "plugins/stripe/user"; const stripePaymentPath = (_b = process.env.PURCHASE_STRIPE_PAYMENTPATH) !== null && _b !== void 0 ? _b : "payment"; const customer = yield stripeClient.customers.retrieve(customerId); let defaultSource = customer.invoice_settings.default_payment_method; const paymentMethods = yield stripeClient.customers.listPaymentMethods(customerId, { type: "card", }); if (!defaultSource && paymentMethods.data.length > 0) { defaultSource = paymentMethods.data[0].id; yield stripeClient.customers.update(customerId, { invoice_settings: { default_payment_method: defaultSource, }, }); } const payments = yield firestoreInstance.collection(`${stripeUserPath}/${userId}/${stripePaymentPath}`).get(); yield firestoreInstance.runTransaction((transaction) => __awaiter(this, void 0, void 0, function* () { payments.docs.forEach((doc) => { const data = doc.data(); const method = paymentMethods.data.find((m) => m.id == data["id"]); if (!method) { transaction.delete(doc.ref); } else { const card = method.card; if (!card) { return; } const update = {}; const isDefault = method.id == defaultSource; if (method.type === data["type"] && card.exp_month === data["expMonth"] && card.exp_year === data["expYear"] && card.brand === data["brand"] && card.last4 === data["numberLast"] && card.last4 === data["numberLast"] && isDefault === data["default"]) { return; } update["type"] = method.type; update["expMonth"] = card.exp_month; update["expYear"] = card.exp_year; update["brand"] = card.brand; update["numberLast"] = card.last4; update["default"] = isDefault; transaction.set(doc.ref, update, { merge: true, }); } }); paymentMethods.data.forEach((method) => __awaiter(this, void 0, void 0, function* () { const card = method.card; if (!card) { return; } const doc = payments.docs.find((item) => item.data()["id"] == method.id); if (doc) { return; } const uid = method.id; const update = {}; const isDefault = method.id == defaultSource; update["@uid"] = uid; update["@time"] = new Date(); update["id"] = uid; update["type"] = method.type; update["expMonth"] = card.exp_month; update["expYear"] = card.exp_year; update["brand"] = card.brand; update["numberLast"] = card.last4; update["default"] = isDefault; transaction.set(firestoreInstance.doc(`${stripeUserPath}/${userId}/${stripePaymentPath}/${uid}`), update, { merge: true, }); })); const update = {}; if (defaultSource) { update["defaultPayment"] = defaultSource; } else { update["defaultPayment"] = admin.firestore.FieldValue.delete(); } yield transaction.set(firestoreInstance.doc(`${stripeUserPath}/${userId}`), update, { merge: true, }); })); }); } //# sourceMappingURL=stripe_webhook.js.map