@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.
package/dist/index.mjs ADDED
@@ -0,0 +1,1708 @@
1
+ import Stripe from 'stripe';
2
+ import { createContext, useContext, useMemo, useState, useCallback } from 'react';
3
+ import { loadStripe } from '@stripe/stripe-js';
4
+ import { Elements, useStripe, useElements, LinkAuthenticationElement, PaymentElement } from '@stripe/react-stripe-js';
5
+ import { jsx, jsxs } from 'react/jsx-runtime';
6
+
7
+ // src/server/stripe-client.ts
8
+ var stripeInstance = null;
9
+ var currentConfig = null;
10
+ function initStripe(config) {
11
+ currentConfig = config;
12
+ stripeInstance = new Stripe(config.secretKey, {
13
+ apiVersion: config.apiVersion ?? "2025-01-27.acacia",
14
+ appInfo: config.appInfo ?? {
15
+ name: "@stripe-sdk/core",
16
+ version: "1.0.0"
17
+ }
18
+ });
19
+ return stripeInstance;
20
+ }
21
+ function getStripe() {
22
+ if (!stripeInstance) {
23
+ throw new Error(
24
+ "[@stripe-sdk/core] Stripe not initialized. Call initStripe({ secretKey, publishableKey }) first."
25
+ );
26
+ }
27
+ return stripeInstance;
28
+ }
29
+ function getConfig() {
30
+ if (!currentConfig) {
31
+ throw new Error(
32
+ "[@stripe-sdk/core] Stripe not initialized. Call initStripe({ secretKey, publishableKey }) first."
33
+ );
34
+ }
35
+ return currentConfig;
36
+ }
37
+
38
+ // src/utils/errors.ts
39
+ function handleStripeError(error) {
40
+ if (error?.type) {
41
+ const stripeError = error;
42
+ return {
43
+ data: null,
44
+ error: {
45
+ message: stripeError.message,
46
+ type: stripeError.type,
47
+ code: stripeError.code,
48
+ statusCode: stripeError.statusCode
49
+ }
50
+ };
51
+ }
52
+ return {
53
+ data: null,
54
+ error: {
55
+ message: error instanceof Error ? error.message : "An unknown error occurred",
56
+ type: "sdk_error"
57
+ }
58
+ };
59
+ }
60
+ function success(data) {
61
+ return { data, error: null };
62
+ }
63
+
64
+ // src/server/payments/index.ts
65
+ async function createPaymentIntent(input) {
66
+ try {
67
+ const stripe = getStripe();
68
+ const paymentIntent = await stripe.paymentIntents.create({
69
+ amount: input.amount,
70
+ currency: input.currency,
71
+ customer: input.customerId,
72
+ payment_method: input.paymentMethodId,
73
+ metadata: input.metadata,
74
+ description: input.description,
75
+ receipt_email: input.receiptEmail,
76
+ setup_future_usage: input.setupFutureUsage,
77
+ automatic_payment_methods: input.automaticPaymentMethods === false || input.paymentMethodId ? void 0 : { enabled: true },
78
+ return_url: input.returnUrl
79
+ });
80
+ return success(paymentIntent);
81
+ } catch (error) {
82
+ return handleStripeError(error);
83
+ }
84
+ }
85
+ async function retrievePaymentIntent(paymentIntentId) {
86
+ try {
87
+ const stripe = getStripe();
88
+ const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
89
+ return success(paymentIntent);
90
+ } catch (error) {
91
+ return handleStripeError(error);
92
+ }
93
+ }
94
+ async function confirmPaymentIntent(input) {
95
+ try {
96
+ const stripe = getStripe();
97
+ const paymentIntent = await stripe.paymentIntents.confirm(input.paymentIntentId, {
98
+ payment_method: input.paymentMethodId,
99
+ return_url: input.returnUrl
100
+ });
101
+ return success(paymentIntent);
102
+ } catch (error) {
103
+ return handleStripeError(error);
104
+ }
105
+ }
106
+ async function cancelPaymentIntent(paymentIntentId) {
107
+ try {
108
+ const stripe = getStripe();
109
+ const paymentIntent = await stripe.paymentIntents.cancel(paymentIntentId);
110
+ return success(paymentIntent);
111
+ } catch (error) {
112
+ return handleStripeError(error);
113
+ }
114
+ }
115
+ async function listPaymentIntents(input) {
116
+ try {
117
+ const stripe = getStripe();
118
+ const paymentIntents = await stripe.paymentIntents.list({
119
+ limit: input?.limit ?? 10,
120
+ starting_after: input?.startingAfter,
121
+ ending_before: input?.endingBefore,
122
+ customer: input?.customerId
123
+ });
124
+ return success(paymentIntents);
125
+ } catch (error) {
126
+ return handleStripeError(error);
127
+ }
128
+ }
129
+ async function createCheckoutSession(input) {
130
+ try {
131
+ const stripe = getStripe();
132
+ const session = await stripe.checkout.sessions.create({
133
+ mode: input.mode,
134
+ line_items: input.lineItems.map((item) => ({
135
+ price: item.priceId,
136
+ quantity: item.quantity
137
+ })),
138
+ success_url: input.successUrl,
139
+ cancel_url: input.cancelUrl,
140
+ customer: input.customerId,
141
+ customer_email: input.customerEmail,
142
+ metadata: input.metadata,
143
+ allow_promotion_codes: input.allowPromotionCodes,
144
+ shipping_address_collection: input.shippingAddressCollection ? { allowed_countries: input.shippingAddressCollection.allowedCountries } : void 0,
145
+ billing_address_collection: input.billingAddressCollection,
146
+ subscription_data: input.trialPeriodDays ? { trial_period_days: input.trialPeriodDays } : void 0,
147
+ tax_id_collection: input.taxIdCollection ? { enabled: true } : void 0,
148
+ automatic_tax: input.automaticTax ? { enabled: true } : void 0,
149
+ locale: input.locale
150
+ });
151
+ return success(session);
152
+ } catch (error) {
153
+ return handleStripeError(error);
154
+ }
155
+ }
156
+ async function retrieveCheckoutSession(sessionId) {
157
+ try {
158
+ const stripe = getStripe();
159
+ const session = await stripe.checkout.sessions.retrieve(sessionId, {
160
+ expand: ["line_items", "payment_intent", "subscription"]
161
+ });
162
+ return success(session);
163
+ } catch (error) {
164
+ return handleStripeError(error);
165
+ }
166
+ }
167
+ async function listCheckoutSessions(input) {
168
+ try {
169
+ const stripe = getStripe();
170
+ const sessions = await stripe.checkout.sessions.list({
171
+ limit: input?.limit ?? 10,
172
+ starting_after: input?.startingAfter,
173
+ ending_before: input?.endingBefore
174
+ });
175
+ return success(sessions);
176
+ } catch (error) {
177
+ return handleStripeError(error);
178
+ }
179
+ }
180
+ async function createPaymentLink(input) {
181
+ try {
182
+ const stripe = getStripe();
183
+ const paymentLink = await stripe.paymentLinks.create({
184
+ line_items: input.lineItems.map((item) => ({
185
+ price: item.priceId,
186
+ quantity: item.quantity,
187
+ adjustable_quantity: item.adjustableQuantity ? {
188
+ enabled: item.adjustableQuantity.enabled,
189
+ minimum: item.adjustableQuantity.minimum,
190
+ maximum: item.adjustableQuantity.maximum
191
+ } : void 0
192
+ })),
193
+ metadata: input.metadata,
194
+ after_completion: input.afterCompletion ? {
195
+ type: input.afterCompletion.type,
196
+ redirect: input.afterCompletion.redirectUrl ? { url: input.afterCompletion.redirectUrl } : void 0
197
+ } : void 0,
198
+ allow_promotion_codes: input.allowPromotionCodes,
199
+ automatic_tax: input.automaticTax ? { enabled: true } : void 0,
200
+ billing_address_collection: input.billingAddressCollection,
201
+ shipping_address_collection: input.shippingAddressCollection ? { allowed_countries: input.shippingAddressCollection.allowedCountries } : void 0
202
+ });
203
+ return success(paymentLink);
204
+ } catch (error) {
205
+ return handleStripeError(error);
206
+ }
207
+ }
208
+ async function retrievePaymentLink(paymentLinkId) {
209
+ try {
210
+ const stripe = getStripe();
211
+ const paymentLink = await stripe.paymentLinks.retrieve(paymentLinkId);
212
+ return success(paymentLink);
213
+ } catch (error) {
214
+ return handleStripeError(error);
215
+ }
216
+ }
217
+ async function createSetupIntent(input) {
218
+ try {
219
+ const stripe = getStripe();
220
+ const setupIntent = await stripe.setupIntents.create({
221
+ customer: input.customerId,
222
+ payment_method_types: input.paymentMethodTypes,
223
+ usage: input.usage,
224
+ metadata: input.metadata
225
+ });
226
+ return success(setupIntent);
227
+ } catch (error) {
228
+ return handleStripeError(error);
229
+ }
230
+ }
231
+ async function retrieveSetupIntent(setupIntentId) {
232
+ try {
233
+ const stripe = getStripe();
234
+ const setupIntent = await stripe.setupIntents.retrieve(setupIntentId);
235
+ return success(setupIntent);
236
+ } catch (error) {
237
+ return handleStripeError(error);
238
+ }
239
+ }
240
+ async function listPaymentMethods(customerId, type) {
241
+ try {
242
+ const stripe = getStripe();
243
+ const paymentMethods = await stripe.paymentMethods.list({
244
+ customer: customerId,
245
+ type: type ?? "card"
246
+ });
247
+ return success(paymentMethods);
248
+ } catch (error) {
249
+ return handleStripeError(error);
250
+ }
251
+ }
252
+ async function attachPaymentMethod(paymentMethodId, customerId) {
253
+ try {
254
+ const stripe = getStripe();
255
+ const paymentMethod = await stripe.paymentMethods.attach(paymentMethodId, {
256
+ customer: customerId
257
+ });
258
+ return success(paymentMethod);
259
+ } catch (error) {
260
+ return handleStripeError(error);
261
+ }
262
+ }
263
+ async function detachPaymentMethod(paymentMethodId) {
264
+ try {
265
+ const stripe = getStripe();
266
+ const paymentMethod = await stripe.paymentMethods.detach(paymentMethodId);
267
+ return success(paymentMethod);
268
+ } catch (error) {
269
+ return handleStripeError(error);
270
+ }
271
+ }
272
+
273
+ // src/server/customers/index.ts
274
+ async function createCustomer(input) {
275
+ try {
276
+ const stripe = getStripe();
277
+ const customer = await stripe.customers.create({
278
+ email: input.email,
279
+ name: input.name,
280
+ phone: input.phone,
281
+ description: input.description,
282
+ metadata: input.metadata,
283
+ payment_method: input.paymentMethodId,
284
+ invoice_settings: input.paymentMethodId ? { default_payment_method: input.paymentMethodId } : void 0,
285
+ address: input.address ? {
286
+ line1: input.address.line1,
287
+ line2: input.address.line2,
288
+ city: input.address.city,
289
+ state: input.address.state,
290
+ postal_code: input.address.postalCode,
291
+ country: input.address.country
292
+ } : void 0
293
+ });
294
+ return success(customer);
295
+ } catch (error) {
296
+ return handleStripeError(error);
297
+ }
298
+ }
299
+ async function retrieveCustomer(customerId) {
300
+ try {
301
+ const stripe = getStripe();
302
+ const customer = await stripe.customers.retrieve(customerId, {
303
+ expand: ["subscriptions", "sources"]
304
+ });
305
+ if (customer.deleted) {
306
+ return {
307
+ data: null,
308
+ error: { message: "Customer has been deleted", type: "invalid_request_error" }
309
+ };
310
+ }
311
+ return success(customer);
312
+ } catch (error) {
313
+ return handleStripeError(error);
314
+ }
315
+ }
316
+ async function updateCustomer(input) {
317
+ try {
318
+ const stripe = getStripe();
319
+ const customer = await stripe.customers.update(input.customerId, {
320
+ email: input.email,
321
+ name: input.name,
322
+ phone: input.phone,
323
+ description: input.description,
324
+ metadata: input.metadata,
325
+ invoice_settings: input.defaultPaymentMethodId ? { default_payment_method: input.defaultPaymentMethodId } : void 0,
326
+ address: input.address ? {
327
+ line1: input.address.line1,
328
+ line2: input.address.line2,
329
+ city: input.address.city,
330
+ state: input.address.state,
331
+ postal_code: input.address.postalCode,
332
+ country: input.address.country
333
+ } : void 0
334
+ });
335
+ return success(customer);
336
+ } catch (error) {
337
+ return handleStripeError(error);
338
+ }
339
+ }
340
+ async function deleteCustomer(customerId) {
341
+ try {
342
+ const stripe = getStripe();
343
+ const deleted = await stripe.customers.del(customerId);
344
+ return success(deleted);
345
+ } catch (error) {
346
+ return handleStripeError(error);
347
+ }
348
+ }
349
+ async function listCustomers(input) {
350
+ try {
351
+ const stripe = getStripe();
352
+ const customers = await stripe.customers.list({
353
+ limit: input?.limit ?? 10,
354
+ starting_after: input?.startingAfter,
355
+ ending_before: input?.endingBefore,
356
+ email: input?.email
357
+ });
358
+ return success(customers);
359
+ } catch (error) {
360
+ return handleStripeError(error);
361
+ }
362
+ }
363
+ async function searchCustomers(query, limit) {
364
+ try {
365
+ const stripe = getStripe();
366
+ const customers = await stripe.customers.search({
367
+ query,
368
+ limit: limit ?? 10
369
+ });
370
+ return success(customers);
371
+ } catch (error) {
372
+ return handleStripeError(error);
373
+ }
374
+ }
375
+ async function createPortalSession(input) {
376
+ try {
377
+ const stripe = getStripe();
378
+ const session = await stripe.billingPortal.sessions.create({
379
+ customer: input.customerId,
380
+ return_url: input.returnUrl,
381
+ configuration: input.configuration
382
+ });
383
+ return success(session);
384
+ } catch (error) {
385
+ return handleStripeError(error);
386
+ }
387
+ }
388
+
389
+ // src/server/subscriptions/index.ts
390
+ async function createSubscription(input) {
391
+ try {
392
+ const stripe = getStripe();
393
+ const items = input.items ? input.items.map((item) => ({ price: item.priceId, quantity: item.quantity })) : [{ price: input.priceId, quantity: input.quantity ?? 1 }];
394
+ const subscription = await stripe.subscriptions.create({
395
+ customer: input.customerId,
396
+ items,
397
+ metadata: input.metadata,
398
+ trial_period_days: input.trialPeriodDays,
399
+ coupon: input.couponId,
400
+ promotion_code: input.promotionCodeId,
401
+ payment_behavior: input.paymentBehavior ?? "default_incomplete",
402
+ cancel_at_period_end: input.cancelAtPeriodEnd,
403
+ billing_cycle_anchor: input.billingCycleAnchor,
404
+ proration_behavior: input.prorationBehavior,
405
+ expand: ["latest_invoice.payment_intent"]
406
+ });
407
+ return success(subscription);
408
+ } catch (error) {
409
+ return handleStripeError(error);
410
+ }
411
+ }
412
+ async function retrieveSubscription(subscriptionId) {
413
+ try {
414
+ const stripe = getStripe();
415
+ const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
416
+ expand: ["latest_invoice.payment_intent", "default_payment_method"]
417
+ });
418
+ return success(subscription);
419
+ } catch (error) {
420
+ return handleStripeError(error);
421
+ }
422
+ }
423
+ async function updateSubscription(input) {
424
+ try {
425
+ const stripe = getStripe();
426
+ const params = {
427
+ metadata: input.metadata,
428
+ cancel_at_period_end: input.cancelAtPeriodEnd,
429
+ proration_behavior: input.prorationBehavior,
430
+ coupon: input.couponId
431
+ };
432
+ if (input.items) {
433
+ params.items = input.items.map((item) => ({
434
+ id: item.id,
435
+ price: item.priceId,
436
+ quantity: item.quantity
437
+ }));
438
+ }
439
+ const subscription = await stripe.subscriptions.update(input.subscriptionId, params);
440
+ return success(subscription);
441
+ } catch (error) {
442
+ return handleStripeError(error);
443
+ }
444
+ }
445
+ async function cancelSubscription(input) {
446
+ try {
447
+ const stripe = getStripe();
448
+ if (input.cancelAtPeriodEnd) {
449
+ const subscription2 = await stripe.subscriptions.update(input.subscriptionId, {
450
+ cancel_at_period_end: true,
451
+ cancellation_details: input.cancellationDetails ? {
452
+ comment: input.cancellationDetails.comment,
453
+ feedback: input.cancellationDetails.feedback
454
+ } : void 0
455
+ });
456
+ return success(subscription2);
457
+ }
458
+ const subscription = await stripe.subscriptions.cancel(input.subscriptionId, {
459
+ cancellation_details: input.cancellationDetails ? {
460
+ comment: input.cancellationDetails.comment,
461
+ feedback: input.cancellationDetails.feedback
462
+ } : void 0
463
+ });
464
+ return success(subscription);
465
+ } catch (error) {
466
+ return handleStripeError(error);
467
+ }
468
+ }
469
+ async function resumeSubscription(subscriptionId) {
470
+ try {
471
+ const stripe = getStripe();
472
+ const subscription = await stripe.subscriptions.update(subscriptionId, {
473
+ cancel_at_period_end: false
474
+ });
475
+ return success(subscription);
476
+ } catch (error) {
477
+ return handleStripeError(error);
478
+ }
479
+ }
480
+ async function listSubscriptions(input) {
481
+ try {
482
+ const stripe = getStripe();
483
+ const subscriptions = await stripe.subscriptions.list({
484
+ limit: input?.limit ?? 10,
485
+ starting_after: input?.startingAfter,
486
+ ending_before: input?.endingBefore,
487
+ customer: input?.customerId,
488
+ status: input?.status
489
+ });
490
+ return success(subscriptions);
491
+ } catch (error) {
492
+ return handleStripeError(error);
493
+ }
494
+ }
495
+
496
+ // src/server/products/index.ts
497
+ async function createProduct(input) {
498
+ try {
499
+ const stripe = getStripe();
500
+ const product = await stripe.products.create({
501
+ name: input.name,
502
+ description: input.description,
503
+ images: input.images,
504
+ metadata: input.metadata,
505
+ active: input.active,
506
+ default_price_data: input.defaultPriceData ? {
507
+ unit_amount: input.defaultPriceData.unitAmount,
508
+ currency: input.defaultPriceData.currency,
509
+ recurring: input.defaultPriceData.recurring ? {
510
+ interval: input.defaultPriceData.recurring.interval,
511
+ interval_count: input.defaultPriceData.recurring.intervalCount
512
+ } : void 0
513
+ } : void 0
514
+ });
515
+ return success(product);
516
+ } catch (error) {
517
+ return handleStripeError(error);
518
+ }
519
+ }
520
+ async function retrieveProduct(productId) {
521
+ try {
522
+ const stripe = getStripe();
523
+ const product = await stripe.products.retrieve(productId);
524
+ return success(product);
525
+ } catch (error) {
526
+ return handleStripeError(error);
527
+ }
528
+ }
529
+ async function updateProduct(input) {
530
+ try {
531
+ const stripe = getStripe();
532
+ const product = await stripe.products.update(input.productId, {
533
+ name: input.name,
534
+ description: input.description,
535
+ images: input.images,
536
+ metadata: input.metadata,
537
+ active: input.active
538
+ });
539
+ return success(product);
540
+ } catch (error) {
541
+ return handleStripeError(error);
542
+ }
543
+ }
544
+ async function archiveProduct(productId) {
545
+ try {
546
+ const stripe = getStripe();
547
+ const product = await stripe.products.update(productId, { active: false });
548
+ return success(product);
549
+ } catch (error) {
550
+ return handleStripeError(error);
551
+ }
552
+ }
553
+ async function listProducts(input) {
554
+ try {
555
+ const stripe = getStripe();
556
+ const products = await stripe.products.list({
557
+ limit: input?.limit ?? 10,
558
+ starting_after: input?.startingAfter,
559
+ ending_before: input?.endingBefore,
560
+ active: input?.active
561
+ });
562
+ return success(products);
563
+ } catch (error) {
564
+ return handleStripeError(error);
565
+ }
566
+ }
567
+ async function createPrice(input) {
568
+ try {
569
+ const stripe = getStripe();
570
+ const params = {
571
+ product: input.productId,
572
+ currency: input.currency,
573
+ metadata: input.metadata,
574
+ active: input.active,
575
+ nickname: input.nickname,
576
+ lookup_key: input.lookupKey,
577
+ billing_scheme: input.billingScheme,
578
+ recurring: input.recurring ? {
579
+ interval: input.recurring.interval,
580
+ interval_count: input.recurring.intervalCount
581
+ } : void 0
582
+ };
583
+ if (input.billingScheme === "tiered" && input.tiers) {
584
+ if (!input.tiersMode) {
585
+ throw new Error('tiersMode is required when billingScheme is "tiered"');
586
+ }
587
+ params.tiers = input.tiers.map((tier) => ({
588
+ up_to: tier.upTo,
589
+ unit_amount: tier.unitAmount,
590
+ flat_amount: tier.flatAmount
591
+ }));
592
+ params.tiers_mode = input.tiersMode;
593
+ } else {
594
+ params.unit_amount = input.unitAmount;
595
+ }
596
+ const price = await stripe.prices.create(params);
597
+ return success(price);
598
+ } catch (error) {
599
+ return handleStripeError(error);
600
+ }
601
+ }
602
+ async function retrievePrice(priceId) {
603
+ try {
604
+ const stripe = getStripe();
605
+ const price = await stripe.prices.retrieve(priceId);
606
+ return success(price);
607
+ } catch (error) {
608
+ return handleStripeError(error);
609
+ }
610
+ }
611
+ async function listPrices(input) {
612
+ try {
613
+ const stripe = getStripe();
614
+ const prices = await stripe.prices.list({
615
+ limit: input?.limit ?? 10,
616
+ starting_after: input?.startingAfter,
617
+ ending_before: input?.endingBefore,
618
+ product: input?.productId,
619
+ active: input?.active,
620
+ type: input?.type
621
+ });
622
+ return success(prices);
623
+ } catch (error) {
624
+ return handleStripeError(error);
625
+ }
626
+ }
627
+ async function archivePrice(priceId) {
628
+ try {
629
+ const stripe = getStripe();
630
+ const price = await stripe.prices.update(priceId, { active: false });
631
+ return success(price);
632
+ } catch (error) {
633
+ return handleStripeError(error);
634
+ }
635
+ }
636
+
637
+ // src/server/invoices/index.ts
638
+ async function createInvoice(input) {
639
+ try {
640
+ const stripe = getStripe();
641
+ const invoice = await stripe.invoices.create({
642
+ customer: input.customerId,
643
+ collection_method: input.collectionMethod ?? "charge_automatically",
644
+ days_until_due: input.daysUntilDue,
645
+ metadata: input.metadata,
646
+ description: input.description,
647
+ auto_advance: input.autoAdvance
648
+ });
649
+ return success(invoice);
650
+ } catch (error) {
651
+ return handleStripeError(error);
652
+ }
653
+ }
654
+ async function retrieveInvoice(invoiceId) {
655
+ try {
656
+ const stripe = getStripe();
657
+ const invoice = await stripe.invoices.retrieve(invoiceId);
658
+ return success(invoice);
659
+ } catch (error) {
660
+ return handleStripeError(error);
661
+ }
662
+ }
663
+ async function finalizeInvoice(invoiceId) {
664
+ try {
665
+ const stripe = getStripe();
666
+ const invoice = await stripe.invoices.finalizeInvoice(invoiceId);
667
+ return success(invoice);
668
+ } catch (error) {
669
+ return handleStripeError(error);
670
+ }
671
+ }
672
+ async function sendInvoice(invoiceId) {
673
+ try {
674
+ const stripe = getStripe();
675
+ const invoice = await stripe.invoices.sendInvoice(invoiceId);
676
+ return success(invoice);
677
+ } catch (error) {
678
+ return handleStripeError(error);
679
+ }
680
+ }
681
+ async function payInvoice(invoiceId) {
682
+ try {
683
+ const stripe = getStripe();
684
+ const invoice = await stripe.invoices.pay(invoiceId);
685
+ return success(invoice);
686
+ } catch (error) {
687
+ return handleStripeError(error);
688
+ }
689
+ }
690
+ async function voidInvoice(invoiceId) {
691
+ try {
692
+ const stripe = getStripe();
693
+ const invoice = await stripe.invoices.voidInvoice(invoiceId);
694
+ return success(invoice);
695
+ } catch (error) {
696
+ return handleStripeError(error);
697
+ }
698
+ }
699
+ async function listInvoices(input) {
700
+ try {
701
+ const stripe = getStripe();
702
+ const invoices = await stripe.invoices.list({
703
+ limit: input?.limit ?? 10,
704
+ starting_after: input?.startingAfter,
705
+ ending_before: input?.endingBefore,
706
+ customer: input?.customerId,
707
+ status: input?.status
708
+ });
709
+ return success(invoices);
710
+ } catch (error) {
711
+ return handleStripeError(error);
712
+ }
713
+ }
714
+ async function getUpcomingInvoice(customerId, subscriptionId) {
715
+ try {
716
+ const stripe = getStripe();
717
+ const invoice = await stripe.invoices.retrieveUpcoming({
718
+ customer: customerId,
719
+ subscription: subscriptionId
720
+ });
721
+ return success(invoice);
722
+ } catch (error) {
723
+ return handleStripeError(error);
724
+ }
725
+ }
726
+ async function createInvoiceItem(input) {
727
+ try {
728
+ const stripe = getStripe();
729
+ const invoiceItem = await stripe.invoiceItems.create({
730
+ customer: input.customerId,
731
+ invoice: input.invoiceId,
732
+ price: input.priceId,
733
+ amount: input.amount,
734
+ currency: input.currency,
735
+ description: input.description,
736
+ quantity: input.quantity,
737
+ metadata: input.metadata
738
+ });
739
+ return success(invoiceItem);
740
+ } catch (error) {
741
+ return handleStripeError(error);
742
+ }
743
+ }
744
+
745
+ // src/server/refunds/index.ts
746
+ async function createRefund(input) {
747
+ try {
748
+ const stripe = getStripe();
749
+ const refund = await stripe.refunds.create({
750
+ payment_intent: input.paymentIntentId,
751
+ charge: input.chargeId,
752
+ amount: input.amount,
753
+ reason: input.reason,
754
+ metadata: input.metadata
755
+ });
756
+ return success(refund);
757
+ } catch (error) {
758
+ return handleStripeError(error);
759
+ }
760
+ }
761
+ async function retrieveRefund(refundId) {
762
+ try {
763
+ const stripe = getStripe();
764
+ const refund = await stripe.refunds.retrieve(refundId);
765
+ return success(refund);
766
+ } catch (error) {
767
+ return handleStripeError(error);
768
+ }
769
+ }
770
+ async function listRefunds(input) {
771
+ try {
772
+ const stripe = getStripe();
773
+ const refunds = await stripe.refunds.list({
774
+ limit: input?.limit ?? 10,
775
+ starting_after: input?.startingAfter,
776
+ ending_before: input?.endingBefore,
777
+ payment_intent: input?.paymentIntentId,
778
+ charge: input?.chargeId
779
+ });
780
+ return success(refunds);
781
+ } catch (error) {
782
+ return handleStripeError(error);
783
+ }
784
+ }
785
+ async function retrieveDispute(disputeId) {
786
+ try {
787
+ const stripe = getStripe();
788
+ const dispute = await stripe.disputes.retrieve(disputeId);
789
+ return success(dispute);
790
+ } catch (error) {
791
+ return handleStripeError(error);
792
+ }
793
+ }
794
+ async function updateDispute(input) {
795
+ try {
796
+ const stripe = getStripe();
797
+ const dispute = await stripe.disputes.update(input.disputeId, {
798
+ evidence: input.evidence ? {
799
+ customer_name: input.evidence.customerName,
800
+ customer_email_address: input.evidence.customerEmailAddress,
801
+ customer_communication: input.evidence.customerCommunication,
802
+ product_description: input.evidence.productDescription,
803
+ shipping_documentation: input.evidence.shippingDocumentation,
804
+ service_documentation: input.evidence.serviceDocumentation,
805
+ uncategorized_text: input.evidence.uncategorizedText
806
+ } : void 0,
807
+ metadata: input.metadata,
808
+ submit: input.submit
809
+ });
810
+ return success(dispute);
811
+ } catch (error) {
812
+ return handleStripeError(error);
813
+ }
814
+ }
815
+ async function closeDispute(disputeId) {
816
+ try {
817
+ const stripe = getStripe();
818
+ const dispute = await stripe.disputes.close(disputeId);
819
+ return success(dispute);
820
+ } catch (error) {
821
+ return handleStripeError(error);
822
+ }
823
+ }
824
+ async function listDisputes(input) {
825
+ try {
826
+ const stripe = getStripe();
827
+ const disputes = await stripe.disputes.list({
828
+ limit: input?.limit ?? 10,
829
+ starting_after: input?.startingAfter,
830
+ ending_before: input?.endingBefore
831
+ });
832
+ return success(disputes);
833
+ } catch (error) {
834
+ return handleStripeError(error);
835
+ }
836
+ }
837
+
838
+ // src/server/connect/index.ts
839
+ async function createConnectAccount(input) {
840
+ try {
841
+ const stripe = getStripe();
842
+ const account = await stripe.accounts.create({
843
+ type: input.type,
844
+ country: input.country,
845
+ email: input.email,
846
+ capabilities: input.capabilities,
847
+ business_type: input.businessType,
848
+ metadata: input.metadata
849
+ });
850
+ return success(account);
851
+ } catch (error) {
852
+ return handleStripeError(error);
853
+ }
854
+ }
855
+ async function retrieveConnectAccount(accountId) {
856
+ try {
857
+ const stripe = getStripe();
858
+ const account = await stripe.accounts.retrieve(accountId);
859
+ return success(account);
860
+ } catch (error) {
861
+ return handleStripeError(error);
862
+ }
863
+ }
864
+ async function deleteConnectAccount(accountId) {
865
+ try {
866
+ const stripe = getStripe();
867
+ const deleted = await stripe.accounts.del(accountId);
868
+ return success(deleted);
869
+ } catch (error) {
870
+ return handleStripeError(error);
871
+ }
872
+ }
873
+ async function listConnectAccounts(input) {
874
+ try {
875
+ const stripe = getStripe();
876
+ const accounts = await stripe.accounts.list({
877
+ limit: input?.limit ?? 10,
878
+ starting_after: input?.startingAfter,
879
+ ending_before: input?.endingBefore
880
+ });
881
+ return success(accounts);
882
+ } catch (error) {
883
+ return handleStripeError(error);
884
+ }
885
+ }
886
+ async function createAccountLink(input) {
887
+ try {
888
+ const stripe = getStripe();
889
+ const accountLink = await stripe.accountLinks.create({
890
+ account: input.accountId,
891
+ refresh_url: input.refreshUrl,
892
+ return_url: input.returnUrl,
893
+ type: input.type
894
+ });
895
+ return success(accountLink);
896
+ } catch (error) {
897
+ return handleStripeError(error);
898
+ }
899
+ }
900
+ async function createTransfer(input) {
901
+ try {
902
+ const stripe = getStripe();
903
+ const transfer = await stripe.transfers.create({
904
+ amount: input.amount,
905
+ currency: input.currency,
906
+ destination: input.destinationAccountId,
907
+ description: input.description,
908
+ metadata: input.metadata,
909
+ source_transaction: input.sourceTransaction
910
+ });
911
+ return success(transfer);
912
+ } catch (error) {
913
+ return handleStripeError(error);
914
+ }
915
+ }
916
+ async function listTransfers(input) {
917
+ try {
918
+ const stripe = getStripe();
919
+ const transfers = await stripe.transfers.list({
920
+ limit: input?.limit ?? 10,
921
+ starting_after: input?.startingAfter,
922
+ ending_before: input?.endingBefore,
923
+ destination: input?.destinationAccountId
924
+ });
925
+ return success(transfers);
926
+ } catch (error) {
927
+ return handleStripeError(error);
928
+ }
929
+ }
930
+ async function createPayout(amount, currency, metadata) {
931
+ try {
932
+ const stripe = getStripe();
933
+ const payout = await stripe.payouts.create({
934
+ amount,
935
+ currency,
936
+ metadata
937
+ });
938
+ return success(payout);
939
+ } catch (error) {
940
+ return handleStripeError(error);
941
+ }
942
+ }
943
+ async function listPayouts(input) {
944
+ try {
945
+ const stripe = getStripe();
946
+ const payouts = await stripe.payouts.list({
947
+ limit: input?.limit ?? 10,
948
+ starting_after: input?.startingAfter,
949
+ ending_before: input?.endingBefore,
950
+ status: input?.status
951
+ });
952
+ return success(payouts);
953
+ } catch (error) {
954
+ return handleStripeError(error);
955
+ }
956
+ }
957
+ async function getBalance() {
958
+ try {
959
+ const stripe = getStripe();
960
+ const balance = await stripe.balance.retrieve();
961
+ return success(balance);
962
+ } catch (error) {
963
+ return handleStripeError(error);
964
+ }
965
+ }
966
+ async function listBalanceTransactions(input) {
967
+ try {
968
+ const stripe = getStripe();
969
+ const transactions = await stripe.balanceTransactions.list({
970
+ limit: input?.limit ?? 10,
971
+ starting_after: input?.startingAfter,
972
+ ending_before: input?.endingBefore,
973
+ type: input?.type
974
+ });
975
+ return success(transactions);
976
+ } catch (error) {
977
+ return handleStripeError(error);
978
+ }
979
+ }
980
+
981
+ // src/server/coupons/index.ts
982
+ async function createCoupon(input) {
983
+ try {
984
+ const stripe = getStripe();
985
+ const coupon = await stripe.coupons.create({
986
+ percent_off: input.percentOff,
987
+ amount_off: input.amountOff,
988
+ currency: input.currency,
989
+ duration: input.duration,
990
+ duration_in_months: input.durationInMonths,
991
+ max_redemptions: input.maxRedemptions,
992
+ redeem_by: input.redeemBy,
993
+ name: input.name,
994
+ metadata: input.metadata
995
+ });
996
+ return success(coupon);
997
+ } catch (error) {
998
+ return handleStripeError(error);
999
+ }
1000
+ }
1001
+ async function retrieveCoupon(couponId) {
1002
+ try {
1003
+ const stripe = getStripe();
1004
+ const coupon = await stripe.coupons.retrieve(couponId);
1005
+ return success(coupon);
1006
+ } catch (error) {
1007
+ return handleStripeError(error);
1008
+ }
1009
+ }
1010
+ async function deleteCoupon(couponId) {
1011
+ try {
1012
+ const stripe = getStripe();
1013
+ const deleted = await stripe.coupons.del(couponId);
1014
+ return success(deleted);
1015
+ } catch (error) {
1016
+ return handleStripeError(error);
1017
+ }
1018
+ }
1019
+ async function listCoupons(input) {
1020
+ try {
1021
+ const stripe = getStripe();
1022
+ const coupons = await stripe.coupons.list({
1023
+ limit: input?.limit ?? 10,
1024
+ starting_after: input?.startingAfter,
1025
+ ending_before: input?.endingBefore
1026
+ });
1027
+ return success(coupons);
1028
+ } catch (error) {
1029
+ return handleStripeError(error);
1030
+ }
1031
+ }
1032
+ async function createPromotionCode(input) {
1033
+ try {
1034
+ const stripe = getStripe();
1035
+ const promotionCode = await stripe.promotionCodes.create({
1036
+ coupon: input.couponId,
1037
+ code: input.code,
1038
+ active: input.active,
1039
+ max_redemptions: input.maxRedemptions,
1040
+ expires_at: input.expiresAt,
1041
+ metadata: input.metadata,
1042
+ restrictions: input.restrictions ? {
1043
+ first_time_transaction: input.restrictions.firstTimeTransaction,
1044
+ minimum_amount: input.restrictions.minimumAmount,
1045
+ minimum_amount_currency: input.restrictions.minimumAmountCurrency
1046
+ } : void 0
1047
+ });
1048
+ return success(promotionCode);
1049
+ } catch (error) {
1050
+ return handleStripeError(error);
1051
+ }
1052
+ }
1053
+ async function retrievePromotionCode(promotionCodeId) {
1054
+ try {
1055
+ const stripe = getStripe();
1056
+ const promotionCode = await stripe.promotionCodes.retrieve(promotionCodeId);
1057
+ return success(promotionCode);
1058
+ } catch (error) {
1059
+ return handleStripeError(error);
1060
+ }
1061
+ }
1062
+ async function listPromotionCodes(input) {
1063
+ try {
1064
+ const stripe = getStripe();
1065
+ const promotionCodes = await stripe.promotionCodes.list({
1066
+ limit: input?.limit ?? 10,
1067
+ starting_after: input?.startingAfter,
1068
+ ending_before: input?.endingBefore,
1069
+ coupon: input?.couponId,
1070
+ active: input?.active,
1071
+ code: input?.code
1072
+ });
1073
+ return success(promotionCodes);
1074
+ } catch (error) {
1075
+ return handleStripeError(error);
1076
+ }
1077
+ }
1078
+
1079
+ // src/server/webhooks/index.ts
1080
+ var MAX_BODY_SIZE = 1024 * 1024;
1081
+ function createWebhookHandler(config) {
1082
+ const webhookSecret = config.secret ?? getConfig().webhookSecret;
1083
+ if (!webhookSecret) {
1084
+ throw new Error(
1085
+ "[@stripe-sdk/core] Webhook secret is required. Pass it via config.secret or initStripe({ webhookSecret })."
1086
+ );
1087
+ }
1088
+ return async function handleWebhook(body, signature) {
1089
+ const stripe = getStripe();
1090
+ let event;
1091
+ try {
1092
+ event = stripe.webhooks.constructEvent(body, signature, webhookSecret);
1093
+ } catch (err) {
1094
+ const verificationError = err instanceof Error ? err : new Error("Webhook signature verification failed");
1095
+ if (config.onError) {
1096
+ await config.onError(verificationError);
1097
+ }
1098
+ throw verificationError;
1099
+ }
1100
+ const handler = config.handlers[event.type];
1101
+ if (handler) {
1102
+ try {
1103
+ await handler(event);
1104
+ } catch (error) {
1105
+ if (config.onError) {
1106
+ await config.onError(error instanceof Error ? error : new Error(String(error)), event);
1107
+ } else {
1108
+ throw error;
1109
+ }
1110
+ }
1111
+ } else if (config.onUnhandledEvent) {
1112
+ await config.onUnhandledEvent(event);
1113
+ }
1114
+ return { received: true, type: event.type };
1115
+ };
1116
+ }
1117
+ function createNextWebhookHandler(config) {
1118
+ const handler = createWebhookHandler(config);
1119
+ return async function POST(request) {
1120
+ try {
1121
+ const contentLength = request.headers.get("content-length");
1122
+ if (contentLength && parseInt(contentLength, 10) > MAX_BODY_SIZE) {
1123
+ return new Response(JSON.stringify({ error: "Webhook body too large" }), {
1124
+ status: 413,
1125
+ headers: { "Content-Type": "application/json" }
1126
+ });
1127
+ }
1128
+ const body = await request.text();
1129
+ const signature = request.headers.get("stripe-signature");
1130
+ if (!signature) {
1131
+ return new Response(JSON.stringify({ error: "Missing stripe-signature header" }), {
1132
+ status: 400,
1133
+ headers: { "Content-Type": "application/json" }
1134
+ });
1135
+ }
1136
+ const result = await handler(body, signature);
1137
+ return new Response(JSON.stringify(result), {
1138
+ status: 200,
1139
+ headers: { "Content-Type": "application/json" }
1140
+ });
1141
+ } catch (error) {
1142
+ const message = error instanceof Error ? error.message : "Webhook handler failed";
1143
+ return new Response(JSON.stringify({ error: message }), {
1144
+ status: 400,
1145
+ headers: { "Content-Type": "application/json" }
1146
+ });
1147
+ }
1148
+ };
1149
+ }
1150
+ function createPagesWebhookHandler(webhookConfig) {
1151
+ const handler = createWebhookHandler(webhookConfig);
1152
+ return async function webhookRoute(req, res) {
1153
+ if (req.method !== "POST") {
1154
+ res.status(405).end();
1155
+ return;
1156
+ }
1157
+ try {
1158
+ const body = await getRawBody(req);
1159
+ const signature = req.headers["stripe-signature"];
1160
+ if (!signature) {
1161
+ res.status(400).json({ error: "Missing stripe-signature header" });
1162
+ return;
1163
+ }
1164
+ const result = await handler(body, signature);
1165
+ res.status(200).json(result);
1166
+ } catch (error) {
1167
+ const message = error instanceof Error ? error.message : "Webhook handler failed";
1168
+ res.status(400).json({ error: message });
1169
+ }
1170
+ };
1171
+ }
1172
+ function getRawBody(req) {
1173
+ return new Promise((resolve, reject) => {
1174
+ if (typeof req.body === "string") {
1175
+ if (req.body.length > MAX_BODY_SIZE) {
1176
+ reject(new Error("Webhook body too large"));
1177
+ return;
1178
+ }
1179
+ resolve(req.body);
1180
+ return;
1181
+ }
1182
+ if (Buffer.isBuffer(req.body)) {
1183
+ if (req.body.length > MAX_BODY_SIZE) {
1184
+ reject(new Error("Webhook body too large"));
1185
+ return;
1186
+ }
1187
+ resolve(req.body.toString("utf8"));
1188
+ return;
1189
+ }
1190
+ if (!req.on) {
1191
+ reject(new Error("Cannot read raw body from request"));
1192
+ return;
1193
+ }
1194
+ const chunks = [];
1195
+ let totalLength = 0;
1196
+ req.on("data", (chunk) => {
1197
+ const buf = Buffer.from(chunk);
1198
+ totalLength += buf.length;
1199
+ if (totalLength > MAX_BODY_SIZE) {
1200
+ reject(new Error("Webhook body too large"));
1201
+ return;
1202
+ }
1203
+ chunks.push(buf);
1204
+ });
1205
+ req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
1206
+ req.on("error", reject);
1207
+ });
1208
+ }
1209
+ var StripeContext = createContext(null);
1210
+ function useStripeConfig() {
1211
+ const context = useContext(StripeContext);
1212
+ if (!context) {
1213
+ throw new Error("useStripeConfig must be used within a <StripeProvider>");
1214
+ }
1215
+ return context;
1216
+ }
1217
+ function StripeProvider({
1218
+ publishableKey,
1219
+ children,
1220
+ options,
1221
+ locale
1222
+ }) {
1223
+ const stripePromise = useMemo(
1224
+ () => loadStripe(publishableKey, locale ? { locale } : void 0),
1225
+ [publishableKey, locale]
1226
+ );
1227
+ return /* @__PURE__ */ jsx(StripeContext.Provider, { value: { publishableKey }, children: /* @__PURE__ */ jsx(Elements, { stripe: stripePromise, options, children }) });
1228
+ }
1229
+ function StripeElementsProvider({
1230
+ publishableKey,
1231
+ clientSecret,
1232
+ children,
1233
+ appearance,
1234
+ locale,
1235
+ loader = "auto"
1236
+ }) {
1237
+ const stripePromise = useMemo(
1238
+ () => loadStripe(publishableKey, locale ? { locale } : void 0),
1239
+ [publishableKey, locale]
1240
+ );
1241
+ const options = useMemo(
1242
+ () => ({
1243
+ clientSecret,
1244
+ appearance,
1245
+ loader
1246
+ }),
1247
+ [clientSecret, appearance, loader]
1248
+ );
1249
+ return /* @__PURE__ */ jsx(StripeContext.Provider, { value: { publishableKey }, children: /* @__PURE__ */ jsx(Elements, { stripe: stripePromise, options, children }) });
1250
+ }
1251
+ function usePayment(options) {
1252
+ const stripe = useStripe();
1253
+ const elements = useElements();
1254
+ const [state, setState] = useState({
1255
+ isProcessing: false,
1256
+ isSuccess: false,
1257
+ error: null,
1258
+ paymentIntentId: null
1259
+ });
1260
+ const processPayment = useCallback(async (overrides) => {
1261
+ if (!stripe || !elements) {
1262
+ setState((s) => ({ ...s, error: "Stripe not loaded yet" }));
1263
+ return { success: false, error: "Stripe not loaded yet" };
1264
+ }
1265
+ setState({ isProcessing: true, isSuccess: false, error: null, paymentIntentId: null });
1266
+ const { error, paymentIntent } = await stripe.confirmPayment({
1267
+ elements,
1268
+ confirmParams: {
1269
+ return_url: overrides?.returnUrl ?? options?.returnUrl ?? (typeof window !== "undefined" ? window.location.href : "")
1270
+ },
1271
+ redirect: "if_required"
1272
+ });
1273
+ if (error) {
1274
+ const message = error.message ?? "Payment failed";
1275
+ setState({ isProcessing: false, isSuccess: false, error: message, paymentIntentId: null });
1276
+ options?.onError?.(message);
1277
+ return { success: false, error: message };
1278
+ }
1279
+ if (paymentIntent?.status === "succeeded") {
1280
+ setState({ isProcessing: false, isSuccess: true, error: null, paymentIntentId: paymentIntent.id });
1281
+ options?.onSuccess?.(paymentIntent.id);
1282
+ return { success: true, paymentIntentId: paymentIntent.id };
1283
+ }
1284
+ setState({ isProcessing: false, isSuccess: false, error: null, paymentIntentId: paymentIntent?.id ?? null });
1285
+ return { success: false, status: paymentIntent?.status };
1286
+ }, [stripe, elements, options]);
1287
+ const reset = useCallback(() => {
1288
+ setState({ isProcessing: false, isSuccess: false, error: null, paymentIntentId: null });
1289
+ }, []);
1290
+ return {
1291
+ ...state,
1292
+ processPayment,
1293
+ reset,
1294
+ isReady: !!stripe && !!elements
1295
+ };
1296
+ }
1297
+ function useSetupIntent(options) {
1298
+ const stripe = useStripe();
1299
+ const elements = useElements();
1300
+ const [state, setState] = useState({
1301
+ isProcessing: false,
1302
+ isSuccess: false,
1303
+ error: null,
1304
+ setupIntentId: null,
1305
+ paymentMethodId: null
1306
+ });
1307
+ const confirmSetup = useCallback(async (overrides) => {
1308
+ if (!stripe || !elements) {
1309
+ setState((s) => ({ ...s, error: "Stripe not loaded yet" }));
1310
+ return { success: false, error: "Stripe not loaded yet" };
1311
+ }
1312
+ setState({ isProcessing: true, isSuccess: false, error: null, setupIntentId: null, paymentMethodId: null });
1313
+ const { error, setupIntent } = await stripe.confirmSetup({
1314
+ elements,
1315
+ confirmParams: {
1316
+ return_url: overrides?.returnUrl ?? options?.returnUrl ?? (typeof window !== "undefined" ? window.location.href : "")
1317
+ },
1318
+ redirect: "if_required"
1319
+ });
1320
+ if (error) {
1321
+ const message = error.message ?? "Setup failed";
1322
+ setState({ isProcessing: false, isSuccess: false, error: message, setupIntentId: null, paymentMethodId: null });
1323
+ options?.onError?.(message);
1324
+ return { success: false, error: message };
1325
+ }
1326
+ if (setupIntent?.status === "succeeded") {
1327
+ const pmId = typeof setupIntent.payment_method === "string" ? setupIntent.payment_method : setupIntent.payment_method?.id ?? null;
1328
+ setState({ isProcessing: false, isSuccess: true, error: null, setupIntentId: setupIntent.id, paymentMethodId: pmId });
1329
+ if (pmId) options?.onSuccess?.(setupIntent.id, pmId);
1330
+ return { success: true, setupIntentId: setupIntent.id, paymentMethodId: pmId };
1331
+ }
1332
+ setState({ isProcessing: false, isSuccess: false, error: null, setupIntentId: setupIntent?.id ?? null, paymentMethodId: null });
1333
+ return { success: false, status: setupIntent?.status };
1334
+ }, [stripe, elements, options]);
1335
+ const reset = useCallback(() => {
1336
+ setState({ isProcessing: false, isSuccess: false, error: null, setupIntentId: null, paymentMethodId: null });
1337
+ }, []);
1338
+ return {
1339
+ ...state,
1340
+ confirmSetup,
1341
+ reset,
1342
+ isReady: !!stripe && !!elements
1343
+ };
1344
+ }
1345
+ function useCheckout(options) {
1346
+ const [state, setState] = useState({
1347
+ isLoading: false,
1348
+ error: null
1349
+ });
1350
+ const redirectToCheckout = useCallback(async (sessionId) => {
1351
+ setState({ isLoading: true, error: null });
1352
+ try {
1353
+ const stripe = await loadStripe(options.publishableKey);
1354
+ if (!stripe) {
1355
+ throw new Error("Failed to load Stripe");
1356
+ }
1357
+ const { error } = await stripe.redirectToCheckout({ sessionId });
1358
+ if (error) {
1359
+ const message = error.message ?? "Redirect to checkout failed";
1360
+ setState({ isLoading: false, error: message });
1361
+ options.onError?.(message);
1362
+ return { success: false, error: message };
1363
+ }
1364
+ return { success: true };
1365
+ } catch (err) {
1366
+ const message = err instanceof Error ? err.message : "Unknown error";
1367
+ setState({ isLoading: false, error: message });
1368
+ options.onError?.(message);
1369
+ return { success: false, error: message };
1370
+ }
1371
+ }, [options]);
1372
+ const redirectToPortal = useCallback((portalUrl) => {
1373
+ window.location.href = portalUrl;
1374
+ }, []);
1375
+ return {
1376
+ ...state,
1377
+ redirectToCheckout,
1378
+ redirectToPortal
1379
+ };
1380
+ }
1381
+ function CheckoutForm({
1382
+ onSuccess,
1383
+ onError,
1384
+ returnUrl,
1385
+ submitLabel = "Pay now",
1386
+ showEmail = false,
1387
+ className,
1388
+ buttonClassName,
1389
+ errorClassName,
1390
+ children,
1391
+ layout = "tabs"
1392
+ }) {
1393
+ const { processPayment, isProcessing, isSuccess, error, isReady } = usePayment({
1394
+ onSuccess,
1395
+ onError,
1396
+ returnUrl
1397
+ });
1398
+ const handleSubmit = async (e) => {
1399
+ e.preventDefault();
1400
+ await processPayment();
1401
+ };
1402
+ if (isSuccess) {
1403
+ return /* @__PURE__ */ jsx("div", { className, children: children ?? /* @__PURE__ */ jsx("p", { children: "Payment successful!" }) });
1404
+ }
1405
+ return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className, children: [
1406
+ showEmail && /* @__PURE__ */ jsx(LinkAuthenticationElement, {}),
1407
+ /* @__PURE__ */ jsx(PaymentElement, { options: { layout } }),
1408
+ error && /* @__PURE__ */ jsx("p", { className: errorClassName, role: "alert", children: error }),
1409
+ /* @__PURE__ */ jsx(
1410
+ "button",
1411
+ {
1412
+ type: "submit",
1413
+ disabled: !isReady || isProcessing,
1414
+ className: buttonClassName,
1415
+ children: isProcessing ? "Processing..." : submitLabel
1416
+ }
1417
+ )
1418
+ ] });
1419
+ }
1420
+ function SetupForm({
1421
+ onSuccess,
1422
+ onError,
1423
+ returnUrl,
1424
+ submitLabel = "Save payment method",
1425
+ className,
1426
+ buttonClassName,
1427
+ errorClassName,
1428
+ successContent,
1429
+ layout = "tabs"
1430
+ }) {
1431
+ const { confirmSetup, isProcessing, isSuccess, error, isReady } = useSetupIntent({
1432
+ onSuccess,
1433
+ onError,
1434
+ returnUrl
1435
+ });
1436
+ const handleSubmit = async (e) => {
1437
+ e.preventDefault();
1438
+ await confirmSetup();
1439
+ };
1440
+ if (isSuccess) {
1441
+ return /* @__PURE__ */ jsx("div", { className, children: successContent ?? /* @__PURE__ */ jsx("p", { children: "Payment method saved!" }) });
1442
+ }
1443
+ return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className, children: [
1444
+ /* @__PURE__ */ jsx(PaymentElement, { options: { layout } }),
1445
+ error && /* @__PURE__ */ jsx("p", { className: errorClassName, role: "alert", children: error }),
1446
+ /* @__PURE__ */ jsx(
1447
+ "button",
1448
+ {
1449
+ type: "submit",
1450
+ disabled: !isReady || isProcessing,
1451
+ className: buttonClassName,
1452
+ children: isProcessing ? "Saving..." : submitLabel
1453
+ }
1454
+ )
1455
+ ] });
1456
+ }
1457
+ function defaultFormatPrice(amount, currency) {
1458
+ return new Intl.NumberFormat(void 0, {
1459
+ style: "currency",
1460
+ currency,
1461
+ minimumFractionDigits: 0
1462
+ }).format(amount / 100);
1463
+ }
1464
+ function PricingTable({
1465
+ plans,
1466
+ onSelectPlan,
1467
+ isLoading,
1468
+ currentPlanId,
1469
+ buttonLabel = "Get started",
1470
+ currentPlanLabel = "Current plan",
1471
+ className,
1472
+ planClassName,
1473
+ highlightedClassName,
1474
+ buttonClassName,
1475
+ formatPrice = defaultFormatPrice,
1476
+ renderFeature
1477
+ }) {
1478
+ const defaultStyles = {
1479
+ container: {
1480
+ display: "grid",
1481
+ gridTemplateColumns: `repeat(${Math.min(plans.length, 4)}, 1fr)`,
1482
+ gap: "1.5rem",
1483
+ maxWidth: "1200px",
1484
+ margin: "0 auto"
1485
+ },
1486
+ plan: {
1487
+ border: "1px solid #e5e7eb",
1488
+ borderRadius: "0.75rem",
1489
+ padding: "2rem",
1490
+ display: "flex",
1491
+ flexDirection: "column"
1492
+ },
1493
+ highlighted: {
1494
+ border: "2px solid #6366f1",
1495
+ boxShadow: "0 4px 14px rgba(99,102,241,0.15)"
1496
+ },
1497
+ badge: {
1498
+ background: "#6366f1",
1499
+ color: "#fff",
1500
+ padding: "0.25rem 0.75rem",
1501
+ borderRadius: "9999px",
1502
+ fontSize: "0.75rem",
1503
+ fontWeight: 600,
1504
+ alignSelf: "flex-start",
1505
+ marginBottom: "0.5rem"
1506
+ },
1507
+ name: { fontSize: "1.25rem", fontWeight: 700, margin: "0 0 0.25rem" },
1508
+ description: { color: "#6b7280", fontSize: "0.875rem", margin: "0 0 1rem" },
1509
+ price: { fontSize: "2.5rem", fontWeight: 800, margin: "0" },
1510
+ interval: { color: "#6b7280", fontSize: "0.875rem", fontWeight: 400 },
1511
+ features: { listStyle: "none", padding: 0, margin: "1.5rem 0", flex: 1 },
1512
+ feature: { padding: "0.375rem 0", fontSize: "0.875rem" },
1513
+ button: {
1514
+ padding: "0.75rem 1.5rem",
1515
+ borderRadius: "0.5rem",
1516
+ border: "none",
1517
+ fontWeight: 600,
1518
+ cursor: "pointer",
1519
+ fontSize: "0.875rem",
1520
+ background: "#6366f1",
1521
+ color: "#fff",
1522
+ width: "100%"
1523
+ },
1524
+ currentButton: {
1525
+ background: "#e5e7eb",
1526
+ color: "#374151",
1527
+ cursor: "default"
1528
+ }
1529
+ };
1530
+ return /* @__PURE__ */ jsx("div", { className, style: !className ? defaultStyles.container : void 0, children: plans.map((plan) => {
1531
+ const isCurrent = plan.id === currentPlanId;
1532
+ return /* @__PURE__ */ jsxs(
1533
+ "div",
1534
+ {
1535
+ className: plan.highlighted ? highlightedClassName : planClassName,
1536
+ style: !planClassName ? { ...defaultStyles.plan, ...plan.highlighted ? defaultStyles.highlighted : {} } : void 0,
1537
+ children: [
1538
+ plan.badge && /* @__PURE__ */ jsx("span", { style: !highlightedClassName ? defaultStyles.badge : void 0, children: plan.badge }),
1539
+ /* @__PURE__ */ jsx("h3", { style: defaultStyles.name, children: plan.name }),
1540
+ plan.description && /* @__PURE__ */ jsx("p", { style: defaultStyles.description, children: plan.description }),
1541
+ /* @__PURE__ */ jsxs("p", { style: defaultStyles.price, children: [
1542
+ formatPrice(plan.amount, plan.currency),
1543
+ plan.interval && /* @__PURE__ */ jsxs("span", { style: defaultStyles.interval, children: [
1544
+ " / ",
1545
+ plan.interval
1546
+ ] })
1547
+ ] }),
1548
+ plan.trialDays && /* @__PURE__ */ jsxs("p", { style: { ...defaultStyles.description, marginTop: "0.5rem" }, children: [
1549
+ plan.trialDays,
1550
+ "-day free trial"
1551
+ ] }),
1552
+ /* @__PURE__ */ jsx("ul", { style: defaultStyles.features, children: plan.features.map((feature, i) => /* @__PURE__ */ jsx("li", { style: defaultStyles.feature, children: renderFeature ? renderFeature(feature) : `\u2713 ${feature}` }, i)) }),
1553
+ /* @__PURE__ */ jsx(
1554
+ "button",
1555
+ {
1556
+ onClick: () => !isCurrent && onSelectPlan(plan),
1557
+ disabled: isLoading || isCurrent,
1558
+ className: buttonClassName,
1559
+ style: !buttonClassName ? { ...defaultStyles.button, ...isCurrent ? defaultStyles.currentButton : {} } : void 0,
1560
+ children: isCurrent ? currentPlanLabel : buttonLabel
1561
+ }
1562
+ )
1563
+ ]
1564
+ },
1565
+ plan.id
1566
+ );
1567
+ }) });
1568
+ }
1569
+ function defaultFormatPrice2(amount, currency) {
1570
+ return new Intl.NumberFormat(void 0, {
1571
+ style: "currency",
1572
+ currency,
1573
+ minimumFractionDigits: 0
1574
+ }).format(amount / 100);
1575
+ }
1576
+ var statusColors = {
1577
+ active: "#10b981",
1578
+ trialing: "#6366f1",
1579
+ past_due: "#f59e0b",
1580
+ canceled: "#ef4444",
1581
+ incomplete: "#f59e0b",
1582
+ unpaid: "#ef4444",
1583
+ paused: "#6b7280"
1584
+ };
1585
+ function SubscriptionManager({
1586
+ subscription,
1587
+ onCancel,
1588
+ onResume,
1589
+ onChangePlan,
1590
+ onManageBilling,
1591
+ className,
1592
+ formatPrice = defaultFormatPrice2,
1593
+ cancelLabel = "Cancel subscription",
1594
+ resumeLabel = "Resume subscription",
1595
+ changePlanLabel = "Change plan",
1596
+ manageBillingLabel = "Manage billing"
1597
+ }) {
1598
+ const [isLoading, setIsLoading] = useState(false);
1599
+ const [showConfirm, setShowConfirm] = useState(false);
1600
+ const endDate = new Date(subscription.currentPeriodEnd).toLocaleDateString();
1601
+ const trialEndDate = subscription.trialEnd ? new Date(subscription.trialEnd).toLocaleDateString() : null;
1602
+ const handleCancel = async () => {
1603
+ setIsLoading(true);
1604
+ try {
1605
+ await onCancel(subscription.id);
1606
+ } finally {
1607
+ setIsLoading(false);
1608
+ setShowConfirm(false);
1609
+ }
1610
+ };
1611
+ const handleResume = async () => {
1612
+ if (!onResume) return;
1613
+ setIsLoading(true);
1614
+ try {
1615
+ await onResume(subscription.id);
1616
+ } finally {
1617
+ setIsLoading(false);
1618
+ }
1619
+ };
1620
+ const styles = {
1621
+ container: { border: "1px solid #e5e7eb", borderRadius: "0.75rem", padding: "1.5rem" },
1622
+ header: { display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: "1rem" },
1623
+ planName: { fontSize: "1.25rem", fontWeight: 700, margin: 0 },
1624
+ status: {
1625
+ padding: "0.25rem 0.75rem",
1626
+ borderRadius: "9999px",
1627
+ fontSize: "0.75rem",
1628
+ fontWeight: 600,
1629
+ color: "#fff",
1630
+ background: statusColors[subscription.status] ?? "#6b7280"
1631
+ },
1632
+ price: { fontSize: "1.5rem", fontWeight: 700, margin: "0 0 0.5rem" },
1633
+ detail: { color: "#6b7280", fontSize: "0.875rem", margin: "0.25rem 0" },
1634
+ actions: { display: "flex", gap: "0.75rem", marginTop: "1.5rem", flexWrap: "wrap" },
1635
+ button: {
1636
+ padding: "0.5rem 1rem",
1637
+ borderRadius: "0.375rem",
1638
+ border: "1px solid #d1d5db",
1639
+ background: "#fff",
1640
+ cursor: "pointer",
1641
+ fontSize: "0.875rem",
1642
+ fontWeight: 500
1643
+ },
1644
+ dangerButton: {
1645
+ padding: "0.5rem 1rem",
1646
+ borderRadius: "0.375rem",
1647
+ border: "1px solid #fca5a5",
1648
+ background: "#fef2f2",
1649
+ color: "#dc2626",
1650
+ cursor: "pointer",
1651
+ fontSize: "0.875rem",
1652
+ fontWeight: 500
1653
+ },
1654
+ confirm: {
1655
+ background: "#fef2f2",
1656
+ border: "1px solid #fca5a5",
1657
+ borderRadius: "0.5rem",
1658
+ padding: "1rem",
1659
+ marginTop: "1rem"
1660
+ }
1661
+ };
1662
+ return /* @__PURE__ */ jsxs("div", { className, style: !className ? styles.container : void 0, children: [
1663
+ /* @__PURE__ */ jsxs("div", { style: styles.header, children: [
1664
+ /* @__PURE__ */ jsx("h3", { style: styles.planName, children: subscription.planName }),
1665
+ /* @__PURE__ */ jsx("span", { style: styles.status, children: subscription.status })
1666
+ ] }),
1667
+ /* @__PURE__ */ jsxs("p", { style: styles.price, children: [
1668
+ formatPrice(subscription.amount, subscription.currency),
1669
+ /* @__PURE__ */ jsxs("span", { style: { fontSize: "0.875rem", fontWeight: 400, color: "#6b7280" }, children: [
1670
+ " ",
1671
+ "/ ",
1672
+ subscription.interval
1673
+ ] })
1674
+ ] }),
1675
+ trialEndDate && subscription.status === "trialing" && /* @__PURE__ */ jsxs("p", { style: styles.detail, children: [
1676
+ "Trial ends on ",
1677
+ trialEndDate
1678
+ ] }),
1679
+ subscription.cancelAtPeriodEnd ? /* @__PURE__ */ jsxs("p", { style: { ...styles.detail, color: "#dc2626" }, children: [
1680
+ "Cancels on ",
1681
+ endDate
1682
+ ] }) : /* @__PURE__ */ jsxs("p", { style: styles.detail, children: [
1683
+ "Renews on ",
1684
+ endDate
1685
+ ] }),
1686
+ /* @__PURE__ */ jsxs("div", { style: styles.actions, children: [
1687
+ onChangePlan && subscription.status === "active" && /* @__PURE__ */ jsx("button", { onClick: () => onChangePlan(subscription.id), style: styles.button, children: changePlanLabel }),
1688
+ onManageBilling && /* @__PURE__ */ jsx("button", { onClick: onManageBilling, style: styles.button, children: manageBillingLabel }),
1689
+ subscription.cancelAtPeriodEnd && onResume ? /* @__PURE__ */ jsx("button", { onClick: handleResume, disabled: isLoading, style: styles.button, children: isLoading ? "Loading..." : resumeLabel }) : subscription.status === "active" && /* @__PURE__ */ jsx("button", { onClick: () => setShowConfirm(true), style: styles.dangerButton, children: cancelLabel })
1690
+ ] }),
1691
+ showConfirm && /* @__PURE__ */ jsxs("div", { style: styles.confirm, children: [
1692
+ /* @__PURE__ */ jsx("p", { style: { margin: "0 0 0.75rem", fontWeight: 500 }, children: "Are you sure you want to cancel?" }),
1693
+ /* @__PURE__ */ jsxs("p", { style: { ...styles.detail, marginBottom: "0.75rem" }, children: [
1694
+ "You will still have access until ",
1695
+ endDate,
1696
+ "."
1697
+ ] }),
1698
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: "0.5rem" }, children: [
1699
+ /* @__PURE__ */ jsx("button", { onClick: handleCancel, disabled: isLoading, style: styles.dangerButton, children: isLoading ? "Cancelling..." : "Confirm cancellation" }),
1700
+ /* @__PURE__ */ jsx("button", { onClick: () => setShowConfirm(false), style: styles.button, children: "Keep subscription" })
1701
+ ] })
1702
+ ] })
1703
+ ] });
1704
+ }
1705
+
1706
+ export { CheckoutForm, PricingTable, SetupForm, StripeElementsProvider, StripeProvider, SubscriptionManager, 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, useCheckout, usePayment, useSetupIntent, useStripeConfig, voidInvoice };
1707
+ //# sourceMappingURL=index.mjs.map
1708
+ //# sourceMappingURL=index.mjs.map