@drawbridge/drawbridge-stripe 0.1.7 → 0.1.8

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 +162 -12
  2. package/dist/index.mjs +162 -12
  3. package/package.json +26 -26
package/dist/index.js CHANGED
@@ -382,22 +382,85 @@ 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 phases = [
408
+ {
409
+ items: [
410
+ {
411
+ price: (_a = currentPlanItem == null ? void 0 : currentPlanItem.price) == null ? void 0 : _a.id,
412
+ quantity: 1
413
+ },
414
+ {
415
+ price: (_b = currentActionsItem == null ? void 0 : currentActionsItem.price) == null ? void 0 : _b.id
416
+ }
417
+ ],
418
+ start_date: live.current_period_start,
419
+ end_date: live.current_period_end,
420
+ proration_behavior: "none"
421
+ },
422
+ {
423
+ items: [
424
+ {
425
+ price: (_c = items == null ? void 0 : items.plan) == null ? void 0 : _c.price,
426
+ quantity: 1
427
+ },
428
+ {
429
+ price: (_d = items == null ? void 0 : items.actions) == null ? void 0 : _d.price
430
+ }
431
+ ],
432
+ metadata,
433
+ proration_behavior: "none"
434
+ }
435
+ ];
436
+ const schedule = await stripe2.subscriptionSchedules.create({
437
+ from_subscription: stripeSubscriptionId
438
+ });
439
+ await stripe2.subscriptionSchedules.update(
440
+ schedule.id,
441
+ {
442
+ end_behavior: "release",
443
+ phases
444
+ }
445
+ );
446
+ return {
447
+ items,
448
+ pending: {
449
+ scheduleId: schedule.id,
450
+ effectiveAt: new Date(live.current_period_end * 1e3)
451
+ }
452
+ };
453
+ }
454
+ ;
392
455
  const props = {
393
456
  metadata
394
457
  };
