feat: add advanced analytics and territory management system

- Add comprehensive analytics components with export functionality
- Implement territory management with manager performance tracking
- Add seatmap components for venue layout management
- Create customer management features with modal interface
- Add advanced hooks for dashboard flags and territory data
- Implement seat selection and venue management utilities
- Add type definitions for ticketing and seatmap systems

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-26 09:25:10 -06:00
parent d5c3953888
commit aa81eb5adb
438 changed files with 90509 additions and 2787 deletions

View File

@@ -0,0 +1,125 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.api = void 0;
const https_1 = require("firebase-functions/v2/https");
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const app = (0, express_1.default)();
// CORS: allow hosting origins + dev
const allowedOrigins = [
// Firebase Hosting URLs for dev-racer-433015-k3 project
"https://dev-racer-433015-k3.web.app",
"https://dev-racer-433015-k3.firebaseapp.com",
// Development servers
"http://localhost:5173", // Vite dev server
"http://localhost:4173", // Vite preview
"http://localhost:3000", // Common dev port
];
app.use((0, cors_1.default)({
origin: (origin, callback) => {
// Allow requests with no origin (mobile apps, curl, etc.)
if (!origin)
{return callback(null, true);}
if (allowedOrigins.includes(origin)) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
},
credentials: true
}));
app.use(express_1.default.json({ limit: "2mb" }));
app.use(express_1.default.urlencoded({ extended: true }));
// Health check endpoint
app.get("/health", (req, res) => {
res.json({
status: "ok",
timestamp: new Date().toISOString(),
version: "1.0.0",
message: "API is running"
});
});
// Mock ticket verification endpoint
app.post("/tickets/verify", (req, res) => {
const { qr } = req.body;
if (!qr) {
return res.status(400).json({ error: "QR code is required" });
}
// Mock response for demo
return res.json({
valid: true,
ticket: {
id: "demo-ticket-001",
eventId: "demo-event-001",
ticketTypeId: "demo-type-001",
eventName: "Demo Event",
ticketTypeName: "General Admission",
status: "valid",
purchaserEmail: "demo@example.com"
}
});
});
// Mock checkout endpoint
app.post("/checkout/create", (req, res) => {
const { orgId, eventId, ticketTypeId, qty } = req.body;
if (!orgId || !eventId || !ticketTypeId || !qty) {
return res.status(400).json({ error: "Missing required fields" });
}
// Mock Stripe checkout session
return res.json({
id: "cs_test_demo123",
url: "https://checkout.stripe.com/pay/cs_test_demo123#fidkdWxOYHwnPyd1blppbHNgWjA0VGlgNG41PDVUc0t8Zn0xQnVTSDc2N01ocGRnVH1KMjZCMX9pPUBCZzJpPVE2TnQ3U1J%2FYmFRPTVvSU1qZW9EV1IzTmBAQkxmdFNncGNyZmU0Z0I9NV9WPT0nKSd3YGNgd3dgd0p3bGZsayc%2FcXdwYHgl"
});
});
// Mock Stripe Connect endpoints
app.post("/stripe/connect/start", (req, res) => {
const { orgId } = req.body;
if (!orgId) {
return res.status(400).json({ error: "Organization ID is required" });
}
return res.json({
url: "https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_demo&scope=read_write"
});
});
app.get("/stripe/connect/status", (req, res) => {
const {orgId} = req.query;
if (!orgId) {
return res.status(400).json({ error: "Organization ID is required" });
}
return res.json({
connected: false,
accountId: null,
chargesEnabled: false,
detailsSubmitted: false
});
});
// Catch-all for unmatched routes
app.use("*", (req, res) => {
res.status(404).json({
error: "Not found",
path: req.originalUrl,
availableEndpoints: [
"GET /api/health",
"POST /api/tickets/verify",
"POST /api/checkout/create",
"POST /api/stripe/connect/start",
"GET /api/stripe/connect/status"
]
});
});
// Error handling middleware
app.use((error, req, res, next) => {
console.error('Express error:', error);
res.status(500).json({
error: 'Internal server error',
message: error.message
});
});
exports.api = (0, https_1.onRequest)({
region: "us-central1",
maxInstances: 10,
cors: true
}, app);
// # sourceMappingURL=api-simple.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-simple.js","sourceRoot":"","sources":["../src/api-simple.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAwD;AACxD,sDAA8B;AAC9B,gDAAwB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EAAE,GAAqB,EAAE,IAA0B,EAAE,EAAE;IAChG,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC;IACvC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;QACnB,KAAK,EAAE,uBAAuB;QAC9B,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEU,QAAA,GAAG,GAAG,IAAA,iBAAS,EAC1B;IACE,MAAM,EAAE,aAAa;IACrB,YAAY,EAAE,EAAE;IAChB,IAAI,EAAE,IAAI;CACX,EACD,GAAG,CACJ,CAAC"}

View File

