@paykit-sdk/stripe 1.2.2 → 1.3.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.js CHANGED
@@ -289,19 +289,33 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
289
289
  name: data.name,
290
290
  email: data.email
291
291
  });
292
+ const {
293
+ billing: _billing,
294
+ metadata,
295
+ provider_metadata,
296
+ name: _name,
297
+ ...stripeParams
298
+ } = data;
292
299
  const customer = await this.stripe.customers.create({
293
- ...params,
294
- phone: params.phone ?? void 0,
295
- name: fullName
300
+ ...provider_metadata,
301
+ ...stripeParams,
302
+ phone: data.phone ?? void 0,
303
+ name: fullName,
304
+ metadata: core.stringifyMetadataValues(metadata ?? {})
296
305
  });
297
306
  return Customer$inboundSchema(customer);
298
307
  };
299
308
  this.updateCustomer = async (id, params) => {
300
- const { provider_metadata, ...rest } = params;
309
+ const {
310
+ provider_metadata,
311
+ billing: _billing,
312
+ metadata,
313
+ ...rest
314
+ } = params;
301
315
  const customer = await this.stripe.customers.update(id, {
302
316
  ...provider_metadata,
303
317
  ...rest,
304
- metadata: core.stringifyMetadataValues(rest.metadata ?? {}),
318
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
305
319
  phone: rest.phone ?? void 0
306
320
  });
307
321
  return Customer$inboundSchema(customer);
@@ -547,7 +561,8 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
547
561
  );
548
562
  }
549
563
  const { provider_metadata, customer, ...rest } = data;
550
- const payment = await this.stripe.paymentIntents.retrieve(id);
564
+ const piId = await this._resolvePaymentIntentId(id);
565
+ const payment = await this.stripe.paymentIntents.retrieve(piId);
551
566
  const paymentOptions = {
552
567
  ...provider_metadata,
553
568
  metadata: core.stringifyMetadataValues({
@@ -569,10 +584,10 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
569
584
  paymentOptions.receipt_email = customer.email;
570
585
  }
571
586
  const updatedPayment = await this.stripe.paymentIntents.update(
572
- id,
587
+ piId,
573
588
  paymentOptions
574
589
  );
575
- return Payment$inboundSchema(updatedPayment);
590
+ return { ...Payment$inboundSchema(updatedPayment), id };
576
591
  };
577
592
  this.retrievePayment = async (id) => {
578
593
  const { error, data } = core.retrievePaymentSchema.safeParse({ id });
@@ -583,6 +598,34 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
583
598
  "retrievePayment"
584
599
  );
585
600
  }
601
+ if (data.id.startsWith("cs_")) {
602
+ const [session, sessionError] = await core.tryCatchAsync(
603
+ this.stripe.checkout.sessions.retrieve(data.id, {
604
+ expand: ["payment_intent"]
605
+ })
606
+ );
607
+ if (!session || sessionError?.code === "resource_missing")
608
+ return null;
609
+ if (session.payment_intent && typeof session.payment_intent !== "string") {
610
+ return {
611
+ ...Payment$inboundSchema(
612
+ session.payment_intent
613
+ ),
614
+ id: data.id
615
+ };
616
+ }
617
+ return {
618
+ id: data.id,
619
+ amount: session.amount_total ?? 0,
620
+ currency: session.currency ?? "",
621
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
622
+ status: "pending",
623
+ metadata: core.omitInternalMetadata(session.metadata ?? {}),
624
+ item_id: null,
625
+ requires_action: true,
626
+ payment_url: session.url
627
+ };
628
+ }
586
629
  const [payment, paymentError] = await core.tryCatchAsync(
587
630
  this.stripe.paymentIntents.retrieve(data.id)
588
631
  );
@@ -599,6 +642,10 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
599
642
  "deletePayment"
600
643
  );
601
644
  }
645
+ if (data.id.startsWith("cs_")) {
646
+ await this.stripe.checkout.sessions.expire(data.id);
647
+ return null;
648
+ }
602
649
  await this.stripe.paymentIntents.cancel(data.id);
603
650
  return null;
604
651
  };
@@ -611,12 +658,27 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
611
658
  "capturePayment"
612
659
  );
613
660
  }
