@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
173 lines • 8.49 kB
JavaScript
;
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 firebase_loader_1 = require("../lib/src/firebase_loader");
/**
* Receive and process webhooks for Stripe Connect.
* If you do not use Stripe Connect, do not configure it as a Webhook.
* 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 Connect用のWebhookを受信して処理します。
* Stripe Connectを利用しない場合は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_WEBHOOKCONNECTSECRET
* 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;
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 stripeWebhookConnectSecret = (_d = process.env.PURCHASE_STRIPE_WEBHOOKCONNECTSECRET) !== null && _d !== void 0 ? _d : "";
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, stripeWebhookConnectSecret);
switch (event.type) {
case "account.updated": {
const account = event.data.object;
const id = account["id"];
if (!id) {
res.status(404).send(JSON.stringify({
"error": "The account id is not found.",
}));
return;
}
const col = yield firestoreInstance.collection(`${stripeUserPath}`).where("account", "==", id).get();
if (col.empty) {
res.status(404).send(JSON.stringify({
"error": "The account data is not found.",
}));
return;
}
const update = {};
if (account["capabilities"]) {
const capability = {};
if (account["capabilities"]["card_payments"]) {
capability["card_payments"] = true;
}
if (account["capabilities"]["transfers"]) {
capability["transfers"] = true;
}
update["capability"] = capability;
}
yield col.docs[0].ref.set(update, {
merge: true,
});
res.status(200).send(JSON.stringify({
"success": true,
}));
return;
}
default: {
res.status(404).send(JSON.stringify({
"error": `Event ${event.type} 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;
}
}));
};
//# sourceMappingURL=stripe_webhook_connect.js.map