Initial import of zappier-edge from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 17:15:28 -04:00
commit b9dca6cd0f
130 changed files with 20724 additions and 0 deletions

41
src/billing/stripe.ts Normal file
View file

@ -0,0 +1,41 @@
import { UsageEntry } from '../usage';
export const METER_EVENT_NAME = 'zappier.api_cents';
export interface MeterEventClient {
createMeterEvent(params: {
eventName: string;
customerId: string;
value: string;
/** Stripe-side dedup key; events with the same identifier are dropped. */
identifier?: string;
}): Promise<void>;
}
export function computeBillableCents(entries: UsageEntry[], monthlyCreditCents: number): number {
const totalCents = entries.reduce((sum, e) => sum + e.cents, 0);
return Math.max(0, totalCents - monthlyCreditCents);
}
export function computeDelta(billableCents: number, previouslyReportedCents: number): number {
return Math.max(0, billableCents - previouslyReportedCents);
}
// Public API: retained for API compatibility. The monthly billing job now uses
// the delta-based path in src/jobs/report-usage.ts (reportMonthlyUsage), which
// shares computeBillableCents with this function. Do not remove.
export async function reportUsage(
client: MeterEventClient,
stripeCustomerId: string,
entries: UsageEntry[],
monthlyCreditCents: number,
): Promise<number> {
const billable = computeBillableCents(entries, monthlyCreditCents);
if (billable <= 0) return 0;
await client.createMeterEvent({
eventName: METER_EVENT_NAME,
customerId: stripeCustomerId,
value: String(billable),
});
return billable;
}