@bagou/payment-stripe 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bagou
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # @bagou/payment-stripe
2
+
3
+ > Part of the [bagou-packages](../../README.md) monorepo.
4
+
5
+ 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.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ bun add @bagou/payment-stripe
11
+ ```
12
+
13
+ Set `STRIPE_SECRET_KEY` in your environment (e.g. via `.env`):
14
+
15
+ ```env
16
+ STRIPE_SECRET_KEY=sk_live_xxx
17
+ STRIPE_WEBHOOK_SECRET=whsec_xxx
18
+ ```
19
+
20
+ ## Services
21
+
22
+ | Class | Responsibility |
23
+ |-------|---------------|
24
+ | `StripeClient` | Holds the authenticated Stripe SDK instance |
25
+ | `StripeProvider` | High-level facade — checkout + webhooks |
26
+ | `StripeCheckoutSession` | Create and retrieve Checkout Sessions |
27
+ | `StripeWebhookEvent` | Verify and parse incoming webhook events |
28
+ | `StripeCustomer` | Customer CRUD + listing |
29
+ | `StripeCustomerPortal` | Create Billing Portal sessions |
30
+ | `StripeDiscount` | Coupon / promo-code management |
31
+ | `StripeProducts` | Product and price CRUD |
32
+ | `StripeAnalytics` | Revenue and subscription summary |
33
+
34
+ ## Usage
35
+
36
+ ### Checkout session
37
+
38
+ ```ts
39
+ import { StripeProvider } from "@bagou/payment-stripe";
40
+
41
+ const provider = new StripeProvider();
42
+
43
+ const session = await provider.createCheckoutSession({
44
+ lineItems: [{ price: "price_xxx", quantity: 1 }],
45
+ mode: "subscription",
46
+ successUrl: "https://example.com/success",
47
+ cancelUrl: "https://example.com/cancel",
48
+ });
49
+
50
+ const retrieved = await provider.retrieveSession(session.id);
51
+ ```
52
+
53
+ ### Webhook verification
54
+
55
+ ```ts
56
+ const event = await provider.constructWebhookEvent(
57
+ rawBody,
58
+ stripeSignatureHeader,
59
+ process.env.STRIPE_WEBHOOK_SECRET!,
60
+ );
61
+
62
+ if (event.type === "checkout.session.completed") {
63
+ const data = event.data; // CheckoutSessionCompletedDataType
64
+ }
65
+ ```
66
+
67
+ ### Customers
68
+
69
+ ```ts
70
+ import { StripeClient, StripeCustomer } from "@bagou/payment-stripe";
71
+
72
+ const customer = new StripeCustomer(new StripeClient());
73
+
74
+ const created = await customer.create({ email: "user@example.com", name: "Alice" });
75
+ const updated = await customer.update(created.id, { name: "Alice B." });
76
+ const found = await customer.get(created.id);
77
+ const list = await customer.list({ limit: 20 });
78
+ await customer.remove(created.id);
79
+ ```
80
+
81
+ ### Billing Portal
82
+
83
+ ```ts
84
+ import { StripeClient, StripeCustomerPortal } from "@bagou/payment-stripe";
85
+
86
+ const portal = new StripeCustomerPortal(new StripeClient());
87
+
88
+ const session = await portal.create({
89
+ customerId: "cus_xxx",
90
+ returnUrl: "https://example.com/account",
91
+ });
92
+
93
+ // Redirect the user to session.url
94
+ ```
95
+
96
+ ### Discounts (coupons)
97
+
98
+ ```ts
99
+ import { StripeClient, StripeDiscount } from "@bagou/payment-stripe";
100
+
101
+ const discount = new StripeDiscount(new StripeClient());
102
+
103
+ const coupon = await discount.create({
104
+ name: "SUMMER20",
105
+ type: "percentage",
106
+ amount: 20,
107
+ duration: "once",
108
+ code: "SUMMER20",
109
+ });
110
+ ```
111
+
112
+ ### Products & prices
113
+
114
+ ```ts
115
+ import { StripeClient, StripeProducts } from "@bagou/payment-stripe";
116
+
117
+ const products = new StripeProducts(new StripeClient());
118
+
119
+ const product = await products.create({ name: "Pro Plan" });
120
+
121
+ const price = await products.createPrice({
122
+ productId: product.id,
123
+ currency: "eur",
124
+ unitAmount: 1999,
125
+ type: "recurring",
126
+ interval: "month",
127
+ });
128
+ ```
129
+
130
+ ### Analytics
131
+
132
+ ```ts
133
+ import { StripeClient, StripeAnalytics } from "@bagou/payment-stripe";
134
+
135
+ const analytics = new StripeAnalytics(new StripeClient());
136
+
137
+ const summary = await analytics.get({
138
+ startDate: new Date("2025-01-01"),
139
+ endDate: new Date("2025-12-31"),
140
+ currency: "eur",
141
+ });
142
+
143
+ console.log(summary.totalRevenue, summary.activeSubscriptions);
144
+ ```
145
+
146
+ ## Supported Webhook Events
147
+
148
+ | Event | Type constant |
149
+ |-------|--------------|
150
+ | `checkout.session.completed` | `EStripeEvent.CheckoutSessionCompleted` |
151
+ | `invoice.paid` | `EStripeEvent.InvoicePaid` |
152
+ | `customer.subscription.deleted` | `EStripeEvent.CustomerSubscriptionDeleted` |
153
+ | `customer.subscription.updated` | `EStripeEvent.CustomerSubscriptionUpdated` |
154
+ | `payment_intent.payment_failed` | `EStripeEvent.PaymentIntentPaymentFailed` |
155
+
156
+ ## License
157
+
158
+ MIT © Bagou
@@ -0,0 +1,379 @@
1
+ import Stripe from "stripe";
2
+ declare class StripeClient {
3
+ private readonly client;
4
+ constructor();
5
+ get sdk(): Stripe;
6
+ }
7
+ declare enum EStripeEvent {
8
+ CheckoutSessionCompleted = "checkout.session.completed",
9
+ InvoicePaid = "invoice.paid",
10
+ CustomerSubscriptionDeleted = "customer.subscription.deleted",
11
+ CustomerSubscriptionUpdated = "customer.subscription.updated",
12
+ PaymentIntentPaymentFailed = "payment_intent.payment_failed"
13
+ }
14
+ type StripeEventType = `${EStripeEvent}`;
15
+ type LineItemType = {
16
+ price: string;
17
+ quantity?: number;
18
+ };
19
+ type CheckoutSessionCreateType = {
20
+ lineItems: LineItemType[];
21
+ mode: "payment" | "subscription" | "setup";
22
+ successUrl: string;
23
+ cancelUrl?: string;
24
+ customerId?: string;
25
+ customerEmail?: string;
26
+ metadata?: Record<string, string>;
27
+ };
28
+ type CheckoutSessionType = {
29
+ id: string;
30
+ url: string | null;
31
+ status: string | null;
32
+ paymentStatus: string;
33
+ customerId: string | null;
34
+ customerEmail: string | null;
35
+ amountTotal: number | null;
36
+ currency: string | null;
37
+ metadata: Record<string, string>;
38
+ };
39
+ type CheckoutSessionCompletedDataType = {
40
+ id: string;
41
+ customerId: string | null;
42
+ customerEmail: string | null;
43
+ amountTotal: number | null;
44
+ currency: string | null;
45
+ paymentStatus: string;
46
+ subscriptionId: string | null;
47
+ metadata: Record<string, string>;
48
+ };
49
+ type InvoicePaidDataType = {
50
+ id: string;
51
+ customerId: string | null;
52
+ subscriptionId: string | null;
53
+ amountPaid: number;
54
+ currency: string;
55
+ status: string | null;
56
+ hostedInvoiceUrl: string | null;
57
+ };
58
+ type CustomerSubscriptionDeletedDataType = {
59
+ id: string;
60
+ customerId: string | null;
61
+ status: string;
62
+ currentPeriodEnd: Date;
63
+ canceledAt: Date | null;
64
+ metadata: Record<string, string>;
65
+ };
66
+ type CustomerSubscriptionUpdatedDataType = {
67
+ id: string;
68
+ customerId: string | null;
69
+ status: string;
70
+ currentPeriodEnd: Date;
71
+ cancelAtPeriodEnd: boolean;
72
+ metadata: Record<string, string>;
73
+ };
74
+ type PaymentIntentPaymentFailedDataType = {
75
+ id: string;
76
+ customerId: string | null;
77
+ amount: number;
78
+ currency: string;
79
+ lastPaymentErrorMessage: string | null;
80
+ };
81
+ type CheckoutSessionCompletedEventType = {
82
+ type: EStripeEvent.CheckoutSessionCompleted;
83
+ id: string;
84
+ created: Date;
85
+ data: CheckoutSessionCompletedDataType;
86
+ };
87
+ type InvoicePaidEventType = {
88
+ type: EStripeEvent.InvoicePaid;
89
+ id: string;
90
+ created: Date;
91
+ data: InvoicePaidDataType;
92
+ };
93
+ type CustomerSubscriptionDeletedEventType = {
94
+ type: EStripeEvent.CustomerSubscriptionDeleted;
95
+ id: string;
96
+ created: Date;
97
+ data: CustomerSubscriptionDeletedDataType;
98
+ };
99
+ type CustomerSubscriptionUpdatedEventType = {
100
+ type: EStripeEvent.CustomerSubscriptionUpdated;
101
+ id: string;
102
+ created: Date;
103
+ data: CustomerSubscriptionUpdatedDataType;
104
+ };
105
+ type PaymentIntentPaymentFailedEventType = {
106
+ type: EStripeEvent.PaymentIntentPaymentFailed;
107
+ id: string;
108
+ created: Date;
109
+ data: PaymentIntentPaymentFailedDataType;
110
+ };
111
+ type WebhookEventType = CheckoutSessionCompletedEventType | InvoicePaidEventType | CustomerSubscriptionDeletedEventType | CustomerSubscriptionUpdatedEventType | PaymentIntentPaymentFailedEventType;
112
+ interface IPaymentProvider {
113
+ createCheckoutSession(data: CheckoutSessionCreateType): Promise<CheckoutSessionType>;
114
+ retrieveSession(id: string): Promise<CheckoutSessionType>;
115
+ constructWebhookEvent(payload: string | Buffer, signature: string, secret: string): Promise<WebhookEventType>;
116
+ }
117
+ type StripeCustomerAddressType = {
118
+ line1?: string;
119
+ line2?: string;
120
+ city?: string;
121
+ state?: string;
122
+ postalCode?: string;
123
+ country?: string;
124
+ };
125
+ type StripeCustomerCreateType = {
126
+ email: string;
127
+ name?: string;
128
+ phone?: string;
129
+ billingAddress?: StripeCustomerAddressType;
130
+ metadata?: Record<string, string>;
131
+ };
132
+ type StripeCustomerUpdateType = {
133
+ email?: string;
134
+ name?: string;
135
+ phone?: string;
136
+ billingAddress?: StripeCustomerAddressType;
137
+ metadata?: Record<string, string>;
138
+ };
139
+ type StripeCustomerType = {
140
+ id: string;
141
+ email: string;
142
+ name?: string;
143
+ phone?: string;
144
+ billingAddress?: StripeCustomerAddressType;
145
+ metadata?: Record<string, string>;
146
+ createdAt?: Date;
147
+ };
148
+ type StripeCustomerListOptionsType = {
149
+ email?: string;
150
+ limit?: number;
151
+ startingAfter?: string;
152
+ };
153
+ type StripeCustomerListResultType = {
154
+ items: StripeCustomerType[];
155
+ hasMore: boolean;
156
+ };
157
+ type StripeCustomerPortalCreateType = {
158
+ customerId: string;
159
+ returnUrl: string;
160
+ };
161
+ type StripeCustomerPortalType = {
162
+ id: string;
163
+ url: string;
164
+ returnUrl: string;
165
+ createdAt?: Date;
166
+ };
167
+ declare enum EStripeDiscountType {
168
+ PERCENTAGE = "percentage",
169
+ FIXED = "fixed"
170
+ }
171
+ type StripeDiscountTypeType = `${EStripeDiscountType}`;
172
+ declare enum EStripeDiscountDuration {
173
+ ONCE = "once",
174
+ REPEATING = "repeating",
175
+ FOREVER = "forever"
176
+ }
177
+ type StripeDiscountDurationType = `${EStripeDiscountDuration}`;
178
+ type StripeDiscountCreateType = {
179
+ name: string;
180
+ type: StripeDiscountTypeType;
181
+ amount: number;
182
+ currency?: string;
183
+ duration: StripeDiscountDurationType;
184
+ durationInMonths?: number;
185
+ code?: string;
186
+ maxRedemptions?: number;
187
+ redeemBy?: Date;
188
+ metadata?: Record<string, string>;
189
+ appliesTo?: string[];
190
+ };
191
+ type StripeDiscountUpdateType = {
192
+ name?: string;
193
+ metadata?: Record<string, string>;
194
+ };
195
+ type StripeDiscountType = {
196
+ id: string;
197
+ name: string;
198
+ type: StripeDiscountTypeType;
199
+ amount: number;
200
+ currency?: string;
201
+ duration: StripeDiscountDurationType;
202
+ durationInMonths?: number;
203
+ code?: string;
204
+ maxRedemptions?: number;
205
+ timesRedeemed?: number;
206
+ redeemBy?: Date;
207
+ isValid?: boolean;
208
+ metadata?: Record<string, string>;
209
+ createdAt?: Date;
210
+ };
211
+ type StripeDiscountListOptionsType = {
212
+ limit?: number;
213
+ startingAfter?: string;
214
+ };
215
+ type StripeDiscountListResultType = {
216
+ items: StripeDiscountType[];
217
+ hasMore: boolean;
218
+ };
219
+ type StripeProductCreateType = {
220
+ name: string;
221
+ description?: string;
222
+ images?: string[];
223
+ metadata?: Record<string, string>;
224
+ active?: boolean;
225
+ };
226
+ type StripeProductUpdateType = {
227
+ name?: string;
228
+ description?: string;
229
+ images?: string[];
230
+ metadata?: Record<string, string>;
231
+ active?: boolean;
232
+ };
233
+ type StripeProductType = {
234
+ id: string;
235
+ name: string;
236
+ description?: string;
237
+ images?: string[];
238
+ active?: boolean;
239
+ metadata?: Record<string, string>;
240
+ createdAt?: Date;
241
+ updatedAt?: Date;
242
+ };
243
+ type StripeProductListOptionsType = {
244
+ limit?: number;
245
+ active?: boolean;
246
+ startingAfter?: string;
247
+ };
248
+ type StripeProductListResultType = {
249
+ items: StripeProductType[];
250
+ hasMore: boolean;
251
+ };
252
+ declare enum EStripePriceType {
253
+ ONE_TIME = "one_time",
254
+ RECURRING = "recurring"
255
+ }
256
+ type StripePriceTypeType = `${EStripePriceType}`;
257
+ declare enum EStripePriceInterval {
258
+ DAY = "day",
259
+ WEEK = "week",
260
+ MONTH = "month",
261
+ YEAR = "year"
262
+ }
263
+ type StripePriceIntervalType = `${EStripePriceInterval}`;
264
+ type StripePriceCreateType = {
265
+ productId: string;
266
+ currency: string;
267
+ unitAmount: number;
268
+ type?: StripePriceTypeType;
269
+ interval?: StripePriceIntervalType;
270
+ intervalCount?: number;
271
+ metadata?: Record<string, string>;
272
+ };
273
+ type StripePriceType = {
274
+ id: string;
275
+ productId: string;
276
+ currency: string;
277
+ unitAmount: number | null;
278
+ type: StripePriceTypeType;
279
+ interval?: StripePriceIntervalType;
280
+ intervalCount?: number;
281
+ active?: boolean;
282
+ metadata?: Record<string, string>;
283
+ createdAt?: Date;
284
+ };
285
+ type StripePriceListOptionsType = {
286
+ limit?: number;
287
+ active?: boolean;
288
+ };
289
+ type StripePriceListResultType = {
290
+ items: StripePriceType[];
291
+ hasMore: boolean;
292
+ };
293
+ type StripeAnalyticsOptionsType = {
294
+ startDate: Date;
295
+ endDate: Date;
296
+ limit?: number;
297
+ currency?: string;
298
+ };
299
+ type StripeAnalyticsPeriodType = {
300
+ date: string;
301
+ revenue: number;
302
+ currency: string;
303
+ transactionCount: number;
304
+ };
305
+ type StripeAnalyticsSummaryType = {
306
+ totalRevenue: number;
307
+ totalTransactions: number;
308
+ currency: string;
309
+ periods: StripeAnalyticsPeriodType[];
310
+ activeSubscriptions?: number;
311
+ newSubscriptions?: number;
312
+ canceledSubscriptions?: number;
313
+ };
314
+ declare class StripeAnalytics {
315
+ private readonly client;
316
+ constructor(client: StripeClient);
317
+ get(options: StripeAnalyticsOptionsType): Promise<StripeAnalyticsSummaryType>;
318
+ }
319
+ declare class StripeCheckoutSession {
320
+ private readonly client;
321
+ constructor(client: StripeClient);
322
+ create(data: CheckoutSessionCreateType): Promise<CheckoutSessionType>;
323
+ get(id: string): Promise<CheckoutSessionType>;
324
+ private mapSession;
325
+ }
326
+ declare class StripeCustomer {
327
+ private readonly client;
328
+ constructor(client: StripeClient);
329
+ create(data: StripeCustomerCreateType): Promise<StripeCustomerType>;
330
+ update(id: string, data: StripeCustomerUpdateType): Promise<StripeCustomerType>;
331
+ remove(id: string): Promise<void>;
332
+ get(id: string): Promise<StripeCustomerType>;
333
+ list(options?: StripeCustomerListOptionsType): Promise<StripeCustomerListResultType>;
334
+ private toStripeAddress;
335
+ private mapCustomer;
336
+ }
337
+ declare class StripeCustomerPortal {
338
+ private readonly client;
339
+ constructor(client: StripeClient);
340
+ create(data: StripeCustomerPortalCreateType): Promise<StripeCustomerPortalType>;
341
+ }
342
+ declare class StripeDiscount {
343
+ private readonly client;
344
+ constructor(client: StripeClient);
345
+ create(data: StripeDiscountCreateType): Promise<StripeDiscountType>;
346
+ update(id: string, data: StripeDiscountUpdateType): Promise<StripeDiscountType>;
347
+ remove(id: string): Promise<void>;
348
+ get(id: string): Promise<StripeDiscountType>;
349
+ list(options?: StripeDiscountListOptionsType): Promise<StripeDiscountListResultType>;
350
+ private mapCoupon;
351
+ }
352
+ declare class StripeProducts {
353
+ private readonly client;
354
+ constructor(client: StripeClient);
355
+ create(data: StripeProductCreateType): Promise<StripeProductType>;
356
+ update(id: string, data: StripeProductUpdateType): Promise<StripeProductType>;
357
+ remove(id: string): Promise<void>;
358
+ get(id: string): Promise<StripeProductType>;
359
+ list(options?: StripeProductListOptionsType): Promise<StripeProductListResultType>;
360
+ createPrice(data: StripePriceCreateType): Promise<StripePriceType>;
361
+ getPrice(id: string): Promise<StripePriceType>;
362
+ listPrices(productId: string, options?: StripePriceListOptionsType): Promise<StripePriceListResultType>;
363
+ private mapProduct;
364
+ private mapPrice;
365
+ }
366
+ declare class StripeWebhookEvent {
367
+ private readonly client;
368
+ constructor(client: StripeClient);
369
+ construct(payload: string | Buffer, signature: string, secret: string): Promise<WebhookEventType>;
370
+ }
371
+ declare class StripeProvider implements IPaymentProvider {
372
+ private readonly checkoutSession;
373
+ private readonly webhookEvent;
374
+ constructor(checkoutSession: StripeCheckoutSession, webhookEvent: StripeWebhookEvent);
375
+ createCheckoutSession(data: CheckoutSessionCreateType): Promise<CheckoutSessionType>;
376
+ retrieveSession(id: string): Promise<CheckoutSessionType>;
377
+ constructWebhookEvent(payload: string | Buffer, signature: string, secret: string): Promise<WebhookEventType>;
378
+ }
379
+ export { WebhookEventType, StripeWebhookEvent, StripeProvider, StripeProducts, StripeProductUpdateType, StripeProductType, StripeProductListResultType, StripeProductListOptionsType, StripeProductCreateType, StripePriceTypeType, StripePriceType, StripePriceListResultType, StripePriceListOptionsType, StripePriceIntervalType, StripePriceCreateType, StripeEventType, StripeDiscountUpdateType, StripeDiscountTypeType, StripeDiscountType, StripeDiscountListResultType, StripeDiscountListOptionsType, StripeDiscountDurationType, StripeDiscountCreateType, StripeDiscount, StripeCustomerUpdateType, StripeCustomerType, StripeCustomerPortalType, StripeCustomerPortalCreateType, StripeCustomerPortal, StripeCustomerListResultType, StripeCustomerListOptionsType, StripeCustomerCreateType, StripeCustomerAddressType, StripeCustomer, StripeClient, StripeCheckoutSession, StripeAnalyticsSummaryType, StripeAnalyticsPeriodType, StripeAnalyticsOptionsType, StripeAnalytics, PaymentIntentPaymentFailedEventType, PaymentIntentPaymentFailedDataType, LineItemType, InvoicePaidEventType, InvoicePaidDataType, IPaymentProvider, EStripePriceType, EStripePriceInterval, EStripeEvent, EStripeDiscountType, EStripeDiscountDuration, CustomerSubscriptionUpdatedEventType, CustomerSubscriptionUpdatedDataType, CustomerSubscriptionDeletedEventType, CustomerSubscriptionDeletedDataType, CheckoutSessionType, CheckoutSessionCreateType, CheckoutSessionCompletedEventType, CheckoutSessionCompletedDataType };