@paykit-sdk/stripe 1.2.0 → 1.2.2

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
@@ -656,232 +656,6 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
656
656
  );
657
657
  return Refund$inboundSchema(refund);
658
658
  };
659
- this.handleWebhook1 = async (params, webhookSecret) => {
660
- if (!webhookSecret) {
661
- throw new core.WebhookError(
662
- "webhookSecret is required for Stripe webhook verification",
663
- { provider: this.providerName }
664
- );
665
- }
666
- const { body, headersAsObject } = params;
667
- const headers = new Headers(headersAsObject);
668
- const stripeSignature = headers.get("stripe-signature");
669
- if (!stripeSignature) {
670
- throw new core.WebhookError("Missing Stripe signature", {
671
- provider: this.providerName
672
- });
673
- }
674
- const event = this.stripe.webhooks.constructEvent(
675
- body,
676
- stripeSignature,
677
- webhookSecret
678
- );
679
- const stripePaymentIntentUpdateEventsWithHandlers = [
680
- "payment_intent.processing",
681
- "payment_intent.requires_action",
682
- "payment_intent.amount_capturable_updated",
683
- "payment_intent.partially_funded"
684
- ];
685
- const webhookHandlers = {
686
- /**
687
- * Invoice
688
- */
689
- "checkout.session.completed": async (event2) => {
690
- const data = event2.data.object;
691
- if (data.mode !== "payment") return null;
692
- const customFields = data.custom_fields.reduce(
693
- (acc, field) => {
694
- if (field.type == "dropdown") {
695
- acc[field.key] = field.dropdown?.value;
696
- } else if (field.type == "text") {
697
- acc[field.key] = field.text?.value;
698
- } else if (field.type == "numeric") {
699
- acc[field.key] = field.numeric?.value;
700
- }
701
- return acc;
702
- },
703
- {}
704
- );
705
- const invoiceData = {
706
- id: data.id,
707
- status: core.invoiceStatusSchema.parse("paid"),
708
- paid_at: new Date(event2.created * 1e3).toISOString(),
709
- amount_paid: data.amount_total ?? 0,
710
- currency: data.currency ?? "",
711
- metadata: core.omitInternalMetadata(data.metadata ?? {}),
712
- customer: {
713
- id: typeof data.customer === "string" ? data.customer : data.customer?.id ?? ""
714
- },
715
- billing_mode: core.billingModeSchema.parse("one_time"),
716
- subscription_id: null,
717
- custom_fields: customFields ?? null,
718
- line_items: data.line_items?.data.map((item) => ({
719
- id: item.price.id,
720
- quantity: item.quantity
721
- })) ?? []
722
- };
723
- const invoice = {
724
- type: "invoice.generated",
725
- created: event2.created,
726
- id: event2.id,
727
- data: invoiceData
728
- };
729
- return [core.paykitEvent$InboundSchema(invoice)];
730
- },
731
- "invoice.paid": async (event2) => {
732
- const data = event2.data.object;
733
- const relevantBillingReasons = [
734
- "subscription_create",
735
- "subscription_cycle"
736
- ];
737
- if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason))
738
- return null;
739
- const invoice = {
740
- type: "invoice.generated",
741
- created: event2.created,
742
- id: event2.id,
743
- data: Invoice$inboundSchema({
744
- ...data,
745
- billingMode: "recurring"
746
- })
747
- };
748
- return [core.paykitEvent$InboundSchema(invoice)];
749
- },
750
- /**
751
- * Customer
752
- */
753
- "customer.created": async (event2) => {
754
- const data = event2.data.object;
755
- const customer = {
756
- type: "customer.created",
757
- created: event2.created,
758
- id: event2.id,
759
- data: Customer$inboundSchema(data)
760
- };
761
- return [core.paykitEvent$InboundSchema(customer)];
762
- },
763
- "customer.updated": async (event2) => {
764
- const data = event2.data.object;
765
- const customer = {
766
- type: "customer.updated",
767
- created: event2.created,
768
- id: event2.id,
769
- data: Customer$inboundSchema(data)
770
- };
771
- return [core.paykitEvent$InboundSchema(customer)];
772
- },
773
- "customer.deleted": async (event2) => {
774
- const customer = {
775
- type: "customer.deleted",
776
- created: event2.created,
777
- id: event2.id,
778
- data: null
779
- };
780
- return [core.paykitEvent$InboundSchema(customer)];
781
- },
782
- /**
783
- * Subscription
784
- */
785
- "customer.subscription.created": async (event2) => {
786
- const data = event2.data.object;
787
- const subscription = Subscription$inboundSchema(data);
788
- return [
789
- core.paykitEvent$InboundSchema({
790
- type: "subscription.created",
791
- created: event2.created,
792
- id: event2.id,
793
- data: subscription
794
- })
795
- ];
796
- },
797
- "customer.subscription.updated": async (event2) => {
798
- const data = event2.data.object;
799
- const subscription = Subscription$inboundSchema(data);
800
- return [
801
- core.paykitEvent$InboundSchema({
802
- type: "subscription.updated",
803
- created: event2.created,
804
- id: event2.id,
805
- data: subscription
806
- })
807
- ];
808
- },
809
- "customer.subscription.deleted": async (event2) => {
810
- const subscription = null;
811
- return [
812
- core.paykitEvent$InboundSchema({
813
- type: "subscription.canceled",
814
- created: event2.created,
815
- id: event2.id,
816
- data: subscription
817
- })
818
- ];
819
- },
820
- "payment_intent.created": async (event2) => {
821
- const data = event2.data.object;
822
- const payment = Payment$inboundSchema(data);
823
- return [
824
- core.paykitEvent$InboundSchema({
825
- type: "payment.created",
826
- created: event2.created,
827
- id: event2.id,
828
- data: payment
829
- })
830
- ];
831
- },
832
- ...stripePaymentIntentUpdateEventsWithHandlers.map(
833
- () => async (event2) => {
834
- const data = event2.data.object;
835
- const payment = Payment$inboundSchema(data);
836
- return [
837
- core.paykitEvent$InboundSchema({
838
- type: "payment.updated",
839
- created: event2.created,
840
- id: event2.id,
841
- data: payment
842
- })
843
- ];
844
- }
845
- ),
846
- "payment_intent.canceled": async (event2) => {
847
- const data = event2.data.object;
848
- const payment = Payment$inboundSchema(data);
849
- return [
850
- core.paykitEvent$InboundSchema({
851
- type: "payment.failed",
852
- created: event2.created,
853
- id: event2.id,
854
- data: payment
855
- })
856
- ];
857
- },
858
- "refund.created": async (event2) => {
859
- const data = event2.data.object;
860
- const refund = Refund$inboundSchema(data);
861
- return [
862
- core.paykitEvent$InboundSchema({
863
- type: "refund.created",
864
- created: event2.created,
865
- id: event2.id,
866
- data: refund
867
- })
868
- ];
869
- }
870
- };
871
- const handler = webhookHandlers[event.type];
872
- if (!handler)
873
- throw new core.WebhookError(`Unhandled event type: ${event.type}`, {
874
- provider: this.providerName
875
- });
876
- const result = await handler(event);
877
- if (!result) {
878
- console.log(
879
- `Skipping event ${event.type} for provider: ${this.providerName} as no action needed`
880
- );
881
- return [];
882
- }
883
- return result;
884
- };
885
659
  this.handleWebhook = async (params, webhookSecret) => {
886
660
  if (!webhookSecret) {
887
661
  throw new core.WebhookError(
@@ -1111,10 +885,11 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
1111
885
  }
1112
886
  return results;
1113
887
  };
1114
- const { debug = true, apiKey, ...rest } = opts;
1115
- this.stripe = new Stripe__default.default(apiKey, rest);
1116
- this.opts = opts;
1117
- this.isSandbox = opts.isSandbox;
888
+ const { debug, apiKey, isSandbox, ...stripeConfig } = opts;
889
+ const resolvedSandbox = isSandbox ?? apiKey.includes("_test_");
890
+ this.stripe = new Stripe__default.default(apiKey, stripeConfig);
891
+ this.opts = { ...opts, debug: debug ?? resolvedSandbox };
892
+ this.isSandbox = resolvedSandbox;
1118
893
  }