614
- const payment = await this.stripe.paymentIntents.capture(id, {
661
+ const piId = await this._resolvePaymentIntentId(id);
662
+ const payment = await this.stripe.paymentIntents.capture(piId, {
615
663
  amount_to_capture: data.amount
616
664
  });
617
- return Payment$inboundSchema(payment);
665
+ return { ...Payment$inboundSchema(payment), id };
618
666
  };
619
667
  this.cancelPayment = async (id) => {
668
+ if (id.startsWith("cs_")) {
669
+ const session = await this.stripe.checkout.sessions.expire(id);
670
+ return {
671
+ id,
672
+ amount: session.amount_total ?? 0,
673
+ currency: session.currency ?? "",
674
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
675
+ status: "canceled",
676
+ metadata: core.omitInternalMetadata(session.metadata ?? {}),
677
+ item_id: null,
678
+ requires_action: false,
679
+ payment_url: null
680
+ };
681
+ }
620
682
  const canceledPayment = await this.stripe.paymentIntents.cancel(id);
621
683
  return Payment$inboundSchema(canceledPayment);
622
684
  };
@@ -646,7 +708,18 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
646
708
  amount: rest.amount,
647
709
  metadata: core.stringifyMetadataValues(rest.metadata ?? {})
648
710
  };
649
- if (data.payment_id.startsWith("pi_")) {
711
+ if (data.payment_id.startsWith("cs_")) {
712
+ const session = await this.stripe.checkout.sessions.retrieve(
713
+ data.payment_id
714
+ );
715
+ if (!session.payment_intent) {
716
+ throw new core.ValidationError(
717
+ "Checkout Session has no associated PaymentIntent to refund",
718
+ { provider: this.providerName, method: "createRefund" }
719
+ );
720
+ }
721
+ stripeRefundOptions.payment_intent = typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
722
+ } else if (data.payment_id.startsWith("pi_")) {
650
723
  stripeRefundOptions.payment_intent = data.payment_id;
651
724
  } else {
652
725
  stripeRefundOptions.charge = data.payment_id;
@@ -894,6 +967,20 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
894
967
  get _native() {
895
968
  return this.stripe;
896
969
  }
970
+ async _resolvePaymentIntentId(id) {
971
+ if (!id.startsWith("cs_")) return id;
972
+ const session = await this.stripe.checkout.sessions.retrieve(id);
973
+ if (!session.payment_intent) {
974
+ throw new core.ValidationError(
975
+ "Checkout Session has no associated PaymentIntent yet",
976
+ {
977
+ provider: this.providerName,
978
+ method: "resolvePaymentIntentId"
979
+ }
980
+ );
981
+ }
982
+ return typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
983
+ }
897
984
  };
898
985
 
899
986
  // src/index.ts
@@ -908,7 +995,7 @@ var stripe = () => {
908
995
  );
909
996
  return createStripe({
910
997
  apiKey: envVars.STRIPE_API_KEY,
911
- apiVersion: "2025-07-30.basil",
998
+ apiVersion: "2026-06-24.dahlia",
912
999
  isSandbox: envVars.STRIPE_API_KEY.includes("_test_") || process.env.NODE_ENV !== "production"
913
1000
  });
914
1001
  };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { schema, Schema, validateRequiredKeys, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, isIdCustomer, isEmailCustomer, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, parseCustomerName, createSubscriptionSchema, InvalidTypeError, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, refundReasonMatcher, WebhookError, omitInternalMetadata, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
1
+ import { schema, Schema, validateRequiredKeys, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, isIdCustomer, isEmailCustomer, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, parseCustomerName, createSubscriptionSchema, InvalidTypeError, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, omitInternalMetadata, deletePaymentSchema, capturePaymentSchema, createRefundSchema, refundReasonMatcher, WebhookError, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
2
2
  import Stripe from 'stripe';
3
3
 
4
4
  // src/index.ts
@@ -283,19 +283,33 @@ var StripeProvider = class extends AbstractPayKitProvider {
283
283
  name: data.name,
284
284
  email: data.email
285
285
  });
286
+ const {
287
+ billing: _billing,
288
+ metadata,
289
+ provider_metadata,
290
+ name: _name,
291
+ ...stripeParams
292
+ } = data;
286
293
  const customer = await this.stripe.customers.create({
287
- ...params,
288
- phone: params.phone ?? void 0,
289
- name: fullName
294
+ ...provider_metadata,
295
+ ...stripeParams,
296
+ phone: data.phone ?? void 0,
297
+ name: fullName,
298
+ metadata: stringifyMetadataValues(metadata ?? {})
290
299
  });
291
300
  return Customer$inboundSchema(customer);
292
301
  };