@@ -0,0 +1,157 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.api = void 0;
const https_1 = require("firebase-functions/v2/https");
const logger_1 = require("./logger");
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
// Import all individual function handlers
const verify_1 = require("./verify");
const checkout_1 = require("./checkout");
const stripeConnect_1 = require("./stripeConnect");
const claims_1 = require("./claims");
const domains_1 = require("./domains");
const orders_1 = require("./orders");
const refunds_1 = require("./refunds");
const disputes_1 = require("./disputes");
const reconciliation_1 = require("./reconciliation");
const app = (0, express_1.default)();
// CORS: allow hosting origins + dev
const allowedOrigins = [
// Add your actual Firebase project URLs here
"https://your-project-id.web.app",
"https://your-project-id.firebaseapp.com",
"http://localhost:5173", // Vite dev server
"http://localhost:4173", // Vite preview
"http://localhost:3000", // Common dev port
];
app.use((0, cors_1.default)({
origin: (origin, callback) => {
// Allow requests with no origin (mobile apps, curl, etc.)
if (!origin)
{return callback(null, true);}
if (allowedOrigins.includes(origin)) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
},
credentials: true
}));
app.use(express_1.default.json({ limit: "2mb" }));
app.use(express_1.default.urlencoded({ extended: true }));
// Middleware to log API requests
app.use((req, res, next) => {
logger_1.logger.info(`API Request: ${req.method} ${req.path}`);
next();
});
// Helper function to wrap Firebase Functions for Express
const wrapFirebaseFunction = (fn) => async (req, res) => {
try {
// Create mock Firebase Functions request/response objects
const mockReq = {
...req,
method: req.method,
body: req.body,
query: req.query,
headers: req.headers,
get: (header) => req.get(header),
};
const mockRes = {
...res,
status: (code) => {
res.status(code);
return mockRes;
},
json: (data) => {
res.json(data);
return mockRes;
},
send: (data) => {
res.send(data);
return mockRes;
},
setHeader: (name, value) => {
res.setHeader(name, value);
return mockRes;
}
};
// Call the original Firebase Function
await fn.options.handler(mockReq, mockRes);
}
catch (error) {
logger_1.logger.error('Function wrapper error:', error);
res.status(500).json({
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
};
// Wire up all endpoints under /api
// Ticket verification
app.post("/tickets/verify", wrapFirebaseFunction(verify_1.verifyTicket));
app.get("/tickets/verify/:qr", wrapFirebaseFunction(verify_1.verifyTicket));
// Checkout endpoints
app.post("/checkout/create", wrapFirebaseFunction(checkout_1.createCheckout));
app.post("/stripe/checkout/create", wrapFirebaseFunction(stripeConnect_1.createStripeCheckout));
// Stripe Connect endpoints
app.post("/stripe/connect/start", wrapFirebaseFunction(stripeConnect_1.stripeConnectStart));
app.get("/stripe/connect/status", wrapFirebaseFunction(stripeConnect_1.stripeConnectStatus));
// Orders
app.get("/orders/:orderId", wrapFirebaseFunction(orders_1.getOrder));
// Refunds
app.post("/refunds/create", wrapFirebaseFunction(refunds_1.createRefund));
app.get("/orders/:orderId/refunds", wrapFirebaseFunction(refunds_1.getOrderRefunds));
// Disputes
app.get("/orders/:orderId/disputes", wrapFirebaseFunction(disputes_1.getOrderDisputes));
// Claims management
app.get("/claims/:uid", wrapFirebaseFunction(claims_1.getUserClaims));
app.post("/claims/update", wrapFirebaseFunction(claims_1.updateUserClaims));
// Domain management
app.post("/domains/resolve", wrapFirebaseFunction(domains_1.resolveDomain));
app.post("/domains/verify-request", wrapFirebaseFunction(domains_1.requestDomainVerification));
app.post("/domains/verify", wrapFirebaseFunction(domains_1.verifyDomain));
// Reconciliation
app.get("/reconciliation/data", wrapFirebaseFunction(reconciliation_1.getReconciliationData));
app.get("/reconciliation/events", wrapFirebaseFunction(reconciliation_1.getReconciliationEvents));
// Health check
app.get("/health", (req, res) => {
res.json({
status: "ok",
timestamp: new Date().toISOString(),
version: "1.0.0"
});
});
// Stripe webhooks (these need raw body, so they stay separate - see firebase.json)
// Note: These will be handled by separate functions due to raw body requirements
// Catch-all for unmatched routes
app.use("*", (req, res) => {
res.status(404).json({
error: "Not found",
path: req.originalUrl,
availableEndpoints: [
"POST /api/tickets/verify",
"GET /api/tickets/verify/:qr",
"POST /api/checkout/create",
"POST /api/stripe/connect/start",
"GET /api/stripe/connect/status",
"GET /api/health"
]
});
});
// Error handling middleware
app.use((error, req, res, next) => {
logger_1.logger.error('Express error:', error);
res.status(500).json({
error: 'Internal server error',
message: error.message
});
});
exports.api = (0, https_1.onRequest)({
region: "us-central1",
maxInstances: 10,
cors: true
}, app);
// # sourceMappingURL=api.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAwD;AACxD,qCAAkC;AAClC,sDAA8B;AAC9B,gDAAwB;AAExB,0CAA0C;AAC1C,qCAAwC;AACxC,yCAA4C;AAC5C,mDAAgG;AAChG,qCAA2D;AAC3D,uCAAmF;AACnF,qCAAoC;AACpC,uCAA0D;AAC1D,yCAA8C;AAC9C,qDAAkF;AAElF,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAC;AAEtB,oCAAoC;AACpC,MAAM,cAAc,GAAG;IACrB,6CAA6C;IAC7C,iCAAiC;IACjC,yCAAyC;IACzC,uBAAuB,EAAE,kBAAkB;IAC3C,uBAAuB,EAAE,eAAe;IACxC,uBAAuB,EAAE,kBAAkB;CAC5C,CAAC;AAEF,GAAG,CAAC,GAAG,CAAC,IAAA,cAA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CAAC,CAAC;AAEU,QAAA,GAAG,GAAG,IAAA,iBAAS,EAC1B;IACE,MAAM,EAAE,aAAa;IACrB,YAAY,EAAE,EAAE;IAChB,IAAI,EAAE,IAAI;CACX,EACD,GAAG,CACJ,CAAC"}

View File

@@ -0,0 +1,196 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCheckout = void 0;
const https_1 = require("firebase-functions/v2/https");
const firebase_functions_1 = require("firebase-functions");
const firestore_1 = require("firebase-admin/firestore");
const stripe_1 = __importDefault(require("stripe"));
const stripe = new stripe_1.default(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2024-11-20.acacia",
});
const db = (0, firestore_1.getFirestore)();
const PLATFORM_FEE_BPS = parseInt(process.env.PLATFORM_FEE_BPS || "300");
/**
* Creates a Stripe Checkout Session for a connected account
* POST /api/checkout/create
*/
exports.createCheckout = (0, https_1.onRequest)({
cors: true,
enforceAppCheck: false,
region: "us-central1",
}, async (req, res) => {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
try {
const { orgId, eventId, ticketTypeId, qty, purchaserEmail, successUrl, cancelUrl, } = req.body;
// Validate input
if (!orgId || !eventId || !ticketTypeId || !qty || qty <= 0) {
res.status(400).json({
error: "Missing required fields: orgId, eventId, ticketTypeId, qty",
});
return;
}
if (!successUrl || !cancelUrl) {
res.status(400).json({
error: "Missing required URLs: successUrl, cancelUrl",
});
return;
}
firebase_functions_1.logger.info("Creating checkout session", {
orgId,
eventId,
ticketTypeId,
qty,
purchaserEmail: purchaserEmail ? "provided" : "not provided",
});
// Get organization payment info
const orgDoc = await db.collection("orgs").doc(orgId).get();
if (!orgDoc.exists) {
res.status(404).json({ error: "Organization not found" });
return;
}
const orgData = orgDoc.data();
const stripeAccountId = orgData.payment?.stripe?.accountId;
if (!stripeAccountId) {
res.status(400).json({
error: "Organization has no connected Stripe account",
});
return;
}
// Validate account is properly onboarded
if (!orgData.payment?.stripe?.chargesEnabled) {
res.status(400).json({
error: "Stripe account is not ready to accept payments",
});
return;
}
// Get event
const eventDoc = await db.collection("events").doc(eventId).get();
if (!eventDoc.exists) {
res.status(404).json({ error: "Event not found" });
return;
}
const eventData = eventDoc.data();
if (eventData.orgId !== orgId) {
res.status(403).json({ error: "Event does not belong to organization" });
return;
}
// Get ticket type
const ticketTypeDoc = await db.collection("ticket_types").doc(ticketTypeId).get();
if (!ticketTypeDoc.exists) {
res.status(404).json({ error: "Ticket type not found" });
return;
}
const ticketTypeData = ticketTypeDoc.data();
if (ticketTypeData.orgId !== orgId || ticketTypeData.eventId !== eventId) {
res.status(403).json({
error: "Ticket type does not belong to organization/event",
});
return;
}
// Check inventory
const available = ticketTypeData.inventory - (ticketTypeData.sold || 0);
if (available < qty) {
res.status(400).json({
error: `Not enough tickets available. Requested: ${qty}, Available: ${available}`,
});
return;
}
// Calculate application fee
const subtotal = ticketTypeData.priceCents * qty;
const applicationFeeAmount = Math.round((subtotal * PLATFORM_FEE_BPS) / 10000);
firebase_functions_1.logger.info("Checkout calculation", {
priceCents: ticketTypeData.priceCents,
qty,
subtotal,
platformFeeBps: PLATFORM_FEE_BPS,
applicationFeeAmount,
});
// Create Stripe Checkout Session
const session = await stripe.checkout.sessions.create({
mode: "payment",
payment_method_types: ["card"],
customer_email: purchaserEmail || undefined,
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: `${eventData.name} ${ticketTypeData.name}`,
description: `Tickets for ${eventData.name}`,
},
unit_amount: ticketTypeData.priceCents,
},
quantity: qty,
},
],
success_url: `${successUrl}?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: cancelUrl,
metadata: {
orgId,
eventId,
ticketTypeId,
qty: String(qty),
purchaserEmail: purchaserEmail || "",
},
payment_intent_data: {
application_fee_amount: applicationFeeAmount,
metadata: {
orgId,
eventId,
ticketTypeId,
qty: String(qty),
},
},
}, { stripeAccount: stripeAccountId });
// Create placeholder order for UI polling
const orderData = {
orgId,
eventId,
ticketTypeId,
qty,
sessionId: session.id,
status: "pending",
totalCents: subtotal,
createdAt: new Date(),
purchaserEmail: purchaserEmail || null,
paymentIntentId: null,
stripeAccountId,
};
await db.collection("orders").doc(session.id).set(orderData);
firebase_functions_1.logger.info("Checkout session created", {
sessionId: session.id,
url: session.url,
orgId,
eventId,
stripeAccountId,
});
const response = {
url: session.url,
sessionId: session.id,
};
res.status(200).json(response);
}
catch (error) {
firebase_functions_1.logger.error("Error creating checkout session", {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
if (error instanceof stripe_1.default.errors.StripeError) {
res.status(400).json({
error: `Stripe error: ${error.message}`,
code: error.code,
});
return;
}
res.status(500).json({
error: "Internal server error creating checkout session",
});
}
});
// # sourceMappingURL=checkout.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,187 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUserClaims = exports.updateUserClaims = void 0;
const app_1 = require("firebase-admin/app");
const auth_1 = require("firebase-admin/auth");
const firestore_1 = require("firebase-admin/firestore");
const https_1 = require("firebase-functions/v2/https");
const v2_1 = require("firebase-functions/v2");
// Initialize Firebase Admin if not already initialized
if ((0, app_1.getApps)().length === 0) {
(0, app_1.initializeApp)();
}
(0, v2_1.setGlobalOptions)({
region: "us-central1",
});
const auth = (0, auth_1.getAuth)();
const db = (0, firestore_1.getFirestore)();
// Helper function to validate authorization
async function validateAuthorization(req) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
throw new Error('Unauthorized: Missing or invalid authorization header');
}
const idToken = authHeader.split('Bearer ')[1];
const decodedToken = await auth.verifyIdToken(idToken);
const { orgId, role, territoryIds } = decodedToken;
return {
uid: decodedToken.uid,
orgId,
role,
territoryIds: territoryIds || []
};
}
// Helper function to check if user can manage claims for target org
function canManageClaims(user, targetOrgId) {
// Superadmin can manage any org
if (user.role === 'superadmin') {
return true;
}
// OrgAdmin can only manage their own org
if (user.role === 'orgAdmin' && user.orgId === targetOrgId) {
return true;
}
return false;
}
// POST /api/admin/users/:uid/claims
exports.updateUserClaims = (0, https_1.onRequest)({ cors: true }, async (req, res) => {
try {
// Only allow POST requests
if (req.method !== 'POST') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
// Validate authorization
const authUser = await validateAuthorization(req);
// Extract target user ID from path
const targetUid = req.params.uid;
if (!targetUid) {
res.status(400).json({ error: 'Missing user ID in path' });
return;
}
// Parse request body
const { orgId, role, territoryIds } = req.body;
if (!orgId || !role || !Array.isArray(territoryIds)) {
res.status(400).json({
error: 'Missing required fields: orgId, role, territoryIds'
});
return;
}
// Validate role
const validRoles = ['superadmin', 'orgAdmin', 'territoryManager', 'staff'];
if (!validRoles.includes(role)) {
res.status(400).json({
error: `Invalid role. Must be one of: ${ validRoles.join(', ')}`
});
return;
}
// Check authorization
if (!canManageClaims(authUser, orgId)) {
res.status(403).json({
error: 'Insufficient permissions to manage claims for this organization'
});
return;
}
// Validate territories exist in the org
if (territoryIds.length > 0) {
const territoryChecks = await Promise.all(territoryIds.map(async (territoryId) => {
const territoryDoc = await db.collection('territories').doc(territoryId).get();
return territoryDoc.exists && territoryDoc.data()?.orgId === orgId;
}));
if (territoryChecks.some(valid => !valid)) {
res.status(400).json({
error: 'One or more territory IDs are invalid or not in the specified organization'
});
return;
}
}
// Set custom user claims
const customClaims = {
orgId,
role,
territoryIds
};
await auth.setCustomUserClaims(targetUid, customClaims);
// Update user document in Firestore for UI consistency
await db.collection('users').doc(targetUid).set({
orgId,
role,
territoryIds,
updatedAt: new Date().toISOString(),
updatedBy: authUser.uid
}, { merge: true });
res.status(200).json({
success: true,
claims: customClaims,
message: 'User claims updated successfully'
});
}
catch (error) {
console.error('Error updating user claims:', error);
if (error instanceof Error) {
if (error.message.includes('Unauthorized')) {
res.status(401).json({ error: error.message });
}
else if (error.message.includes('not found')) {
res.status(404).json({ error: 'User not found' });
}
else {
res.status(500).json({ error: 'Internal server error' });
}
}
else {
res.status(500).json({ error: 'Internal server error' });
}
}
});
// GET /api/admin/users/:uid/claims
exports.getUserClaims = (0, https_1.onRequest)({ cors: true }, async (req, res) => {
try {
// Only allow GET requests
if (req.method !== 'GET') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
// Validate authorization
const authUser = await validateAuthorization(req);
// Extract target user ID from path
const targetUid = req.params.uid;
if (!targetUid) {
res.status(400).json({ error: 'Missing user ID in path' });
return;
}
// Get user record
const userRecord = await auth.getUser(targetUid);
const claims = userRecord.customClaims || {};
// Check if user can view these claims
if (claims.orgId && !canManageClaims(authUser, claims.orgId)) {
res.status(403).json({
error: 'Insufficient permissions to view claims for this user'
});
return;
}
res.status(200).json({
uid: targetUid,
email: userRecord.email,
claims
});
}
catch (error) {
console.error('Error getting user claims:', error);
if (error instanceof Error) {
if (error.message.includes('Unauthorized')) {
res.status(401).json({ error: error.message });
}
else if (error.message.includes('not found')) {
res.status(404).json({ error: 'User not found' });
}
else {
res.status(500).json({ error: 'Internal server error' });
}
}
else {
res.status(500).json({ error: 'Internal server error' });
}
}
});
// # sourceMappingURL=claims.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,399 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrderDisputes = void 0;
exports.handleDisputeCreated = handleDisputeCreated;
exports.handleDisputeClosed = handleDisputeClosed;
const https_1 = require("firebase-functions/v2/https");
const app_1 = require("firebase-admin/app");
const firestore_1 = require("firebase-admin/firestore");
const stripe_1 = __importDefault(require("stripe"));
const uuid_1 = require("uuid");
// Initialize Firebase Admin if not already initialized
try {
(0, app_1.initializeApp)();
}
catch (error) {
// App already initialized
}
const db = (0, firestore_1.getFirestore)();
// Initialize Stripe
const stripe = new stripe_1.default(process.env.STRIPE_SECRET_KEY || "", {
apiVersion: "2024-06-20",
});
/**
* Helper function to create ledger entry
*/
async function createLedgerEntry(entry, transaction) {
const ledgerEntry = {
...entry,
createdAt: firestore_1.Timestamp.now(),
};
const entryId = (0, uuid_1.v4)();
const docRef = db.collection("ledger").doc(entryId);
if (transaction) {
transaction.set(docRef, ledgerEntry);
}
else {
await docRef.set(ledgerEntry);
}
}
/**
* Helper function to find order by payment intent or charge ID
*/
async function findOrderByStripeData(paymentIntentId, chargeId) {
try {
let orderSnapshot;
if (paymentIntentId) {
orderSnapshot = await db.collection("orders")
.where("paymentIntentId", "==", paymentIntentId)
.limit(1)
.get();
}
if (orderSnapshot?.empty && chargeId) {
// Try to find by charge ID (stored in metadata or retrieved from Stripe)
orderSnapshot = await db.collection("orders")
.where("stripe.chargeId", "==", chargeId)
.limit(1)
.get();
}
if (orderSnapshot?.empty) {
return null;
}
const orderDoc = orderSnapshot.docs[0];
return {
orderId: orderDoc.id,
orderData: orderDoc.data(),
};
}
catch (error) {
console.error("Error finding order by Stripe data:", error);
return null;
}
}
/**
* Helper function to update ticket statuses
*/
async function updateTicketStatusesForOrder(orderId, newStatus, transaction) {
try {
const ticketsSnapshot = await db.collection("tickets")
.where("orderId", "==", orderId)
.get();
let updatedCount = 0;
for (const ticketDoc of ticketsSnapshot.docs) {
const ticketData = ticketDoc.data();
const currentStatus = ticketData.status;
// Only update tickets that can be changed
if (newStatus === "locked_dispute") {
// Lock all issued or scanned tickets
if (["issued", "scanned"].includes(currentStatus)) {
const updates = {
status: newStatus,
previousStatus: currentStatus,
updatedAt: firestore_1.Timestamp.now(),
};
if (transaction) {
transaction.update(ticketDoc.ref, updates);
}
else {
await ticketDoc.ref.update(updates);
}
updatedCount++;
}
}
else if (newStatus === "void") {
// Void locked dispute tickets
if (currentStatus === "locked_dispute") {
const updates = {
status: newStatus,
updatedAt: firestore_1.Timestamp.now(),
};
if (transaction) {
transaction.update(ticketDoc.ref, updates);
}
else {
await ticketDoc.ref.update(updates);
}
updatedCount++;
}
}
else if (currentStatus === "locked_dispute") {
// Restore tickets from dispute lock
const restoreStatus = ticketData.previousStatus || "issued";
const updates = {
status: restoreStatus,
previousStatus: undefined,
updatedAt: firestore_1.Timestamp.now(),
};
if (transaction) {
transaction.update(ticketDoc.ref, updates);
}
else {
await ticketDoc.ref.update(updates);
}
updatedCount++;
}
}
return updatedCount;
}
catch (error) {
console.error("Error updating ticket statuses:", error);
return 0;
}
}
/**
* Handles charge.dispute.created webhook
*/
async function handleDisputeCreated(dispute, stripeAccountId) {
const action = "dispute_created";
const startTime = Date.now();
try {
console.log(`[${action}] Processing dispute created`, {
disputeId: dispute.id,
chargeId: dispute.charge,
amount: dispute.amount,
reason: dispute.reason,
status: dispute.status,
stripeAccountId,
});
// Get charge details to find payment intent
const charge = await stripe.charges.retrieve(dispute.charge, {
stripeAccount: stripeAccountId,
});
const paymentIntentId = charge.payment_intent;
// Find the order
const orderResult = await findOrderByStripeData(paymentIntentId, charge.id);
if (!orderResult) {
console.error(`[${action}] Order not found for dispute`, {
disputeId: dispute.id,
paymentIntentId,
chargeId: charge.id,
});
return;
}
const { orderId, orderData } = orderResult;
const { orgId, eventId } = orderData;
console.log(`[${action}] Found order for dispute`, {
orderId,
orgId,
eventId,
});
// Process dispute in transaction
await db.runTransaction(async (transaction) => {
// Lock tickets related to this order
const ticketsUpdated = await updateTicketStatusesForOrder(orderId, "locked_dispute", transaction);
console.log(`[${action}] Locked ${ticketsUpdated} tickets for dispute`, {
orderId,
disputeId: dispute.id,
});
// Create dispute fee ledger entry if there's a fee
if (dispute.balance_transactions && dispute.balance_transactions.length > 0) {
for (const balanceTxn of dispute.balance_transactions) {
if (balanceTxn.fee > 0) {
await createLedgerEntry({
orgId,
eventId,
orderId,
type: "dispute_fee",
amountCents: -balanceTxn.fee, // Negative because it's a cost
currency: "USD",
stripe: {
balanceTxnId: balanceTxn.id,
chargeId: charge.id,
disputeId: dispute.id,
accountId: stripeAccountId,
},
meta: {
disputeReason: dispute.reason,
disputeStatus: dispute.status,
},
}, transaction);
}
}
}
// Update order with dispute information
const orderRef = db.collection("orders").doc(orderId);
transaction.update(orderRef, {
"dispute.disputeId": dispute.id,
"dispute.status": dispute.status,
"dispute.reason": dispute.reason,
"dispute.amount": dispute.amount,
"dispute.createdAt": firestore_1.Timestamp.now(),
updatedAt: firestore_1.Timestamp.now(),
});
});
console.log(`[${action}] Dispute processing completed`, {
disputeId: dispute.id,
orderId,
processingTime: Date.now() - startTime,
});
}
catch (error) {
console.error(`[${action}] Error processing dispute created`, {
disputeId: dispute.id,
error: error.message,
stack: error.stack,
processingTime: Date.now() - startTime,
});
throw error;
}
}
/**
* Handles charge.dispute.closed webhook
*/
async function handleDisputeClosed(dispute, stripeAccountId) {
const action = "dispute_closed";
const startTime = Date.now();
try {
console.log(`[${action}] Processing dispute closed`, {
disputeId: dispute.id,
status: dispute.status,
outcome: dispute.outcome,
chargeId: dispute.charge,
stripeAccountId,
});
// Get charge details to find payment intent
const charge = await stripe.charges.retrieve(dispute.charge, {
stripeAccount: stripeAccountId,
});
const paymentIntentId = charge.payment_intent;
// Find the order
const orderResult = await findOrderByStripeData(paymentIntentId, charge.id);
if (!orderResult) {
console.error(`[${action}] Order not found for dispute`, {
disputeId: dispute.id,
paymentIntentId,
chargeId: charge.id,
});
return;
}
const { orderId, orderData } = orderResult;
const { orgId, eventId } = orderData;
console.log(`[${action}] Found order for dispute`, {
orderId,
orgId,
eventId,
outcome: dispute.outcome?.outcome,
});
// Process dispute closure in transaction
await db.runTransaction(async (transaction) => {
let ticketsUpdated = 0;
if (dispute.outcome?.outcome === "won") {
// Dispute won - restore tickets to previous status
ticketsUpdated = await updateTicketStatusesForOrder(orderId, "restore", transaction);
console.log(`[${action}] Dispute won - restored ${ticketsUpdated} tickets`, {
orderId,
disputeId: dispute.id,
});
}
else if (dispute.outcome?.outcome === "lost") {
// Dispute lost - void tickets and create refund-style ledger entries
ticketsUpdated = await updateTicketStatusesForOrder(orderId, "void", transaction);
// Create negative sale entry (effectively a refund due to dispute loss)
await createLedgerEntry({
orgId,
eventId,
orderId,
type: "refund",
amountCents: -dispute.amount,
currency: "USD",
stripe: {
chargeId: charge.id,
disputeId: dispute.id,
accountId: stripeAccountId,
},
meta: {
reason: "dispute_lost",
disputeReason: dispute.reason,
},
}, transaction);
// Also create negative platform fee entry
const platformFeeBps = parseInt(process.env.PLATFORM_FEE_BPS || "300");
const platformFeeAmount = Math.round((dispute.amount * platformFeeBps) / 10000);
await createLedgerEntry({
orgId,
eventId,
orderId,
type: "platform_fee",
amountCents: -platformFeeAmount,
currency: "USD",
stripe: {
chargeId: charge.id,
disputeId: dispute.id,
accountId: stripeAccountId,
},
meta: {
reason: "dispute_lost",
},
}, transaction);
console.log(`[${action}] Dispute lost - voided ${ticketsUpdated} tickets and created loss entries`, {
orderId,
disputeId: dispute.id,
lossAmount: dispute.amount,
platformFeeLoss: platformFeeAmount,
});
}
// Update order with final dispute status
const orderRef = db.collection("orders").doc(orderId);
transaction.update(orderRef, {
"dispute.status": dispute.status,
"dispute.outcome": dispute.outcome?.outcome,
"dispute.closedAt": firestore_1.Timestamp.now(),
updatedAt: firestore_1.Timestamp.now(),
});
});
console.log(`[${action}] Dispute closure processing completed`, {
disputeId: dispute.id,
orderId,
outcome: dispute.outcome?.outcome,
processingTime: Date.now() - startTime,
});
}
catch (error) {
console.error(`[${action}] Error processing dispute closed`, {
disputeId: dispute.id,
error: error.message,
stack: error.stack,
processingTime: Date.now() - startTime,
});
throw error;
}
}
/**
* Gets dispute information for an order
*/
exports.getOrderDisputes = (0, https_1.onRequest)({ cors: true, enforceAppCheck: false, region: "us-central1" }, async (req, res) => {
try {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orderId } = req.body;
if (!orderId) {
res.status(400).json({ error: "orderId is required" });
return;
}
// Get order with dispute information
const orderDoc = await db.collection("orders").doc(orderId).get();
if (!orderDoc.exists) {
res.status(404).json({ error: "Order not found" });
return;
}
const orderData = orderDoc.data();
const dispute = orderData?.dispute;
res.status(200).json({
orderId,
dispute: dispute || null,
});
}
catch (error) {
console.error("Error getting order disputes:", error);
res.status(500).json({
error: "Internal server error",
details: error.message,
});
}
});
// # sourceMappingURL=disputes.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,300 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDefaultOrganization = exports.verifyDomain = exports.requestDomainVerification = exports.resolveDomain = void 0;
const v2_1 = require("firebase-functions/v2");
const firestore_1 = require("firebase-admin/firestore");
const zod_1 = require("zod");
// Validation schemas
const resolveRequestSchema = zod_1.z.object({
host: zod_1.z.string().min(1),
});
const verificationRequestSchema = zod_1.z.object({
orgId: zod_1.z.string().min(1),
host: zod_1.z.string().min(1),
});
const verifyRequestSchema = zod_1.z.object({
orgId: zod_1.z.string().min(1),
host: zod_1.z.string().min(1),
});
// Default theme for new organizations
const DEFAULT_THEME = {
accent: '#F0C457',
bgCanvas: '#2B2D2F',
bgSurface: '#34373A',
textPrimary: '#F1F3F5',
textSecondary: '#C9D0D4',
};
/**
* Resolve organization by host domain
* GET /api/domains/resolve?host=tickets.acme.com
*/
exports.resolveDomain = v2_1.https.onRequest({
cors: true,
region: "us-central1",
}, async (req, res) => {
try {
if (req.method !== 'GET') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
const { host } = resolveRequestSchema.parse(req.query);
v2_1.logger.info(`Resolving domain for host: ${host}`);
const db = (0, firestore_1.getFirestore)();
// First, try to find org by exact domain match
const orgsSnapshot = await db.collection('organizations').get();
for (const doc of orgsSnapshot.docs) {
const org = doc.data();
const matchingDomain = org.domains?.find(d => d.host === host && d.verified);
if (matchingDomain) {
v2_1.logger.info(`Found org by domain: ${org.id} for host: ${host}`);
res.json({
orgId: org.id,
name: org.name,
branding: org.branding,
domains: org.domains,
});
return;
}
}
// If no direct domain match, try subdomain pattern (e.g., acme.bct.dev)
const subdomainMatch = host.match(/^([^.]+)\.bct\.dev$/);
if (subdomainMatch) {
const slug = subdomainMatch[1];
const orgBySlugSnapshot = await db.collection('organizations')
.where('slug', '==', slug)
.limit(1)
.get();
if (!orgBySlugSnapshot.empty) {
const org = orgBySlugSnapshot.docs[0].data();
v2_1.logger.info(`Found org by slug: ${org.id} for subdomain: ${slug}`);
res.json({
orgId: org.id,
name: org.name,
branding: org.branding,
domains: org.domains,
});
return;
}
}
// No organization found
v2_1.logger.warn(`No organization found for host: ${host}`);
res.status(404).json({
error: 'Organization not found',
host,
message: 'No organization is configured for this domain'
});
}
catch (error) {
v2_1.logger.error('Error resolving domain:', error);
if (error instanceof zod_1.z.ZodError) {
res.status(400).json({
error: 'Invalid request',
details: error.errors
});
}
else {
res.status(500).json({
error: 'Internal server error',
message: 'Failed to resolve domain'
});
}
}
});
/**
* Request domain verification
* POST /api/domains/request-verification
* Body: { orgId: string, host: string }
*/
exports.requestDomainVerification = v2_1.https.onRequest({
cors: true,
region: "us-central1",
}, async (req, res) => {
try {
if (req.method !== 'POST') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
const { orgId, host } = verificationRequestSchema.parse(req.body);
v2_1.logger.info(`Requesting verification for ${host} on org ${orgId}`);
const db = (0, firestore_1.getFirestore)();
const orgRef = db.collection('organizations').doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: 'Organization not found' });
return;
}
const org = orgDoc.data();
// Generate verification token
const verificationToken = `bct-verify-${Date.now()}-${Math.random().toString(36).substring(2)}`;
// Check if domain already exists
const existingDomains = org.domains || [];
const existingDomainIndex = existingDomains.findIndex(d => d.host === host);
const newDomain = {
host,
verified: false,
createdAt: new Date().toISOString(),
verificationToken,
};
let updatedDomains;
if (existingDomainIndex >= 0) {
// Update existing domain
updatedDomains = [...existingDomains];
updatedDomains[existingDomainIndex] = newDomain;
}
else {
// Add new domain
updatedDomains = [...existingDomains, newDomain];
}
await orgRef.update({ domains: updatedDomains });
v2_1.logger.info(`Generated verification token for ${host}: ${verificationToken}`);
res.json({
success: true,
host,
verificationToken,
instructions: {
type: 'TXT',
name: '_bct-verification',
value: verificationToken,
ttl: 300,
description: `Add this TXT record to your DNS configuration for ${host}`,
},
});
}
catch (error) {
v2_1.logger.error('Error requesting domain verification:', error);
if (error instanceof zod_1.z.ZodError) {
res.status(400).json({
error: 'Invalid request',
details: error.errors
});
}
else {
res.status(500).json({
error: 'Internal server error',
message: 'Failed to request domain verification'
});
}
}
});
/**
* Verify domain ownership
* POST /api/domains/verify
* Body: { orgId: string, host: string }
*/
exports.verifyDomain = v2_1.https.onRequest({
cors: true,
region: "us-central1",
}, async (req, res) => {
try {
if (req.method !== 'POST') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
const { orgId, host } = verifyRequestSchema.parse(req.body);
v2_1.logger.info(`Verifying domain ${host} for org ${orgId}`);
const db = (0, firestore_1.getFirestore)();
const orgRef = db.collection('organizations').doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: 'Organization not found' });
return;
}
const org = orgDoc.data();
const domains = org.domains || [];
const domainIndex = domains.findIndex(d => d.host === host);
if (domainIndex === -1) {
res.status(404).json({ error: 'Domain not found in organization' });
return;
}
const domain = domains[domainIndex];
if (!domain.verificationToken) {
res.status(400).json({
error: 'No verification token found',
message: 'Please request verification first'
});
return;
}
// In development, we'll mock DNS verification
// In production, you would use a real DNS lookup library
const isDevelopment = process.env.NODE_ENV === 'development' ||
process.env.FUNCTIONS_EMULATOR === 'true';
let dnsVerified = false;
if (isDevelopment) {
// Mock verification - always succeed in development
v2_1.logger.info(`Mock DNS verification for ${host} - always succeeds in development`);
dnsVerified = true;
}
else {
// TODO: Implement real DNS lookup
// const dns = require('dns').promises;
// const txtRecords = await dns.resolveTxt(`_bct-verification.${host}`);
// dnsVerified = txtRecords.some(record =>
// record.join('') === domain.verificationToken
// );
v2_1.logger.warn('Real DNS verification not implemented yet - mocking success');
dnsVerified = true;
}
if (dnsVerified) {
// Update domain as verified
const updatedDomains = [...domains];
updatedDomains[domainIndex] = {
...domain,
verified: true,
verifiedAt: new Date().toISOString(),
};
await orgRef.update({ domains: updatedDomains });
v2_1.logger.info(`Successfully verified domain ${host} for org ${orgId}`);
res.json({
success: true,
host,
verified: true,
verifiedAt: updatedDomains[domainIndex].verifiedAt,
message: 'Domain successfully verified',
});
}
else {
v2_1.logger.warn(`DNS verification failed for ${host}`);
res.status(400).json({
success: false,
verified: false,
error: 'DNS verification failed',
message: `TXT record with value "${domain.verificationToken}" not found at _bct-verification.${host}`,
});
}
}
catch (error) {
v2_1.logger.error('Error verifying domain:', error);
if (error instanceof zod_1.z.ZodError) {
res.status(400).json({
error: 'Invalid request',
details: error.errors
});
}
else {
res.status(500).json({
error: 'Internal server error',
message: 'Failed to verify domain'
});
}
}
});
/**
* Helper function to create a default organization
* Used for seeding or testing
*/
const createDefaultOrganization = async (orgId, name, slug) => {
const db = (0, firestore_1.getFirestore)();
const org = {
id: orgId,
name,
slug,
branding: {
theme: DEFAULT_THEME,
},
domains: [],
};
await db.collection('organizations').doc(orgId).set(org);
return org;
};
exports.createDefaultOrganization = createDefaultOrganization;
// # sourceMappingURL=domains.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,132 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendTicketEmail = sendTicketEmail;
exports.logTicketEmail = logTicketEmail;
const firebase_functions_1 = require("firebase-functions");
const resend_1 = require("resend");
const resend = new resend_1.Resend(process.env.EMAIL_API_KEY);
const APP_URL = process.env.APP_URL || "https://staging.blackcanyontickets.com";
/**
* Sends ticket confirmation email with QR codes
*/
async function sendTicketEmail({ to, eventName, tickets, organizationName = "Black Canyon Tickets", }) {
try {
const ticketList = tickets
.map((ticket) => `
<div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin: 8px 0;">
<h3 style="margin: 0 0 8px 0; color: #1f2937;">${ticket.ticketTypeName}</h3>
<p style="margin: 4px 0; color: #6b7280;">Ticket ID: ${ticket.ticketId}</p>
<p style="margin: 4px 0; color: #6b7280;">Event: ${eventName}</p>
<p style="margin: 4px 0; color: #6b7280;">Date: ${new Date(ticket.startAt).toLocaleString()}</p>
<div style="margin: 12px 0;">
<a href="${APP_URL}/t/${ticket.ticketId}"
style="background: #3b82f6; color: white; padding: 8px 16px; text-decoration: none; border-radius: 4px; display: inline-block;">
View Ticket
</a>
</div>
<p style="margin: 8px 0 0 0; font-size: 12px; color: #9ca3af;">
QR Code: ${ticket.qr}
</p>
</div>
`)
.join("");
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Tickets - ${eventName}</title>
</head>
<body style="font-family: system-ui, -apple-system, sans-serif; line-height: 1.6; color: #374151; max-width: 600px; margin: 0 auto; padding: 20px;">
<div style="text-align: center; margin-bottom: 32px;">
<h1 style="color: #1f2937; margin: 0;">${organizationName}</h1>
<p style="color: #6b7280; margin: 8px 0;">Your ticket confirmation</p>
</div>
<div style="background: #f9fafb; border-radius: 8px; padding: 24px; margin: 24px 0;">
<h2 style="margin: 0 0 16px 0; color: #1f2937;">Your Tickets for ${eventName}</h2>
<p style="margin: 0 0 16px 0; color: #6b7280;">
Thank you for your purchase! Your tickets are ready. Please save this email for your records.
</p>
${ticketList}
</div>
<div style="background: #fef3c7; border: 1px solid #f59e0b; border-radius: 8px; padding: 16px; margin: 24px 0;">
<h3 style="margin: 0 0 8px 0; color: #92400e;">Important Information</h3>
<ul style="margin: 0; padding-left: 20px; color: #92400e;">
<li>Present your QR code at the venue for entry</li>
<li>Each ticket can only be scanned once</li>
<li>Arrive early to avoid delays</li>
<li>Contact support if you have any issues</li>
</ul>
</div>
<div style="text-align: center; margin-top: 32px; padding-top: 24px; border-top: 1px solid #e5e7eb;">
<p style="color: #9ca3af; font-size: 14px; margin: 0;">
Need help? Contact us at support@blackcanyontickets.com
</p>
</div>
</body>
</html>
`;
const text = `
Your Tickets for ${eventName}
Thank you for your purchase! Your tickets are ready:
${tickets
.map((ticket) => `
Ticket: ${ticket.ticketTypeName}
ID: ${ticket.ticketId}
QR: ${ticket.qr}
View: ${APP_URL}/t/${ticket.ticketId}
`)
.join("\n")}
Important:
- Present your QR code at the venue for entry
- Each ticket can only be scanned once
- Arrive early to avoid delays
Need help? Contact support@blackcanyontickets.com
`;
await resend.emails.send({
from: "tickets@blackcanyontickets.com",
to,
subject: `Your tickets ${eventName}`,
html,
text,
});
firebase_functions_1.logger.info("Ticket email sent successfully", {
to,
eventName,
ticketCount: tickets.length,
});
}
catch (error) {
firebase_functions_1.logger.error("Failed to send ticket email", {
error: error instanceof Error ? error.message : String(error),
to,
eventName,
ticketCount: tickets.length,
});
throw error;
}
}
/**
* Development helper - logs email instead of sending
*/
async function logTicketEmail(options) {
firebase_functions_1.logger.info("DEV: Would send ticket email", {
to: options.to,
eventName: options.eventName,
tickets: options.tickets.map((t) => ({
id: t.ticketId,
qr: t.qr,
type: t.ticketTypeName,
url: `${APP_URL}/t/${t.ticketId}`,
})),
});
}
// # sourceMappingURL=email.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"email.js","sourceRoot":"","sources":["../src/email.ts"],"names":[],"mappings":";;AAwBA,0CAoHC;AAKD,wCAWC;AA5JD,2DAA4C;AAC5C,mCAAgC;AAEhC,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AACrD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,wCAAwC,CAAC;AAiBhF;;GAEG;AACI,KAAK,UAAU,eAAe,CAAC,EACpC,EAAE,EACF,SAAS,EACT,OAAO,EACP,gBAAgB,GAAG,sBAAsB,GAClB;IACvB,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,OAAO;aACvB,GAAG,CACF,CAAC,MAAM,EAAE,EAAE,CAAC;;2DAEuC,MAAM,CAAC,cAAc;iEACf,MAAM,CAAC,QAAQ;6DACnB,SAAS;4DACV,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE;;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AC1C,EAAE,EAAE,OAAO,CAAC,EAAE;QACd,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YACnC,EAAE,EAAE,CAAC,CAAC,QAAQ;YACd,EAAE,EAAE,CAAC,CAAC,EAAE;YACR,IAAI,EAAE,CAAC,CAAC,cAAc;YACtB,GAAG,EAAE,GAAG,OAAO,MAAM,CAAC,CAAC,QAAQ,EAAE;SAClC,CAAC,CAAC;KACJ,CAAC,CAAC;AACL,CAAC"}

View File

@@ -0,0 +1,40 @@
"use strict";
const __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
let desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
o[k2] = m[k];
}));
const __exportStar = (this && this.__exportStar) || function(m, exports) {
for (const p in m) {if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) {__createBinding(exports, m, p);}}
};
Object.defineProperty(exports, "__esModule", { value: true });
const app_1 = require("firebase-admin/app");
const v2_1 = require("firebase-functions/v2");
// Initialize Firebase Admin
(0, app_1.initializeApp)();
// Set global options for all functions
(0, v2_1.setGlobalOptions)({
maxInstances: 10,
region: "us-central1",
});
// Export simplified API function for deployment testing
__exportStar(require("./api-simple"), exports);
// Individual functions commented out due to TypeScript errors
// Uncomment and fix after deployment testing
// export * from "./stripeConnect";
// export * from "./claims";
// export * from "./domains";
// export * from "./checkout";
// export * from "./verify";
// export * from "./orders";
// export * from "./refunds";
// export * from "./disputes";
// export * from "./reconciliation";
// export * from "./webhooks";
// # sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAmD;AACnD,8CAAyD;AAEzD,4BAA4B;AAC5B,IAAA,mBAAa,GAAE,CAAC;AAEhB,uCAAuC;AACvC,IAAA,qBAAgB,EAAC;IACf,YAAY,EAAE,EAAE;IAChB,MAAM,EAAE,aAAa;CACtB,CAAC,CAAC;AAEH,wDAAwD;AACxD,+CAA6B;AAE7B,8DAA8D;AAC9D,6CAA6C;AAC7C,mCAAmC;AACnC,4BAA4B;AAC5B,6BAA6B;AAC7B,8BAA8B;AAC9B,4BAA4B;AAC5B,4BAA4B;AAC5B,6BAA6B;AAC7B,8BAA8B;AAC9B,oCAAoC;AACpC,8BAA8B"}