1119
894
  get _native() {
1120
895
  return this.stripe;
@@ -1131,12 +906,10 @@ var stripe = () => {
1131
906
  process.env ?? {},
1132
907
  "Missing required environment variables: {keys}"
1133
908
  );
1134
- const isSandbox = process.env.NODE_ENV === "development";
1135
909
  return createStripe({
1136
910
  apiKey: envVars.STRIPE_API_KEY,
1137
- isSandbox,
1138
- debug: isSandbox,
1139
- apiVersion: "2025-07-30.basil"
911
+ apiVersion: "2025-07-30.basil",
912
+ isSandbox: envVars.STRIPE_API_KEY.includes("_test_") || process.env.NODE_ENV !== "production"
1140
913
  });
1141
914
  };
1142
915
 
package/dist/index.mjs CHANGED
@@ -650,232 +650,6 @@ var StripeProvider = class extends AbstractPayKitProvider {
650
650
  );
651
651
  return Refund$inboundSchema(refund);
652
652
  };
653
- this.handleWebhook1 = async (params, webhookSecret) => {
654
- if (!webhookSecret) {
655
- throw new WebhookError(
656
- "webhookSecret is required for Stripe webhook verification",
657
- { provider: this.providerName }
658
- );
659
- }
660
- const { body, headersAsObject } = params;
661
- const headers = new Headers(headersAsObject);
662
- const stripeSignature = headers.get("stripe-signature");
663
- if (!stripeSignature) {
664
- throw new WebhookError("Missing Stripe signature", {
665
- provider: this.providerName
666
- });
667
- }
668
- const event = this.stripe.webhooks.constructEvent(
669
- body,
670
- stripeSignature,
671
- webhookSecret
672
- );
673
- const stripePaymentIntentUpdateEventsWithHandlers = [
674
- "payment_intent.processing",
675
- "payment_intent.requires_action",
676
- "payment_intent.amount_capturable_updated",
677
- "payment_intent.partially_funded"
678
- ];
679
- const webhookHandlers = {
680
- /**
681
- * Invoice
682
- */
683
- "checkout.session.completed": async (event2) => {
684
- const data = event2.data.object;
685
- if (data.mode !== "payment") return null;
686
- const customFields = data.custom_fields.reduce(
687
- (acc, field) => {
688
- if (field.type == "dropdown") {
689
- acc[field.key] = field.dropdown?.value;
690
- } else if (field.type == "text") {
691
- acc[field.key] = field.text?.value;
692
- } else if (field.type == "numeric") {
693
- acc[field.key] = field.numeric?.value;
694
- }
695
- return acc;
696
- },
697
- {}
698
- );
699
- const invoiceData = {
700
- id: data.id,
701
- status: invoiceStatusSchema.parse("paid"),
702
- paid_at: new Date(event2.created * 1e3).toISOString(),
703
- amount_paid: data.amount_total ?? 0,
704
- currency: data.currency ?? "",
705
- metadata: omitInternalMetadata(data.metadata ?? {}),
706
- customer: {
707
- id: typeof data.customer === "string" ? data.customer : data.customer?.id ?? ""
708
- },
709
- billing_mode: billingModeSchema.parse("one_time"),
710
- subscription_id: null,
711
- custom_fields: customFields ?? null,
712
- line_items: data.line_items?.data.map((item) => ({
713
- id: item.price.id,
714
- quantity: item.quantity
715
- })) ?? []
716
- };
717
- const invoice = {
718
- type: "invoice.generated",
719
- created: event2.created,
720
- id: event2.id,
721
- data: invoiceData
722
- };
723
- return [paykitEvent$InboundSchema(invoice)];
724
- },
725
- "invoice.paid": async (event2) => {
726
- const data = event2.data.object;
727
- const relevantBillingReasons = [
728
- "subscription_create",
729
- "subscription_cycle"
730
- ];
731
- if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason))
732
- return null;
733
- const invoice = {
734
- type: "invoice.generated",
735
- created: event2.created,
736
- id: event2.id,
737
- data: Invoice$inboundSchema({
738
- ...data,
739
- billingMode: "recurring"
740
- })
741
- };
742
- return [paykitEvent$InboundSchema(invoice)];
743
- },
744
- /**
745
- * Customer
746
- */
747
- "customer.created": async (event2) => {
748
- const data = event2.data.object;
749
- const customer = {
750
- type: "customer.created",
751
- created: event2.created,
752
- id: event2.id,
753
- data: Customer$inboundSchema(data)
754
- };
755
- return [paykitEvent$InboundSchema(customer)];
756
- },
757
- "customer.updated": async (event2) => {
758
- const data = event2.data.object;
759
- const customer = {
760
- type: "customer.updated",
761
- created: event2.created,
762
- id: event2.id,
763
- data: Customer$inboundSchema(data)
764
- };
765
- return [paykitEvent$InboundSchema(customer)];
766
- },
767
- "customer.deleted": async (event2) => {
768
- const customer = {
769
- type: "customer.deleted",
770
- created: event2.created,
771
- id: event2.id,
772
- data: null
773
- };
774
- return [paykitEvent$InboundSchema(customer)];
775
- },
776
- /**
777
- * Subscription
778
- */
779
- "customer.subscription.created": async (event2) => {
780
- const data = event2.data.object;
781
- const subscription = Subscription$inboundSchema(data);
782
- return [
783
- paykitEvent$InboundSchema({
784
- type: "subscription.created",
785
- created: event2.created,
786
- id: event2.id,
787
- data: subscription
788
- })
789
- ];
790
- },
791
- "customer.subscription.updated": async (event2) => {
792
- const data = event2.data.object;
793
- const subscription = Subscription$inboundSchema(data);
794
- return [
795
- paykitEvent$InboundSchema({
796
- type: "subscription.updated",
797
- created: event2.created,
798
- id: event2.id,
799
- data: subscription
800
- })
801
- ];
802
- },
803
- "customer.subscription.deleted": async (event2) => {
804
- const subscription = null;
805
- return [
806
- paykitEvent$InboundSchema({
807
- type: "subscription.canceled",
808
- created: event2.created,
809
- id: event2.id,
810
- data: subscription
811
- })
812
- ];
813
- },
814
- "payment_intent.created": async (event2) => {
815
- const data = event2.data.object;
816
- const payment = Payment$inboundSchema(data);
817
- return [
818
- paykitEvent$InboundSchema({
819
- type: "payment.created",
820
- created: event2.created,
821
- id: event2.id,
822
- data: payment
823
- })
824
- ];
825
- },
826
- ...stripePaymentIntentUpdateEventsWithHandlers.map(
827
- () => async (event2) => {
828
- const data = event2.data.object;
829
- const payment = Payment$inboundSchema(data);
830
- return [
831
- paykitEvent$InboundSchema({
832
- type: "payment.updated",
833
- created: event2.created,
834
- id: event2.id,
835
- data: payment
836
- })
837
- ];
838
- }
839
- ),
840
- "payment_intent.canceled": async (event2) => {
841
- const data = event2.data.object;
842
- const payment = Payment$inboundSchema(data);
843
- return [
844
- paykitEvent$InboundSchema({
845
- type: "payment.failed",
846
- created: event2.created,
847
- id: event2.id,
848
- data: payment
849
- })
850
- ];
851
- },
852
- "refund.created": async (event2) => {
853
- const data = event2.data.object;
854
- const refund = Refund$inboundSchema(data);
855
- return [
856
- paykitEvent$InboundSchema({
857
- type: "refund.created",
858
- created: event2.created,
859
- id: event2.id,
860
- data: refund
861
- })
862
- ];
863
- }
864
- };
865
- const handler = webhookHandlers[event.type];
866
- if (!handler)
867
- throw new WebhookError(`Unhandled event type: ${event.type}`, {
868
- provider: this.providerName
869
- });
870
- const result = await handler(event);
871
- if (!result) {
872
- console.log(
873
- `Skipping event ${event.type} for provider: ${this.providerName} as no action needed`
874
- );
875
- return [];
876
- }
877
- return result;
878
- };
879
653
  this.handleWebhook = async (params, webhookSecret) => {
880
654
  if (!webhookSecret) {
881
655
  throw new WebhookError(
@@ -1105,10 +879,11 @@ var StripeProvider = class extends AbstractPayKitProvider {
1105
879
  }
1106
880
  return results;
1107
881
  };
1108
- const { debug = true, apiKey, ...rest } = opts;
1109
- this.stripe = new Stripe(apiKey, rest);
1110
- this.opts = opts;
1111
- this.isSandbox = opts.isSandbox;
882
+ const { debug, apiKey, isSandbox, ...stripeConfig } = opts;
883
+ const resolvedSandbox = isSandbox ?? apiKey.includes("_test_");
884
+ this.stripe = new Stripe(apiKey, stripeConfig);
885
+ this.opts = { ...opts, debug: debug ?? resolvedSandbox };
886
+ this.isSandbox = resolvedSandbox;
1112
887
  }
1113
888
  get _native() {
1114
889
  return this.stripe;
@@ -1125,12 +900,10 @@ var stripe = () => {
1125
900
  process.env ?? {},
1126
901
  "Missing required environment variables: {keys}"
1127
902
  );
1128
- const isSandbox = process.env.NODE_ENV === "development";
1129
903
  return createStripe({
1130
904
  apiKey: envVars.STRIPE_API_KEY,
1131
- isSandbox,
1132
- debug: isSandbox,
1133
- apiVersion: "2025-07-30.basil"
905
+ apiVersion: "2025-07-30.basil",
906
+ isSandbox: envVars.STRIPE_API_KEY.includes("_test_") || process.env.NODE_ENV !== "production"
1134
907
  });
1135
908
  };
1136
909
 
@@ -9,6 +9,7 @@ interface StripeMetadata extends ProviderMetadataRegistry {
9
9
  refund: Stripe.RefundCreateParams;
10
10
  }
11
11
  interface StripeOptions extends PaykitProviderOptions, Stripe.StripeConfig {
12
+ /** Stripe secret key (sk_test_... or sk_live_...) */
12
13
  apiKey: string;
13
14
  }
14
15
  type StripeRawEvents = {
@@ -49,7 +50,6 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
49
50
  * Refund management
50
51
  */
51
52
  createRefund: (params: CreateRefundSchema<StripeMetadata["refund"]>) => Promise<Refund>;
52
- handleWebhook1: (params: WebhookHandlerConfig, webhookSecret: string | null) => Promise<Array<WebhookEventPayload>>;
53
53
  handleWebhook: (params: WebhookHandlerConfig, webhookSecret: string | null) => Promise<Array<WebhookEventPayload<StripeRawEvents>>>;
54
54
  }
55
55
 
@@ -9,6 +9,7 @@ interface StripeMetadata extends ProviderMetadataRegistry {
9
9
  refund: Stripe.RefundCreateParams;
10
10
  }
11
11
  interface StripeOptions extends PaykitProviderOptions, Stripe.StripeConfig {
12
+ /** Stripe secret key (sk_test_... or sk_live_...) */
12
13
  apiKey: string;
13
14
  }
14
15
  type StripeRawEvents = {
@@ -49,7 +50,6 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
49
50
  * Refund management
50
51
  */
51
52
  createRefund: (params: CreateRefundSchema<StripeMetadata["refund"]>) => Promise<Refund>;
52
- handleWebhook1: (params: WebhookHandlerConfig, webhookSecret: string | null) => Promise<Array<WebhookEventPayload>>;
53
53
  handleWebhook: (params: WebhookHandlerConfig, webhookSecret: string | null) => Promise<Array<WebhookEventPayload<StripeRawEvents>>>;
54
54
  }
55
55
 
@@ -656,232 +656,6 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
656
656
  );