293
302
  this.updateCustomer = async (id, params) => {
294
- const { provider_metadata, ...rest } = params;
303
+ const {
304
+ provider_metadata,
305
+ billing: _billing,
306
+ metadata,
307
+ ...rest
308
+ } = params;
295
309
  const customer = await this.stripe.customers.update(id, {
296
310
  ...provider_metadata,
297
311
  ...rest,
298
- metadata: stringifyMetadataValues(rest.metadata ?? {}),
312
+ metadata: stringifyMetadataValues(metadata ?? {}),
299
313
  phone: rest.phone ?? void 0
300
314
  });
301
315
  return Customer$inboundSchema(customer);
@@ -541,7 +555,8 @@ var StripeProvider = class extends AbstractPayKitProvider {
541
555
  );
542
556
  }
543
557
  const { provider_metadata, customer, ...rest } = data;
544
- const payment = await this.stripe.paymentIntents.retrieve(id);
558
+ const piId = await this._resolvePaymentIntentId(id);
559
+ const payment = await this.stripe.paymentIntents.retrieve(piId);
545
560
  const paymentOptions = {
546
561
  ...provider_metadata,
547
562
  metadata: stringifyMetadataValues({
@@ -563,10 +578,10 @@ var StripeProvider = class extends AbstractPayKitProvider {
563
578
  paymentOptions.receipt_email = customer.email;
564
579
  }
565
580
  const updatedPayment = await this.stripe.paymentIntents.update(
566
- id,
581
+ piId,
567
582
  paymentOptions
568
583
  );
569
- return Payment$inboundSchema(updatedPayment);
584
+ return { ...Payment$inboundSchema(updatedPayment), id };
570
585
  };
571
586
  this.retrievePayment = async (id) => {
572
587
  const { error, data } = retrievePaymentSchema.safeParse({ id });
@@ -577,6 +592,34 @@ var StripeProvider = class extends AbstractPayKitProvider {
577
592
  "retrievePayment"
578
593
  );
579
594
  }
595
+ if (data.id.startsWith("cs_")) {
596
+ const [session, sessionError] = await tryCatchAsync(
597
+ this.stripe.checkout.sessions.retrieve(data.id, {
598
+ expand: ["payment_intent"]
599
+ })
600
+ );
601
+ if (!session || sessionError?.code === "resource_missing")
602
+ return null;
603
+ if (session.payment_intent && typeof session.payment_intent !== "string") {
604
+ return {
605
+ ...Payment$inboundSchema(
606
+ session.payment_intent
607
+ ),
608
+ id: data.id
609
+ };
610
+ }
611
+ return {
612
+ id: data.id,
613
+ amount: session.amount_total ?? 0,
614
+ currency: session.currency ?? "",
615
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
616
+ status: "pending",
617
+ metadata: omitInternalMetadata(session.metadata ?? {}),
618
+ item_id: null,
619
+ requires_action: true,
620
+ payment_url: session.url
621
+ };
622
+ }
580
623
  const [payment, paymentError] = await tryCatchAsync(
581
624
  this.stripe.paymentIntents.retrieve(data.id)
582
625
  );
@@ -593,6 +636,10 @@ var StripeProvider = class extends AbstractPayKitProvider {
593
636
  "deletePayment"
594
637
  );
595
638
  }
639
+ if (data.id.startsWith("cs_")) {
640
+ await this.stripe.checkout.sessions.expire(data.id);
641
+ return null;
642
+ }
596
643
  await this.stripe.paymentIntents.cancel(data.id);
597
644
  return null;
598
645
  };
@@ -605,12 +652,27 @@ var StripeProvider = class extends AbstractPayKitProvider {
605
652
  "capturePayment"
606
653
  );
607
654
  }