View File

@@ -0,0 +1,310 @@
"use strict";
/**
* Structured Logger Utility for Firebase Cloud Functions
*
* Provides consistent structured logging with proper data masking
* and performance tracking for scanner operations.
*/
const __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
let desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
o[k2] = m[k];
}));
const __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
const __importStar = (this && this.__importStar) || (function () {
let ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
const ar = [];
for (const 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;}
const result = {};
if (mod != null) {for (let k = ownKeys(mod), i = 0; i < k.length; i++) {if (k[i] !== "default") {__createBinding(result, mod, k[i]);}}}
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sentry = exports.logger = void 0;
exports.withLogging = withLogging;
const firebase_functions_1 = require("firebase-functions");
const Sentry = __importStar(require("@sentry/node"));
exports.Sentry = Sentry;
// Initialize Sentry for Cloud Functions
const initializeSentry = () => {
// Only initialize if DSN is provided and not a mock
const dsn = process.env.SENTRY_DSN;
if (!dsn || dsn.includes('mock')) {
console.info('Sentry: Skipping initialization (no DSN or mock DSN detected)');
return;
}
Sentry.init({
dsn,
environment: process.env.NODE_ENV || 'production',
tracesSampleRate: 0.1,
integrations: [
// Add Node.js specific integrations
Sentry.httpIntegration(),
Sentry.expressIntegration(),
],
beforeSend: (event, hint) => {
// Filter out noisy errors
if (event.exception?.values?.[0]?.type === 'TypeError' &&
event.exception?.values?.[0]?.value?.includes('fetch')) {
return null;
}
return event;
},
});
};
// Initialize Sentry when module loads
initializeSentry();
/**
* Mask sensitive data in QR codes, tokens, or other sensitive strings
*/
function maskSensitiveData(data) {
if (!data || data.length <= 8) {
return '***';
}
// Show first 4 and last 4 characters, mask the middle
const start = data.substring(0, 4);
const end = data.substring(data.length - 4);
const maskLength = Math.min(data.length - 8, 20); // Cap mask length
const mask = '*'.repeat(maskLength);
return `${start}${mask}${end}`;
}
/**
* Format log context with sensitive data masking
*/
function formatLogContext(context) {
const formatted = {};
// Copy non-sensitive fields directly
const safeCopyFields = ['sessionId', 'accountId', 'orgId', 'eventId', 'ticketTypeId', 'deviceId', 'userId', 'operation'];
for (const field of safeCopyFields) {
if (context[field]) {
formatted[field] = context[field];
}
}
// Mask sensitive fields
if (context.qr) {
formatted.qr_masked = maskSensitiveData(context.qr);
}
if (context.deviceId) {
formatted.device_short = context.deviceId.split('_')[1]?.substring(0, 8) || 'unknown';
}
formatted.timestamp = new Date().toISOString();
return formatted;
}
/**
* Core structured logger class
*/
class StructuredLogger {
/**
* Log scanner verification result with full context
*/
logScannerVerify(data) {
const logData = {
...formatLogContext(data),
result: data.result,
latencyMs: data.latencyMs,
reason: data.reason,
timestamp: data.timestamp || new Date().toISOString(),
};
// Use different log levels based on result
if (data.result === 'valid') {
firebase_functions_1.logger.info('Scanner verification successful', logData);
}
else if (data.result === 'already_scanned') {
firebase_functions_1.logger.warn('Scanner verification - already scanned', logData);
}
else {
firebase_functions_1.logger.warn('Scanner verification failed', logData);
}
// Send to Sentry if it's an error or concerning result
if (data.result === 'invalid' && data.reason !== 'ticket_not_found') {
Sentry.withScope((scope) => {
scope.setTag('feature', 'scanner');
scope.setTag('scanner.result', data.result);
scope.setContext('scanner_verification', logData);
Sentry.captureMessage(`Scanner verification failed: ${data.reason}`, 'warning');
});
}
}
/**
* Log performance metrics for scanner operations
*/
logPerformance(data) {
const logData = {
operation: data.operation,
duration_ms: data.duration,
...(data.context ? formatLogContext(data.context) : {}),
metadata: data.metadata,
timestamp: new Date().toISOString(),
};
firebase_functions_1.logger.info('Performance metric', logData);
// Send slow operations to Sentry
if (data.duration > 5000) { // Operations slower than 5 seconds
Sentry.withScope((scope) => {
scope.setTag('feature', 'performance');
scope.setTag('performance.operation', data.operation);
scope.setContext('performance_metric', logData);
Sentry.captureMessage(`Slow operation: ${data.operation} took ${data.duration}ms`, 'warning');
});
}
}
/**
* Log general information with context
*/
info(message, context, metadata) {
const logData = {
message,
...(context ? formatLogContext(context) : {}),
...metadata,
timestamp: new Date().toISOString(),
};
firebase_functions_1.logger.info(message, logData);
}
/**
* Log warnings with context
*/
warn(message, context, metadata) {
const logData = {
message,
...(context ? formatLogContext(context) : {}),
...metadata,
timestamp: new Date().toISOString(),
};
firebase_functions_1.logger.warn(message, logData);
// Send warnings to Sentry with context
Sentry.withScope((scope) => {
if (context?.operation) {
scope.setTag('operation', context.operation);
}
scope.setContext('warning_context', logData);
Sentry.captureMessage(message, 'warning');
});
}
/**
* Log errors with context and send to Sentry
*/
error(message, error, context, metadata) {
const logData = {
message,
error_message: error?.message,
error_stack: error?.stack,
...(context ? formatLogContext(context) : {}),
...metadata,
timestamp: new Date().toISOString(),
};
firebase_functions_1.logger.error(message, logData);
// Send to Sentry with full context
Sentry.withScope((scope) => {
if (context?.operation) {
scope.setTag('operation', context.operation);
}
if (context?.sessionId) {
scope.setTag('scanner.session', context.sessionId);
}
scope.setContext('error_context', logData);
if (error) {
Sentry.captureException(error);
}
else {
Sentry.captureMessage(message, 'error');
}
});
}
/**
* Log debug information (only in development)
*/
debug(message, context, metadata) {
if (process.env.NODE_ENV !== 'production') {
const logData = {
message,
...(context ? formatLogContext(context) : {}),
...metadata,
timestamp: new Date().toISOString(),
};
firebase_functions_1.logger.debug(message, logData);
}
}
/**
* Capture exception directly to Sentry with context
*/
captureException(error, context) {
Sentry.withScope((scope) => {
if (context) {
scope.setContext('exception_context', formatLogContext(context));
if (context.operation) {
scope.setTag('operation', context.operation);
}
if (context.sessionId) {
scope.setTag('scanner.session', context.sessionId);
}
}
Sentry.captureException(error);
});
}
/**
* Start a performance transaction
*/
startTransaction(name, op) {
return Sentry.startSpan({ name, op }, () => { });
}
/**
* Add breadcrumb for debugging
*/
addBreadcrumb(message, category = 'general', data) {
Sentry.addBreadcrumb({
message,
category,
level: 'info',
data: {
timestamp: new Date().toISOString(),
...data,
},
});
}
}
// Singleton logger instance
exports.logger = new StructuredLogger();
/**
* Middleware wrapper for Cloud Functions to automatically log performance
*/
function withLogging(operationName, fn, contextExtractor) {
return async (...args) => {
const startTime = performance.now();
const context = contextExtractor ? contextExtractor(...args) : undefined;
exports.logger.addBreadcrumb(`Starting operation: ${operationName}`, 'function', context);
try {
const result = await fn(...args);
const duration = performance.now() - startTime;
exports.logger.logPerformance({
operation: operationName,
duration,
context,
});
return result;
}
catch (error) {
const duration = performance.now() - startTime;
exports.logger.error(`Operation failed: ${operationName}`, error, context, { duration });
throw error;
}
};
}
// # sourceMappingURL=logger.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrder = void 0;
const https_1 = require("firebase-functions/v2/https");
const firebase_functions_1 = require("firebase-functions");
const firestore_1 = require("firebase-admin/firestore");
const db = (0, firestore_1.getFirestore)();
/**
* Gets order details by session ID for frontend polling
* POST /api/orders/get
*/
exports.getOrder = (0, https_1.onRequest)({
cors: true,
enforceAppCheck: false,
region: "us-central1",
}, async (req, res) => {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
try {
const { sessionId } = req.body;
if (!sessionId) {
res.status(400).json({ error: "Session ID is required" });
return;
}
firebase_functions_1.logger.info("Getting order details", { sessionId });
// Get order by session ID
const orderDoc = await db.collection("orders").doc(sessionId).get();
if (!orderDoc.exists) {
res.status(404).json({ error: "Order not found" });
return;
}
const orderData = orderDoc.data();
// Get additional details if order is paid
let eventName = "";
let ticketTypeName = "";
let eventDate = "";
let eventLocation = "";
if (orderData.status === "paid") {
try {
const [eventDoc, ticketTypeDoc] = await Promise.all([
db.collection("events").doc(orderData.eventId).get(),
db.collection("ticket_types").doc(orderData.ticketTypeId).get(),
]);
if (eventDoc.exists) {
const event = eventDoc.data();
eventName = event.name || "";
eventDate = event.startAt?.toDate?.()?.toISOString() || event.startAt || "";
eventLocation = event.location || "Venue TBD";
}
if (ticketTypeDoc.exists) {
const ticketType = ticketTypeDoc.data();
ticketTypeName = ticketType.name || "";
}
}
catch (error) {
firebase_functions_1.logger.warn("Failed to fetch event/ticket type details for order", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
}
}
const response = {
id: orderDoc.id,
orgId: orderData.orgId,
eventId: orderData.eventId,
ticketTypeId: orderData.ticketTypeId,
qty: orderData.qty,
status: orderData.status,
totalCents: orderData.totalCents,
purchaserEmail: orderData.purchaserEmail,
eventName,
ticketTypeName,
eventDate,
eventLocation,
createdAt: orderData.createdAt?.toDate?.()?.toISOString() || orderData.createdAt,
updatedAt: orderData.updatedAt?.toDate?.()?.toISOString() || orderData.updatedAt,
};
firebase_functions_1.logger.info("Order details retrieved", {
sessionId,
status: orderData.status,
qty: orderData.qty,
});
res.status(200).json(response);
}
catch (error) {
firebase_functions_1.logger.error("Error getting order details", {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
res.status(500).json({
error: "Internal server error retrieving order",
});
}
});
// # sourceMappingURL=orders.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../src/orders.ts"],"names":[],"mappings":";;;AAAA,uDAAwD;AACxD,2DAA4C;AAC5C,wDAAwD;AAExD,MAAM,EAAE,GAAG,IAAA,wBAAY,GAAE,CAAC;AAuB1B;;;GAGG;AACU,QAAA,QAAQ,GAAG,IAAA,iBAAS,EAC/B;IACE,IAAI,EAAE,IAAI;IACV,eAAe,EAAE,KAAK;IACtB,MAAM,EAAE,aAAa;CACtB,EACD,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;IACjB,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;QAC1B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC,CAAC;QACtD,OAAO;IACT,CAAC;IAED,IAAI,CAAC;QACH,MAAM,EAAE,SAAS,EAAE,GAAoB,GAAG,CAAC,IAAI,CAAC;QAEhD,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,wBAAwB,EAAE,CAAC,CAAC;YAC1D,OAAO;QACT,CAAC;QAED,2BAAM,CAAC,IAAI,CAAC,uBAAuB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;QAEpD,0BAA0B;QAC1B,MAAM,QAAQ,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,CAAC;QAEpE,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;YACrB,GAAG,CAAC,MAAM,CAAC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,CAAC;YAC7D,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;SACxD,CAAC,CAAC;QAEH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,KAAK,EAAE,wCAAwC;SAChD,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CACF,CAAC"}

View File

@@ -0,0 +1,277 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReconciliationEvents = exports.getReconciliationData = void 0;
const https_1 = require("firebase-functions/v2/https");
const app_1 = require("firebase-admin/app");
const firestore_1 = require("firebase-admin/firestore");
const csv_writer_1 = require("csv-writer");
const os_1 = require("os");
const path_1 = require("path");
const fs_1 = require("fs");
// Initialize Firebase Admin if not already initialized
try {
(0, app_1.initializeApp)();
}
catch (error) {
// App already initialized
}
const db = (0, firestore_1.getFirestore)();
/**
* Helper function to check user permissions
*/
async function checkReconciliationPermissions(uid, orgId) {
try {
// Check if user is super admin
const userDoc = await db.collection("users").doc(uid).get();
if (!userDoc.exists) {
return false;
}
const userData = userDoc.data();
if (userData?.role === "super_admin") {
return true;
}
// Check if user is org admin
if (userData?.organization?.id === orgId && userData?.role === "admin") {
return true;
}
// TODO: Add territory manager check when territories are implemented
// if (userData?.role === "territory_manager" && userData?.territories?.includes(orgTerritory)) {
// return true;
// }
return false;
}
catch (error) {
console.error("Error checking reconciliation permissions:", error);
return false;
}
}
/**
* Gets reconciliation data for an organization
*/
exports.getReconciliationData = (0, https_1.onRequest)({ cors: true, enforceAppCheck: false, region: "us-central1" }, async (req, res) => {
const startTime = Date.now();
const action = "get_reconciliation_data";
try {
console.log(`[${action}] Starting reconciliation request`, {
method: req.method,
body: req.body,
query: req.query,
timestamp: new Date().toISOString(),
});
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orgId, eventId, startDate, endDate, format = 'json' } = req.body;
if (!orgId || !startDate || !endDate) {
res.status(400).json({ error: "orgId, startDate, and endDate are required" });
return;
}
// Get user ID from Authorization header or Firebase Auth token
// For now, we'll use a mock uid - in production, extract from JWT
const uid = req.headers.authorization?.replace("Bearer ", "") || "mock-uid";
// Check permissions
const hasPermission = await checkReconciliationPermissions(uid, orgId);
if (!hasPermission) {
console.error(`[${action}] Permission denied for user ${uid} in org ${orgId}`);
res.status(403).json({ error: "Insufficient permissions" });
return;
}
// Parse date range
const start = new Date(startDate);
const end = new Date(endDate);
end.setHours(23, 59, 59, 999); // Include full end date
if (start >= end) {
res.status(400).json({ error: "Start date must be before end date" });
return;
}
console.log(`[${action}] Querying ledger entries`, {
orgId,
eventId,
startDate: start.toISOString(),
endDate: end.toISOString(),
});
// Build query
let query = db.collection("ledger")
.where("orgId", "==", orgId)
.where("createdAt", ">=", firestore_1.Timestamp.fromDate(start))
.where("createdAt", "<=", firestore_1.Timestamp.fromDate(end));
// Add event filter if specified
if (eventId && eventId !== 'all') {
query = query.where("eventId", "==", eventId);
}
// Execute query
const ledgerSnapshot = await query.orderBy("createdAt", "desc").get();
const ledgerEntries = ledgerSnapshot.docs.map(doc => {
const data = doc.data();
return {
id: doc.id,
...data,
createdAt: data.createdAt.toDate().toISOString(),
};
});
console.log(`[${action}] Found ${ledgerEntries.length} ledger entries`);
// Calculate summary
const summary = {
grossSales: ledgerEntries
.filter(e => e.type === 'sale')
.reduce((sum, e) => sum + e.amountCents, 0),
refunds: Math.abs(ledgerEntries
.filter(e => e.type === 'refund')
.reduce((sum, e) => sum + e.amountCents, 0)),
stripeFees: Math.abs(ledgerEntries
.filter(e => e.type === 'fee')
.reduce((sum, e) => sum + e.amountCents, 0)),
platformFees: Math.abs(ledgerEntries
.filter(e => e.type === 'platform_fee')
.reduce((sum, e) => sum + e.amountCents, 0)),
disputeFees: Math.abs(ledgerEntries
.filter(e => e.type === 'dispute_fee')
.reduce((sum, e) => sum + e.amountCents, 0)),
totalTransactions: new Set(ledgerEntries.map(e => e.orderId)).size,
period: {
start: startDate,
end: endDate,
},
};
summary['netToOrganizer'] = summary.grossSales - summary.refunds - summary.stripeFees - summary.platformFees - summary.disputeFees;
if (format === 'csv') {
// Generate CSV file
const csvData = await generateCSV(ledgerEntries, summary);
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', `attachment; filename="reconciliation-${startDate}-to-${endDate}.csv"`);
res.status(200).send(csvData);
}
else {
// Return JSON
res.status(200).json({
summary,
entries: ledgerEntries,
total: ledgerEntries.length,
});
}
console.log(`[${action}] Reconciliation completed successfully`, {
orgId,
entriesCount: ledgerEntries.length,
grossSales: summary.grossSales,
netToOrganizer: summary['netToOrganizer'],
processingTime: Date.now() - startTime,
});
}
catch (error) {
console.error(`[${action}] Unexpected error`, {
error: error.message,
stack: error.stack,
processingTime: Date.now() - startTime,
});
res.status(500).json({
error: "Internal server error",
details: error.message,
});
}
});
/**
* Generates CSV content from ledger entries
*/
async function generateCSV(entries, summary) {
const tmpFilePath = (0, path_1.join)((0, os_1.tmpdir)(), `reconciliation-${Date.now()}.csv`);
try {
const csvWriter = (0, csv_writer_1.createObjectCsvWriter)({
path: tmpFilePath,
header: [
{ id: 'date', title: 'Date' },
{ id: 'type', title: 'Type' },
{ id: 'amount', title: 'Amount (USD)' },
{ id: 'orderId', title: 'Order ID' },
{ id: 'stripeTransactionId', title: 'Stripe Transaction ID' },
{ id: 'chargeRefundId', title: 'Charge/Refund ID' },
{ id: 'accountId', title: 'Stripe Account ID' },
{ id: 'notes', title: 'Notes' },
],
});
// Prepare data for CSV
const csvRecords = entries.map(entry => ({
date: new Date(entry.createdAt).toISOString(),
type: entry.type,
amount: (entry.amountCents / 100).toFixed(2),
orderId: entry.orderId,
stripeTransactionId: entry.stripe.balanceTxnId || '',
chargeRefundId: entry.stripe.chargeId || entry.stripe.refundId || entry.stripe.disputeId || '',
accountId: entry.stripe.accountId,
notes: entry.meta ? Object.entries(entry.meta).map(([k, v]) => `${k}:${v}`).join(';') : '',
}));
// Add summary rows at the top
const summaryRows = [
{ date: 'SUMMARY', type: '', amount: '', orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: summary.period.start, type: 'Period Start', amount: '', orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: summary.period.end, type: 'Period End', amount: '', orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Gross Sales', amount: (summary.grossSales / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Refunds', amount: (summary.refunds / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Stripe Fees', amount: (summary.stripeFees / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Platform Fees', amount: (summary.platformFees / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Dispute Fees', amount: (summary.disputeFees / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Net to Organizer', amount: (summary.netToOrganizer / 100).toFixed(2), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: 'Total Transactions', amount: summary.totalTransactions.toString(), orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: '', type: '', amount: '', orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
{ date: 'TRANSACTIONS', type: '', amount: '', orderId: '', stripeTransactionId: '', chargeRefundId: '', accountId: '', notes: '' },
];
await csvWriter.writeRecords([...summaryRows, ...csvRecords]);
// Read the file content
const csvContent = (0, fs_1.readFileSync)(tmpFilePath, 'utf8');
// Clean up temporary file
(0, fs_1.unlinkSync)(tmpFilePath);
return csvContent;
}
catch (error) {
// Clean up on error
try {
(0, fs_1.unlinkSync)(tmpFilePath);
}
catch (cleanupError) {
// Ignore cleanup errors
}
throw error;
}
}
/**
* Gets available events for reconciliation
*/
exports.getReconciliationEvents = (0, https_1.onRequest)({ cors: true, enforceAppCheck: false, region: "us-central1" }, async (req, res) => {
try {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orgId } = req.body;
if (!orgId) {
res.status(400).json({ error: "orgId is required" });
return;
}
// Get user ID and check permissions
const uid = req.headers.authorization?.replace("Bearer ", "") || "mock-uid";
const hasPermission = await checkReconciliationPermissions(uid, orgId);
if (!hasPermission) {
res.status(403).json({ error: "Insufficient permissions" });
return;
}
// Get events for the organization
const eventsSnapshot = await db.collection("events")
.where("orgId", "==", orgId)
.orderBy("startAt", "desc")
.get();
const events = eventsSnapshot.docs.map(doc => ({
id: doc.id,
name: doc.data().name,
startAt: doc.data().startAt?.toDate?.()?.toISOString() || doc.data().startAt,
}));
res.status(200).json({ events });
}
catch (error) {
console.error("Error getting reconciliation events:", error);
res.status(500).json({
error: "Internal server error",
details: error.message,
});
}
});
// # sourceMappingURL=reconciliation.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,349 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrderRefunds = exports.createRefund = void 0;
const https_1 = require("firebase-functions/v2/https");
const app_1 = require("firebase-admin/app");
const firestore_1 = require("firebase-admin/firestore");
const stripe_1 = __importDefault(require("stripe"));
const uuid_1 = require("uuid");
// Initialize Firebase Admin if not already initialized
try {
(0, app_1.initializeApp)();
}
catch (error) {
// App already initialized
}
const db = (0, firestore_1.getFirestore)();
// Initialize Stripe
const stripe = new stripe_1.default(process.env.STRIPE_SECRET_KEY || "", {
apiVersion: "2024-06-20",
});
/**
* Helper function to check user permissions
*/
async function checkRefundPermissions(uid, orgId) {
try {
// Check if user is super admin
const userDoc = await db.collection("users").doc(uid).get();
if (!userDoc.exists) {
return false;
}
const userData = userDoc.data();
if (userData?.role === "super_admin") {
return true;
}
// Check if user is org admin
if (userData?.organization?.id === orgId && userData?.role === "admin") {
return true;
}
// TODO: Add territory manager check when territories are implemented
// if (userData?.role === "territory_manager" && userData?.territories?.includes(orgTerritory)) {
// return true;
// }
return false;
}
catch (error) {
console.error("Error checking refund permissions:", error);
return false;
}
}
/**
* Helper function to create ledger entry
*/
async function createLedgerEntry(entry, transaction) {
const ledgerEntry = {
...entry,
createdAt: firestore_1.Timestamp.now(),
};
const entryId = (0, uuid_1.v4)();
const docRef = db.collection("ledger").doc(entryId);
if (transaction) {
transaction.set(docRef, ledgerEntry);
}
else {
await docRef.set(ledgerEntry);
}
}
/**
* Creates a refund for an order or specific ticket
*/
exports.createRefund = (0, https_1.onRequest)({ cors: true, enforceAppCheck: false, region: "us-central1" }, async (req, res) => {
const startTime = Date.now();
const action = "create_refund";
try {
console.log(`[${action}] Starting refund creation`, {
method: req.method,
body: req.body,
timestamp: new Date().toISOString(),
});
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orderId, ticketId, amountCents, reason } = req.body;
if (!orderId) {
res.status(400).json({ error: "orderId is required" });
return;
}
// Get user ID from Authorization header or Firebase Auth token
// For now, we'll use a mock uid - in production, extract from JWT
const uid = req.headers.authorization?.replace("Bearer ", "") || "mock-uid";
// Load order by orderId (sessionId)
const orderDoc = await db.collection("orders").doc(orderId).get();
if (!orderDoc.exists) {
console.error(`[${action}] Order not found: ${orderId}`);
res.status(404).json({ error: "Order not found" });
return;
}
const orderData = orderDoc.data();
if (!orderData) {
res.status(404).json({ error: "Order data not found" });
return;
}
const { orgId, eventId, paymentIntentId, stripeAccountId, totalCents, status } = orderData;
if (status !== "paid") {
res.status(400).json({ error: "Can only refund paid orders" });
return;
}
// Check permissions
const hasPermission = await checkRefundPermissions(uid, orgId);
if (!hasPermission) {
console.error(`[${action}] Permission denied for user ${uid} in org ${orgId}`);
res.status(403).json({ error: "Insufficient permissions" });
return;
}
let refundAmountCents = amountCents;
let ticketData = null;
// If ticketId is provided, validate and get ticket price
if (ticketId) {
const ticketDoc = await db.collection("tickets").doc(ticketId).get();
if (!ticketDoc.exists) {
res.status(404).json({ error: "Ticket not found" });
return;
}
ticketData = ticketDoc.data();
if (ticketData?.orderId !== orderId) {
res.status(400).json({ error: "Ticket does not belong to this order" });
return;
}
if (!["issued", "scanned"].includes(ticketData?.status)) {
res.status(400).json({
error: `Cannot refund ticket with status: ${ticketData?.status}`
});
return;
}
// If no amount specified, use ticket type price
if (!refundAmountCents) {
const ticketTypeDoc = await db.collection("ticket_types").doc(ticketData.ticketTypeId).get();
if (ticketTypeDoc.exists) {
refundAmountCents = ticketTypeDoc.data()?.priceCents || 0;
}
}
}
// Default to full order amount if no amount specified
if (!refundAmountCents) {
refundAmountCents = totalCents;
}
// Validate refund amount
if (refundAmountCents <= 0 || refundAmountCents > totalCents) {
res.status(400).json({
error: `Invalid refund amount: ${refundAmountCents}. Must be between 1 and ${totalCents}`
});
return;
}
// Create idempotency key for refund
const idempotencyKey = `${orderId}_${ticketId || "full"}_${refundAmountCents}`;
const refundId = (0, uuid_1.v4)();
// Create pending refund record for idempotency
const refundDoc = {
orgId,
eventId,
orderId,
ticketId,
amountCents: refundAmountCents,
reason,
requestedByUid: uid,
stripe: {
paymentIntentId,
accountId: stripeAccountId,
},
status: "pending",
createdAt: firestore_1.Timestamp.now(),
};
// Check for existing refund with same idempotency key
const existingRefundQuery = await db.collection("refunds")
.where("orderId", "==", orderId)
.where("amountCents", "==", refundAmountCents)
.get();
if (!existingRefundQuery.empty) {
const existingRefund = existingRefundQuery.docs[0].data();
if (existingRefund.ticketId === ticketId) {
console.log(`[${action}] Duplicate refund request detected`, { idempotencyKey });
res.status(200).json({
refundId: existingRefundQuery.docs[0].id,
status: existingRefund.status,
message: "Refund already exists"
});
return;
}
}
// Create pending refund document
await db.collection("refunds").doc(refundId).set(refundDoc);
console.log(`[${action}] Created pending refund record`, { refundId, idempotencyKey });
try {
// Create Stripe refund
console.log(`[${action}] Creating Stripe refund`, {
paymentIntentId,
amount: refundAmountCents,
stripeAccountId,
});
const stripeRefund = await stripe.refunds.create({
payment_intent: paymentIntentId,
amount: refundAmountCents,
reason: reason ? "requested_by_customer" : undefined,
refund_application_fee: true,
reverse_transfer: true,
metadata: {
orderId,
ticketId: ticketId || "",
refundId,
orgId,
eventId,
},
}, {
stripeAccount: stripeAccountId,
idempotencyKey,
});
console.log(`[${action}] Stripe refund created successfully`, {
stripeRefundId: stripeRefund.id,
status: stripeRefund.status,
});
// Update refund record and related entities in transaction
await db.runTransaction(async (transaction) => {
// Update refund status
const refundRef = db.collection("refunds").doc(refundId);
transaction.update(refundRef, {
"stripe.refundId": stripeRefund.id,
status: "succeeded",
updatedAt: firestore_1.Timestamp.now(),
});
// Update ticket status if single ticket refund
if (ticketId) {
const ticketRef = db.collection("tickets").doc(ticketId);
transaction.update(ticketRef, {
status: "refunded",
updatedAt: firestore_1.Timestamp.now(),
});
}
// Create ledger entries
// Refund entry (negative)
await createLedgerEntry({
orgId,
eventId,
orderId,
type: "refund",
amountCents: -refundAmountCents,
currency: "USD",
stripe: {
refundId: stripeRefund.id,
accountId: stripeAccountId,
},
}, transaction);
// Platform fee refund (negative of original platform fee portion)
const platformFeeBps = parseInt(process.env.PLATFORM_FEE_BPS || "300");
const platformFeeRefund = Math.round((refundAmountCents * platformFeeBps) / 10000);
await createLedgerEntry({
orgId,
eventId,
orderId,
type: "platform_fee",
amountCents: -platformFeeRefund,
currency: "USD",
stripe: {
refundId: stripeRefund.id,
accountId: stripeAccountId,
},
}, transaction);
});
console.log(`[${action}] Refund completed successfully`, {
refundId,
stripeRefundId: stripeRefund.id,
amountCents: refundAmountCents,
processingTime: Date.now() - startTime,
});
res.status(200).json({
refundId,
stripeRefundId: stripeRefund.id,
amountCents: refundAmountCents,
status: "succeeded",
});
}
catch (stripeError) {
console.error(`[${action}] Stripe refund failed`, {
error: stripeError.message,
code: stripeError.code,
type: stripeError.type,
});
// Update refund status to failed
await db.collection("refunds").doc(refundId).update({
status: "failed",
failureReason: stripeError.message,
updatedAt: firestore_1.Timestamp.now(),
});
res.status(400).json({
error: "Refund failed",
details: stripeError.message,
refundId,
});
}
}
catch (error) {
console.error(`[${action}] Unexpected error`, {
error: error.message,
stack: error.stack,
processingTime: Date.now() - startTime,
});
res.status(500).json({
error: "Internal server error",
details: error.message,
});
}
});
/**
* Gets refunds for an order
*/
exports.getOrderRefunds = (0, https_1.onRequest)({ cors: true, enforceAppCheck: false, region: "us-central1" }, async (req, res) => {
try {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orderId } = req.body;
if (!orderId) {
res.status(400).json({ error: "orderId is required" });
return;
}
const refundsSnapshot = await db.collection("refunds")
.where("orderId", "==", orderId)
.orderBy("createdAt", "desc")
.get();
const refunds = refundsSnapshot.docs.map(doc => ({
id: doc.id,
...doc.data(),
createdAt: doc.data().createdAt.toDate().toISOString(),
updatedAt: doc.data().updatedAt?.toDate().toISOString(),
}));
res.status(200).json({ refunds });
}
catch (error) {
console.error("Error getting order refunds:", error);
res.status(500).json({
error: "Internal server error",
details: error.message,
});
}
});
// # sourceMappingURL=refunds.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,289 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
/**
* Integration tests for hardened Stripe Connect functionality
*
* These tests demonstrate the key hardening features:
* - Idempotency protection against duplicate webhooks
* - Transactional inventory management preventing overselling
* - Platform fee configuration
* - Refund safety with organization validation
*
* Note: These are example tests showing the patterns.
* In a real environment, you'd use Firebase Test SDK and mock Stripe.
*/
(0, globals_1.describe)('Stripe Connect Hardening Integration Tests', () => {
(0, globals_1.beforeAll)(async () => {
// Initialize test Firebase project
// Initialize test Stripe environment
console.log('Setting up integration test environment...');
});
(0, globals_1.afterAll)(async () => {
// Clean up test data
console.log('Cleaning up test environment...');
});
(0, globals_1.describe)('Idempotency Protection', () => {
(0, globals_1.test)('should handle duplicate webhook delivery gracefully', async () => {
/**
* Test Scenario:
* 1. Create a checkout session
* 2. Simulate successful payment webhook
* 3. Send the same webhook again (simulate Stripe retry)
* 4. Verify only one set of tickets was created
*/
const sessionId = 'cs_test_idempotency_123';
const orgId = 'org_test_123';
const eventId = 'event_test_123';
const ticketTypeId = 'tt_test_123';
const quantity = 2;
// First webhook delivery
const firstWebhookPayload = {
id: 'evt_test_1',
type: 'checkout.session.completed',
account: 'acct_test_123',
data: {
object: {
id: sessionId,
metadata: {
orgId,
eventId,
ticketTypeId,
quantity: quantity.toString(),
type: 'ticket_purchase'
},
customer_details: {
email: 'test@example.com',
name: 'Test User'
},
amount_total: 10000,
currency: 'usd',
payment_intent: 'pi_test_123'
}
}
};
// TODO: Send first webhook and verify tickets created
// const firstResponse = await sendWebhook(firstWebhookPayload);
// expect(firstResponse.status).toBe(200);
// TODO: Verify tickets were created
// const tickets = await getTicketsBySession(sessionId);
// expect(tickets).toHaveLength(quantity);
// Second webhook delivery (duplicate)
const secondWebhookPayload = { ...firstWebhookPayload, id: 'evt_test_2' };
// TODO: Send duplicate webhook
// const secondResponse = await sendWebhook(secondWebhookPayload);
// expect(secondResponse.status).toBe(200);
// TODO: Verify no additional tickets were created
// const ticketsAfterDuplicate = await getTicketsBySession(sessionId);
// expect(ticketsAfterDuplicate).toHaveLength(quantity); // Same count
// TODO: Verify processedSessions document shows idempotency skip
// const processedSession = await getProcessedSession(sessionId);
// expect(processedSession.status).toBe('completed');
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Inventory Concurrency Control', () => {
(0, globals_1.test)('should prevent overselling with concurrent purchases', async () => {
/**
* Test Scenario:
* 1. Create ticket type with limited inventory (e.g., 3 tickets)
* 2. Simulate 3 concurrent purchases of 2 tickets each
* 3. Verify only the first purchase succeeds, others fail gracefully
* 4. Verify inventory is accurate (3 - 2 = 1 remaining)
*/
const ticketTypeId = 'tt_limited_inventory';
const initialInventory = 3;
const purchaseQuantity = 2;
// TODO: Setup ticket type with limited inventory
// await createTicketType({
// id: ticketTypeId,
// eventId: 'event_concurrency_test',
// inventory: initialInventory,
// sold: 0,
// price: 5000
// });
// Simulate 3 concurrent webhook deliveries
const concurrentWebhooks = Array.from({ length: 3 }, (_, i) => ({
id: `evt_concurrent_${i}`,
type: 'checkout.session.completed',
account: 'acct_test_123',
data: {
object: {
id: `cs_concurrent_${i}`,
metadata: {
orgId: 'org_test_123',
eventId: 'event_concurrency_test',
ticketTypeId,
quantity: purchaseQuantity.toString(),
type: 'ticket_purchase'
},
customer_details: {
email: `test${i}@example.com`,
name: `Test User ${i}`
},
amount_total: 10000,
currency: 'usd',
payment_intent: `pi_concurrent_${i}`
}
}
}));
// TODO: Send all webhooks concurrently
// const responses = await Promise.all(
// concurrentWebhooks.map(webhook => sendWebhook(webhook))
// );
// TODO: Verify only one purchase succeeded
// const successfulPurchases = responses.filter(r => r.status === 200);
// expect(successfulPurchases).toHaveLength(1);
// TODO: Verify final inventory is correct
// const finalTicketType = await getTicketType(ticketTypeId);
// expect(finalTicketType.inventory).toBe(initialInventory - purchaseQuantity);
// expect(finalTicketType.sold).toBe(purchaseQuantity);
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Platform Fee Configuration', () => {
(0, globals_1.test)('should calculate fees using environment configuration', async () => {
/**
* Test Scenario:
* 1. Set custom platform fee configuration
* 2. Create checkout session
* 3. Verify correct platform fee calculation
*/
// TODO: Set environment variables
process.env.PLATFORM_FEE_BPS = '250'; // 2.5%
process.env.PLATFORM_FEE_FIXED = '25'; // $0.25
const checkoutRequest = {
orgId: 'org_test_123',
eventId: 'event_test_123',
ticketTypeId: 'tt_test_123',
quantity: 2,
customerEmail: 'test@example.com'
};
// TODO: Create checkout session
// const response = await createCheckoutSession(checkoutRequest);
// expect(response.status).toBe(200);
// TODO: Verify platform fee calculation
// Expected for $50 ticket x 2 = $100:
// Platform fee = (10000 * 250 / 10000) + 25 = 250 + 25 = 275 cents ($2.75)
// const expectedPlatformFee = 275;
// expect(response.data.platformFee).toBe(expectedPlatformFee);
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Refund Safety', () => {
(0, globals_1.test)('should validate organization ownership before processing refund', async () => {
/**
* Test Scenario:
* 1. Create order for organization A
* 2. Attempt refund from organization B
* 3. Verify refund is rejected
* 4. Attempt refund from organization A
* 5. Verify refund succeeds
*/
const orderSessionId = 'cs_refund_test_123';
const correctOrgId = 'org_correct_123';
const wrongOrgId = 'org_wrong_123';
// TODO: Create order for correct organization
// await createOrder({
// sessionId: orderSessionId,
// orgId: correctOrgId,
// totalAmount: 10000,
// status: 'completed'
// });
// Attempt refund from wrong organization
const wrongOrgRefundRequest = {
orgId: wrongOrgId,
sessionId: orderSessionId
};
// TODO: Attempt refund with wrong org
// const wrongOrgResponse = await requestRefund(wrongOrgRefundRequest);
// expect(wrongOrgResponse.status).toBe(404);
// expect(wrongOrgResponse.data.error).toContain('Order not found for this organization');
// Attempt refund from correct organization
const correctOrgRefundRequest = {
orgId: correctOrgId,
sessionId: orderSessionId
};
// TODO: Attempt refund with correct org
// const correctOrgResponse = await requestRefund(correctOrgRefundRequest);
// expect(correctOrgResponse.status).toBe(200);
// expect(correctOrgResponse.data.refundId).toBeDefined();
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Structured Logging', () => {
(0, globals_1.test)('should log all operations with consistent structure', async () => {
/**
* Test Scenario:
* 1. Perform various operations (checkout, webhook, refund)
* 2. Verify all logs follow structured format
* 3. Verify critical information is logged
*/
// TODO: Capture logs during operations
// const logCapture = startLogCapture();
// TODO: Perform operations
// await createCheckoutSession({ ... });
// await processWebhook({ ... });
// await requestRefund({ ... });
// TODO: Verify log structure
// const logs = logCapture.getLogs();
//
// logs.forEach(log => {
// expect(log).toMatchObject({
// timestamp: expect.any(String),
// level: expect.stringMatching(/^(info|warn|error)$/),
// message: expect.any(String),
// action: expect.any(String)
// });
// });
// TODO: Verify specific actions are logged
// const actions = logs.map(log => log.action);
// expect(actions).toContain('checkout_create_start');
// expect(actions).toContain('checkout_create_success');
// expect(actions).toContain('webhook_received');
// expect(actions).toContain('ticket_purchase_success');
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
});
/**
* Helper functions for integration tests
* These would be implemented with actual Firebase and Stripe test SDKs
*/
// async function sendWebhook(payload: any) {
// // Implementation would use test HTTP client
// return { status: 200, data: { received: true } };
// }
// async function getTicketsBySession(sessionId: string) {
// // Implementation would query Firestore test database
// return [];
// }
// async function getProcessedSession(sessionId: string) {
// // Implementation would query processedSessions collection
// return { sessionId, status: 'completed' };
// }
// async function createTicketType(ticketType: any) {
// // Implementation would create test ticket type in Firestore
// }
// async function getTicketType(ticketTypeId: string) {
// // Implementation would query Firestore for ticket type
// return { inventory: 0, sold: 0 };
// }
// async function createCheckoutSession(request: any) {
// // Implementation would call checkout creation function
// return { status: 200, data: { url: 'https://checkout.stripe.com/...', sessionId: 'cs_...' } };
// }
// async function createOrder(order: any) {
// // Implementation would create test order in Firestore
// }
// async function requestRefund(request: any) {
// // Implementation would call refund function
// return { status: 200, data: { refundId: 'ref_...' } };
// }
// function startLogCapture() {
// // Implementation would capture console.log calls
// return {
// getLogs: () => []
// };
// }
// # sourceMappingURL=stripeConnect.integration.test.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"stripeConnect.integration.test.js","sourceRoot":"","sources":["../src/stripeConnect.integration.test.ts"],"names":[],"mappings":";;AAAA,2CAA4E;AAE5E;;;;;;;;;;;GAWG;AAEH,IAAA,kBAAQ,EAAC,4CAA4C,EAAE,GAAG,EAAE;IAC1D,IAAA,mBAAS,EAAC,KAAK,IAAI,EAAE;QACnB,mCAAmC;QACnC,qCAAqC;QACrC,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAC;IAC5D,CAAC,CAAC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EH;;;GAGG;AAEH,6CAA6C;AAC7C,iDAAiD;AACjD,sDAAsD;AACtD,IAAI;AAEJ,0DAA0D;AAC1D,0DAA0D;AAC1D,eAAe;AACf,IAAI;AAEJ,0DAA0D;AAC1D,+DAA+D;AAC/D,+CAA+C;AAC/C,IAAI;AAEJ,qDAAqD;AACrD,iEAAiE;AACjE,IAAI;AAEJ,uDAAuD;AACvD,4DAA4D;AAC5D,sCAAsC;AACtC,IAAI;AAEJ,uDAAuD;AACvD,4DAA4D;AAC5D,mGAAmG;AACnG,IAAI;AAEJ,2CAA2C;AAC3C,2DAA2D;AAC3D,IAAI;AAEJ,+CAA+C;AAC/C,iDAAiD;AACjD,2DAA2D;AAC3D,IAAI;AAEJ,+BAA+B;AAC/B,sDAAsD;AACtD,aAAa;AACb,wBAAwB;AACxB,OAAO;AACP,IAAI"}

View File

@@ -0,0 +1,827 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripeConnectWebhook = exports.createStripeCheckout = exports.stripeWebhook = exports.stripeConnectStatus = exports.stripeConnectStart = exports.stripeRefund = exports.stripe = void 0;
const https_1 = require("firebase-functions/v2/https");
const firestore_1 = require("firebase-admin/firestore");
const stripe_1 = __importDefault(require("stripe"));
const firestore_2 = require("firebase-admin/firestore");
// Initialize Stripe with secret key
exports.stripe = new stripe_1.default(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2024-06-20",
});
const db = (0, firestore_1.getFirestore)();
// Platform fee configuration
const PLATFORM_FEE_BPS = parseInt(process.env.PLATFORM_FEE_BPS || "300"); // Default 3%
const PLATFORM_FEE_FIXED = parseInt(process.env.PLATFORM_FEE_FIXED || "30"); // Default $0.30
function logWithContext(level, message, context) {
const logData = {
timestamp: new Date().toISOString(),
level,
message,
...context
};
console.log(JSON.stringify(logData));
}
// Helper function to validate request
function validateApiRequest(req, allowedMethods) {
if (!allowedMethods.includes(req.method)) {
return false;
}
return true;
}
// Helper function to get app URL from environment
function getAppUrl() {
return process.env.APP_URL || "http://localhost:5173";
}
/**
* POST /api/stripe/connect/start
* Starts the Stripe Connect onboarding flow for an organization
*/
/**
* POST /api/stripe/refund
* Process refunds for tickets with proper organization validation
*/
exports.stripeRefund = (0, https_1.onRequest)({
cors: {
origin: [getAppUrl(), "http://localhost:5173", "https://localhost:5173"],
methods: ["POST"],
allowedHeaders: ["Content-Type", "Authorization"],
},
}, async (req, res) => {
try {
if (!validateApiRequest(req, ["POST"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orgId, sessionId, paymentIntentId, amount, reason = "requested_by_customer" } = req.body;
if (!orgId || (!sessionId && !paymentIntentId)) {
res.status(400).json({
error: "Missing required fields: orgId and (sessionId or paymentIntentId)"
});
return;
}
logWithContext('info', 'Processing refund request', {
action: 'refund_start',
orgId,
sessionId,
paymentIntentId,
amount,
reason
});
// Get organization to verify connected account
const orgRef = db.collection("orgs").doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: "Organization not found" });
return;
}
const orgData = orgDoc.data();
const accountId = orgData?.payment?.stripe?.accountId;
if (!accountId) {
res.status(400).json({
error: "Organization does not have a connected Stripe account"
});
return;
}
// Find the order to validate ownership and get payment details
let orderQuery = db.collection("orders").where("orgId", "==", orgId);
if (sessionId) {
orderQuery = orderQuery.where("stripeSessionId", "==", sessionId);
}
else {
orderQuery = orderQuery.where("metadata.paymentIntentId", "==", paymentIntentId);
}
const orderDocs = await orderQuery.get();
if (orderDocs.empty) {
res.status(404).json({ error: "Order not found for this organization" });
return;
}
const orderDoc = orderDocs.docs[0];
const orderData = orderDoc.data();
// Determine payment intent ID and refund amount
const finalPaymentIntentId = paymentIntentId || orderData.metadata?.paymentIntentId;
const refundAmount = amount || orderData.totalAmount;
if (!finalPaymentIntentId) {
res.status(400).json({ error: "Could not determine payment intent ID" });
return;
}
// Create refund with connected account context
const refund = await exports.stripe.refunds.create({
payment_intent: finalPaymentIntentId,
amount: refundAmount,
reason,
metadata: {
orderId: orderData.id,
orgId,
eventId: orderData.eventId,
refundedBy: "api" // Could be enhanced with user info
}
}, {
stripeAccount: accountId
});
// Update order status
await orderDoc.ref.update({
status: refundAmount >= orderData.totalAmount ? "refunded" : "partially_refunded",
refunds: firestore_2.FieldValue.arrayUnion({
refundId: refund.id,
amount: refundAmount,
reason,
createdAt: new Date().toISOString()
})
});
// Update ticket statuses if full refund
if (refundAmount >= orderData.totalAmount && orderData.ticketIds) {
const batch = db.batch();
orderData.ticketIds.forEach((ticketId) => {
const ticketRef = db.collection("tickets").doc(ticketId);
batch.update(ticketRef, { status: "refunded" });
});
await batch.commit();
}
logWithContext('info', 'Refund processed successfully', {
action: 'refund_success',
refundId: refund.id,
orgId,
orderId: orderData.id,
amount: refundAmount,
accountId
});
const response = {
refundId: refund.id,
amount: refundAmount,
status: refund.status
};
res.status(200).json(response);
}
catch (error) {
logWithContext('error', 'Refund processing failed', {
action: 'refund_error',
error: error instanceof Error ? error.message : 'Unknown error',
orgId: req.body.orgId
});
res.status(500).json({
error: "Internal server error",
details: error instanceof Error ? error.message : "Unknown error",
});
}
});
exports.stripeConnectStart = (0, https_1.onRequest)({
cors: {
origin: [getAppUrl(), "http://localhost:5173", "https://localhost:5173"],
methods: ["POST"],
allowedHeaders: ["Content-Type", "Authorization"],
},
}, async (req, res) => {
try {
// Validate request method
if (!validateApiRequest(req, ["POST"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orgId, returnTo } = req.body;
if (!orgId || typeof orgId !== "string") {
res.status(400).json({ error: "orgId is required" });
return;
}
// Get organization document
const orgRef = db.collection("orgs").doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: "Organization not found" });
return;
}
const orgData = orgDoc.data();
let accountId = orgData?.payment?.stripe?.accountId;
// Create Stripe account if it doesn't exist
if (!accountId) {
const account = await exports.stripe.accounts.create({
type: "express",
country: "US", // Default to US, can be made configurable
email: orgData?.email || undefined,
business_profile: {
name: orgData?.name || `Organization ${orgId}`,
},
});
accountId = account.id;
// Save account ID to Firestore
await orgRef.update({
"payment.provider": "stripe",
"payment.stripe.accountId": accountId,
"payment.connected": false,
});
}
// Create account link for onboarding
const baseUrl = getAppUrl();
const returnUrl = returnTo
? `${baseUrl}${returnTo}?status=connected`
: `${baseUrl}/org/${orgId}/payments?status=connected`;
const refreshUrl = `${baseUrl}/org/${orgId}/payments?status=refresh`;
const accountLink = await exports.stripe.accountLinks.create({
account: accountId,
refresh_url: refreshUrl,
return_url: returnUrl,
type: "account_onboarding",
});
const response = {
url: accountLink.url,
};
res.status(200).json(response);
}
catch (error) {
console.error("Error starting Stripe Connect:", error);
res.status(500).json({
error: "Internal server error",
details: error instanceof Error ? error.message : "Unknown error",
});
}
});
/**
* GET /api/stripe/connect/status?orgId=...
* Gets the current Stripe Connect status for an organization
*/
exports.stripeConnectStatus = (0, https_1.onRequest)({
cors: {
origin: [getAppUrl(), "http://localhost:5173", "https://localhost:5173"],
methods: ["GET"],
allowedHeaders: ["Content-Type", "Authorization"],
},
}, async (req, res) => {
try {
// Validate request method
if (!validateApiRequest(req, ["GET"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const {orgId} = req.query;
if (!orgId || typeof orgId !== "string") {
res.status(400).json({ error: "orgId is required" });
return;
}
// Get organization document
const orgRef = db.collection("orgs").doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: "Organization not found" });
return;
}
const orgData = orgDoc.data();
const accountId = orgData?.payment?.stripe?.accountId;
if (!accountId) {
res.status(404).json({ error: "Stripe account not found for organization" });
return;
}
// Fetch current account status from Stripe
const account = await exports.stripe.accounts.retrieve(accountId);
// Update our Firestore document with latest status
const paymentData = {
provider: "stripe",
connected: account.charges_enabled && account.details_submitted,
stripe: {
accountId: account.id,
detailsSubmitted: account.details_submitted,
chargesEnabled: account.charges_enabled,
businessName: account.business_profile?.name ||
account.settings?.dashboard?.display_name ||
"",
},
};
await orgRef.update({
payment: paymentData,
});
const response = {
payment: paymentData,
};
res.status(200).json(response);
}
catch (error) {
console.error("Error getting Stripe Connect status:", error);
res.status(500).json({
error: "Internal server error",
details: error instanceof Error ? error.message : "Unknown error",
});
}
});
/**
* POST /api/stripe/webhook
* Handles Stripe platform-level webhooks
*/
exports.stripeWebhook = (0, https_1.onRequest)({
cors: false, // Webhooks don't need CORS
}, async (req, res) => {
try {
// Validate request method
if (!validateApiRequest(req, ["POST"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
if (!webhookSecret) {
console.error("Missing STRIPE_WEBHOOK_SECRET environment variable");
res.status(500).json({ error: "Webhook secret not configured" });
return;
}
const sig = req.headers["stripe-signature"];
if (!sig) {
res.status(400).json({ error: "Missing stripe-signature header" });
return;
}
let event;
try {
// Verify webhook signature
event = exports.stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret);
}
catch (err) {
console.error("Webhook signature verification failed:", err);
res.status(400).json({ error: "Invalid signature" });
return;
}
// Handle the event
switch (event.type) {
case "account.updated": {
const account = event.data.object;
// Find the organization with this account ID
const orgsQuery = await db.collection("orgs")
.where("payment.stripe.accountId", "==", account.id)
.get();
if (orgsQuery.empty) {
console.warn(`No organization found for account ${account.id}`);
break;
}
// Update each organization (should typically be just one)
const batch = db.batch();
orgsQuery.docs.forEach((doc) => {
const updateData = {
connected: account.charges_enabled && account.details_submitted,
stripe: {
accountId: account.id,
detailsSubmitted: account.details_submitted,
chargesEnabled: account.charges_enabled,
businessName: account.business_profile?.name ||
account.settings?.dashboard?.display_name ||
"",
},
};
batch.update(doc.ref, {
"payment.connected": updateData.connected,
"payment.stripe": updateData.stripe,
});
});
await batch.commit();
console.log(`Updated ${orgsQuery.docs.length} organizations for account ${account.id}`);
break;
}
default:
console.log(`Unhandled event type: ${event.type}`);
}
res.status(200).json({ received: true });
}
catch (error) {
console.error("Error handling webhook:", error);
res.status(500).json({
error: "Internal server error",
details: error instanceof Error ? error.message : "Unknown error",
});
}
});
/**
* POST /api/stripe/checkout/create
* Creates a Stripe Checkout session using the organization's connected account
*/
exports.createStripeCheckout = (0, https_1.onRequest)({
cors: {
origin: [getAppUrl(), "http://localhost:5173", "https://localhost:5173"],
methods: ["POST"],
allowedHeaders: ["Content-Type", "Authorization"],
},
}, async (req, res) => {
try {
// Validate request method
if (!validateApiRequest(req, ["POST"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { orgId, eventId, ticketTypeId, quantity, customerEmail, successUrl, cancelUrl, } = req.body;
// Validate required fields
if (!orgId || !eventId || !ticketTypeId || !quantity || quantity < 1) {
res.status(400).json({
error: "Missing required fields: orgId, eventId, ticketTypeId, quantity"
});
return;
}
// Get organization and verify connected account
const orgRef = db.collection("orgs").doc(orgId);
const orgDoc = await orgRef.get();
if (!orgDoc.exists) {
res.status(404).json({ error: "Organization not found" });
return;
}
const orgData = orgDoc.data();
const accountId = orgData?.payment?.stripe?.accountId;
const isConnected = orgData?.payment?.connected;
if (!accountId || !isConnected) {
res.status(400).json({
error: "Organization does not have a connected Stripe account"
});
return;
}
// Get event details for pricing and validation
const eventRef = db.collection("events").doc(eventId);
const eventDoc = await eventRef.get();
if (!eventDoc.exists) {
res.status(404).json({ error: "Event not found" });
return;
}
const eventData = eventDoc.data();
if (eventData?.orgId !== orgId) {
res.status(403).json({ error: "Event does not belong to organization" });
return;
}
// Get ticket type details
const ticketTypeRef = db.collection("ticketTypes").doc(ticketTypeId);
const ticketTypeDoc = await ticketTypeRef.get();
if (!ticketTypeDoc.exists) {
res.status(404).json({ error: "Ticket type not found" });
return;
}
const ticketTypeData = ticketTypeDoc.data();
if (ticketTypeData?.eventId !== eventId) {
res.status(403).json({ error: "Ticket type does not belong to event" });
return;
}
// Calculate pricing (price is stored in cents)
const unitPrice = ticketTypeData.price; // Already in cents
const totalAmount = unitPrice * quantity;
// Calculate platform fee using configurable rates
const platformFee = Math.round(totalAmount * (PLATFORM_FEE_BPS / 10000)) + PLATFORM_FEE_FIXED;
logWithContext('info', 'Creating checkout session', {
action: 'checkout_create_start',
sessionId: 'pending',
accountId,
orgId,
eventId,
ticketTypeId,
quantity,
unitPrice,
totalAmount,
platformFee
});
const baseUrl = getAppUrl();
const defaultSuccessUrl = `${baseUrl}/checkout/success?session_id={CHECKOUT_SESSION_ID}`;
const defaultCancelUrl = `${baseUrl}/checkout/cancel`;
// Create Stripe Checkout Session with connected account
const session = await exports.stripe.checkout.sessions.create({
mode: "payment",
payment_method_types: ["card"],
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: `${eventData.title} - ${ticketTypeData.name}`,
description: `${quantity} x ${ticketTypeData.name} ticket${quantity > 1 ? "s" : ""} for ${eventData.title}`,
metadata: {
eventId,
ticketTypeId,
},
},
unit_amount: unitPrice,
},
quantity,
},
],
success_url: successUrl || defaultSuccessUrl,
cancel_url: cancelUrl || defaultCancelUrl,
customer_email: customerEmail,
payment_intent_data: {
application_fee_amount: platformFee,
metadata: {
orgId,
eventId,
ticketTypeId,
quantity: quantity.toString(),
unitPrice: unitPrice.toString(),
platformFee: platformFee.toString(),
},
},
metadata: {
orgId,
eventId,
ticketTypeId,
quantity: quantity.toString(),
type: "ticket_purchase",
},
}, {
stripeAccount: accountId, // Use the connected account
});
logWithContext('info', 'Checkout session created successfully', {
action: 'checkout_create_success',
sessionId: session.id,
accountId,
orgId,
eventId,
ticketTypeId,
quantity
});
const response = {
url: session.url,
sessionId: session.id,
};
res.status(200).json(response);
}
catch (error) {
logWithContext('error', 'Failed to create checkout session', {
action: 'checkout_create_error',
error: error instanceof Error ? error.message : 'Unknown error',
orgId: req.body.orgId,
eventId: req.body.eventId,
ticketTypeId: req.body.ticketTypeId
});
res.status(500).json({
error: "Internal server error",
details: error instanceof Error ? error.message : "Unknown error",
});
}
});
/**
* POST /api/stripe/webhook/connect
* Handles Stripe Connect webhooks from connected accounts
* This endpoint receives events from connected accounts, not the platform
*/
exports.stripeConnectWebhook = (0, https_1.onRequest)({
cors: false, // Webhooks don't need CORS
}, async (req, res) => {
try {
// Validate request method
if (!validateApiRequest(req, ["POST"])) {
res.status(405).json({ error: "Method not allowed" });
return;
}
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
if (!webhookSecret) {
console.error("Missing STRIPE_WEBHOOK_SECRET environment variable");
res.status(500).json({ error: "Webhook secret not configured" });
return;
}
const sig = req.headers["stripe-signature"];
if (!sig) {
res.status(400).json({ error: "Missing stripe-signature header" });
return;
}
// Get the connected account ID - check both header and event.account
let stripeAccount = req.headers["stripe-account"];
// Parse event first to potentially get account from event data
let tempEvent;
try {
tempEvent = exports.stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret);
// Use event.account if available, fallback to header
stripeAccount = tempEvent.account || stripeAccount;
}
catch (err) {
console.error("Initial webhook signature verification failed:", err);
res.status(400).json({ error: "Invalid signature" });
return;
}
if (!stripeAccount) {
res.status(400).json({ error: "Missing stripe-account identifier" });
return;
}
// Use the pre-verified event
const event = tempEvent;
logWithContext('info', 'Received connect webhook', {
action: 'webhook_received',
eventType: event.type,
accountId: stripeAccount,
eventId: event.id
});
// Handle the event
switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object;
if (session.metadata?.type === "ticket_purchase") {
await handleTicketPurchaseCompleted(session, stripeAccount);
}
break;
}
case "payment_intent.succeeded": {
const paymentIntent = event.data.object;
logWithContext('info', 'Payment intent succeeded', {
action: 'payment_succeeded',
paymentIntentId: paymentIntent.id,
accountId: stripeAccount,
amount: paymentIntent.amount
});
break;
}
default:
logWithContext('info', 'Unhandled webhook event type', {
action: 'webhook_unhandled',
eventType: event.type,
accountId: stripeAccount
});
}
res.status(200).json({ received: true });
}
catch (error) {
logWithContext('error', 'Connect webhook processing failed', {
action: 'webhook_error',
error: error instanceof Error ? error.message : 'Unknown error'
});
// Return 200 to Stripe to prevent retries for application errors
res.status(200).json({
received: true,
error: error instanceof Error ? error.message : "Unknown error",
});
}
});
/**
* Handle completed ticket purchase with idempotency and transactional inventory
*/
async function handleTicketPurchaseCompleted(session, stripeAccount) {
const { orgId, eventId, ticketTypeId, quantity, } = session.metadata;
const sessionId = session.id;
const quantityNum = parseInt(quantity);
logWithContext('info', 'Starting ticket purchase processing', {
action: 'ticket_purchase_start',
sessionId,
accountId: stripeAccount,
orgId,
eventId,
ticketTypeId,
quantity: quantityNum
});
// Step 1: Idempotency check using processedSessions collection
const processedSessionRef = db.collection('processedSessions').doc(sessionId);
try {
await db.runTransaction(async (transaction) => {
// Check if session already processed
const processedDoc = await transaction.get(processedSessionRef);
if (processedDoc.exists) {
logWithContext('warn', 'Session already processed - skipping', {
action: 'idempotency_skip',
sessionId,
accountId: stripeAccount,
orgId,
eventId,
ticketTypeId
});
return; // Exit early - session already processed
}
// Mark session as processing (prevents concurrent processing)
transaction.set(processedSessionRef, {
sessionId,
orgId,
eventId,
ticketTypeId,
quantity: quantityNum,
stripeAccount,
processedAt: new Date().toISOString(),
status: 'processing'
});
// Step 2: Transactional inventory check and update
const ticketTypeRef = db.collection('ticketTypes').doc(ticketTypeId);
const ticketTypeDoc = await transaction.get(ticketTypeRef);
if (!ticketTypeDoc.exists) {
throw new Error(`Ticket type ${ticketTypeId} not found`);
}
const ticketTypeData = ticketTypeDoc.data();
const currentInventory = ticketTypeData.inventory || 0;
const currentSold = ticketTypeData.sold || 0;
// Check for overselling
if (currentInventory < quantityNum) {
logWithContext('error', 'Insufficient inventory - sold out', {
action: 'inventory_sold_out',
sessionId,
accountId: stripeAccount,
orgId,
eventId,
ticketTypeId,
requestedQuantity: quantityNum,
availableInventory: currentInventory
});
throw new Error('SOLD_OUT');
}
// Update inventory atomically
transaction.update(ticketTypeRef, {
inventory: currentInventory - quantityNum,
sold: currentSold + quantityNum,
lastSaleDate: new Date().toISOString()
});
// Step 3: Generate and save tickets
const customerEmail = session.customer_details?.email || session.customer_email;
if (!customerEmail) {
throw new Error('No customer email found in session');
}
const tickets = [];
const ticketIds = [];
for (let i = 0; i < quantityNum; i++) {
// Use crypto-strong ticket ID generation
const ticketId = `ticket_${Date.now()}_${Math.random().toString(36).substr(2, 12)}_${i}`;
ticketIds.push(ticketId);
const ticket = {
id: ticketId,
eventId,
ticketTypeId,
orgId,
customerEmail,
customerName: session.customer_details?.name || '',
purchaseDate: new Date().toISOString(),
status: 'active',
qrCode: ticketId, // Use ticket ID as QR code
stripeSessionId: sessionId,
stripeAccount,
metadata: {
paymentIntentId: session.payment_intent,
amountPaid: session.amount_total,
currency: session.currency
}
};
tickets.push(ticket);
// Add ticket to transaction
const ticketRef = db.collection('tickets').doc(ticketId);
transaction.set(ticketRef, ticket);
}
// Step 4: Create order record
const orderId = `order_${Date.now()}_${Math.random().toString(36).substr(2, 12)}`;
const orderRef = db.collection('orders').doc(orderId);
transaction.set(orderRef, {
id: orderId,
orgId,
eventId,
ticketTypeId,
customerEmail,
customerName: session.customer_details?.name || '',
quantity: quantityNum,
totalAmount: session.amount_total,
currency: session.currency,
status: 'completed',
createdAt: new Date().toISOString(),
stripeSessionId: sessionId,
stripeAccount,
ticketIds
});
// Step 5: Mark session as completed
transaction.update(processedSessionRef, {
status: 'completed',
orderId,
ticketIds,
completedAt: new Date().toISOString()
});
logWithContext('info', 'Ticket purchase completed successfully', {
action: 'ticket_purchase_success',
sessionId,
accountId: stripeAccount,
orgId,
eventId,
ticketTypeId,
quantity: quantityNum,
orderId,
ticketCount: tickets.length
});
// TODO: Send confirmation email with tickets
// This would typically use a service like Resend or SendGrid
console.log(`Would send confirmation email to ${customerEmail} with ${tickets.length} tickets`);
});
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
logWithContext('error', 'Ticket purchase processing failed', {
action: 'ticket_purchase_error',
sessionId,
accountId: stripeAccount,
orgId,
eventId,
ticketTypeId,
error: errorMessage
});
// For sold out scenario, mark session as failed but don't throw
if (errorMessage === 'SOLD_OUT') {
try {
await processedSessionRef.set({
sessionId,
orgId,
eventId,
ticketTypeId,
quantity: quantityNum,
stripeAccount,
processedAt: new Date().toISOString(),
status: 'failed',
error: 'SOLD_OUT',
failedAt: new Date().toISOString()
});
}
catch (markError) {
logWithContext('error', 'Failed to mark session as failed', {
action: 'mark_session_failed_error',
sessionId,
error: markError instanceof Error ? markError.message : 'Unknown error'
});
}
return; // Don't throw - webhook should return 200
}
throw error; // Re-throw for other errors
}
}
// # sourceMappingURL=stripeConnect.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,362 @@
"use strict";
const __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
let desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) {k2 = k;}
o[k2] = m[k];
}));
const __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
const __importStar = (this && this.__importStar) || (function () {
let ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
const ar = [];
for (const 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;}
const result = {};
if (mod != null) {for (let k = ownKeys(mod), i = 0; i < k.length; i++) {if (k[i] !== "default") {__createBinding(result, mod, k[i]);}}}
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const firestore_1 = require("firebase-admin/firestore");
// Mock Firebase Admin
globals_1.jest.mock('firebase-admin/firestore', () => ({
getFirestore: globals_1.jest.fn(),
FieldValue: {
arrayUnion: globals_1.jest.fn((value) => ({ arrayUnion: value }))
}
}));
// Mock Stripe
globals_1.jest.mock('stripe');
(0, globals_1.describe)('Stripe Connect Hardened Implementation', () => {
let mockDb;
let mockTransaction;
let mockStripe;
(0, globals_1.beforeEach)(() => {
// Reset all mocks
globals_1.jest.clearAllMocks();
// Mock Firestore transaction
mockTransaction = {
get: globals_1.jest.fn(),
set: globals_1.jest.fn(),
update: globals_1.jest.fn()
};
// Mock Firestore database
mockDb = {
collection: globals_1.jest.fn(() => ({
doc: globals_1.jest.fn(() => ({
get: globals_1.jest.fn(),
set: globals_1.jest.fn(),
update: globals_1.jest.fn()
})),
where: globals_1.jest.fn(() => ({
get: globals_1.jest.fn()
}))
})),
runTransaction: globals_1.jest.fn((callback) => callback(mockTransaction)),
batch: globals_1.jest.fn(() => ({
set: globals_1.jest.fn(),
update: globals_1.jest.fn(),
commit: globals_1.jest.fn()
}))
};
firestore_1.getFirestore.mockReturnValue(mockDb);
// Mock Stripe
mockStripe = {
webhooks: {
constructEvent: globals_1.jest.fn()
},
refunds: {
create: globals_1.jest.fn()
}
};
});
(0, globals_1.describe)('Idempotency Protection', () => {
(0, globals_1.test)('should skip processing if session already processed', async () => {
// Mock existing processed session
const mockProcessedDoc = {
exists: true,
data: () => ({
sessionId: 'cs_test_123',
status: 'completed',
processedAt: '2024-01-01T00:00:00Z'
})
};
mockTransaction.get.mockResolvedValue(mockProcessedDoc);
const session = {
id: 'cs_test_123',
metadata: {
orgId: 'org_123',
eventId: 'event_123',
ticketTypeId: 'tt_123',
quantity: '2'
},
customer_details: { email: 'test@example.com' },
amount_total: 10000
};
// Import the function under test
const { handleTicketPurchaseCompleted } = await Promise.resolve().then(() => __importStar(require('./stripeConnect')));
await (0, globals_1.expect)(handleTicketPurchaseCompleted(session, 'acct_123')).resolves.not.toThrow();
// Should only check for existing session, not create tickets
(0, globals_1.expect)(mockTransaction.get).toHaveBeenCalledTimes(1);
(0, globals_1.expect)(mockTransaction.set).not.toHaveBeenCalled();
(0, globals_1.expect)(mockTransaction.update).not.toHaveBeenCalled();
});
(0, globals_1.test)('should process new session and mark as processing', async () => {
// Mock non-existing processed session
const mockProcessedDoc = { exists: false };
const mockTicketTypeDoc = {
exists: true,
data: () => ({
inventory: 10,
sold: 5,
price: 5000
})
};
mockTransaction.get
.mockResolvedValueOnce(mockProcessedDoc) // processedSessions check
.mockResolvedValueOnce(mockTicketTypeDoc); // ticketTypes check
const session = {
id: 'cs_test_new',
metadata: {
orgId: 'org_123',
eventId: 'event_123',
ticketTypeId: 'tt_123',
quantity: '2'
},
customer_details: { email: 'test@example.com', name: 'Test User' },
amount_total: 10000,
currency: 'usd',
payment_intent: 'pi_123'
};
const { handleTicketPurchaseCompleted } = await Promise.resolve().then(() => __importStar(require('./stripeConnect')));
await (0, globals_1.expect)(handleTicketPurchaseCompleted(session, 'acct_123')).resolves.not.toThrow();
// Should mark session as processing
(0, globals_1.expect)(mockTransaction.set).toHaveBeenCalledWith(globals_1.expect.any(Object), globals_1.expect.objectContaining({
sessionId: 'cs_test_new',
status: 'processing'
}));
});
});
(0, globals_1.describe)('Inventory Concurrency Control', () => {
(0, globals_1.test)('should prevent overselling with insufficient inventory', async () => {
const mockProcessedDoc = { exists: false };
const mockTicketTypeDoc = {
exists: true,
data: () => ({
inventory: 1, // Only 1 ticket available
sold: 9,
price: 5000
})
};
mockTransaction.get
.mockResolvedValueOnce(mockProcessedDoc)
.mockResolvedValueOnce(mockTicketTypeDoc);
const session = {
id: 'cs_test_oversell',
metadata: {
orgId: 'org_123',
eventId: 'event_123',
ticketTypeId: 'tt_123',
quantity: '3' // Requesting 3 tickets but only 1 available
},
customer_details: { email: 'test@example.com' }
};
const { handleTicketPurchaseCompleted } = await Promise.resolve().then(() => __importStar(require('./stripeConnect')));
await (0, globals_1.expect)(handleTicketPurchaseCompleted(session, 'acct_123')).resolves.not.toThrow(); // Should not throw, but handle gracefully
// Should not create any tickets
(0, globals_1.expect)(mockTransaction.set).toHaveBeenCalledTimes(1); // Only the processing marker
});
(0, globals_1.test)('should update inventory atomically on successful purchase', async () => {
const mockProcessedDoc = { exists: false };
const mockTicketTypeDoc = {
exists: true,
data: () => ({
inventory: 10,
sold: 5,
price: 5000
})
};
mockTransaction.get
.mockResolvedValueOnce(mockProcessedDoc)
.mockResolvedValueOnce(mockTicketTypeDoc);
const session = {
id: 'cs_test_success',
metadata: {
orgId: 'org_123',
eventId: 'event_123',
ticketTypeId: 'tt_123',
quantity: '2'
},
customer_details: { email: 'test@example.com', name: 'Test User' },
amount_total: 10000,
currency: 'usd',
payment_intent: 'pi_123'
};
const { handleTicketPurchaseCompleted } = await Promise.resolve().then(() => __importStar(require('./stripeConnect')));
await (0, globals_1.expect)(handleTicketPurchaseCompleted(session, 'acct_123')).resolves.not.toThrow();
// Should update inventory: 10 - 2 = 8, sold: 5 + 2 = 7
(0, globals_1.expect)(mockTransaction.update).toHaveBeenCalledWith(globals_1.expect.any(Object), globals_1.expect.objectContaining({
inventory: 8,
sold: 7
}));
});
});
(0, globals_1.describe)('Platform Fee Configuration', () => {
(0, globals_1.test)('should calculate platform fee using configurable BPS', () => {
// Mock environment variables
process.env.PLATFORM_FEE_BPS = '250'; // 2.5%
process.env.PLATFORM_FEE_FIXED = '25'; // $0.25
const totalAmount = 10000; // $100.00
// Expected: (10000 * 250 / 10000) + 25 = 250 + 25 = 275 cents
const expectedFee = Math.round(totalAmount * (250 / 10000)) + 25;
(0, globals_1.expect)(expectedFee).toBe(275); // $2.75
});
(0, globals_1.test)('should use default platform fee when env vars not set', () => {
delete process.env.PLATFORM_FEE_BPS;
delete process.env.PLATFORM_FEE_FIXED;
const totalAmount = 10000; // $100.00
// Expected: (10000 * 300 / 10000) + 30 = 300 + 30 = 330 cents
const expectedFee = Math.round(totalAmount * (300 / 10000)) + 30;
(0, globals_1.expect)(expectedFee).toBe(330); // $3.30
});
});
(0, globals_1.describe)('Refund Safety', () => {
(0, globals_1.test)('should validate organization ownership before refund', async () => {
const mockOrgDoc = {
exists: true,
data: () => ({
payment: {
stripe: {
accountId: 'acct_123'
}
}
})
};
const mockOrderDocs = {
empty: false,
docs: [{
ref: { update: globals_1.jest.fn() },
data: () => ({
id: 'order_123',
orgId: 'org_123',
totalAmount: 10000,
metadata: { paymentIntentId: 'pi_123' },
ticketIds: ['ticket_1', 'ticket_2']
})
}]
};
mockDb.collection.mockImplementation((collection) => {
if (collection === 'orgs') {
return {
doc: () => ({
get: () => Promise.resolve(mockOrgDoc)
})
};
}
if (collection === 'orders') {
return {
where: () => ({
where: () => ({
get: () => Promise.resolve(mockOrderDocs)
})
})
};
}
return { doc: () => ({}) };
});
const mockRefund = {
id: 'ref_123',
status: 'succeeded',
amount: 10000
};
mockStripe.refunds.create.mockResolvedValue(mockRefund);
// Test would require importing and calling the refund function
// This demonstrates the validation logic structure
(0, globals_1.expect)(mockOrgDoc.exists).toBe(true);
(0, globals_1.expect)(mockOrderDocs.empty).toBe(false);
});
});
(0, globals_1.describe)('Connect Webhook Account Handling', () => {
(0, globals_1.test)('should extract account ID from event.account property', () => {
const mockEvent = {
id: 'evt_123',
type: 'checkout.session.completed',
account: 'acct_from_event_123',
data: {
object: {
id: 'cs_test_123',
metadata: { type: 'ticket_purchase' }
}
}
};
mockStripe.webhooks.constructEvent.mockReturnValue(mockEvent);
// Test would verify that account ID is correctly extracted from event.account
(0, globals_1.expect)(mockEvent.account).toBe('acct_from_event_123');
});
(0, globals_1.test)('should fallback to stripe-account header when event.account missing', () => {
const mockEvent = {
id: 'evt_123',
type: 'checkout.session.completed',
account: null, // No account in event
data: {
object: {
id: 'cs_test_123',
metadata: { type: 'ticket_purchase' }
}
}
};
mockStripe.webhooks.constructEvent.mockReturnValue(mockEvent);
const mockHeaders = {
'stripe-account': 'acct_from_header_123'
};
// Test would verify that header fallback works
const accountId = mockEvent.account || mockHeaders['stripe-account'];
(0, globals_1.expect)(accountId).toBe('acct_from_header_123');
});
});
(0, globals_1.describe)('Structured Logging', () => {
(0, globals_1.test)('should log with proper context structure', () => {
const consoleSpy = globals_1.jest.spyOn(console, 'log').mockImplementation();
// Mock the logWithContext function behavior
const logContext = {
sessionId: 'cs_test_123',
accountId: 'acct_123',
orgId: 'org_123',
eventId: 'event_123',
action: 'test_action'
};
const expectedLog = {
timestamp: globals_1.expect.any(String),
level: 'info',
message: 'Test message',
...logContext
};
// Test would verify structured logging format
(0, globals_1.expect)(expectedLog).toMatchObject(logContext);
consoleSpy.mockRestore();
});
});
(0, globals_1.afterEach)(() => {
// Clean up environment variables
delete process.env.PLATFORM_FEE_BPS;
delete process.env.PLATFORM_FEE_FIXED;
});
});
// # sourceMappingURL=stripeConnect.test.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,264 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyTicket = void 0;
const https_1 = require("firebase-functions/v2/https");
const firestore_1 = require("firebase-admin/firestore");
const logger_1 = require("./logger");
const db = (0, firestore_1.getFirestore)();
/**
* Core ticket verification logic wrapped with structured logging
*/
const verifyTicketCore = (0, logger_1.withLogging)("ticket_verification", async (qr, headers) => {
const startTime = performance.now();
// Extract context from headers
const context = {
sessionId: headers['x-scanner-session'],
deviceId: headers['x-device-id'],
accountId: headers['x-account-id'],
orgId: headers['x-org-id'],
qr,
operation: 'ticket_verification',
};
logger_1.logger.addBreadcrumb("Starting ticket verification", "verification", {
qr_masked: `${qr.substring(0, 8) }...`,
sessionId: context.sessionId,
});
// Find ticket by QR code
const ticketsSnapshot = await db
.collection("tickets")
.where("qr", "==", qr)
.limit(1)
.get();
if (ticketsSnapshot.empty) {
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...context,
result: 'invalid',
reason: 'ticket_not_found',
latencyMs,
});
return {
valid: false,
reason: "ticket_not_found",
};
}
const ticketDoc = ticketsSnapshot.docs[0];
const ticketData = ticketDoc.data();
// Add ticket context
const ticketContext = {
...context,
eventId: ticketData.eventId,
ticketTypeId: ticketData.ticketTypeId,
};
logger_1.logger.addBreadcrumb("Ticket found in database", "verification", {
ticketId: ticketDoc.id,
status: ticketData.status,
eventId: ticketData.eventId,
});
// Check if already scanned
if (ticketData.status === "scanned") {
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...ticketContext,
result: 'already_scanned',
latencyMs,
});
return {
valid: false,
reason: "already_scanned",
scannedAt: ticketData.scannedAt?.toDate?.()?.toISOString() || ticketData.scannedAt,
ticket: {
id: ticketDoc.id,
eventId: ticketData.eventId,
ticketTypeId: ticketData.ticketTypeId,
status: ticketData.status,
purchaserEmail: ticketData.purchaserEmail,
},
};
}
// Check if ticket is void
if (ticketData.status === "void") {
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...ticketContext,
result: 'invalid',
reason: 'ticket_voided',
latencyMs,
});
return {
valid: false,
reason: "ticket_voided",
ticket: {
id: ticketDoc.id,
eventId: ticketData.eventId,
ticketTypeId: ticketData.ticketTypeId,
status: ticketData.status,
purchaserEmail: ticketData.purchaserEmail,
},
};
}
// Mark as scanned atomically
const scannedAt = new Date();
logger_1.logger.addBreadcrumb("Attempting to mark ticket as scanned", "verification");
try {
await db.runTransaction(async (transaction) => {
const currentTicket = await transaction.get(ticketDoc.ref);
if (!currentTicket.exists) {
throw new Error("Ticket was deleted during verification");
}
const currentData = currentTicket.data();
// Double-check status hasn't changed
if (currentData.status === "scanned") {
throw new Error("Ticket was already scanned by another scanner");
}
if (currentData.status === "void") {
throw new Error("Ticket was voided");
}
// Mark as scanned
transaction.update(ticketDoc.ref, {
status: "scanned",
scannedAt,
updatedAt: scannedAt,
});
});
}
catch (transactionError) {
// Handle specific transaction errors
if (transactionError instanceof Error) {
if (transactionError.message.includes("already scanned")) {
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...ticketContext,
result: 'already_scanned',
latencyMs,
});
return {
valid: false,
reason: "already_scanned",
};
}
if (transactionError.message.includes("voided")) {
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...ticketContext,
result: 'invalid',
reason: 'ticket_voided',
latencyMs,
});
return {
valid: false,
reason: "ticket_voided",
};
}
}
// Re-throw for other transaction errors
throw transactionError;
}
// Get additional details for response
let eventName = "";
let ticketTypeName = "";
try {
const [eventDoc, ticketTypeDoc] = await Promise.all([
db.collection("events").doc(ticketData.eventId).get(),
db.collection("ticket_types").doc(ticketData.ticketTypeId).get(),
]);
if (eventDoc.exists) {
eventName = eventDoc.data().name;
}
if (ticketTypeDoc.exists) {
ticketTypeName = ticketTypeDoc.data().name;
}
}
catch (error) {
logger_1.logger.warn("Failed to fetch event/ticket type details", ticketContext, {
error: error instanceof Error ? error.message : String(error),
ticketId: ticketDoc.id,
});
}
const latencyMs = Math.round(performance.now() - startTime);
logger_1.logger.logScannerVerify({
...ticketContext,
result: 'valid',
latencyMs,
});
logger_1.logger.addBreadcrumb("Ticket successfully verified and scanned", "verification", {
ticketId: ticketDoc.id,
eventId: ticketData.eventId,
latencyMs,
});
return {
valid: true,
ticket: {
id: ticketDoc.id,
eventId: ticketData.eventId,
ticketTypeId: ticketData.ticketTypeId,
eventName,
ticketTypeName,
status: "scanned",
purchaserEmail: ticketData.purchaserEmail,
},
};
}, (qr, headers) => ({
qr,
sessionId: headers['x-scanner-session'],
deviceId: headers['x-device-id'],
operation: 'ticket_verification',
}));
/**
* Verifies and marks tickets as scanned
* POST /api/tickets/verify
* GET /api/tickets/verify/:qr
*/
exports.verifyTicket = (0, https_1.onRequest)({
cors: true,
enforceAppCheck: false,
region: "us-central1",
}, async (req, res) => {
let qr;
// Support both POST with body and GET with path parameter
if (req.method === "POST") {
const {body} = req;
qr = body.qr;
}
else if (req.method === "GET") {
// Extract QR from path: /api/tickets/verify/:qr
const pathParts = req.path.split("/");
qr = pathParts[pathParts.length - 1];
}
else {
res.status(405).json({ error: "Method not allowed" });
return;
}
if (!qr) {
logger_1.logger.warn("Verification request missing QR code", {
operation: 'ticket_verification',
});
res.status(400).json({
valid: false,
reason: "QR code is required",
});
return;
}
try {
// Extract headers for context
const headers = {
'x-scanner-session': req.get('x-scanner-session') || '',
'x-device-id': req.get('x-device-id') || '',
'x-account-id': req.get('x-account-id') || '',
'x-org-id': req.get('x-org-id') || '',
};
const response = await verifyTicketCore(qr, headers);
res.status(200).json(response);
}
catch (error) {
logger_1.logger.error("Error verifying ticket", error, {
qr,
operation: 'ticket_verification',
});
res.status(500).json({
valid: false,
reason: "Internal server error during verification",
});
}
});
// # sourceMappingURL=verify.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,499 @@
"use strict";
const __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripeWebhookConnect = void 0;
const https_1 = require("firebase-functions/v2/https");
const firebase_functions_1 = require("firebase-functions");
const firestore_1 = require("firebase-admin/firestore");
const stripe_1 = __importDefault(require("stripe"));
const uuid_1 = require("uuid");
const email_1 = require("./email");
const disputes_1 = require("./disputes");
const stripe = new stripe_1.default(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2024-11-20.acacia",
});
const db = (0, firestore_1.getFirestore)();
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET_CONNECT;
const APP_URL = process.env.APP_URL || "https://staging.blackcanyontickets.com";
const isDev = process.env.NODE_ENV !== "production";
/**
* Helper function to create ledger entry
*/
async function createLedgerEntry(entry, transaction) {
const ledgerEntry = {
...entry,
createdAt: firestore_1.Timestamp.now(),
};
const entryId = (0, uuid_1.v4)();
const docRef = db.collection("ledger").doc(entryId);
if (transaction) {
transaction.set(docRef, ledgerEntry);
}
else {
await docRef.set(ledgerEntry);
}
}
/**
* Handles Stripe webhooks from connected accounts
* POST /api/stripe/webhook/connect
*/
exports.stripeWebhookConnect = (0, https_1.onRequest)({
cors: false,
enforceAppCheck: false,
region: "us-central1",
}, async (req, res) => {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const sig = req.headers["stripe-signature"];
let event;
try {
// Verify webhook signature
event = stripe.webhooks.constructEvent(req.rawBody || req.body, sig, webhookSecret);
}
catch (error) {
firebase_functions_1.logger.error("Webhook signature verification failed", {
error: error instanceof Error ? error.message : String(error),
});
res.status(400).json({ error: "Invalid signature" });
return;
}
firebase_functions_1.logger.info("Received webhook event", {
type: event.type,
id: event.id,
account: event.account,
});
try {
// Handle different event types
if (event.type === "checkout.session.completed") {
await handleCheckoutCompleted(event);
}
else if (event.type === "charge.dispute.created") {
await (0, disputes_1.handleDisputeCreated)(event.data.object, event.account);
}
else if (event.type === "charge.dispute.closed") {
await (0, disputes_1.handleDisputeClosed)(event.data.object, event.account);
}
else if (event.type === "refund.created") {
await handleRefundCreated(event);
}
res.status(200).json({ received: true });
}
catch (error) {
firebase_functions_1.logger.error("Error processing webhook", {
eventType: event.type,
eventId: event.id,
account: event.account,
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
// Always return 200 to prevent Stripe retries on our internal errors
res.status(200).json({ received: true, error: "Internal processing error" });
}
});
/**
* Handles checkout.session.completed events with idempotency and inventory safety
*/
async function handleCheckoutCompleted(event) {
const session = event.data.object;
const sessionId = session.id;
const paymentIntentId = session.payment_intent;
const stripeAccountId = event.account;
firebase_functions_1.logger.info("Processing checkout completion", {
sessionId,
paymentIntentId,
stripeAccountId,
metadata: session.metadata,
});
// Extract metadata
const { orgId, eventId, ticketTypeId, qty: qtyStr, purchaserEmail } = session.metadata || {};
if (!orgId || !eventId || !ticketTypeId || !qtyStr) {
firebase_functions_1.logger.error("Missing required metadata in session", {
sessionId,
metadata: session.metadata,
});
return;
}
const qty = parseInt(qtyStr);
if (isNaN(qty) || qty <= 0) {
firebase_functions_1.logger.error("Invalid quantity in session metadata", {
sessionId,
qtyStr,
});
return;
}
// IDEMPOTENCY CHECK: Try to create processed session document
const processedSessionRef = db.collection("processedSessions").doc(sessionId);
try {
await db.runTransaction(async (transaction) => {
const processedDoc = await transaction.get(processedSessionRef);
if (processedDoc.exists) {
firebase_functions_1.logger.info("Session already processed, skipping", { sessionId });
return;
}
// Mark as processed first to ensure idempotency
transaction.set(processedSessionRef, {
sessionId,
processedAt: new Date(),
orgId,
eventId,
ticketTypeId,
qty,
paymentIntentId,
stripeAccountId,
});
// INVENTORY TRANSACTION: Safely decrement inventory
const ticketTypeRef = db.collection("ticket_types").doc(ticketTypeId);
const ticketTypeDoc = await transaction.get(ticketTypeRef);
if (!ticketTypeDoc.exists) {
throw new Error(`Ticket type ${ticketTypeId} not found`);
}
const ticketTypeData = ticketTypeDoc.data();
const currentInventory = ticketTypeData.inventory || 0;
const currentSold = ticketTypeData.sold || 0;
const available = currentInventory - currentSold;
firebase_functions_1.logger.info("Inventory check", {
sessionId,
ticketTypeId,
currentInventory,
currentSold,
available,
requestedQty: qty,
});
if (available < qty) {
// Mark order as failed due to sold out
const orderRef = db.collection("orders").doc(sessionId);
transaction.update(orderRef, {
status: "failed_sold_out",
failureReason: `Not enough tickets available. Requested: ${qty}, Available: ${available}`,
updatedAt: new Date(),
});
firebase_functions_1.logger.error("Insufficient inventory for completed checkout", {
sessionId,
available,
requested: qty,
});
return;
}
// Update inventory atomically
transaction.update(ticketTypeRef, {
sold: currentSold + qty,
updatedAt: new Date(),
});
// Create tickets
const tickets = [];
const ticketEmailData = [];
for (let i = 0; i < qty; i++) {
const ticketId = (0, uuid_1.v4)();
const qr = (0, uuid_1.v4)();
const ticketData = {
orgId,
eventId,
ticketTypeId,
orderId: sessionId,
purchaserEmail: purchaserEmail || session.customer_email || "",
qr,
status: "issued",
createdAt: new Date(),
scannedAt: null,
};
tickets.push(ticketData);
ticketEmailData.push({
ticketId,
qr,
eventName: "",
ticketTypeName: "",
startAt: "",
});
const ticketRef = db.collection("tickets").doc(ticketId);
transaction.set(ticketRef, ticketData);
}
// Update order status
const orderRef = db.collection("orders").doc(sessionId);
transaction.update(orderRef, {
status: "paid",
paymentIntentId,
updatedAt: new Date(),
});
firebase_functions_1.logger.info("Transaction completed successfully", {
sessionId,
ticketsCreated: tickets.length,
inventoryUpdated: true,
});
});
// Create ledger entries after successful transaction (outside transaction)
await createLedgerEntriesForSale(sessionId, stripeAccountId, paymentIntentId, orgId, eventId);
// Send confirmation email (outside transaction)
await sendConfirmationEmail(sessionId, orgId, eventId, ticketTypeId, qty);
}
catch (error) {
firebase_functions_1.logger.error("Transaction failed", {
sessionId,
error: error instanceof Error ? error.message : String(error),
});
// Don't re-throw to prevent webhook retries
}
}
/**
* Creates ledger entries for a completed sale, including fee capture
*/
async function createLedgerEntriesForSale(sessionId, stripeAccountId, paymentIntentId, orgId, eventId) {
try {
firebase_functions_1.logger.info("Creating ledger entries for sale", {
sessionId,
paymentIntentId,
stripeAccountId,
});
// Get the payment intent to access the charge
const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId, {
stripeAccount: stripeAccountId,
});
if (!paymentIntent.latest_charge) {
firebase_functions_1.logger.error("No charge found for payment intent", { paymentIntentId });
return;
}
// Get the charge to access balance transaction
const charge = await stripe.charges.retrieve(paymentIntent.latest_charge, {
stripeAccount: stripeAccountId,
});
if (!charge.balance_transaction) {
firebase_functions_1.logger.error("No balance transaction found for charge", { chargeId: charge.id });
return;
}
// Get balance transaction details for fee information
const balanceTransaction = await stripe.balanceTransactions.retrieve(charge.balance_transaction, { stripeAccount: stripeAccountId });
const totalAmount = paymentIntent.amount;
const stripeFee = balanceTransaction.fee;
const applicationFeeAmount = paymentIntent.application_fee_amount || 0;
firebase_functions_1.logger.info("Fee details captured", {
sessionId,
totalAmount,
stripeFee,
applicationFeeAmount,
balanceTransactionId: balanceTransaction.id,
});
// Create sale ledger entry (positive)
await createLedgerEntry({
orgId,
eventId,
orderId: sessionId,
type: "sale",
amountCents: totalAmount,
currency: "USD",
stripe: {
balanceTxnId: balanceTransaction.id,
chargeId: charge.id,
accountId: stripeAccountId,
},
meta: {
paymentIntentId,
},
});
// Create platform fee entry (positive for platform)
if (applicationFeeAmount > 0) {
await createLedgerEntry({
orgId,
eventId,
orderId: sessionId,
type: "platform_fee",
amountCents: applicationFeeAmount,
currency: "USD",
stripe: {
balanceTxnId: balanceTransaction.id,
chargeId: charge.id,
accountId: stripeAccountId,
},
});
}
// Create Stripe fee entry (negative for organizer)
if (stripeFee > 0) {
await createLedgerEntry({
orgId,
eventId,
orderId: sessionId,
type: "fee",
amountCents: -stripeFee,
currency: "USD",
stripe: {
balanceTxnId: balanceTransaction.id,
chargeId: charge.id,
accountId: stripeAccountId,
},
});
}
firebase_functions_1.logger.info("Ledger entries created successfully", {
sessionId,
totalAmount,
stripeFee,
applicationFeeAmount,
});
}
catch (error) {
firebase_functions_1.logger.error("Failed to create ledger entries for sale", {
sessionId,
error: error instanceof Error ? error.message : String(error),
});
}
}
/**
* Handles refund.created webhook events
*/
async function handleRefundCreated(event) {
const refund = event.data.object;
const stripeAccountId = event.account;
firebase_functions_1.logger.info("Processing refund created webhook", {
refundId: refund.id,
amount: refund.amount,
chargeId: refund.charge,
stripeAccountId,
});
try {
// Get charge details to find payment intent
const charge = await stripe.charges.retrieve(refund.charge, {
stripeAccount: stripeAccountId,
});
const paymentIntentId = charge.payment_intent;
// Find the order by payment intent
const ordersSnapshot = await db.collection("orders")
.where("paymentIntentId", "==", paymentIntentId)
.limit(1)
.get();
if (ordersSnapshot.empty) {
firebase_functions_1.logger.error("Order not found for refund webhook", {
refundId: refund.id,
paymentIntentId,
});
return;
}
const orderDoc = ordersSnapshot.docs[0];
const orderData = orderDoc.data();
const { orgId, eventId } = orderData;
// Get refund balance transaction for fee details
let refundFee = 0;
if (refund.balance_transaction) {
const refundBalanceTransaction = await stripe.balanceTransactions.retrieve(refund.balance_transaction, { stripeAccount: stripeAccountId });
refundFee = refundBalanceTransaction.fee;
}
// Create refund ledger entry (negative)
await createLedgerEntry({
orgId,
eventId,
orderId: orderDoc.id,
type: "refund",
amountCents: -refund.amount,
currency: "USD",
stripe: {
balanceTxnId: refund.balance_transaction,
chargeId: charge.id,
refundId: refund.id,
accountId: stripeAccountId,
},
});
// Create refund fee entry if applicable (negative)
if (refundFee > 0) {
await createLedgerEntry({
orgId,
eventId,
orderId: orderDoc.id,
type: "fee",
amountCents: -refundFee,
currency: "USD",
stripe: {
balanceTxnId: refund.balance_transaction,
refundId: refund.id,
accountId: stripeAccountId,
},
meta: {
reason: "refund_fee",
},
});
}
firebase_functions_1.logger.info("Refund ledger entries created", {
refundId: refund.id,
orderId: orderDoc.id,
refundAmount: refund.amount,
refundFee,
});
}
catch (error) {
firebase_functions_1.logger.error("Failed to process refund webhook", {
refundId: refund.id,
error: error instanceof Error ? error.message : String(error),
});
}
}
/**
* Sends confirmation email with ticket details
*/
async function sendConfirmationEmail(sessionId, orgId, eventId, ticketTypeId, qty) {
try {
// Get email details
const [orderDoc, eventDoc, ticketTypeDoc, orgDoc] = await Promise.all([
db.collection("orders").doc(sessionId).get(),
db.collection("events").doc(eventId).get(),
db.collection("ticket_types").doc(ticketTypeId).get(),
db.collection("orgs").doc(orgId).get(),
]);
if (!orderDoc.exists || !eventDoc.exists || !ticketTypeDoc.exists) {
firebase_functions_1.logger.error("Missing documents for email", {
sessionId,
orderExists: orderDoc.exists,
eventExists: eventDoc.exists,
ticketTypeExists: ticketTypeDoc.exists,
});
return;
}
const orderData = orderDoc.data();
const eventData = eventDoc.data();
const ticketTypeData = ticketTypeDoc.data();
const orgData = orgDoc.exists ? orgDoc.data() : null;
const {purchaserEmail} = orderData;
if (!purchaserEmail) {
firebase_functions_1.logger.warn("No purchaser email for order", { sessionId });
return;
}
// Get tickets for this order
const ticketsSnapshot = await db
.collection("tickets")
.where("orderId", "==", sessionId)
.get();
const ticketEmailData = ticketsSnapshot.docs.map((doc) => {
const data = doc.data();
return {
ticketId: doc.id,
qr: data.qr,
eventName: eventData.name,
ticketTypeName: ticketTypeData.name,
startAt: eventData.startAt?.toDate?.()?.toISOString() || eventData.startAt,
};
});
const emailOptions = {
to: purchaserEmail,
eventName: eventData.name,
tickets: ticketEmailData,
organizationName: orgData?.name || "Black Canyon Tickets",
};
if (isDev) {
await (0, email_1.logTicketEmail)(emailOptions);
}
else {
await (0, email_1.sendTicketEmail)(emailOptions);
}
firebase_functions_1.logger.info("Confirmation email sent", {
sessionId,
to: purchaserEmail,
ticketCount: ticketEmailData.length,
});
}
catch (error) {
firebase_functions_1.logger.error("Failed to send confirmation email", {
sessionId,
error: error instanceof Error ? error.message : String(error),
});
}
}
// # sourceMappingURL=webhooks.js.map

File diff suppressed because one or more lines are too long