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