608
- const payment = await this.stripe.paymentIntents.capture(id, {
655
+ const piId = await this._resolvePaymentIntentId(id);
656
+ const payment = await this.stripe.paymentIntents.capture(piId, {
609
657
  amount_to_capture: data.amount
610
658
  });
611
- return Payment$inboundSchema(payment);
659
+ return { ...Payment$inboundSchema(payment), id };
612
660
  };
613
661
  this.cancelPayment = async (id) => {
662
+ if (id.startsWith("cs_")) {
663
+ const session = await this.stripe.checkout.sessions.expire(id);
664
+ return {
665
+ id,
666
+ amount: session.amount_total ?? 0,
667
+ currency: session.currency ?? "",
668
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
669
+ status: "canceled",
670
+ metadata: omitInternalMetadata(session.metadata ?? {}),
671
+ item_id: null,
672
+ requires_action: false,
673
+ payment_url: null
674
+ };
675
+ }
614
676
  const canceledPayment = await this.stripe.paymentIntents.cancel(id);
615
677
  return Payment$inboundSchema(canceledPayment);
616
678
  };
@@ -640,7 +702,18 @@ var StripeProvider = class extends AbstractPayKitProvider {
640
702
  amount: rest.amount,
641
703
  metadata: stringifyMetadataValues(rest.metadata ?? {})
642
704
  };
643
- if (data.payment_id.startsWith("pi_")) {
705
+ if (data.payment_id.startsWith("cs_")) {
706
+ const session = await this.stripe.checkout.sessions.retrieve(
707
+ data.payment_id
708
+ );
709
+ if (!session.payment_intent) {
710
+ throw new ValidationError(
711
+ "Checkout Session has no associated PaymentIntent to refund",
712
+ { provider: this.providerName, method: "createRefund" }
713
+ );
714
+ }
715
+ stripeRefundOptions.payment_intent = typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
716
+ } else if (data.payment_id.startsWith("pi_")) {
644
717
  stripeRefundOptions.payment_intent = data.payment_id;
645
718
  } else {
646
719
  stripeRefundOptions.charge = data.payment_id;
@@ -888,6 +961,20 @@ var StripeProvider = class extends AbstractPayKitProvider {
888
961
  get _native() {
889
962
  return this.stripe;
890
963
  }
964
+ async _resolvePaymentIntentId(id) {
965
+ if (!id.startsWith("cs_")) return id;
966
+ const session = await this.stripe.checkout.sessions.retrieve(id);
967
+ if (!session.payment_intent) {
968
+ throw new ValidationError(
969
+ "Checkout Session has no associated PaymentIntent yet",
970
+ {
971
+ provider: this.providerName,
972
+ method: "resolvePaymentIntentId"
973
+ }
974
+ );
975
+ }
976
+ return typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
977
+ }
891
978
  };
892
979
 
893
980
  // src/index.ts
@@ -902,7 +989,7 @@ var stripe = () => {
902
989
  );
903
990
  return createStripe({
904
991
  apiKey: envVars.STRIPE_API_KEY,
905
- apiVersion: "2025-07-30.basil",
992
+ apiVersion: "2026-06-24.dahlia",
906
993
  isSandbox: envVars.STRIPE_API_KEY.includes("_test_") || process.env.NODE_ENV !== "production"
907
994
  });
908
995
  };
@@ -8,9 +8,10 @@ interface StripeMetadata extends ProviderMetadataRegistry {
8
8
  subscription: Stripe.SubscriptionCreateParams;
9
9
  refund: Stripe.RefundCreateParams;
10
10
  }
11
- interface StripeOptions extends PaykitProviderOptions, Stripe.StripeConfig {
11
+ interface StripeOptions extends Omit<PaykitProviderOptions, 'isSandbox'>, Stripe.StripeConfig {
12
12
  /** Stripe secret key (sk_test_... or sk_live_...) */
13
13
  apiKey: string;
14
+ isSandbox?: boolean;
14
15
  }
15
16
  type StripeRawEvents = {
16
17
  [K in Stripe.Event.Type as `stripe.${K}`]: Extract<Stripe.Event, {
@@ -24,6 +25,7 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
24
25
  constructor(opts: StripeOptions);
25
26
  readonly providerName = "stripe";
26
27
  get _native(): Stripe;
28
+ private _resolvePaymentIntentId;
27
29
  createCheckout: (params: CreateCheckoutSchema<StripeMetadata["checkout"]>) => Promise<Checkout>;
28
30
  retrieveCheckout: (id: string) => Promise<Checkout>;
29
31
  updateCheckout: (id: string, params: UpdateCheckoutSchema) => Promise<Checkout>;
@@ -8,9 +8,10 @@ interface StripeMetadata extends ProviderMetadataRegistry {
8
8
  subscription: Stripe.SubscriptionCreateParams;
9
9
  refund: Stripe.RefundCreateParams;
10
10
  }
11
- interface StripeOptions extends PaykitProviderOptions, Stripe.StripeConfig {
11
+ interface StripeOptions extends Omit<PaykitProviderOptions, 'isSandbox'>, Stripe.StripeConfig {
12
12
  /** Stripe secret key (sk_test_... or sk_live_...) */
13
13
  apiKey: string;
14
+ isSandbox?: boolean;
14
15
  }
15
16
  type StripeRawEvents = {
16
17
  [K in Stripe.Event.Type as `stripe.${K}`]: Extract<Stripe.Event, {
@@ -24,6 +25,7 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
24
25
  constructor(opts: StripeOptions);
25
26
  readonly providerName = "stripe";
26
27
  get _native(): Stripe;
28
+ private _resolvePaymentIntentId;
27
29
  createCheckout: (params: CreateCheckoutSchema<StripeMetadata["checkout"]>) => Promise<Checkout>;
28
30
  retrieveCheckout: (id: string) => Promise<Checkout>;
29
31
  updateCheckout: (id: string, params: UpdateCheckoutSchema) => Promise<Checkout>;
@@ -289,19 +289,33 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
289
289
  name: data.name,
290
290
  email: data.email
291
291
  });
292
+ const {
293
+ billing: _billing,
294
+ metadata,
295
+ provider_metadata,
296
+ name: _name,
297
+ ...stripeParams
298
+ } = data;
292
299
  const customer = await this.stripe.customers.create({
293
- ...params,
294
- phone: params.phone ?? void 0,
295
- name: fullName
300
+ ...provider_metadata,
301
+ ...stripeParams,
302
+ phone: data.phone ?? void 0,
303
+ name: fullName,
304
+ metadata: core.stringifyMetadataValues(metadata ?? {})
296
305
  });
297
306
  return Customer$inboundSchema(customer);
298
307
  };
299
308
  this.updateCustomer = async (id, params) => {
300
- const { provider_metadata, ...rest } = params;
309
+ const {
310
+ provider_metadata,
311
+ billing: _billing,
312
+ metadata,
313
+ ...rest
314
+ } = params;
301
315
  const customer = await this.stripe.customers.update(id, {
302
316
  ...provider_metadata,
303
317
  ...rest,
304
- metadata: core.stringifyMetadataValues(rest.metadata ?? {}),
318
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
305
319
  phone: rest.phone ?? void 0
306
320
  });
307
321
  return Customer$inboundSchema(customer);
@@ -547,7 +561,8 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
547
561
  );
548
562
  }
549
563
  const { provider_metadata, customer, ...rest } = data;
550
- const payment = await this.stripe.paymentIntents.retrieve(id);
564
+ const piId = await this._resolvePaymentIntentId(id);
565
+ const payment = await this.stripe.paymentIntents.retrieve(piId);
551
566
  const paymentOptions = {
552
567
  ...provider_metadata,
553
568
  metadata: core.stringifyMetadataValues({
@@ -569,10 +584,10 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
569
584
  paymentOptions.receipt_email = customer.email;
570
585
  }
571
586
  const updatedPayment = await this.stripe.paymentIntents.update(
572
- id,
587
+ piId,
573
588
  paymentOptions
574
589
  );
575
- return Payment$inboundSchema(updatedPayment);
590
+ return { ...Payment$inboundSchema(updatedPayment), id };
576
591
  };
577
592
  this.retrievePayment = async (id) => {
578
593
  const { error, data } = core.retrievePaymentSchema.safeParse({ id });
@@ -583,6 +598,34 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
583
598
  "retrievePayment"
584
599
  );
585
600
  }
601
+ if (data.id.startsWith("cs_")) {
602
+ const [session, sessionError] = await core.tryCatchAsync(
603
+ this.stripe.checkout.sessions.retrieve(data.id, {
604
+ expand: ["payment_intent"]
605
+ })
606
+ );
607
+ if (!session || sessionError?.code === "resource_missing")
608
+ return null;
609
+ if (session.payment_intent && typeof session.payment_intent !== "string") {
610
+ return {
611
+ ...Payment$inboundSchema(
612
+ session.payment_intent
613
+ ),
614
+ id: data.id
615
+ };
616
+ }
617
+ return {
618
+ id: data.id,
619
+ amount: session.amount_total ?? 0,
620
+ currency: session.currency ?? "",
621
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
622
+ status: "pending",
623
+ metadata: core.omitInternalMetadata(session.metadata ?? {}),
624
+ item_id: null,
625
+ requires_action: true,
626
+ payment_url: session.url
627
+ };
628
+ }
586
629
  const [payment, paymentError] = await core.tryCatchAsync(
587
630
  this.stripe.paymentIntents.retrieve(data.id)
588
631
  );
@@ -599,6 +642,10 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
599
642
  "deletePayment"
600
643
  );
601
644
  }
645
+ if (data.id.startsWith("cs_")) {
646
+ await this.stripe.checkout.sessions.expire(data.id);
647
+ return null;
648
+ }
602
649
  await this.stripe.paymentIntents.cancel(data.id);
603
650
  return null;
604
651
  };
@@ -611,12 +658,27 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
611
658
  "capturePayment"
612
659
  );