657
657
  return Refund$inboundSchema(refund);
658
658
  };
659
- this.handleWebhook1 = async (params, webhookSecret) => {
660
- if (!webhookSecret) {
661
- throw new core.WebhookError(
662
- "webhookSecret is required for Stripe webhook verification",
663
- { provider: this.providerName }
664
- );
665
- }
666
- const { body, headersAsObject } = params;
667
- const headers = new Headers(headersAsObject);
668
- const stripeSignature = headers.get("stripe-signature");
669
- if (!stripeSignature) {
670
- throw new core.WebhookError("Missing Stripe signature", {
671
- provider: this.providerName
672
- });
673
- }
674
- const event = this.stripe.webhooks.constructEvent(
675
- body,
676
- stripeSignature,
677
- webhookSecret
678
- );
679
- const stripePaymentIntentUpdateEventsWithHandlers = [
680
- "payment_intent.processing",
681
- "payment_intent.requires_action",
682
- "payment_intent.amount_capturable_updated",
683
- "payment_intent.partially_funded"
684
- ];
685
- const webhookHandlers = {
686
- /**
687
- * Invoice
688
- */
689
- "checkout.session.completed": async (event2) => {
690
- const data = event2.data.object;
691
- if (data.mode !== "payment") return null;
692
- const customFields = data.custom_fields.reduce(
693
- (acc, field) => {
694
- if (field.type == "dropdown") {
695
- acc[field.key] = field.dropdown?.value;
696
- } else if (field.type == "text") {
697
- acc[field.key] = field.text?.value;
698
- } else if (field.type == "numeric") {
699
- acc[field.key] = field.numeric?.value;
700
- }
701
- return acc;
702
- },
703
- {}
704
- );
705
- const invoiceData = {
706
- id: data.id,
707
- status: core.invoiceStatusSchema.parse("paid"),
708
- paid_at: new Date(event2.created * 1e3).toISOString(),
709
- amount_paid: data.amount_total ?? 0,
710
- currency: data.currency ?? "",
711
- metadata: core.omitInternalMetadata(data.metadata ?? {}),
712
- customer: {
713
- id: typeof data.customer === "string" ? data.customer : data.customer?.id ?? ""
714
- },
715
- billing_mode: core.billingModeSchema.parse("one_time"),
716
- subscription_id: null,
717
- custom_fields: customFields ?? null,
718
- line_items: data.line_items?.data.map((item) => ({
719
- id: item.price.id,
720
- quantity: item.quantity
721
- })) ?? []
722
- };
723
- const invoice = {
724
- type: "invoice.generated",
725
- created: event2.created,
726
- id: event2.id,
727
- data: invoiceData
728
- };
729
- return [core.paykitEvent$InboundSchema(invoice)];
730
- },
731
- "invoice.paid": async (event2) => {
732
- const data = event2.data.object;
733
- const relevantBillingReasons = [
734
- "subscription_create",
735
- "subscription_cycle"
736
- ];
737
- if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason))
738
- return null;
739
- const invoice = {
740
- type: "invoice.generated",
741
- created: event2.created,
742
- id: event2.id,
743
- data: Invoice$inboundSchema({
744
- ...data,
745
- billingMode: "recurring"
746
- })
747
- };
748
- return [core.paykitEvent$InboundSchema(invoice)];
749
- },
750
- /**
751
- * Customer
752
- */
753
- "customer.created": async (event2) => {
754
- const data = event2.data.object;
755
- const customer = {
756
- type: "customer.created",
757
- created: event2.created,
758
- id: event2.id,
759
- data: Customer$inboundSchema(data)
760
- };
761
- return [core.paykitEvent$InboundSchema(customer)];
762
- },
763
- "customer.updated": async (event2) => {
764
- const data = event2.data.object;
765
- const customer = {
766
- type: "customer.updated",
767
- created: event2.created,
768
- id: event2.id,
769
- data: Customer$inboundSchema(data)
770
- };
771
- return [core.paykitEvent$InboundSchema(customer)];
772
- },
773
- "customer.deleted": async (event2) => {
774
- const customer = {
775
- type: "customer.deleted",
776
- created: event2.created,
777
- id: event2.id,
778
- data: null
779
- };
780
- return [core.paykitEvent$InboundSchema(customer)];
781
- },
782
- /**
783
- * Subscription
784
- */
785
- "customer.subscription.created": async (event2) => {
786
- const data = event2.data.object;
787
- const subscription = Subscription$inboundSchema(data);
788
- return [
789
- core.paykitEvent$InboundSchema({
790
- type: "subscription.created",
791
- created: event2.created,
792
- id: event2.id,
793
- data: subscription
794
- })
795
- ];
796
- },
797
- "customer.subscription.updated": async (event2) => {
798
- const data = event2.data.object;
799
- const subscription = Subscription$inboundSchema(data);
800
- return [
801
- core.paykitEvent$InboundSchema({
802
- type: "subscription.updated",
803
- created: event2.created,
804
- id: event2.id,
805
- data: subscription
806
- })
807
- ];
808
- },
809
- "customer.subscription.deleted": async (event2) => {
810
- const subscription = null;
811
- return [
812
- core.paykitEvent$InboundSchema({
813
- type: "subscription.canceled",
814
- created: event2.created,
815
- id: event2.id,
816
- data: subscription
817
- })
818
- ];
819
- },
820
- "payment_intent.created": async (event2) => {
821
- const data = event2.data.object;
822
- const payment = Payment$inboundSchema(data);
823
- return [
824
- core.paykitEvent$InboundSchema({
825
- type: "payment.created",
826
- created: event2.created,
827
- id: event2.id,
828
- data: payment
829
- })
830
- ];
831
- },
832
- ...stripePaymentIntentUpdateEventsWithHandlers.map(
833
- () => async (event2) => {
834
- const data = event2.data.object;
835
- const payment = Payment$inboundSchema(data);
836
- return [
837
- core.paykitEvent$InboundSchema({
838
- type: "payment.updated",
839
- created: event2.created,
840
- id: event2.id,
841
- data: payment
842
- })
843
- ];
844
- }
845
- ),
846
- "payment_intent.canceled": async (event2) => {
847
- const data = event2.data.object;
848
- const payment = Payment$inboundSchema(data);
849
- return [
850
- core.paykitEvent$InboundSchema({
851
- type: "payment.failed",
852
- created: event2.created,
853
- id: event2.id,
854
- data: payment
855
- })
856
- ];
857
- },
858
- "refund.created": async (event2) => {
859
- const data = event2.data.object;
860
- const refund = Refund$inboundSchema(data);
861
- return [
862
- core.paykitEvent$InboundSchema({
863
- type: "refund.created",
864
- created: event2.created,
865
- id: event2.id,
866
- data: refund
867
- })
868
- ];
869
- }
870
- };
871
- const handler = webhookHandlers[event.type];
872
- if (!handler)
873
- throw new core.WebhookError(`Unhandled event type: ${event.type}`, {
874
- provider: this.providerName
875
- });
876
- const result = await handler(event);
877
- if (!result) {
878
- console.log(
879
- `Skipping event ${event.type} for provider: ${this.providerName} as no action needed`
880
- );
881
- return [];
882
- }
883
- return result;
884
- };
885
659
  this.handleWebhook = async (params, webhookSecret) => {
886
660
  if (!webhookSecret) {
887
661
  throw new core.WebhookError(
@@ -1111,10 +885,11 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
1111
885
  }
1112
886
  return results;
1113
887
  };
1114
- const { debug = true, apiKey, ...rest } = opts;
1115
- this.stripe = new Stripe__default.default(apiKey, rest);
1116
- this.opts = opts;
1117
- this.isSandbox = opts.isSandbox;
888
+ const { debug, apiKey, isSandbox, ...stripeConfig } = opts;
889
+ const resolvedSandbox = isSandbox ?? apiKey.includes("_test_");
890
+ this.stripe = new Stripe__default.default(apiKey, stripeConfig);
891
+ this.opts = { ...opts, debug: debug ?? resolvedSandbox };
892
+ this.isSandbox = resolvedSandbox;
1118
893
  }