395
458
  const plan = await stripe2.subscriptionItems.update(
396
- (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
459
+ (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
397
460
  {
398
461
  ...props,
399
- price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
400
- proration_behavior: "create_prorations"
462
+ price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
463
+ proration_behavior: "always_invoice"
401
464
  }
402
465
  );
403
466
  let actions = (items == null ? void 0 : items.actions) || {};
@@ -432,8 +495,8 @@ var require_subscription = __commonJS({
432
495
  items: {
433
496
  plan: {
434
497
  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
498
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
499
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
437
500
  },
438
501
  actions
439
502
  }
@@ -508,17 +571,104 @@ var require_subscription = __commonJS({
508
571
  },
509
572
  update: async ({
510
573
  current,
574
+ direction,
511
575
  metadata,
512
576
  stripeSubscriptionId,
513
577
  to
514
578
  }) => {
515
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
579
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
516
580
  try {
581
+ if (direction === "downgrade") {
582
+ const live = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
583
+ let actions2;
584
+ if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
585
+ actions2 = await createTier({
586
+ currency: to.currency,
587
+ interval: to.interval,
588
+ limit: to.limits.organization.actions,
589
+ metadata,
590
+ meter: current.items.actions.meter,
591
+ name: "actions",
592
+ overage: to.overages.actions
593
+ });
594
+ } else {
595
+ const created = await createItem({
596
+ currency: to.currency,
597
+ interval: to.interval,
598
+ limit: to.limits.organization.actions,
599
+ metadata,
600
+ name: "actions",
601
+ overage: to.overages.actions,
602
+ proration_behavior: "none"
603
+ });
604
+ actions2 = {
605
+ meter: created.meter,
606
+ price: created.price,
607
+ product: created.product,
608
+ usage: created.usage
609
+ };
610
+ }
611
+ ;
612
+ const product = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
613
+ const phases = [
614
+ {
615
+ items: [
616
+ {
617
+ price: (_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.plan) == null ? void 0 : _d.price,
618
+ quantity: 1
619
+ },
620
+ {
621
+ price: (_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.price
622
+ }
623
+ ],
624
+ start_date: live.current_period_start,
625
+ end_date: live.current_period_end,
626
+ proration_behavior: "none"
627
+ },
628
+ {
629
+ items: [
630
+ {
631
+ price: to == null ? void 0 : to.stripePriceId,
632
+ quantity: 1
633
+ },
634
+ {
635
+ price: actions2.price
636
+ }
637
+ ],
638
+ metadata,
639
+ proration_behavior: "none"
640
+ }
641
+ ];
642
+ const schedule = await stripe2.subscriptionSchedules.create({
643
+ from_subscription: stripeSubscriptionId
644
+ });
645
+ await stripe2.subscriptionSchedules.update(
646
+ schedule.id,
647
+ {
648
+ end_behavior: "release",
649
+ phases
650
+ }
651
+ );
652
+ return {
653
+ items: {
654
+ plan: {
655
+ price: to == null ? void 0 : to.stripePriceId,
656
+ product: product == null ? void 0 : product.product
657
+ },
658
+ actions: actions2
659
+ },
660
+ pending: {
661
+ scheduleId: schedule.id,
662
+ effectiveAt: new Date(live.current_period_end * 1e3)
663
+ }
664
+ };
665
+ }
666
+ ;
517
667
  const props = {
518
668
  metadata
519
669
  };
520
670
  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,
671
+ (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
522
672
  {
523
673
  ...props,
524
674
  price: to.stripePriceId,
@@ -526,7 +676,7 @@ var require_subscription = __commonJS({
526
676
  }
527
677
  );
528
678
  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)) {
679
+ 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
680
  actions = await createTier({
531
681
  currency: to.currency,
532
682
  interval: to.interval,
@@ -567,11 +717,11 @@ var require_subscription = __commonJS({
567
717
  items: {
568
718
  plan: {
569
719
  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
720
+ price: (_m = plan == null ? void 0 : plan.price) == null ? void 0 : _m.id,
721
+ product: (_n = plan == null ? void 0 : plan.price) == null ? void 0 : _n.product
572
722
  },
573
723
  actions: {
574
- ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) || {},
724
+ ...((_o = current == null ? void 0 : current.items) == null ? void 0 : _o.actions) || {},
575
725
  ...actions
576
726
  }
577
727
  }
package/dist/index.mjs CHANGED
@@ -388,22 +388,85 @@ 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 phases = [
414
+ {
415
+ items: [
416
+ {
417
+ price: (_a = currentPlanItem == null ? void 0 : currentPlanItem.price) == null ? void 0 : _a.id,
418
+ quantity: 1
419
+ },
420
+ {
421
+ price: (_b = currentActionsItem == null ? void 0 : currentActionsItem.price) == null ? void 0 : _b.id
422
+ }
423
+ ],
424
+ start_date: live.current_period_start,
425
+ end_date: live.current_period_end,
426
+ proration_behavior: "none"
427
+ },
428
+ {
429
+ items: [
430
+ {
431
+ price: (_c = items == null ? void 0 : items.plan) == null ? void 0 : _c.price,
432
+ quantity: 1
433
+ },
434
+ {
435
+ price: (_d = items == null ? void 0 : items.actions) == null ? void 0 : _d.price
436
+ }
437
+ ],
438
+ metadata,
439
+ proration_behavior: "none"
440
+ }
441
+ ];
442
+ const schedule = await stripe.subscriptionSchedules.create({
443
+ from_subscription: stripeSubscriptionId
444
+ });
445
+ await stripe.subscriptionSchedules.update(
446
+ schedule.id,
447
+ {
448
+ end_behavior: "release",
449
+ phases
450
+ }
451
+ );
452
+ return {
453
+ items,
454
+ pending: {
455
+ scheduleId: schedule.id,
456
+ effectiveAt: new Date(live.current_period_end * 1e3)
457
+ }
458
+ };
459
+ }
460
+ ;
398
461
  const props = {
399
462
  metadata
400
463
  };
401
464
  const plan = await stripe.subscriptionItems.update(
402
- (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
465
+ (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
403
466
  {
404
467
  ...props,
405
- price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
406
- proration_behavior: "create_prorations"
468
+ price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
469
+ proration_behavior: "always_invoice"
407
470
  }
408
471
  );
409
472
  let actions = (items == null ? void 0 : items.actions) || {};
@@ -438,8 +501,8 @@ var require_subscription = __commonJS({
438
501
  items: {
439
502
  plan: {
440
503
  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
504
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
505
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
443
506
  },
444
507
  actions
445
508
  }
@@ -514,17 +577,104 @@ var require_subscription = __commonJS({
514
577
  },
515
578
  update: async ({
516
579
  current,
580
+ direction,
517
581
  metadata,
518
582
  stripeSubscriptionId,
519
583
  to
520
584
  }) => {
521
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
585
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
522
586
  try {
587
+ if (direction === "downgrade") {
588
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
589
+ let actions2;
590
+ if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
591
+ actions2 = await createTier({
592
+ currency: to.currency,
593
+ interval: to.interval,
594
+ limit: to.limits.organization.actions,
595
+ metadata,
596
+ meter: current.items.actions.meter,
597
+ name: "actions",
598
+ overage: to.overages.actions
599
+ });
600
+ } else {
601
+ const created = await createItem({
602
+ currency: to.currency,
603
+ interval: to.interval,
604
+ limit: to.limits.organization.actions,
605
+ metadata,
606
+ name: "actions",
607
+ overage: to.overages.actions,
608
+ proration_behavior: "none"
609
+ });
610
+ actions2 = {
611
+ meter: created.meter,
612
+ price: created.price,
613
+ product: created.product,
614
+ usage: created.usage
615
+ };
616
+ }
617
+ ;
618
+ const product = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
619
+ const phases = [
620
+ {
621
+ items: [
622
+ {
623
+ price: (_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.plan) == null ? void 0 : _d.price,
624
+ quantity: 1
625
+ },
626
+ {
627
+ price: (_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.actions) == null ? void 0 : _f.price
628
+ }
629
+ ],
630
+ start_date: live.current_period_start,
631
+ end_date: live.current_period_end,
632
+ proration_behavior: "none"
633
+ },
634
+ {
635
+ items: [
636
+ {
637
+ price: to == null ? void 0 : to.stripePriceId,
638
+ quantity: 1
639
+ },
640
+ {
641
+ price: actions2.price
642
+ }
643
+ ],
644
+ metadata,
645
+ proration_behavior: "none"
646
+ }
647
+ ];
648
+ const schedule = await stripe.subscriptionSchedules.create({
649
+ from_subscription: stripeSubscriptionId
650
+ });
651
+ await stripe.subscriptionSchedules.update(
652
+ schedule.id,
653
+ {
654
+ end_behavior: "release",
655
+ phases
656
+ }
657
+ );
658
+ return {
659
+ items: {
660
+ plan: {
661
+ price: to == null ? void 0 : to.stripePriceId,
662
+ product: product == null ? void 0 : product.product
663
+ },
664
+ actions: actions2
665
+ },
666
+ pending: {
667
+ scheduleId: schedule.id,
668
+ effectiveAt: new Date(live.current_period_end * 1e3)
669
+ }
670
+ };
671
+ }
672
+ ;
523
673
  const props = {
524
674
  metadata
525
675
  };
526
676
  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,
677
+ (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
528
678
  {
529
679
  ...props,
530
680
  price: to.stripePriceId,
@@ -532,7 +682,7 @@ var require_subscription = __commonJS({
532
682
  }
533
683
  );
534
684
  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)) {
685
+ 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
686
  actions = await createTier({
537
687
  currency: to.currency,
538
688
  interval: to.interval,
@@ -573,11 +723,11 @@ var require_subscription = __commonJS({
573
723
  items: {
574
724
  plan: {
575
725
  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
726
+ price: (_m = plan == null ? void 0 : plan.price) == null ? void 0 : _m.id,
727
+ product: (_n = plan == null ? void 0 : plan.price) == null ? void 0 : _n.product
578
728
  },
579
729
  actions: {
580
- ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) || {},
730
+ ...((_o = current == null ? void 0 : current.items) == null ? void 0 : _o.actions) || {},
581
731
  ...actions
582
732
  }
583
733
  }
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.8"
28
28
  }