@bagou/payment-stripe
v0.2.0
Published
Stripe payment abstractions (StripeClient, CheckoutSession, WebhookEvent) for bagou-packages
Maintainers
Readme
@bagou/payment-stripe
Part of the bagou-packages monorepo.
Stripe payment abstractions built on top of the official Stripe SDK. Covers checkout sessions, webhooks, customers, subscriptions, products, discounts, and analytics — all with typed, injectable services.
Install
bun add @bagou/payment-stripeSet STRIPE_SECRET_KEY in your environment (e.g. via .env):
STRIPE_SECRET_KEY=sk_live_xxx
STRIPE_WEBHOOK_SECRET=whsec_xxxServices
| Class | Responsibility |
|-------|---------------|
| StripeClient | Holds the authenticated Stripe SDK instance |
| StripeProvider | High-level facade — checkout + webhooks |
| StripeCheckoutSession | Create and retrieve Checkout Sessions |
| StripeWebhookEvent | Verify and parse incoming webhook events |
| StripeCustomer | Customer CRUD + listing |
| StripeCustomerPortal | Create Billing Portal sessions |
| StripeDiscount | Coupon / promo-code management |
| StripeProducts | Product and price CRUD |
| StripeAnalytics | Revenue and subscription summary |
Usage
Checkout session
import { StripeProvider } from "@bagou/payment-stripe";
const provider = new StripeProvider();
const session = await provider.createCheckoutSession({
lineItems: [{ price: "price_xxx", quantity: 1 }],
mode: "subscription",
successUrl: "https://example.com/success",
cancelUrl: "https://example.com/cancel",
});
const retrieved = await provider.retrieveSession(session.id);Webhook verification
const event = await provider.constructWebhookEvent(
rawBody,
stripeSignatureHeader,
process.env.STRIPE_WEBHOOK_SECRET!,
);
if (event.type === "checkout.session.completed") {
const data = event.data; // CheckoutSessionCompletedDataType
}Customers
import { StripeClient, StripeCustomer } from "@bagou/payment-stripe";
const customer = new StripeCustomer(new StripeClient());
const created = await customer.create({ email: "[email protected]", name: "Alice" });
const updated = await customer.update(created.id, { name: "Alice B." });
const found = await customer.get(created.id);
const list = await customer.list({ limit: 20 });
await customer.remove(created.id);Billing Portal
import { StripeClient, StripeCustomerPortal } from "@bagou/payment-stripe";
const portal = new StripeCustomerPortal(new StripeClient());
const session = await portal.create({
customerId: "cus_xxx",
returnUrl: "https://example.com/account",
});
// Redirect the user to session.urlDiscounts (coupons)
import { StripeClient, StripeDiscount } from "@bagou/payment-stripe";
const discount = new StripeDiscount(new StripeClient());
const coupon = await discount.create({
name: "SUMMER20",
type: "percentage",
amount: 20,
duration: "once",
code: "SUMMER20",
});Products & prices
import { StripeClient, StripeProducts } from "@bagou/payment-stripe";
const products = new StripeProducts(new StripeClient());
const product = await products.create({ name: "Pro Plan" });
const price = await products.createPrice({
productId: product.id,
currency: "eur",
unitAmount: 1999,
type: "recurring",
interval: "month",
});Analytics
import { StripeClient, StripeAnalytics } from "@bagou/payment-stripe";
const analytics = new StripeAnalytics(new StripeClient());
const summary = await analytics.get({
startDate: new Date("2025-01-01"),
endDate: new Date("2025-12-31"),
currency: "eur",
});
console.log(summary.totalRevenue, summary.activeSubscriptions);Supported Webhook Events
| Event | Type constant |
|-------|--------------|
| checkout.session.completed | EStripeEvent.CheckoutSessionCompleted |
| invoice.paid | EStripeEvent.InvoicePaid |
| customer.subscription.deleted | EStripeEvent.CustomerSubscriptionDeleted |
| customer.subscription.updated | EStripeEvent.CustomerSubscriptionUpdated |
| payment_intent.payment_failed | EStripeEvent.PaymentIntentPaymentFailed |
License
MIT © Bagou