1119
894
  get _native() {
1120
895
  return this.stripe;
@@ -650,232 +650,6 @@ var StripeProvider = class extends AbstractPayKitProvider {
650
650
  );
651
651
  return Refund$inboundSchema(refund);
652
652
  };
653
- this.handleWebhook1 = async (params, webhookSecret) => {
654
- if (!webhookSecret) {
655
- throw new WebhookError(
656
- "webhookSecret is required for Stripe webhook verification",
657
- { provider: this.providerName }
658
- );
659
- }
660
- const { body, headersAsObject } = params;
661
- const headers = new Headers(headersAsObject);
662
- const stripeSignature = headers.get("stripe-signature");
663
- if (!stripeSignature) {
664
- throw new WebhookError("Missing Stripe signature", {
665
- provider: this.providerName
666
- });
667
- }
668
- const event = this.stripe.webhooks.constructEvent(
669
- body,
670
- stripeSignature,
671
- webhookSecret
672
- );
673
- const stripePaymentIntentUpdateEventsWithHandlers = [
674
- "payment_intent.processing",
675
- "payment_intent.requires_action",
676
- "payment_intent.amount_capturable_updated",
677
- "payment_intent.partially_funded"
678
- ];
679
- const webhookHandlers = {
680
- /**
681
- * Invoice
682
- */
683
- "checkout.session.completed": async (event2) => {
684
- const data = event2.data.object;
685
- if (data.mode !== "payment") return null;
686
- const customFields = data.custom_fields.reduce(
687
- (acc, field) => {
688
- if (field.type == "dropdown") {
689
- acc[field.key] = field.dropdown?.value;
690
- } else if (field.type == "text") {
691
- acc[field.key] = field.text?.value;
692
- } else if (field.type == "numeric") {
693
- acc[field.key] = field.numeric?.value;
694
- }
695
- return acc;
696
- },
697
- {}
698
- );
699
- const invoiceData = {
700
- id: data.id,
701
- status: invoiceStatusSchema.parse("paid"),
702
- paid_at: new Date(event2.created * 1e3).toISOString(),
703
- amount_paid: data.amount_total ?? 0,
704
- currency: data.currency ?? "",
705
- metadata: omitInternalMetadata(data.metadata ?? {}),
706
- customer: {
707
- id: typeof data.customer === "string" ? data.customer : data.customer?.id ?? ""
708
- },
709
- billing_mode: billingModeSchema.parse("one_time"),
710
- subscription_id: null,
711
- custom_fields: customFields ?? null,
712
- line_items: data.line_items?.data.map((item) => ({
713
- id: item.price.id,
714
- quantity: item.quantity
715
- })) ?? []
716
- };
717
- const invoice = {
718
- type: "invoice.generated",
719
- created: event2.created,
720
- id: event2.id,
721
- data: invoiceData
722
- };
723
- return [paykitEvent$InboundSchema(invoice)];
724
- },
725
- "invoice.paid": async (event2) => {
726
- const data = event2.data.object;
727
- const relevantBillingReasons = [
728
- "subscription_create",
729
- "subscription_cycle"
730
- ];
731
- if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason))
732
- return null;
733
- const invoice = {
734
- type: "invoice.generated",
735
- created: event2.created,
736
- id: event2.id,
737
- data: Invoice$inboundSchema({
738
- ...data,
739
- billingMode: "recurring"
740
- })
741
- };
742
- return [paykitEvent$InboundSchema(invoice)];
743
- },
744
- /**
745
- * Customer
746
- */
747
- "customer.created": async (event2) => {
748
- const data = event2.data.object;
749
- const customer = {
750
- type: "customer.created",
751
- created: event2.created,
752
- id: event2.id,
753
- data: Customer$inboundSchema(data)
754
- };
755
- return [paykitEvent$InboundSchema(customer)];
756
- },
757
- "customer.updated": async (event2) => {
758
- const data = event2.data.object;
759
- const customer = {
760
- type: "customer.updated",
761
- created: event2.created,
762
- id: event2.id,
763
- data: Customer$inboundSchema(data)
764
- };
765
- return [paykitEvent$InboundSchema(customer)];
766
- },
767
- "customer.deleted": async (event2) => {
768
- const customer = {
769
- type: "customer.deleted",
770
- created: event2.created,
771
- id: event2.id,
772
- data: null
773
- };
774
- return [paykitEvent$InboundSchema(customer)];
775
- },
776
- /**
777
- * Subscription
778
- */
779
- "customer.subscription.created": async (event2) => {
780
- const data = event2.data.object;
781
- const subscription = Subscription$inboundSchema(data);
782
- return [
783
- paykitEvent$InboundSchema({
784
- type: "subscription.created",
785
- created: event2.created,
786
- id: event2.id,
787
- data: subscription
788
- })
789
- ];
790
- },
791
- "customer.subscription.updated": async (event2) => {
792
- const data = event2.data.object;
793
- const subscription = Subscription$inboundSchema(data);
794
- return [
795
- paykitEvent$InboundSchema({
796
- type: "subscription.updated",
797
- created: event2.created,
798
- id: event2.id,
799
- data: subscription
800
- })
801
- ];
802
- },
803
- "customer.subscription.deleted": async (event2) => {
804
- const subscription = null;
805
- return [
806
- paykitEvent$InboundSchema({
807
- type: "subscription.canceled",
808
- created: event2.created,
809
- id: event2.id,
810
- data: subscription
811
- })
812
- ];
813
- },
814
- "payment_intent.created": async (event2) => {
815
- const data = event2.data.object;
816
- const payment = Payment$inboundSchema(data);
817
- return [
818
- paykitEvent$InboundSchema({
819
- type: "payment.created",
820
- created: event2.created,
821
- id: event2.id,
822
- data: payment
823
- })
824
- ];
825
- },
826
- ...stripePaymentIntentUpdateEventsWithHandlers.map(
827
- () => async (event2) => {
828
- const data = event2.data.object;
829
- const payment = Payment$inboundSchema(data);
830
- return [
831
- paykitEvent$InboundSchema({
832
- type: "payment.updated",
833
- created: event2.created,
834
- id: event2.id,
835
- data: payment
836
- })
837
- ];
838
- }
839
- ),
840
- "payment_intent.canceled": async (event2) => {
841
- const data = event2.data.object;
842
- const payment = Payment$inboundSchema(data);
843
- return [
844
- paykitEvent$InboundSchema({
845
- type: "payment.failed",
846
- created: event2.created,
847
- id: event2.id,
848
- data: payment
849
- })
850
- ];
851
- },
852
- "refund.created": async (event2) => {
853
- const data = event2.data.object;
854
- const refund = Refund$inboundSchema(data);
855
- return [
856
- paykitEvent$InboundSchema({
857
- type: "refund.created",
858
- created: event2.created,
859
- id: event2.id,
860
- data: refund
861
- })
862
- ];
863
- }
864
- };
865
- const handler = webhookHandlers[event.type];
866
- if (!handler)
867
- throw new WebhookError(`Unhandled event type: ${event.type}`, {
868
- provider: this.providerName
869
- });
870
- const result = await handler(event);
871
- if (!result) {
872
- console.log(
873
- `Skipping event ${event.type} for provider: ${this.providerName} as no action needed`
874
- );
875
- return [];
876
- }
877
- return result;
878
- };
879
653
  this.handleWebhook = async (params, webhookSecret) => {
880
654
  if (!webhookSecret) {
881
655
  throw new WebhookError(
@@ -1105,10 +879,11 @@ var StripeProvider = class extends AbstractPayKitProvider {
1105
879
  }
1106
880
  return results;
1107
881
  };
1108
- const { debug = true, apiKey, ...rest } = opts;
1109
- this.stripe = new Stripe(apiKey, rest);
1110
- this.opts = opts;
1111
- this.isSandbox = opts.isSandbox;
882
+ const { debug, apiKey, isSandbox, ...stripeConfig } = opts;
883
+ const resolvedSandbox = isSandbox ?? apiKey.includes("_test_");
884
+ this.stripe = new Stripe(apiKey, stripeConfig);
885
+ this.opts = { ...opts, debug: debug ?? resolvedSandbox };
886
+ this.isSandbox = resolvedSandbox;
1112
887
  }
1113
888
  get _native() {
1114
889
  return this.stripe;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/stripe",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Stripe provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",