@saasifier/billing
v0.1.1
Published
Provider-independent billing primitives for SaaSify — the `BillingProvider` interface and types that `@saasifier/core` builds subscription billing on top of, plus an in-memory mock provider for tests and local development.
Readme
@saasifier/billing
Provider-independent billing primitives for SaaSify — the BillingProvider interface and types that @saasifier/core builds subscription billing on top of, plus an in-memory mock provider for tests and local development.
Installation
npm install @saasifier/billing
# or
pnpm add @saasifier/billingOverview
SaaSify's core never talks to a payment provider's SDK directly. Instead, every provider (Stripe, and eventually Paddle, Lemon Squeezy, or a custom one) implements the same BillingProvider interface defined here, and @saasifier/core depends only on that interface. This package owns the provider-independent shape of a subscription, the webhook event types SaaSify understands, and a fully in-memory createMockBillingProvider implementation that lets you exercise checkout, subscription lifecycle, and webhook-signature-failure paths without any real billing credentials.
Usage
import { createMockBillingProvider } from "@saasifier/billing";
const billing = createMockBillingProvider({ webhookSecret: "test-secret" });
const { customerId } = await billing.createCustomer({
organizationId: "org_123",
email: "[email protected]"
});
const { url } = await billing.createCheckout({
organizationId: "org_123",
customerId,
planKey: "pro",
successUrl: "https://app.example.com/billing/success",
cancelUrl: "https://app.example.com/billing/cancel"
});
const subscription = await billing.createSubscription({
organizationId: "org_123",
customerId,
planKey: "pro"
});
// Verifying an inbound webhook payload (throws WebhookVerificationError on mismatch)
const event = billing.verifyWebhook({
payload: JSON.stringify({ id: "evt_1", type: "subscription.created", subscription }),
signature: "test-secret"
});In a real app, @saasifier/core is what actually calls a BillingProvider — this package is typically consumed indirectly, with @saasifier/billing's types used to type your own provider config or a custom adapter.
API Reference
BillingProvider
The interface every billing provider adapter (e.g. @saasifier/billing-stripe) implements. core depends only on this interface, never a provider SDK.
name: string— provider identifier (e.g."stripe","mock").createCustomer({ organizationId, email?, name? })— creates a billing customer for an organization, returns{ customerId }.createCheckout({ organizationId, customerId, planKey, successUrl, cancelUrl })— creates a hosted checkout session, returns{ url }.createSubscription({ organizationId, customerId, planKey })— creates a subscription directly (no checkout), returns aProviderSubscription.cancelSubscription({ subscriptionId })— cancels a subscription, returns the updatedProviderSubscription.changeSubscription({ subscriptionId, planKey })— changes the plan on an existing subscription.getSubscription({ subscriptionId })— fetches a subscription, ornullif it doesn't exist.createPortalSession({ customerId, returnUrl })— creates a hosted billing-management portal session, returns{ url }.verifyWebhook({ payload, signature })— verifies a webhook's signature and parses it into aBillingWebhookEvent. ThrowsWebhookVerificationError(from@saasifier/types) on a signature mismatch. Returnsnullfor an authentically-signed event whose type SaaSify doesn't model — callers should acknowledge it (HTTP 200) without further processing.
createMockBillingProvider(options?)
Returns a BillingProvider backed entirely by in-memory Maps — no network calls. Useful for unit tests, the playground app, and any environment without real billing credentials.
options.webhookSecret?: string— the signatureverifyWebhookrequires exact-match against (defaults to"mock-webhook-secret"). Lets you exercise signature-mismatch handling without a real provider.
Types
ProviderSubscription— normalized subscription state:providerSubscriptionId,providerCustomerId,organizationId,planKey,status(SubscriptionStatus),currentPeriodEnd?,cancelAtPeriodEnd.BillingEventType— the webhook event types SaaSify models:"subscription.created","subscription.updated","subscription.cancelled","payment.succeeded","payment.failed","invoice.created","invoice.paid".BillingWebhookEvent—{ id, type, subscription? }, the normalized result ofverifyWebhook.
Design notes
organizationIdis recovered by the adapter, not stored by this package — each provider is responsible for attaching and reading back the organization id (e.g. via Stripe customer/subscription metadata) so multi-tenant billing events can be routed to the right tenant.- Signature failures must throw, never degrade into a best-effort guess.
verifyWebhookeither returns a trustworthyBillingWebhookEvent, returnsnullfor an event type SaaSify doesn't handle, or throwsWebhookVerificationError. - This package intentionally has no dependency on any provider SDK — only
@saasifier/types.