613
660
  }
614
- const payment = await this.stripe.paymentIntents.capture(id, {
661
+ const piId = await this._resolvePaymentIntentId(id);
662
+ const payment = await this.stripe.paymentIntents.capture(piId, {
615
663
  amount_to_capture: data.amount
616
664
  });
617
- return Payment$inboundSchema(payment);
665
+ return { ...Payment$inboundSchema(payment), id };
618
666
  };
619
667
  this.cancelPayment = async (id) => {
668
+ if (id.startsWith("cs_")) {
669
+ const session = await this.stripe.checkout.sessions.expire(id);
670
+ return {
671
+ id,
672
+ amount: session.amount_total ?? 0,
673
+ currency: session.currency ?? "",
674
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
675
+ status: "canceled",
676
+ metadata: core.omitInternalMetadata(session.metadata ?? {}),
677
+ item_id: null,
678
+ requires_action: false,
679
+ payment_url: null
680
+ };
681
+ }
620
682
  const canceledPayment = await this.stripe.paymentIntents.cancel(id);
621
683
  return Payment$inboundSchema(canceledPayment);
622
684
  };
@@ -646,7 +708,18 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
646
708
  amount: rest.amount,
647
709
  metadata: core.stringifyMetadataValues(rest.metadata ?? {})
648
710
  };
