@drawbridge/drawbridge-stripe 0.1.19 → 0.1.21

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
@@ -464,14 +464,14 @@ var require_subscription = __commonJS({
464
464
  var _a, _b, _c, _d, _e, _f, _g, _h;
465
465
  try {
466
466
  if (direction === "downgrade") {
467
- const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
468
- const currentPlanItem = live.items.data.find(
467
+ const live2 = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
468
+ const currentPlanItem = live2.items.data.find(
469
469
  (item) => {
470
470
  var _a2, _b2;
471
471
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
472
472
  }
473
473
  );
474
- const currentActionsItem = live.items.data.find(
474
+ const currentActionsItem = live2.items.data.find(
475
475
  (item) => {
476
476
  var _a2, _b2;
477
477
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
@@ -544,8 +544,21 @@ var require_subscription = __commonJS({
544
544
  const props = {
545
545
  metadata
546
546
  };
547
+ const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
548
+ const livePlanItem = live.items.data.find(
549
+ (item) => {
550
+ var _a2, _b2;
551
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
552
+ }
553
+ );
554
+ const liveActionsItem = live.items.data.find(
555
+ (item) => {
556
+ var _a2, _b2;
557
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
558
+ }
559
+ );
547
560
  const plan = await stripe2.subscriptionItems.update(
548
- (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
561
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id),
549
562
  {
550
563
  ...props,
551
564
  price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
@@ -554,9 +567,10 @@ var require_subscription = __commonJS({
554
567
  idem(idempotencyKey, "plan.update")
555
568
  );
556
569
  let actions = (items == null ? void 0 : items.actions) || {};
557
- if ((actions == null ? void 0 : actions.id) && (actions == null ? void 0 : actions.price)) {
570
+ const actionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || (actions == null ? void 0 : actions.id);
571
+ if (actionsItemId && (actions == null ? void 0 : actions.price)) {
558
572
  ({ id: actions["id"] } = await stripe2.subscriptionItems.update(
559
- actions.id,
573
+ actionsItemId,
560
574
  {
561
575
  ...props,
562
576
  price: actions.price,
@@ -721,7 +735,7 @@ var require_subscription = __commonJS({
721
735
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
722
736
  try {
723
737
  if (direction === "downgrade") {
724
- const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
738
+ const live2 = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
725
739
  let actions2;
726
740
  if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
727
741
  actions2 = await createTier({
@@ -754,14 +768,14 @@ var require_subscription = __commonJS({
754
768
  }
755
769
  ;
756
770
  const product = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
757
- const livePlanItem = live.items.data.find(
771
+ const livePlanItem2 = live2.items.data.find(
758
772
  (item) => {
759
773
  var _a2, _b2;
760
774
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
761
775
  }
762
776
  );
763
- const currentPeriodStart = livePlanItem == null ? void 0 : livePlanItem.current_period_start;
764
- const currentPeriodEnd = livePlanItem == null ? void 0 : livePlanItem.current_period_end;
777
+ const currentPeriodStart = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_start;
778
+ const currentPeriodEnd = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_end;
765
779
  const phases = [
766
780
  {
767
781
  items: [
@@ -833,8 +847,22 @@ var require_subscription = __commonJS({
833
847
  const props = {
834
848
  metadata
835
849
  };
850
+ const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
851
+ const livePlanItem = live.items.data.find(
852
+ (item) => {
853
+ var _a2, _b2;
854
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
855
+ }
856
+ );
857
+ const liveActionsItem = live.items.data.find(
858
+ (item) => {
859
+ var _a2, _b2;
860
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
861
+ }
862
+ );
863
+ const liveActionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || ((_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.actions) == null ? void 0 : _h.id);
836
864
  const plan = await stripe2.subscriptionItems.update(
837
- (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
865
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.plan) == null ? void 0 : _j.id),
838
866
  {
839
867
  ...props,
840
868
  price: to.stripePriceId,
@@ -843,7 +871,7 @@ var require_subscription = __commonJS({
843
871
  idem(idempotencyKey, "plan.update")
844
872
  );
845
873
  let actions;
846
- if (((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) == null ? void 0 : _j.id) && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
874
+ if (liveActionsItemId && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
847
875
  actions = await createTier({
848
876
  currency: to.currency,
849
877
  idempotencyKey,
@@ -855,7 +883,7 @@ var require_subscription = __commonJS({
855
883
  overage: to.overages.actions
856
884
  });
857
885
  ({ id: actions["id"] } = await stripe2.subscriptionItems.update(
858
- current.items.actions.id,
886
+ liveActionsItemId,
859
887
  {
860
888
  ...props,
861
889
  price: actions == null ? void 0 : actions.price,
@@ -900,6 +928,52 @@ var require_subscription = __commonJS({
900
928
  } catch (error) {
901
929
  throw error;
902
930
  }
931
+ },
932
+ // Pure. Given a subscription document, return the Stripe artifacts that
933
+ // are safe to deactivate at org-delete — the terminal point, the only
934
+ // place deactivation is ever safe. Meters are per-org (actions/ai/
935
+ // storage) and the actions price/product are created fresh per
936
+ // subscription, so all are reclaimable. Never the plan price/product
937
+ // (shared STRIPE_PRICE_* catalog) and never the ai/storage price/product
938
+ // (they only carry a meter, and the ai price is a shared env catalog
939
+ // price). `items` is an untyped, possibly-partial object — optional-chain
940
+ // everything and drop entries without an id.
941
+ deactivatableItems: (subscription) => {
942
+ var _a, _b, _c, _d, _e;
943
+ const items = (subscription == null ? void 0 : subscription.items) || {};
944
+ return [
945
+ { type: "meter", id: (_a = items == null ? void 0 : items.actions) == null ? void 0 : _a.meter },
946
+ { type: "price", id: (_b = items == null ? void 0 : items.actions) == null ? void 0 : _b.price },
947
+ { type: "product", id: (_c = items == null ? void 0 : items.actions) == null ? void 0 : _c.product },
948
+ { type: "meter", id: (_d = items == null ? void 0 : items.ai) == null ? void 0 : _d.meter },
949
+ { type: "meter", id: (_e = items == null ? void 0 : items.storage) == null ? void 0 : _e.meter }
950
+ ].filter((entry) => entry.id);
951
+ },
952
+ // Consume a deactivatableItems list. Each artifact is deactivated with an
953
+ // artifact-id idempotency key (`<type>.deactivate:<id>`) so re-running a
954
+ // delete is a safe no-op. Promise.allSettled means a single Stripe failure
955
+ // never rejects — the caller inspects the settled results. No logging here:
956
+ // drawbridge-stripe stays a pure SDK wrapper, so the consumer (which owns
957
+ // telemetry) logs the outcome.
958
+ deactivate: async ({
959
+ items = []
960
+ }) => {
961
+ const deactivateOne = ({ type, id }) => {
962
+ const options = idem(type + ".deactivate", id);
963
+ if (type === "meter") {
964
+ return stripe2.billing.meters.deactivate(id, {}, options);
965
+ }
966
+ ;
967
+ if (type === "price") {
968
+ return stripe2.prices.update(id, { active: false }, options);
969
+ }
970
+ ;
971
+ if (type === "product") {
972
+ return stripe2.products.update(id, { active: false }, options);
973
+ }
974
+ ;
975
+ };
976
+ return Promise.allSettled(items.map(deactivateOne));
903
977
  }
904
978
  };
905
979
  };
package/dist/index.mjs CHANGED
@@ -470,14 +470,14 @@ var require_subscription = __commonJS({
470
470
  var _a, _b, _c, _d, _e, _f, _g, _h;
471
471
  try {
472
472
  if (direction === "downgrade") {
473
- const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
474
- const currentPlanItem = live.items.data.find(
473
+ const live2 = await stripe.subscriptions.retrieve(stripeSubscriptionId);
474
+ const currentPlanItem = live2.items.data.find(
475
475
  (item) => {
476
476
  var _a2, _b2;
477
477
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
478
478
  }
479
479
  );
480
- const currentActionsItem = live.items.data.find(
480
+ const currentActionsItem = live2.items.data.find(
481
481
  (item) => {
482
482
  var _a2, _b2;
483
483
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
@@ -550,8 +550,21 @@ var require_subscription = __commonJS({
550
550
  const props = {
551
551
  metadata
552
552
  };
553
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
554
+ const livePlanItem = live.items.data.find(
555
+ (item) => {
556
+ var _a2, _b2;
557
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
558
+ }
559
+ );
560
+ const liveActionsItem = live.items.data.find(
561
+ (item) => {
562
+ var _a2, _b2;
563
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
564
+ }
565
+ );
553
566
  const plan = await stripe.subscriptionItems.update(
554
- (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
567
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id),
555
568
  {
556
569
  ...props,
557
570
  price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
@@ -560,9 +573,10 @@ var require_subscription = __commonJS({
560
573
  idem(idempotencyKey, "plan.update")
561
574
  );
562
575
  let actions = (items == null ? void 0 : items.actions) || {};
563
- if ((actions == null ? void 0 : actions.id) && (actions == null ? void 0 : actions.price)) {
576
+ const actionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || (actions == null ? void 0 : actions.id);
577
+ if (actionsItemId && (actions == null ? void 0 : actions.price)) {
564
578
  ({ id: actions["id"] } = await stripe.subscriptionItems.update(
565
- actions.id,
579
+ actionsItemId,
566
580
  {
567
581
  ...props,
568
582
  price: actions.price,
@@ -727,7 +741,7 @@ var require_subscription = __commonJS({
727
741
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
728
742
  try {
729
743
  if (direction === "downgrade") {
730
- const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
744
+ const live2 = await stripe.subscriptions.retrieve(stripeSubscriptionId);
731
745
  let actions2;
732
746
  if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
733
747
  actions2 = await createTier({
@@ -760,14 +774,14 @@ var require_subscription = __commonJS({
760
774
  }
761
775
  ;
762
776
  const product = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
763
- const livePlanItem = live.items.data.find(
777
+ const livePlanItem2 = live2.items.data.find(
764
778
  (item) => {
765
779
  var _a2, _b2;
766
780
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
767
781
  }
768
782
  );
769
- const currentPeriodStart = livePlanItem == null ? void 0 : livePlanItem.current_period_start;
770
- const currentPeriodEnd = livePlanItem == null ? void 0 : livePlanItem.current_period_end;
783
+ const currentPeriodStart = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_start;
784
+ const currentPeriodEnd = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_end;
771
785
  const phases = [
772
786
  {
773
787
  items: [
@@ -839,8 +853,22 @@ var require_subscription = __commonJS({
839
853
  const props = {
840
854
  metadata
841
855
  };
856
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
857
+ const livePlanItem = live.items.data.find(
858
+ (item) => {
859
+ var _a2, _b2;
860
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
861
+ }
862
+ );
863
+ const liveActionsItem = live.items.data.find(
864
+ (item) => {
865
+ var _a2, _b2;
866
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
867
+ }
868
+ );
869
+ const liveActionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || ((_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.actions) == null ? void 0 : _h.id);
842
870
  const plan = await stripe.subscriptionItems.update(
843
- (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
871
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.plan) == null ? void 0 : _j.id),
844
872
  {
845
873
  ...props,
846
874
  price: to.stripePriceId,
@@ -849,7 +877,7 @@ var require_subscription = __commonJS({
849
877
  idem(idempotencyKey, "plan.update")
850
878
  );
851
879
  let actions;
852
- if (((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) == null ? void 0 : _j.id) && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
880
+ if (liveActionsItemId && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
853
881
  actions = await createTier({
854
882
  currency: to.currency,
855
883
  idempotencyKey,
@@ -861,7 +889,7 @@ var require_subscription = __commonJS({
861
889
  overage: to.overages.actions
862
890
  });
863
891
  ({ id: actions["id"] } = await stripe.subscriptionItems.update(
864
- current.items.actions.id,
892
+ liveActionsItemId,
865
893
  {
866
894
  ...props,
867
895
  price: actions == null ? void 0 : actions.price,
@@ -906,6 +934,52 @@ var require_subscription = __commonJS({
906
934
  } catch (error) {
907
935
  throw error;
908
936
  }
937
+ },
938
+ // Pure. Given a subscription document, return the Stripe artifacts that
939
+ // are safe to deactivate at org-delete — the terminal point, the only
940
+ // place deactivation is ever safe. Meters are per-org (actions/ai/
941
+ // storage) and the actions price/product are created fresh per
942
+ // subscription, so all are reclaimable. Never the plan price/product
943
+ // (shared STRIPE_PRICE_* catalog) and never the ai/storage price/product
944
+ // (they only carry a meter, and the ai price is a shared env catalog
945
+ // price). `items` is an untyped, possibly-partial object — optional-chain
946
+ // everything and drop entries without an id.
947
+ deactivatableItems: (subscription) => {
948
+ var _a, _b, _c, _d, _e;
949
+ const items = (subscription == null ? void 0 : subscription.items) || {};
950
+ return [
951
+ { type: "meter", id: (_a = items == null ? void 0 : items.actions) == null ? void 0 : _a.meter },
952
+ { type: "price", id: (_b = items == null ? void 0 : items.actions) == null ? void 0 : _b.price },
953
+ { type: "product", id: (_c = items == null ? void 0 : items.actions) == null ? void 0 : _c.product },
954
+ { type: "meter", id: (_d = items == null ? void 0 : items.ai) == null ? void 0 : _d.meter },
955
+ { type: "meter", id: (_e = items == null ? void 0 : items.storage) == null ? void 0 : _e.meter }
956
+ ].filter((entry) => entry.id);
957
+ },
958
+ // Consume a deactivatableItems list. Each artifact is deactivated with an
959
+ // artifact-id idempotency key (`<type>.deactivate:<id>`) so re-running a
960
+ // delete is a safe no-op. Promise.allSettled means a single Stripe failure
961
+ // never rejects — the caller inspects the settled results. No logging here:
962
+ // drawbridge-stripe stays a pure SDK wrapper, so the consumer (which owns
963
+ // telemetry) logs the outcome.
964
+ deactivate: async ({
965
+ items = []
966
+ }) => {
967
+ const deactivateOne = ({ type, id }) => {
968
+ const options = idem(type + ".deactivate", id);
969
+ if (type === "meter") {
970
+ return stripe.billing.meters.deactivate(id, {}, options);
971
+ }
972
+ ;
973
+ if (type === "price") {
974
+ return stripe.prices.update(id, { active: false }, options);
975
+ }
976
+ ;
977
+ if (type === "product") {
978
+ return stripe.products.update(id, { active: false }, options);
979
+ }
980
+ ;
981
+ };
982
+ return Promise.allSettled(items.map(deactivateOne));
909
983
  }
910
984
  };
911
985
  };
package/package.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "dependencies": {
3
+ "@drawbridge/drawbridge-agents": "0.0.6",
3
4
  "@drawbridge/drawbridge-utils": "0.0.25",
4
5
  "stripe": "20.4.0",
5
6
  "tsup": "8.5.1",
@@ -21,9 +22,9 @@
21
22
  "access": "public"
22
23
  },
23
24
  "scripts": {
24
- "sync": ". \"$HOME/.nvm/nvm.sh\" && nvm use && npm prune && npm install",
25
+ "sync": ". \"$HOME/.nvm/nvm.sh\" && nvm use && npm prune && npm install && npx drawbridge-agents-sync",
25
26
  "build": "tsup ./index.js && npm publish"
26
27
  },
27
28
  "types": "dist/index.d.ts",
28
- "version": "0.1.19"
29
+ "version": "0.1.21"
29
30
  }