@stripe-sdk/core 1.0.0

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.
@@ -0,0 +1,1208 @@
1
+ import Stripe from 'stripe';
2
+
3
+ // src/server/stripe-client.ts
4
+ var stripeInstance = null;
5
+ var currentConfig = null;
6
+ function initStripe(config) {
7
+ currentConfig = config;
8
+ stripeInstance = new Stripe(config.secretKey, {
9
+ apiVersion: config.apiVersion ?? "2025-01-27.acacia",
10
+ appInfo: config.appInfo ?? {
11
+ name: "@stripe-sdk/core",
12
+ version: "1.0.0"
13
+ }
14
+ });
15
+ return stripeInstance;
16
+ }
17
+ function getStripe() {
18
+ if (!stripeInstance) {
19
+ throw new Error(
20
+ "[@stripe-sdk/core] Stripe not initialized. Call initStripe({ secretKey, publishableKey }) first."
21
+ );
22
+ }
23
+ return stripeInstance;
24
+ }
25
+ function getConfig() {
26
+ if (!currentConfig) {
27
+ throw new Error(
28
+ "[@stripe-sdk/core] Stripe not initialized. Call initStripe({ secretKey, publishableKey }) first."
29
+ );
30
+ }
31
+ return currentConfig;
32
+ }
33
+
34
+ // src/utils/errors.ts
35
+ function handleStripeError(error) {
36
+ if (error?.type) {
37
+ const stripeError = error;
38
+ return {
39
+ data: null,
40
+ error: {
41
+ message: stripeError.message,
42
+ type: stripeError.type,
43
+ code: stripeError.code,
44
+ statusCode: stripeError.statusCode
45
+ }
46
+ };
47
+ }
48
+ return {
49
+ data: null,
50
+ error: {
51
+ message: error instanceof Error ? error.message : "An unknown error occurred",
52
+ type: "sdk_error"
53
+ }
54
+ };
55
+ }
56
+ function success(data) {
57
+ return { data, error: null };
58
+ }
59
+
60
+ // src/server/payments/index.ts
61
+ async function createPaymentIntent(input) {
62
+ try {
63
+ const stripe = getStripe();
64
+ const paymentIntent = await stripe.paymentIntents.create({
65
+ amount: input.amount,
66
+ currency: input.currency,
67
+ customer: input.customerId,
68
+ payment_method: input.paymentMethodId,
69
+ metadata: input.metadata,
70
+ description: input.description,
71
+ receipt_email: input.receiptEmail,
72
+ setup_future_usage: input.setupFutureUsage,
73
+ automatic_payment_methods: input.automaticPaymentMethods === false || input.paymentMethodId ? void 0 : { enabled: true },
74
+ return_url: input.returnUrl
75
+ });
76
+ return success(paymentIntent);
77
+ } catch (error) {
78
+ return handleStripeError(error);
79
+ }
80
+ }
81
+ async function retrievePaymentIntent(paymentIntentId) {
82
+ try {
83
+ const stripe = getStripe();
84
+ const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
85
+ return success(paymentIntent);
86
+ } catch (error) {
87
+ return handleStripeError(error);
88
+ }
89
+ }
90
+ async function confirmPaymentIntent(input) {
91
+ try {
92
+ const stripe = getStripe();
93
+ const paymentIntent = await stripe.paymentIntents.confirm(input.paymentIntentId, {
94
+ payment_method: input.paymentMethodId,
95
+ return_url: input.returnUrl
96
+ });
97
+ return success(paymentIntent);
98
+ } catch (error) {
99
+ return handleStripeError(error);
100
+ }
101
+ }
102
+ async function cancelPaymentIntent(paymentIntentId) {
103
+ try {
104
+ const stripe = getStripe();
105
+ const paymentIntent = await stripe.paymentIntents.cancel(paymentIntentId);
106
+ return success(paymentIntent);
107
+ } catch (error) {
108
+ return handleStripeError(error);
109
+ }
110
+ }
111
+ async function listPaymentIntents(input) {
112
+ try {
113
+ const stripe = getStripe();
114
+ const paymentIntents = await stripe.paymentIntents.list({
115
+ limit: input?.limit ?? 10,
116
+ starting_after: input?.startingAfter,
117
+ ending_before: input?.endingBefore,
118
+ customer: input?.customerId
119
+ });
120
+ return success(paymentIntents);
121
+ } catch (error) {
122
+ return handleStripeError(error);
123
+ }
124
+ }
125
+ async function createCheckoutSession(input) {
126
+ try {
127
+ const stripe = getStripe();
128
+ const session = await stripe.checkout.sessions.create({
129
+ mode: input.mode,
130
+ line_items: input.lineItems.map((item) => ({
131
+ price: item.priceId,
132
+ quantity: item.quantity
133
+ })),
134
+ success_url: input.successUrl,
135
+ cancel_url: input.cancelUrl,
136
+ customer: input.customerId,
137
+ customer_email: input.customerEmail,
138
+ metadata: input.metadata,
139
+ allow_promotion_codes: input.allowPromotionCodes,
140
+ shipping_address_collection: input.shippingAddressCollection ? { allowed_countries: input.shippingAddressCollection.allowedCountries } : void 0,
141
+ billing_address_collection: input.billingAddressCollection,
142
+ subscription_data: input.trialPeriodDays ? { trial_period_days: input.trialPeriodDays } : void 0,
143
+ tax_id_collection: input.taxIdCollection ? { enabled: true } : void 0,
144
+ automatic_tax: input.automaticTax ? { enabled: true } : void 0,
145
+ locale: input.locale
146
+ });
147
+ return success(session);
148
+ } catch (error) {
149
+ return handleStripeError(error);
150
+ }
151
+ }
152
+ async function retrieveCheckoutSession(sessionId) {
153
+ try {
154
+ const stripe = getStripe();
155
+ const session = await stripe.checkout.sessions.retrieve(sessionId, {
156
+ expand: ["line_items", "payment_intent", "subscription"]
157
+ });
158
+ return success(session);
159
+ } catch (error) {
160
+ return handleStripeError(error);
161
+ }
162
+ }
163
+ async function listCheckoutSessions(input) {
164
+ try {
165
+ const stripe = getStripe();
166
+ const sessions = await stripe.checkout.sessions.list({
167
+ limit: input?.limit ?? 10,
168
+ starting_after: input?.startingAfter,
169
+ ending_before: input?.endingBefore
170
+ });
171
+ return success(sessions);
172
+ } catch (error) {
173
+ return handleStripeError(error);
174
+ }
175
+ }
176
+ async function createPaymentLink(input) {
177
+ try {
178
+ const stripe = getStripe();
179
+ const paymentLink = await stripe.paymentLinks.create({
180
+ line_items: input.lineItems.map((item) => ({
181
+ price: item.priceId,
182
+ quantity: item.quantity,
183
+ adjustable_quantity: item.adjustableQuantity ? {
184
+ enabled: item.adjustableQuantity.enabled,
185
+ minimum: item.adjustableQuantity.minimum,
186
+ maximum: item.adjustableQuantity.maximum
187
+ } : void 0
188
+ })),
189
+ metadata: input.metadata,
190
+ after_completion: input.afterCompletion ? {
191
+ type: input.afterCompletion.type,
192
+ redirect: input.afterCompletion.redirectUrl ? { url: input.afterCompletion.redirectUrl } : void 0
193
+ } : void 0,
194
+ allow_promotion_codes: input.allowPromotionCodes,
195
+ automatic_tax: input.automaticTax ? { enabled: true } : void 0,
196
+ billing_address_collection: input.billingAddressCollection,
197
+ shipping_address_collection: input.shippingAddressCollection ? { allowed_countries: input.shippingAddressCollection.allowedCountries } : void 0
198
+ });
199
+ return success(paymentLink);
200
+ } catch (error) {
201
+ return handleStripeError(error);
202
+ }
203
+ }
204
+ async function retrievePaymentLink(paymentLinkId) {
205
+ try {
206
+ const stripe = getStripe();
207
+ const paymentLink = await stripe.paymentLinks.retrieve(paymentLinkId);
208
+ return success(paymentLink);
209
+ } catch (error) {
210
+ return handleStripeError(error);
211
+ }
212
+ }
213
+ async function createSetupIntent(input) {
214
+ try {
215
+ const stripe = getStripe();
216
+ const setupIntent = await stripe.setupIntents.create({
217
+ customer: input.customerId,
218
+ payment_method_types: input.paymentMethodTypes,
219
+ usage: input.usage,
220
+ metadata: input.metadata
221
+ });
222
+ return success(setupIntent);
223
+ } catch (error) {
224
+ return handleStripeError(error);
225
+ }
226
+ }
227
+ async function retrieveSetupIntent(setupIntentId) {
228
+ try {
229
+ const stripe = getStripe();
230
+ const setupIntent = await stripe.setupIntents.retrieve(setupIntentId);
231
+ return success(setupIntent);
232
+ } catch (error) {
233
+ return handleStripeError(error);
234
+ }
235
+ }
236
+ async function listPaymentMethods(customerId, type) {
237
+ try {
238
+ const stripe = getStripe();
239
+ const paymentMethods = await stripe.paymentMethods.list({
240
+ customer: customerId,
241
+ type: type ?? "card"
242
+ });
243
+ return success(paymentMethods);
244
+ } catch (error) {
245
+ return handleStripeError(error);
246
+ }
247
+ }
248
+ async function attachPaymentMethod(paymentMethodId, customerId) {
249
+ try {
250
+ const stripe = getStripe();
251
+ const paymentMethod = await stripe.paymentMethods.attach(paymentMethodId, {
252
+ customer: customerId
253
+ });
254
+ return success(paymentMethod);
255
+ } catch (error) {
256
+ return handleStripeError(error);
257
+ }
258
+ }
259
+ async function detachPaymentMethod(paymentMethodId) {
260
+ try {
261
+ const stripe = getStripe();
262
+ const paymentMethod = await stripe.paymentMethods.detach(paymentMethodId);
263
+ return success(paymentMethod);
264
+ } catch (error) {
265
+ return handleStripeError(error);
266
+ }
267
+ }
268
+
269
+ // src/server/customers/index.ts
270
+ async function createCustomer(input) {
271
+ try {
272
+ const stripe = getStripe();
273
+ const customer = await stripe.customers.create({
274
+ email: input.email,
275
+ name: input.name,
276
+ phone: input.phone,
277
+ description: input.description,
278
+ metadata: input.metadata,
279
+ payment_method: input.paymentMethodId,
280
+ invoice_settings: input.paymentMethodId ? { default_payment_method: input.paymentMethodId } : void 0,
281
+ address: input.address ? {
282
+ line1: input.address.line1,
283
+ line2: input.address.line2,
284
+ city: input.address.city,
285
+ state: input.address.state,
286
+ postal_code: input.address.postalCode,
287
+ country: input.address.country
288
+ } : void 0
289
+ });
290
+ return success(customer);
291
+ } catch (error) {
292
+ return handleStripeError(error);
293
+ }
294
+ }
295
+ async function retrieveCustomer(customerId) {
296
+ try {
297
+ const stripe = getStripe();
298
+ const customer = await stripe.customers.retrieve(customerId, {
299
+ expand: ["subscriptions", "sources"]
300
+ });
301
+ if (customer.deleted) {
302
+ return {
303
+ data: null,
304
+ error: { message: "Customer has been deleted", type: "invalid_request_error" }
305
+ };
306
+ }
307
+ return success(customer);
308
+ } catch (error) {
309
+ return handleStripeError(error);
310
+ }
311
+ }
312
+ async function updateCustomer(input) {
313
+ try {
314
+ const stripe = getStripe();
315
+ const customer = await stripe.customers.update(input.customerId, {
316
+ email: input.email,
317
+ name: input.name,
318
+ phone: input.phone,
319
+ description: input.description,
320
+ metadata: input.metadata,
321
+ invoice_settings: input.defaultPaymentMethodId ? { default_payment_method: input.defaultPaymentMethodId } : void 0,
322
+ address: input.address ? {
323
+ line1: input.address.line1,
324
+ line2: input.address.line2,
325
+ city: input.address.city,
326
+ state: input.address.state,
327
+ postal_code: input.address.postalCode,
328
+ country: input.address.country
329
+ } : void 0
330
+ });
331
+ return success(customer);
332
+ } catch (error) {
333
+ return handleStripeError(error);
334
+ }
335
+ }
336
+ async function deleteCustomer(customerId) {
337
+ try {
338
+ const stripe = getStripe();
339
+ const deleted = await stripe.customers.del(customerId);
340
+ return success(deleted);
341
+ } catch (error) {
342
+ return handleStripeError(error);
343
+ }
344
+ }
345
+ async function listCustomers(input) {
346
+ try {
347
+ const stripe = getStripe();
348
+ const customers = await stripe.customers.list({
349
+ limit: input?.limit ?? 10,
350
+ starting_after: input?.startingAfter,
351
+ ending_before: input?.endingBefore,
352
+ email: input?.email
353
+ });
354
+ return success(customers);
355
+ } catch (error) {
356
+ return handleStripeError(error);
357
+ }
358
+ }
359
+ async function searchCustomers(query, limit) {
360
+ try {
361
+ const stripe = getStripe();
362
+ const customers = await stripe.customers.search({
363
+ query,
364
+ limit: limit ?? 10
365
+ });
366
+ return success(customers);
367
+ } catch (error) {
368
+ return handleStripeError(error);
369
+ }
370
+ }
371
+ async function createPortalSession(input) {
372
+ try {
373
+ const stripe = getStripe();
374
+ const session = await stripe.billingPortal.sessions.create({
375
+ customer: input.customerId,
376
+ return_url: input.returnUrl,
377
+ configuration: input.configuration
378
+ });
379
+ return success(session);
380
+ } catch (error) {
381
+ return handleStripeError(error);
382
+ }
383
+ }
384
+
385
+ // src/server/subscriptions/index.ts
386
+ async function createSubscription(input) {
387
+ try {
388
+ const stripe = getStripe();
389
+ const items = input.items ? input.items.map((item) => ({ price: item.priceId, quantity: item.quantity })) : [{ price: input.priceId, quantity: input.quantity ?? 1 }];
390
+ const subscription = await stripe.subscriptions.create({
391
+ customer: input.customerId,
392
+ items,
393
+ metadata: input.metadata,
394
+ trial_period_days: input.trialPeriodDays,
395
+ coupon: input.couponId,
396
+ promotion_code: input.promotionCodeId,
397
+ payment_behavior: input.paymentBehavior ?? "default_incomplete",
398
+ cancel_at_period_end: input.cancelAtPeriodEnd,
399
+ billing_cycle_anchor: input.billingCycleAnchor,
400
+ proration_behavior: input.prorationBehavior,
401
+ expand: ["latest_invoice.payment_intent"]
402
+ });
403
+ return success(subscription);
404
+ } catch (error) {
405
+ return handleStripeError(error);
406
+ }
407
+ }
408
+ async function retrieveSubscription(subscriptionId) {
409
+ try {
410
+ const stripe = getStripe();
411
+ const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
412
+ expand: ["latest_invoice.payment_intent", "default_payment_method"]
413
+ });
414
+ return success(subscription);
415
+ } catch (error) {
416
+ return handleStripeError(error);
417
+ }
418
+ }
419
+ async function updateSubscription(input) {
420
+ try {
421
+ const stripe = getStripe();
422
+ const params = {
423
+ metadata: input.metadata,
424
+ cancel_at_period_end: input.cancelAtPeriodEnd,
425
+ proration_behavior: input.prorationBehavior,
426
+ coupon: input.couponId
427
+ };
428
+ if (input.items) {
429
+ params.items = input.items.map((item) => ({
430
+ id: item.id,
431
+ price: item.priceId,
432
+ quantity: item.quantity
433
+ }));
434
+ }
435
+ const subscription = await stripe.subscriptions.update(input.subscriptionId, params);
436
+ return success(subscription);
437
+ } catch (error) {
438
+ return handleStripeError(error);
439
+ }
440
+ }
441
+ async function cancelSubscription(input) {
442
+ try {
443
+ const stripe = getStripe();
444
+ if (input.cancelAtPeriodEnd) {
445
+ const subscription2 = await stripe.subscriptions.update(input.subscriptionId, {
446
+ cancel_at_period_end: true,
447
+ cancellation_details: input.cancellationDetails ? {
448
+ comment: input.cancellationDetails.comment,
449
+ feedback: input.cancellationDetails.feedback
450
+ } : void 0
451
+ });
452
+ return success(subscription2);
453
+ }
454
+ const subscription = await stripe.subscriptions.cancel(input.subscriptionId, {
455
+ cancellation_details: input.cancellationDetails ? {
456
+ comment: input.cancellationDetails.comment,
457
+ feedback: input.cancellationDetails.feedback
458
+ } : void 0
459
+ });
460
+ return success(subscription);
461
+ } catch (error) {
462
+ return handleStripeError(error);
463
+ }
464
+ }
465
+ async function resumeSubscription(subscriptionId) {
466
+ try {
467
+ const stripe = getStripe();
468
+ const subscription = await stripe.subscriptions.update(subscriptionId, {
469
+ cancel_at_period_end: false
470
+ });
471
+ return success(subscription);
472
+ } catch (error) {
473
+ return handleStripeError(error);
474
+ }
475
+ }
476
+ async function listSubscriptions(input) {
477
+ try {
478
+ const stripe = getStripe();
479
+ const subscriptions = await stripe.subscriptions.list({
480
+ limit: input?.limit ?? 10,
481
+ starting_after: input?.startingAfter,
482
+ ending_before: input?.endingBefore,
483
+ customer: input?.customerId,
484
+ status: input?.status
485
+ });
486
+ return success(subscriptions);
487
+ } catch (error) {
488
+ return handleStripeError(error);
489
+ }
490
+ }
491
+
492
+ // src/server/products/index.ts
493
+ async function createProduct(input) {
494
+ try {
495
+ const stripe = getStripe();
496
+ const product = await stripe.products.create({
497
+ name: input.name,
498
+ description: input.description,
499
+ images: input.images,
500
+ metadata: input.metadata,
501
+ active: input.active,
502
+ default_price_data: input.defaultPriceData ? {
503
+ unit_amount: input.defaultPriceData.unitAmount,
504
+ currency: input.defaultPriceData.currency,
505
+ recurring: input.defaultPriceData.recurring ? {
506
+ interval: input.defaultPriceData.recurring.interval,
507
+ interval_count: input.defaultPriceData.recurring.intervalCount
508
+ } : void 0
509
+ } : void 0
510
+ });
511
+ return success(product);
512
+ } catch (error) {
513
+ return handleStripeError(error);
514
+ }
515
+ }
516
+ async function retrieveProduct(productId) {
517
+ try {
518
+ const stripe = getStripe();
519
+ const product = await stripe.products.retrieve(productId);
520
+ return success(product);
521
+ } catch (error) {
522
+ return handleStripeError(error);
523
+ }
524
+ }
525
+ async function updateProduct(input) {
526
+ try {
527
+ const stripe = getStripe();
528
+ const product = await stripe.products.update(input.productId, {
529
+ name: input.name,
530
+ description: input.description,
531
+ images: input.images,
532
+ metadata: input.metadata,
533
+ active: input.active
534
+ });
535
+ return success(product);
536
+ } catch (error) {
537
+ return handleStripeError(error);
538
+ }
539
+ }
540
+ async function archiveProduct(productId) {
541
+ try {
542
+ const stripe = getStripe();
543
+ const product = await stripe.products.update(productId, { active: false });
544
+ return success(product);
545
+ } catch (error) {
546
+ return handleStripeError(error);
547
+ }
548
+ }
549
+ async function listProducts(input) {
550
+ try {
551
+ const stripe = getStripe();
552
+ const products = await stripe.products.list({
553
+ limit: input?.limit ?? 10,
554
+ starting_after: input?.startingAfter,
555
+ ending_before: input?.endingBefore,
556
+ active: input?.active
557
+ });
558
+ return success(products);
559
+ } catch (error) {
560
+ return handleStripeError(error);
561
+ }
562
+ }
563
+ async function createPrice(input) {
564
+ try {
565
+ const stripe = getStripe();
566
+ const params = {
567
+ product: input.productId,
568
+ currency: input.currency,
569
+ metadata: input.metadata,
570
+ active: input.active,
571
+ nickname: input.nickname,
572
+ lookup_key: input.lookupKey,
573
+ billing_scheme: input.billingScheme,
574
+ recurring: input.recurring ? {
575
+ interval: input.recurring.interval,
576
+ interval_count: input.recurring.intervalCount
577
+ } : void 0
578
+ };
579
+ if (input.billingScheme === "tiered" && input.tiers) {
580
+ if (!input.tiersMode) {
581
+ throw new Error('tiersMode is required when billingScheme is "tiered"');
582
+ }
583
+ params.tiers = input.tiers.map((tier) => ({
584
+ up_to: tier.upTo,
585
+ unit_amount: tier.unitAmount,
586
+ flat_amount: tier.flatAmount
587
+ }));
588
+ params.tiers_mode = input.tiersMode;
589
+ } else {
590
+ params.unit_amount = input.unitAmount;
591
+ }
592
+ const price = await stripe.prices.create(params);
593
+ return success(price);
594
+ } catch (error) {
595
+ return handleStripeError(error);
596
+ }
597
+ }
598
+ async function retrievePrice(priceId) {
599
+ try {
600
+ const stripe = getStripe();
601
+ const price = await stripe.prices.retrieve(priceId);
602
+ return success(price);
603
+ } catch (error) {
604
+ return handleStripeError(error);
605
+ }
606
+ }
607
+ async function listPrices(input) {
608
+ try {
609
+ const stripe = getStripe();
610
+ const prices = await stripe.prices.list({
611
+ limit: input?.limit ?? 10,
612
+ starting_after: input?.startingAfter,
613
+ ending_before: input?.endingBefore,
614
+ product: input?.productId,
615
+ active: input?.active,
616
+ type: input?.type
617
+ });
618
+ return success(prices);
619
+ } catch (error) {
620
+ return handleStripeError(error);
621
+ }
622
+ }
623
+ async function archivePrice(priceId) {
624
+ try {
625
+ const stripe = getStripe();
626
+ const price = await stripe.prices.update(priceId, { active: false });
627
+ return success(price);
628
+ } catch (error) {
629
+ return handleStripeError(error);
630
+ }
631
+ }
632
+
633
+ // src/server/invoices/index.ts
634
+ async function createInvoice(input) {
635
+ try {
636
+ const stripe = getStripe();
637
+ const invoice = await stripe.invoices.create({
638
+ customer: input.customerId,
639
+ collection_method: input.collectionMethod ?? "charge_automatically",
640
+ days_until_due: input.daysUntilDue,
641
+ metadata: input.metadata,
642
+ description: input.description,
643
+ auto_advance: input.autoAdvance
644
+ });
645
+ return success(invoice);
646
+ } catch (error) {
647
+ return handleStripeError(error);
648
+ }
649
+ }
650
+ async function retrieveInvoice(invoiceId) {
651
+ try {
652
+ const stripe = getStripe();
653
+ const invoice = await stripe.invoices.retrieve(invoiceId);
654
+ return success(invoice);
655
+ } catch (error) {
656
+ return handleStripeError(error);
657
+ }
658
+ }
659
+ async function finalizeInvoice(invoiceId) {
660
+ try {
661
+ const stripe = getStripe();
662
+ const invoice = await stripe.invoices.finalizeInvoice(invoiceId);
663
+ return success(invoice);
664
+ } catch (error) {
665
+ return handleStripeError(error);
666
+ }
667
+ }
668
+ async function sendInvoice(invoiceId) {
669
+ try {
670
+ const stripe = getStripe();
671
+ const invoice = await stripe.invoices.sendInvoice(invoiceId);
672
+ return success(invoice);
673
+ } catch (error) {
674
+ return handleStripeError(error);
675
+ }
676
+ }
677
+ async function payInvoice(invoiceId) {
678
+ try {
679
+ const stripe = getStripe();
680
+ const invoice = await stripe.invoices.pay(invoiceId);
681
+ return success(invoice);
682
+ } catch (error) {
683
+ return handleStripeError(error);
684
+ }
685
+ }
686
+ async function voidInvoice(invoiceId) {
687
+ try {
688
+ const stripe = getStripe();
689
+ const invoice = await stripe.invoices.voidInvoice(invoiceId);
690
+ return success(invoice);
691
+ } catch (error) {
692
+ return handleStripeError(error);
693
+ }
694
+ }
695
+ async function listInvoices(input) {
696
+ try {
697
+ const stripe = getStripe();
698
+ const invoices = await stripe.invoices.list({
699
+ limit: input?.limit ?? 10,
700
+ starting_after: input?.startingAfter,
701
+ ending_before: input?.endingBefore,
702
+ customer: input?.customerId,
703
+ status: input?.status
704
+ });
705
+ return success(invoices);
706
+ } catch (error) {
707
+ return handleStripeError(error);
708
+ }
709
+ }
710
+ async function getUpcomingInvoice(customerId, subscriptionId) {
711
+ try {
712
+ const stripe = getStripe();
713
+ const invoice = await stripe.invoices.retrieveUpcoming({
714
+ customer: customerId,
715
+ subscription: subscriptionId
716
+ });
717
+ return success(invoice);
718
+ } catch (error) {
719
+ return handleStripeError(error);
720
+ }
721
+ }
722
+ async function createInvoiceItem(input) {
723
+ try {
724
+ const stripe = getStripe();
725
+ const invoiceItem = await stripe.invoiceItems.create({
726
+ customer: input.customerId,
727
+ invoice: input.invoiceId,
728
+ price: input.priceId,
729
+ amount: input.amount,
730
+ currency: input.currency,
731
+ description: input.description,
732
+ quantity: input.quantity,
733
+ metadata: input.metadata
734
+ });
735
+ return success(invoiceItem);
736
+ } catch (error) {
737
+ return handleStripeError(error);
738
+ }
739
+ }
740
+
741
+ // src/server/refunds/index.ts
742
+ async function createRefund(input) {
743
+ try {
744
+ const stripe = getStripe();
745
+ const refund = await stripe.refunds.create({
746
+ payment_intent: input.paymentIntentId,
747
+ charge: input.chargeId,
748
+ amount: input.amount,
749
+ reason: input.reason,
750
+ metadata: input.metadata
751
+ });
752
+ return success(refund);
753
+ } catch (error) {
754
+ return handleStripeError(error);
755
+ }
756
+ }
757
+ async function retrieveRefund(refundId) {
758
+ try {
759
+ const stripe = getStripe();
760
+ const refund = await stripe.refunds.retrieve(refundId);
761
+ return success(refund);
762
+ } catch (error) {
763
+ return handleStripeError(error);
764
+ }
765
+ }
766
+ async function listRefunds(input) {
767
+ try {
768
+ const stripe = getStripe();
769
+ const refunds = await stripe.refunds.list({
770
+ limit: input?.limit ?? 10,
771
+ starting_after: input?.startingAfter,
772
+ ending_before: input?.endingBefore,
773
+ payment_intent: input?.paymentIntentId,
774
+ charge: input?.chargeId
775
+ });
776
+ return success(refunds);
777
+ } catch (error) {
778
+ return handleStripeError(error);
779
+ }
780
+ }
781
+ async function retrieveDispute(disputeId) {
782
+ try {
783
+ const stripe = getStripe();
784
+ const dispute = await stripe.disputes.retrieve(disputeId);
785
+ return success(dispute);
786
+ } catch (error) {
787
+ return handleStripeError(error);
788
+ }
789
+ }
790
+ async function updateDispute(input) {
791
+ try {
792
+ const stripe = getStripe();
793
+ const dispute = await stripe.disputes.update(input.disputeId, {
794
+ evidence: input.evidence ? {
795
+ customer_name: input.evidence.customerName,
796
+ customer_email_address: input.evidence.customerEmailAddress,
797
+ customer_communication: input.evidence.customerCommunication,
798
+ product_description: input.evidence.productDescription,
799
+ shipping_documentation: input.evidence.shippingDocumentation,
800
+ service_documentation: input.evidence.serviceDocumentation,
801
+ uncategorized_text: input.evidence.uncategorizedText
802
+ } : void 0,
803
+ metadata: input.metadata,
804
+ submit: input.submit
805
+ });
806
+ return success(dispute);
807
+ } catch (error) {
808
+ return handleStripeError(error);
809
+ }
810
+ }
811
+ async function closeDispute(disputeId) {
812
+ try {
813
+ const stripe = getStripe();
814
+ const dispute = await stripe.disputes.close(disputeId);
815
+ return success(dispute);
816
+ } catch (error) {
817
+ return handleStripeError(error);
818
+ }
819
+ }
820
+ async function listDisputes(input) {
821
+ try {
822
+ const stripe = getStripe();
823
+ const disputes = await stripe.disputes.list({
824
+ limit: input?.limit ?? 10,
825
+ starting_after: input?.startingAfter,
826
+ ending_before: input?.endingBefore
827
+ });
828
+ return success(disputes);
829
+ } catch (error) {
830
+ return handleStripeError(error);
831
+ }
832
+ }
833
+
834
+ // src/server/connect/index.ts
835
+ async function createConnectAccount(input) {
836
+ try {
837
+ const stripe = getStripe();
838
+ const account = await stripe.accounts.create({
839
+ type: input.type,
840
+ country: input.country,
841
+ email: input.email,
842
+ capabilities: input.capabilities,
843
+ business_type: input.businessType,
844
+ metadata: input.metadata
845
+ });
846
+ return success(account);
847
+ } catch (error) {
848
+ return handleStripeError(error);
849
+ }
850
+ }
851
+ async function retrieveConnectAccount(accountId) {
852
+ try {
853
+ const stripe = getStripe();
854
+ const account = await stripe.accounts.retrieve(accountId);
855
+ return success(account);
856
+ } catch (error) {
857
+ return handleStripeError(error);
858
+ }
859
+ }
860
+ async function deleteConnectAccount(accountId) {
861
+ try {
862
+ const stripe = getStripe();
863
+ const deleted = await stripe.accounts.del(accountId);
864
+ return success(deleted);
865
+ } catch (error) {
866
+ return handleStripeError(error);
867
+ }
868
+ }
869
+ async function listConnectAccounts(input) {
870
+ try {
871
+ const stripe = getStripe();
872
+ const accounts = await stripe.accounts.list({
873
+ limit: input?.limit ?? 10,
874
+ starting_after: input?.startingAfter,
875
+ ending_before: input?.endingBefore
876
+ });
877
+ return success(accounts);
878
+ } catch (error) {
879
+ return handleStripeError(error);
880
+ }
881
+ }
882
+ async function createAccountLink(input) {
883
+ try {
884
+ const stripe = getStripe();
885
+ const accountLink = await stripe.accountLinks.create({
886
+ account: input.accountId,
887
+ refresh_url: input.refreshUrl,
888
+ return_url: input.returnUrl,
889
+ type: input.type
890
+ });
891
+ return success(accountLink);
892
+ } catch (error) {
893
+ return handleStripeError(error);
894
+ }
895
+ }
896
+ async function createTransfer(input) {
897
+ try {
898
+ const stripe = getStripe();
899
+ const transfer = await stripe.transfers.create({
900
+ amount: input.amount,
901
+ currency: input.currency,
902
+ destination: input.destinationAccountId,
903
+ description: input.description,
904
+ metadata: input.metadata,
905
+ source_transaction: input.sourceTransaction
906
+ });
907
+ return success(transfer);
908
+ } catch (error) {
909
+ return handleStripeError(error);
910
+ }
911
+ }
912
+ async function listTransfers(input) {
913
+ try {
914
+ const stripe = getStripe();
915
+ const transfers = await stripe.transfers.list({
916
+ limit: input?.limit ?? 10,
917
+ starting_after: input?.startingAfter,
918
+ ending_before: input?.endingBefore,
919
+ destination: input?.destinationAccountId
920
+ });
921
+ return success(transfers);
922
+ } catch (error) {
923
+ return handleStripeError(error);
924
+ }
925
+ }
926
+ async function createPayout(amount, currency, metadata) {
927
+ try {
928
+ const stripe = getStripe();
929
+ const payout = await stripe.payouts.create({
930
+ amount,
931
+ currency,
932
+ metadata
933
+ });
934
+ return success(payout);
935
+ } catch (error) {
936
+ return handleStripeError(error);
937
+ }
938
+ }
939
+ async function listPayouts(input) {
940
+ try {
941
+ const stripe = getStripe();
942
+ const payouts = await stripe.payouts.list({
943
+ limit: input?.limit ?? 10,
944
+ starting_after: input?.startingAfter,
945
+ ending_before: input?.endingBefore,
946
+ status: input?.status
947
+ });
948
+ return success(payouts);
949
+ } catch (error) {
950
+ return handleStripeError(error);
951
+ }
952
+ }
953
+ async function getBalance() {
954
+ try {
955
+ const stripe = getStripe();
956
+ const balance = await stripe.balance.retrieve();
957
+ return success(balance);
958
+ } catch (error) {
959
+ return handleStripeError(error);
960
+ }
961
+ }
962
+ async function listBalanceTransactions(input) {
963
+ try {
964
+ const stripe = getStripe();
965
+ const transactions = await stripe.balanceTransactions.list({
966
+ limit: input?.limit ?? 10,
967
+ starting_after: input?.startingAfter,
968
+ ending_before: input?.endingBefore,
969
+ type: input?.type
970
+ });
971
+ return success(transactions);
972
+ } catch (error) {
973
+ return handleStripeError(error);
974
+ }
975
+ }
976
+
977
+ // src/server/coupons/index.ts
978
+ async function createCoupon(input) {
979
+ try {
980
+ const stripe = getStripe();
981
+ const coupon = await stripe.coupons.create({
982
+ percent_off: input.percentOff,
983
+ amount_off: input.amountOff,
984
+ currency: input.currency,
985
+ duration: input.duration,
986
+ duration_in_months: input.durationInMonths,
987
+ max_redemptions: input.maxRedemptions,
988
+ redeem_by: input.redeemBy,
989
+ name: input.name,
990
+ metadata: input.metadata
991
+ });
992
+ return success(coupon);
993
+ } catch (error) {
994
+ return handleStripeError(error);
995
+ }
996
+ }
997
+ async function retrieveCoupon(couponId) {
998
+ try {
999
+ const stripe = getStripe();
1000
+ const coupon = await stripe.coupons.retrieve(couponId);
1001
+ return success(coupon);
1002
+ } catch (error) {
1003
+ return handleStripeError(error);
1004
+ }
1005
+ }
1006
+ async function deleteCoupon(couponId) {
1007
+ try {
1008
+ const stripe = getStripe();
1009
+ const deleted = await stripe.coupons.del(couponId);
1010
+ return success(deleted);
1011
+ } catch (error) {
1012
+ return handleStripeError(error);
1013
+ }
1014
+ }
1015
+ async function listCoupons(input) {
1016
+ try {
1017
+ const stripe = getStripe();
1018
+ const coupons = await stripe.coupons.list({
1019
+ limit: input?.limit ?? 10,
1020
+ starting_after: input?.startingAfter,
1021
+ ending_before: input?.endingBefore
1022
+ });
1023
+ return success(coupons);
1024
+ } catch (error) {
1025
+ return handleStripeError(error);
1026
+ }
1027
+ }
1028
+ async function createPromotionCode(input) {
1029
+ try {
1030
+ const stripe = getStripe();
1031
+ const promotionCode = await stripe.promotionCodes.create({
1032
+ coupon: input.couponId,
1033
+ code: input.code,
1034
+ active: input.active,
1035
+ max_redemptions: input.maxRedemptions,
1036
+ expires_at: input.expiresAt,
1037
+ metadata: input.metadata,
1038
+ restrictions: input.restrictions ? {
1039
+ first_time_transaction: input.restrictions.firstTimeTransaction,
1040
+ minimum_amount: input.restrictions.minimumAmount,
1041
+ minimum_amount_currency: input.restrictions.minimumAmountCurrency
1042
+ } : void 0
1043
+ });
1044
+ return success(promotionCode);
1045
+ } catch (error) {
1046
+ return handleStripeError(error);
1047
+ }
1048
+ }
1049
+ async function retrievePromotionCode(promotionCodeId) {
1050
+ try {
1051
+ const stripe = getStripe();
1052
+ const promotionCode = await stripe.promotionCodes.retrieve(promotionCodeId);
1053
+ return success(promotionCode);
1054
+ } catch (error) {
1055
+ return handleStripeError(error);
1056
+ }
1057
+ }
1058
+ async function listPromotionCodes(input) {
1059
+ try {
1060
+ const stripe = getStripe();
1061
+ const promotionCodes = await stripe.promotionCodes.list({
1062
+ limit: input?.limit ?? 10,
1063
+ starting_after: input?.startingAfter,
1064
+ ending_before: input?.endingBefore,
1065
+ coupon: input?.couponId,
1066
+ active: input?.active,
1067
+ code: input?.code
1068
+ });
1069
+ return success(promotionCodes);
1070
+ } catch (error) {
1071
+ return handleStripeError(error);
1072
+ }
1073
+ }
1074
+
1075
+ // src/server/webhooks/index.ts
1076
+ var MAX_BODY_SIZE = 1024 * 1024;
1077
+ function createWebhookHandler(config) {
1078
+ const webhookSecret = config.secret ?? getConfig().webhookSecret;
1079
+ if (!webhookSecret) {
1080
+ throw new Error(
1081
+ "[@stripe-sdk/core] Webhook secret is required. Pass it via config.secret or initStripe({ webhookSecret })."
1082
+ );
1083
+ }
1084
+ return async function handleWebhook(body, signature) {
1085
+ const stripe = getStripe();
1086
+ let event;
1087
+ try {
1088
+ event = stripe.webhooks.constructEvent(body, signature, webhookSecret);
1089
+ } catch (err) {
1090
+ const verificationError = err instanceof Error ? err : new Error("Webhook signature verification failed");
1091
+ if (config.onError) {
1092
+ await config.onError(verificationError);
1093
+ }
1094
+ throw verificationError;
1095
+ }
1096
+ const handler = config.handlers[event.type];
1097
+ if (handler) {
1098
+ try {
1099
+ await handler(event);
1100
+ } catch (error) {
1101
+ if (config.onError) {
1102
+ await config.onError(error instanceof Error ? error : new Error(String(error)), event);
1103
+ } else {
1104
+ throw error;
1105
+ }
1106
+ }
1107
+ } else if (config.onUnhandledEvent) {
1108
+ await config.onUnhandledEvent(event);
1109
+ }
1110
+ return { received: true, type: event.type };
1111
+ };
1112
+ }
1113
+ function createNextWebhookHandler(config) {
1114
+ const handler = createWebhookHandler(config);
1115
+ return async function POST(request) {
1116
+ try {
1117
+ const contentLength = request.headers.get("content-length");
1118
+ if (contentLength && parseInt(contentLength, 10) > MAX_BODY_SIZE) {
1119
+ return new Response(JSON.stringify({ error: "Webhook body too large" }), {
1120
+ status: 413,
1121
+ headers: { "Content-Type": "application/json" }
1122
+ });
1123
+ }
1124
+ const body = await request.text();
1125
+ const signature = request.headers.get("stripe-signature");
1126
+ if (!signature) {
1127
+ return new Response(JSON.stringify({ error: "Missing stripe-signature header" }), {
1128
+ status: 400,
1129
+ headers: { "Content-Type": "application/json" }
1130
+ });
1131
+ }
1132
+ const result = await handler(body, signature);
1133
+ return new Response(JSON.stringify(result), {
1134
+ status: 200,
1135
+ headers: { "Content-Type": "application/json" }
1136
+ });
1137
+ } catch (error) {
1138
+ const message = error instanceof Error ? error.message : "Webhook handler failed";
1139
+ return new Response(JSON.stringify({ error: message }), {
1140
+ status: 400,
1141
+ headers: { "Content-Type": "application/json" }
1142
+ });
1143
+ }
1144
+ };
1145
+ }
1146
+ function createPagesWebhookHandler(webhookConfig) {
1147
+ const handler = createWebhookHandler(webhookConfig);
1148
+ return async function webhookRoute(req, res) {
1149
+ if (req.method !== "POST") {
1150
+ res.status(405).end();
1151
+ return;
1152
+ }
1153
+ try {
1154
+ const body = await getRawBody(req);
1155
+ const signature = req.headers["stripe-signature"];
1156
+ if (!signature) {
1157
+ res.status(400).json({ error: "Missing stripe-signature header" });
1158
+ return;
1159
+ }
1160
+ const result = await handler(body, signature);
1161
+ res.status(200).json(result);
1162
+ } catch (error) {
1163
+ const message = error instanceof Error ? error.message : "Webhook handler failed";
1164
+ res.status(400).json({ error: message });
1165
+ }
1166
+ };
1167
+ }
1168
+ function getRawBody(req) {
1169
+ return new Promise((resolve, reject) => {
1170
+ if (typeof req.body === "string") {
1171
+ if (req.body.length > MAX_BODY_SIZE) {
1172
+ reject(new Error("Webhook body too large"));
1173
+ return;
1174
+ }
1175
+ resolve(req.body);
1176
+ return;
1177
+ }
1178
+ if (Buffer.isBuffer(req.body)) {
1179
+ if (req.body.length > MAX_BODY_SIZE) {
1180
+ reject(new Error("Webhook body too large"));
1181
+ return;
1182
+ }
1183
+ resolve(req.body.toString("utf8"));
1184
+ return;
1185
+ }
1186
+ if (!req.on) {
1187
+ reject(new Error("Cannot read raw body from request"));
1188
+ return;
1189
+ }
1190
+ const chunks = [];
1191
+ let totalLength = 0;
1192
+ req.on("data", (chunk) => {
1193
+ const buf = Buffer.from(chunk);
1194
+ totalLength += buf.length;
1195
+ if (totalLength > MAX_BODY_SIZE) {
1196
+ reject(new Error("Webhook body too large"));
1197
+ return;
1198
+ }
1199
+ chunks.push(buf);
1200
+ });
1201
+ req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
1202
+ req.on("error", reject);
1203
+ });
1204
+ }
1205
+
1206
+ export { archivePrice, archiveProduct, attachPaymentMethod, cancelPaymentIntent, cancelSubscription, closeDispute, confirmPaymentIntent, createAccountLink, createCheckoutSession, createConnectAccount, createCoupon, createCustomer, createInvoice, createInvoiceItem, createNextWebhookHandler, createPagesWebhookHandler, createPaymentIntent, createPaymentLink, createPayout, createPortalSession, createPrice, createProduct, createPromotionCode, createRefund, createSetupIntent, createSubscription, createTransfer, createWebhookHandler, deleteConnectAccount, deleteCoupon, deleteCustomer, detachPaymentMethod, finalizeInvoice, getBalance, getConfig, getStripe, getUpcomingInvoice, initStripe, listBalanceTransactions, listCheckoutSessions, listConnectAccounts, listCoupons, listCustomers, listDisputes, listInvoices, listPaymentIntents, listPaymentMethods, listPayouts, listPrices, listProducts, listPromotionCodes, listRefunds, listSubscriptions, listTransfers, payInvoice, resumeSubscription, retrieveCheckoutSession, retrieveConnectAccount, retrieveCoupon, retrieveCustomer, retrieveDispute, retrieveInvoice, retrievePaymentIntent, retrievePaymentLink, retrievePrice, retrieveProduct, retrievePromotionCode, retrieveRefund, retrieveSetupIntent, retrieveSubscription, searchCustomers, sendInvoice, updateCustomer, updateDispute, updateProduct, updateSubscription, voidInvoice };
1207
+ //# sourceMappingURL=index.mjs.map
1208
+ //# sourceMappingURL=index.mjs.map