649
- if (data.payment_id.startsWith("pi_")) {
711
+ if (data.payment_id.startsWith("cs_")) {
712
+ const session = await this.stripe.checkout.sessions.retrieve(
713
+ data.payment_id
714
+ );
715
+ if (!session.payment_intent) {
716
+ throw new core.ValidationError(
717
+ "Checkout Session has no associated PaymentIntent to refund",
718
+ { provider: this.providerName, method: "createRefund" }
719
+ );
720
+ }
721
+ stripeRefundOptions.payment_intent = typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
722
+ } else if (data.payment_id.startsWith("pi_")) {
650
723
  stripeRefundOptions.payment_intent = data.payment_id;
651
724
  } else {
652
725
  stripeRefundOptions.charge = data.payment_id;
@@ -894,6 +967,20 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
894
967
  get _native() {
895
968
  return this.stripe;
896
969
  }
970
+ async _resolvePaymentIntentId(id) {
971
+ if (!id.startsWith("cs_")) return id;
972
+ const session = await this.stripe.checkout.sessions.retrieve(id);
973
+ if (!session.payment_intent) {
974
+ throw new core.ValidationError(
975
+ "Checkout Session has no associated PaymentIntent yet",
976
+ {
977
+ provider: this.providerName,
978
+ method: "resolvePaymentIntentId"
979
+ }
980
+ );
981
+ }
982
+ return typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
983
+ }
897
984
  };
898
985
 
899
986
  exports.StripeProvider = StripeProvider;
@@ -1,4 +1,4 @@
1
- import { schema, Schema, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, isIdCustomer, isEmailCustomer, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, parseCustomerName, createSubscriptionSchema, InvalidTypeError, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, refundReasonMatcher, WebhookError, validateRequiredKeys, omitInternalMetadata, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
1
+ import { schema, Schema, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, isIdCustomer, isEmailCustomer, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, parseCustomerName, createSubscriptionSchema, InvalidTypeError, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, omitInternalMetadata, deletePaymentSchema, capturePaymentSchema, createRefundSchema, refundReasonMatcher, WebhookError, validateRequiredKeys, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
2
2
  import Stripe from 'stripe';
3
3
 
4
4
  // src/stripe-provider.ts
@@ -283,19 +283,33 @@ var StripeProvider = class extends AbstractPayKitProvider {
283
283
  name: data.name,
284
284
  email: data.email
285
285
  });
286
+ const {
287
+ billing: _billing,
288
+ metadata,
289
+ provider_metadata,
290
+ name: _name,
291
+ ...stripeParams
292
+ } = data;
286
293
  const customer = await this.stripe.customers.create({
287
- ...params,
288
- phone: params.phone ?? void 0,
289
- name: fullName
294
+ ...provider_metadata,
295
+ ...stripeParams,
296
+ phone: data.phone ?? void 0,
297
+ name: fullName,
298
+ metadata: stringifyMetadataValues(metadata ?? {})
290
299
  });
291
300
  return Customer$inboundSchema(customer);
292
301
  };
293
302
  this.updateCustomer = async (id, params) => {
294
- const { provider_metadata, ...rest } = params;
303
+ const {
304
+ provider_metadata,
305
+ billing: _billing,
306
+ metadata,
307
+ ...rest
308
+ } = params;
295
309
  const customer = await this.stripe.customers.update(id, {
296
310
  ...provider_metadata,
297
311
  ...rest,
298
- metadata: stringifyMetadataValues(rest.metadata ?? {}),
312
+ metadata: stringifyMetadataValues(metadata ?? {}),
299
313
  phone: rest.phone ?? void 0
300
314
  });
301
315
  return Customer$inboundSchema(customer);
@@ -541,7 +555,8 @@ var StripeProvider = class extends AbstractPayKitProvider {
541
555
  );
542
556
  }
543
557
  const { provider_metadata, customer, ...rest } = data;
544
- const payment = await this.stripe.paymentIntents.retrieve(id);
558
+ const piId = await this._resolvePaymentIntentId(id);
559
+ const payment = await this.stripe.paymentIntents.retrieve(piId);
545
560
  const paymentOptions = {
546
561
  ...provider_metadata,
547
562
  metadata: stringifyMetadataValues({
@@ -563,10 +578,10 @@ var StripeProvider = class extends AbstractPayKitProvider {
563
578
  paymentOptions.receipt_email = customer.email;
564
579
  }
565
580
  const updatedPayment = await this.stripe.paymentIntents.update(
566
- id,
581
+ piId,
567
582
  paymentOptions
568
583
  );
569
- return Payment$inboundSchema(updatedPayment);
584
+ return { ...Payment$inboundSchema(updatedPayment), id };
570
585
  };
571
586
  this.retrievePayment = async (id) => {
572
587
  const { error, data } = retrievePaymentSchema.safeParse({ id });
@@ -577,6 +592,34 @@ var StripeProvider = class extends AbstractPayKitProvider {
577
592
  "retrievePayment"
578
593
  );
579
594
  }
595
+ if (data.id.startsWith("cs_")) {
596
+ const [session, sessionError] = await tryCatchAsync(
597
+ this.stripe.checkout.sessions.retrieve(data.id, {
598
+ expand: ["payment_intent"]
599
+ })
600
+ );
601
+ if (!session || sessionError?.code === "resource_missing")
602
+ return null;
603
+ if (session.payment_intent && typeof session.payment_intent !== "string") {
604
+ return {
605
+ ...Payment$inboundSchema(
606
+ session.payment_intent
607
+ ),
608
+ id: data.id
609
+ };
610
+ }
611
+ return {
612
+ id: data.id,
613
+ amount: session.amount_total ?? 0,
614
+ currency: session.currency ?? "",
615
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
616
+ status: "pending",
617
+ metadata: omitInternalMetadata(session.metadata ?? {}),
618
+ item_id: null,
619
+ requires_action: true,
620
+ payment_url: session.url
621
+ };
622
+ }
580
623
  const [payment, paymentError] = await tryCatchAsync(
581
624
  this.stripe.paymentIntents.retrieve(data.id)
582
625
  );
@@ -593,6 +636,10 @@ var StripeProvider = class extends AbstractPayKitProvider {
593
636
  "deletePayment"
594
637
  );
595
638
  }
639
+ if (data.id.startsWith("cs_")) {
640
+ await this.stripe.checkout.sessions.expire(data.id);
641
+ return null;
642
+ }
596
643
  await this.stripe.paymentIntents.cancel(data.id);
597
644
  return null;
598
645
  };
@@ -605,12 +652,27 @@ var StripeProvider = class extends AbstractPayKitProvider {
605
652
  "capturePayment"
606
653
  );
607
654
  }
