@drawbridge/drawbridge-stripe 0.1.7 → 0.1.9

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.
Files changed (3) hide show
  1. package/dist/index.js +172 -12
  2. package/dist/index.mjs +172 -12
  3. package/package.json +26 -26
package/dist/index.js CHANGED
@@ -382,22 +382,87 @@ var require_subscription = __commonJS({
382
382
  };
383
383
  return {
384
384
  change: async ({
385
+ direction,
385
386
  items,
386
387
  metadata,
387
388
  stripeSubscriptionId,
388
389
  to
389
390
  }) => {
390
- var _a, _b, _c, _d;
391
+ var _a, _b, _c, _d, _e, _f, _g, _h;
391
392
  try {
393
+ if (direction === "downgrade") {
394
+ const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
395
+ const currentPlanItem = live.items.data.find(
396
+ (item) => {
397
+ var _a2, _b2;
398
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
399
+ }
400
+ );
401
+ const currentActionsItem = live.items.data.find(
402
+ (item) => {
403
+ var _a2, _b2;
404
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
405
+ }
406
+ );
407
+ const currentPeriodStart = currentPlanItem == null ? void 0 : currentPlanItem.current_period_start;
408
+ const currentPeriodEnd = currentPlanItem == null ? void 0 : currentPlanItem.current_period_end;
409
+ const phases = [
410
+ {
411
+ items: [
412
+ {
413
+ price: (_a = currentPlanItem == null ? void 0 : currentPlanItem.price) == null ? void 0 : _a.id,
414
+ quantity: 1
415
+ },
416
+ {
417
+ price: (_b = currentActionsItem == null ? void 0 : currentActionsItem.price) == null ? void 0 : _b.id
418
+ }
419
+ ],
420
+ start_date: currentPeriodStart,
421
+ end_date: currentPeriodEnd,
422
+ proration_behavior: "none"
423
+ },
424
+ {
425
+ items: [
426
+ {
427
+ price: (_c = items == null ? void 0 : items.plan) == null ? void 0 : _c.price,
428
+ quantity: 1
429
+ },
430
+ {
431
+ price: (_d = items == null ? void 0 : items.actions) == null ? void 0 : _d.price
432
+ }
433
+ ],
434
+ metadata,
435
+ proration_behavior: "none"
436
+ }
437
+ ];
438
+ const schedule = await stripe2.subscriptionSchedules.create({
439
+ from_subscription: stripeSubscriptionId
440
+ });
441
+ await stripe2.subscriptionSchedules.update(
442
+ schedule.id,
443
+ {
444
+ end_behavior: "release",
445
+ phases
446
+ }
447
+ );
448
+ return {
449
+ items,
450
+ pending: {
451
+ scheduleId: schedule.id,
452
+ effectiveAt: new Date(currentPeriodEnd * 1e3)
453
+ }
454
+ };
455
+ }
456
+ ;
392
457
  const props = {
393
458
  metadata
394
459
  };
395
460
  const plan = await stripe2.subscriptionItems.update(
396
- (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
461
+ (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
397
462
  {
398
463
  ...props,
399
- price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
400
- proration_behavior: "create_prorations"
464
+ price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
465
+ proration_behavior: "always_invoice"
401
466
  }
402
467
  );
403
468
  let actions = (items == null ? void 0 : items.actions) || {};
@@ -432,8 +497,8 @@ var require_subscription = __commonJS({
432
497
  items: {
433
498
  plan: {
434
499
  id: plan.id,
435
- price: (_c = plan == null ? void 0 : plan.price) == null ? void 0 : _c.id,
436
- product: (_d = plan == null ? void 0 : plan.price) == null ? void 0 : _d.product
500
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
501
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
437
502
  },
438
503
  actions
439
504
  }
@@ -508,17 +573,112 @@ var require_subscription = __commonJS({
508
573
  },
509
574
  update: async ({
510
575
  current,
576
+ direction,
511
577
  metadata,
512
578
  stripeSubscriptionId,
513
579
  to
514
580
  }) => {
515
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
581
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
516
582
  try {
583
+ if (direction === "downgrade") {
584
+ const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
585
+ let actions2;
586
+ if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
587
+ actions2 = await createTier({
588
+ currency: to.currency,
589
+ interval: to.interval,
590
+ limit: to.limits.organization.actions,
591
+ metadata,
592
+ meter: current.items.actions.meter,
593
+ name: "actions",
594
+ overage: to.overages.actions
595
+ });
596
+ } else {
597
+ const created = await createItem({
598
+ currency: to.currency,
599
+ interval: to.interval,
600
+ limit: to.limits.organization.actions,
601
+ metadata,
602
+ name: "actions",
603
+ overage: to.overages.actions,
604
+ proration_behavior: "none"
605
+ });
606
+ actions2 = {
607
+ meter: created.meter,
608
+ price: created.price,
609
+ product: created.product,
610
+ usage: created.usage
611
+ };
612
+ }
613
+ ;
614
+ const product = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
615
+ const livePlanItem = live.items.data.find(
616
+ (item) => {
617
+ var _a2, _b2;
618
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
619
+ }
620
+ );
621
+ const currentPeriodStart = livePlanItem == null ? void 0 : livePlanItem.current_period_start;
622
+ const currentPeriodEnd = livePlanItem == null ? void 0 : livePlanItem.current_period_end;
623
+ const phases = [
624
+ {
625
+ items: [
626
+ {
627
+ price: (_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.plan) == null ? void 0 : _d.price,
628
+ quantity: 1
629
+ },
630
+ {
631
+ price: (_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.price
632
+ }
633
+ ],
634
+ start_date: currentPeriodStart,
635
+ end_date: currentPeriodEnd,
636
+ proration_behavior: "none"
637
+ },
638
+ {
639
+ items: [
640
+ {
641
+ price: to == null ? void 0 : to.stripePriceId,
642
+ quantity: 1
643
+ },
644
+ {
645
+ price: actions2.price
646
+ }
647
+ ],
648
+ metadata,
649
+ proration_behavior: "none"
650
+ }
651
+ ];
652
+ const schedule = await stripe2.subscriptionSchedules.create({
653
+ from_subscription: stripeSubscriptionId
654
+ });
655
+ await stripe2.subscriptionSchedules.update(
656
+ schedule.id,
657
+ {
658
+ end_behavior: "release",
659
+ phases
660
+ }
661
+ );
662
+ return {
663
+ items: {
664
+ plan: {
665
+ price: to == null ? void 0 : to.stripePriceId,
666
+ product: product == null ? void 0 : product.product
667
+ },
668
+ actions: actions2
669
+ },
670
+ pending: {
671
+ scheduleId: schedule.id,
672
+ effectiveAt: new Date(currentPeriodEnd * 1e3)
673
+ }
674
+ };
675
+ }
676
+ ;
517
677
  const props = {
518
678
  metadata
519
679
  };
520
680
  const plan = await stripe2.subscriptionItems.update(
521
- (_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.plan) == null ? void 0 : _b.id,
681
+ (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
522
682
  {
523
683
  ...props,
524
684
  price: to.stripePriceId,
@@ -526,7 +686,7 @@ var require_subscription = __commonJS({
526
686
  }
527
687
  );
528
688
  let actions;
529
- if (((_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.actions) == null ? void 0 : _d.id) && ((_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.meter)) {
689
+ 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)) {
530
690
  actions = await createTier({
531
691
  currency: to.currency,
532
692
  interval: to.interval,
@@ -567,11 +727,11 @@ var require_subscription = __commonJS({
567
727
  items: {
568
728
  plan: {
569
729
  id: plan.id,
570
- price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
571
- product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
730
+ price: (_m = plan == null ? void 0 : plan.price) == null ? void 0 : _m.id,
731
+ product: (_n = plan == null ? void 0 : plan.price) == null ? void 0 : _n.product
572
732
  },
573
733
  actions: {
574
- ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) || {},
734
+ ...((_o = current == null ? void 0 : current.items) == null ? void 0 : _o.actions) || {},
575
735
  ...actions
576
736
  }
577
737
  }
package/dist/index.mjs CHANGED
@@ -388,22 +388,87 @@ var require_subscription = __commonJS({
388
388
  };
389
389
  return {
390
390
  change: async ({
391
+ direction,
391
392
  items,
392
393
  metadata,
393
394
  stripeSubscriptionId,
394
395
  to
395
396
  }) => {
396
- var _a, _b, _c, _d;
397
+ var _a, _b, _c, _d, _e, _f, _g, _h;
397
398
  try {
399
+ if (direction === "downgrade") {
400
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
401
+ const currentPlanItem = live.items.data.find(
402
+ (item) => {
403
+ var _a2, _b2;
404
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
405
+ }
406
+ );
407
+ const currentActionsItem = live.items.data.find(
408
+ (item) => {
409
+ var _a2, _b2;
410
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
411
+ }
412
+ );
413
+ const currentPeriodStart = currentPlanItem == null ? void 0 : currentPlanItem.current_period_start;
414
+ const currentPeriodEnd = currentPlanItem == null ? void 0 : currentPlanItem.current_period_end;
415
+ const phases = [
416
+ {
417
+ items: [
418
+ {
419
+ price: (_a = currentPlanItem == null ? void 0 : currentPlanItem.price) == null ? void 0 : _a.id,
420
+ quantity: 1
421
+ },
422
+ {
423
+ price: (_b = currentActionsItem == null ? void 0 : currentActionsItem.price) == null ? void 0 : _b.id
424
+ }
425
+ ],
426
+ start_date: currentPeriodStart,
427
+ end_date: currentPeriodEnd,
428
+ proration_behavior: "none"
429
+ },
430
+ {
431
+ items: [
432
+ {
433
+ price: (_c = items == null ? void 0 : items.plan) == null ? void 0 : _c.price,
434
+ quantity: 1
435
+ },
436
+ {
437
+ price: (_d = items == null ? void 0 : items.actions) == null ? void 0 : _d.price
438
+ }
439
+ ],
440
+ metadata,
441
+ proration_behavior: "none"
442
+ }
443
+ ];
444
+ const schedule = await stripe.subscriptionSchedules.create({
445
+ from_subscription: stripeSubscriptionId
446
+ });
447
+ await stripe.subscriptionSchedules.update(
448
+ schedule.id,
449
+ {
450
+ end_behavior: "release",
451
+ phases
452
+ }
453
+ );
454
+ return {
455
+ items,
456
+ pending: {
457
+ scheduleId: schedule.id,
458
+ effectiveAt: new Date(currentPeriodEnd * 1e3)
459
+ }
460
+ };
461
+ }
462
+ ;
398
463
  const props = {
399
464
  metadata
400
465
  };
401
466
  const plan = await stripe.subscriptionItems.update(
402
- (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
467
+ (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
403
468
  {
404
469
  ...props,
405
- price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
406
- proration_behavior: "create_prorations"
470
+ price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
471
+ proration_behavior: "always_invoice"
407
472
  }
408
473
  );
409
474
  let actions = (items == null ? void 0 : items.actions) || {};
@@ -438,8 +503,8 @@ var require_subscription = __commonJS({
438
503
  items: {
439
504
  plan: {
440
505
  id: plan.id,
441
- price: (_c = plan == null ? void 0 : plan.price) == null ? void 0 : _c.id,
442
- product: (_d = plan == null ? void 0 : plan.price) == null ? void 0 : _d.product
506
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
507
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
443
508
  },
444
509
  actions
445
510
  }
@@ -514,17 +579,112 @@ var require_subscription = __commonJS({
514
579
  },
515
580
  update: async ({
516
581
  current,
582
+ direction,
517
583
  metadata,
518
584
  stripeSubscriptionId,
519
585
  to
520
586
  }) => {
521
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
587
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
522
588
  try {
589
+ if (direction === "downgrade") {
590
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
591
+ let actions2;
592
+ if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
593
+ actions2 = await createTier({
594
+ currency: to.currency,
595
+ interval: to.interval,
596
+ limit: to.limits.organization.actions,
597
+ metadata,
598
+ meter: current.items.actions.meter,
599
+ name: "actions",
600
+ overage: to.overages.actions
601
+ });
602
+ } else {
603
+ const created = await createItem({
604
+ currency: to.currency,
605
+ interval: to.interval,
606
+ limit: to.limits.organization.actions,
607
+ metadata,
608
+ name: "actions",
609
+ overage: to.overages.actions,
610
+ proration_behavior: "none"
611
+ });
612
+ actions2 = {
613
+ meter: created.meter,
614
+ price: created.price,
615
+ product: created.product,
616
+ usage: created.usage
617
+ };
618
+ }
619
+ ;
620
+ const product = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
621
+ const livePlanItem = live.items.data.find(
622
+ (item) => {
623
+ var _a2, _b2;
624
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
625
+ }
626
+ );
627
+ const currentPeriodStart = livePlanItem == null ? void 0 : livePlanItem.current_period_start;
628
+ const currentPeriodEnd = livePlanItem == null ? void 0 : livePlanItem.current_period_end;
629
+ const phases = [
630
+ {
631
+ items: [
632
+ {
633
+ price: (_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.plan) == null ? void 0 : _d.price,
634
+ quantity: 1
635
+ },
636
+ {
637
+ price: (_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.price
638
+ }
639
+ ],
640
+ start_date: currentPeriodStart,
641
+ end_date: currentPeriodEnd,
642
+ proration_behavior: "none"
643
+ },
644
+ {
645
+ items: [
646
+ {
647
+ price: to == null ? void 0 : to.stripePriceId,
648
+ quantity: 1
649
+ },
650
+ {
651
+ price: actions2.price
652
+ }
653
+ ],
654
+ metadata,
655
+ proration_behavior: "none"
656
+ }
657
+ ];
658
+ const schedule = await stripe.subscriptionSchedules.create({
659
+ from_subscription: stripeSubscriptionId
660
+ });
661
+ await stripe.subscriptionSchedules.update(
662
+ schedule.id,
663
+ {
664
+ end_behavior: "release",
665
+ phases
666
+ }
667
+ );
668
+ return {
669
+ items: {
670
+ plan: {
671
+ price: to == null ? void 0 : to.stripePriceId,
672
+ product: product == null ? void 0 : product.product
673
+ },
674
+ actions: actions2
675
+ },
676
+ pending: {
677
+ scheduleId: schedule.id,
678
+ effectiveAt: new Date(currentPeriodEnd * 1e3)
679
+ }
680
+ };
681
+ }
682
+ ;
523
683
  const props = {
524
684
  metadata
525
685
  };
526
686
  const plan = await stripe.subscriptionItems.update(
527
- (_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.plan) == null ? void 0 : _b.id,
687
+ (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
528
688
  {
529
689
  ...props,
530
690
  price: to.stripePriceId,
@@ -532,7 +692,7 @@ var require_subscription = __commonJS({
532
692
  }
533
693
  );
534
694
  let actions;
535
- if (((_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.actions) == null ? void 0 : _d.id) && ((_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.meter)) {
695
+ 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)) {
536
696
  actions = await createTier({
537
697
  currency: to.currency,
538
698
  interval: to.interval,
@@ -573,11 +733,11 @@ var require_subscription = __commonJS({
573
733
  items: {
574
734
  plan: {
575
735
  id: plan.id,
576
- price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
577
- product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
736
+ price: (_m = plan == null ? void 0 : plan.price) == null ? void 0 : _m.id,
737
+ product: (_n = plan == null ? void 0 : plan.price) == null ? void 0 : _n.product
578
738
  },
579
739
  actions: {
580
- ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) || {},
740
+ ...((_o = current == null ? void 0 : current.items) == null ? void 0 : _o.actions) || {},
581
741
  ...actions
582
742
  }
583
743
  }
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
1
  {
2
- "dependencies": {
3
- "axios": "^1.13.6",
4
- "stripe": "^20.4.0",
5
- "tsup": "^8.5.1",
6
- "typescript": "^5.9.3"
7
- },
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "default": "./dist/index.js"
12
- }
13
- },
14
- "files": [
15
- "dist"
16
- ],
17
- "license": "ISC",
18
- "main": "dist/index.js",
19
- "name": "@drawbridge/drawbridge-stripe",
20
- "publishConfig": {
21
- "access": "public"
22
- },
23
- "scripts": {
24
- "build": "tsup ./index.js && npm publish"
25
- },
26
- "types": "dist/index.d.ts",
27
- "version": "0.1.7"
2
+ "dependencies": {
3
+ "axios": "1.16.0",
4
+ "stripe": "20.4.0",
5
+ "tsup": "8.5.1",
6
+ "typescript": "5.9.3"
7
+ },
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "license": "ISC",
18
+ "main": "dist/index.js",
19
+ "name": "@drawbridge/drawbridge-stripe",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup ./index.js && npm publish"
25
+ },
26
+ "types": "dist/index.d.ts",
27
+ "version": "0.1.9"
28
28
  }