608
- const payment = await this.stripe.paymentIntents.capture(id, {
655
+ const piId = await this._resolvePaymentIntentId(id);
656
+ const payment = await this.stripe.paymentIntents.capture(piId, {
609
657
  amount_to_capture: data.amount
610
658
  });
611
- return Payment$inboundSchema(payment);
659
+ return { ...Payment$inboundSchema(payment), id };
612
660
  };
613
661
  this.cancelPayment = async (id) => {
662
+ if (id.startsWith("cs_")) {
663
+ const session = await this.stripe.checkout.sessions.expire(id);
664
+ return {
665
+ id,
666
+ amount: session.amount_total ?? 0,
667
+ currency: session.currency ?? "",
668
+ customer: typeof session.customer === "string" ? { id: session.customer } : session.customer?.id ? { id: session.customer.id } : session.customer_email ? { email: session.customer_email } : null,
669
+ status: "canceled",
670
+ metadata: omitInternalMetadata(session.metadata ?? {}),
671
+ item_id: null,
672
+ requires_action: false,
673
+ payment_url: null
674
+ };
675
+ }
614
676
  const canceledPayment = await this.stripe.paymentIntents.cancel(id);
615
677
  return Payment$inboundSchema(canceledPayment);
616
678
  };
@@ -640,7 +702,18 @@ var StripeProvider = class extends AbstractPayKitProvider {
640
702
  amount: rest.amount,
641
703
  metadata: stringifyMetadataValues(rest.metadata ?? {})
642
704
  };
643
- if (data.payment_id.startsWith("pi_")) {
705
+ if (data.payment_id.startsWith("cs_")) {
706
+ const session = await this.stripe.checkout.sessions.retrieve(
707
+ data.payment_id
708
+ );
709
+ if (!session.payment_intent) {
710
+ throw new ValidationError(
711
+ "Checkout Session has no associated PaymentIntent to refund",
712
+ { provider: this.providerName, method: "createRefund" }
713
+ );
714
+ }
715
+ stripeRefundOptions.payment_intent = typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
716
+ } else if (data.payment_id.startsWith("pi_")) {
644
717
  stripeRefundOptions.payment_intent = data.payment_id;
645
718
  } else {
646
719
  stripeRefundOptions.charge = data.payment_id;
@@ -888,6 +961,20 @@ var StripeProvider = class extends AbstractPayKitProvider {
888
961
  get _native() {
889
962
  return this.stripe;
890
963
  }
964
+ async _resolvePaymentIntentId(id) {
965
+ if (!id.startsWith("cs_")) return id;
966
+ const session = await this.stripe.checkout.sessions.retrieve(id);
967
+ if (!session.payment_intent) {
968
+ throw new ValidationError(
969
+ "Checkout Session has no associated PaymentIntent yet",
970
+ {
971
+ provider: this.providerName,
972
+ method: "resolvePaymentIntentId"
973
+ }
974
+ );
975
+ }
976
+ return typeof session.payment_intent === "string" ? session.payment_intent : session.payment_intent.id;
977
+ }
891
978
  };
892
979
 
893
980
  export { StripeProvider };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/stripe",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "Stripe provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -8,9 +8,6 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
- "scripts": {
12
- "build": "tsup"
13
- },
14
11
  "paykit": {
15
12
  "type": "provider"
16
13
  },
@@ -23,15 +20,15 @@
23
20
  "author": "Emmanuel Odii",
24
21
  "license": "ISC",
25
22
  "dependencies": {
26
- "stripe": "^18.2.1"
23
+ "stripe": "^22.3.1"
27
24
  },
28
25
  "peerDependencies": {
29
- "@paykit-sdk/core": ">=1.2.0"
26
+ "@paykit-sdk/core": "^1.2.3"
30
27
  },
31
28
  "devDependencies": {
32
- "@paykit-sdk/core": "workspace:*",
33
29
  "tsup": "^8.0.0",
34
- "typescript": "^5.0.0"
30
+ "typescript": "^5.0.0",
31
+ "@paykit-sdk/core": "1.3.0"
35
32
  },
36
33
  "publishConfig": {
37
34
  "access": "public"
@@ -42,5 +39,10 @@
42
39
  },
43
40
  "bugs": {
44
41
  "url": "https://github.com/usepaykit/paykit-sdk/issues"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup",
45
+ "test": "vitest run --config ../../vitest.config.ts packages/stripe/src",
46
+ "typecheck": "tsc --noEmit"
45
47
  }
46
- }
48
+ }