@drawbridge/drawbridge-stripe 0.1.6 → 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 +167 -13
  2. package/dist/index.mjs +167 -13
  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
  }
@@ -443,6 +506,7 @@ var require_subscription = __commonJS({
443
506
  }
444
507
  },
445
508
  create: async ({
509
+ billingCycleAnchor,
446
510
  customer,
447
511
  discounts = [],
448
512
  metadata,
@@ -473,7 +537,10 @@ var require_subscription = __commonJS({
473
537
  price: actions.price
474
538
  }
475
539
  ],
476
- metadata
540
+ metadata,
541
+ ...billingCycleAnchor && {
542
+ billing_cycle_anchor: billingCycleAnchor
543
+ }
477
544
  });
478
545
  const { product } = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
479
546
  const plan = await stripe2.subscriptionItems.create({
@@ -504,17 +571,104 @@ var require_subscription = __commonJS({
504
571
  },
505
572
  update: async ({
506
573
  current,
574
+ direction,
507
575
  metadata,
508
576
  stripeSubscriptionId,
509
577
  to
510
578
  }) => {
511
- 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;
512
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
+ ;
513
667
  const props = {
514
668
  metadata
515
669
  };
516
670
  const plan = await stripe2.subscriptionItems.update(
517
- (_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,
518
672
  {
519
673
  ...props,
520
674
  price: to.stripePriceId,
@@ -522,7 +676,7 @@ var require_subscription = __commonJS({
522
676
  }
523
677
  );
524
678
  let actions;
525
- 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)) {
526
680
  actions = await createTier({
527
681
  currency: to.currency,
528
682
  interval: to.interval,
@@ -563,11 +717,11 @@ var require_subscription = __commonJS({
563
717
  items: {
564
718
  plan: {
565
719
  id: plan.id,
566
- price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
567
- 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
568
722
  },
569
723
  actions: {
570
- ...((_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) || {},
571
725
  ...actions
572
726
  }
573
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
  }
@@ -449,6 +512,7 @@ var require_subscription = __commonJS({
449
512
  }
450
513
  },
451
514
  create: async ({
515
+ billingCycleAnchor,
452
516
  customer,
453
517
  discounts = [],
454
518
  metadata,
@@ -479,7 +543,10 @@ var require_subscription = __commonJS({
479
543
  price: actions.price
480
544
  }
481
545
  ],
482
- metadata
546
+ metadata,
547
+ ...billingCycleAnchor && {
548
+ billing_cycle_anchor: billingCycleAnchor
549
+ }
483
550
  });
484
551
  const { product } = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
485
552
  const plan = await stripe.subscriptionItems.create({
@@ -510,17 +577,104 @@ var require_subscription = __commonJS({
510
577
  },
511
578
  update: async ({
512
579
  current,
580
+ direction,
513
581
  metadata,
514
582
  stripeSubscriptionId,
515
583
  to
516
584
  }) => {
517
- 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;
518
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
+ ;
519
673
  const props = {
520
674
  metadata
521
675
  };
522
676
  const plan = await stripe.subscriptionItems.update(
523
- (_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,
524
678
  {
525
679
  ...props,
526
680
  price: to.stripePriceId,
@@ -528,7 +682,7 @@ var require_subscription = __commonJS({
528
682
  }
529
683
  );
530
684
  let actions;
531
- 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)) {
532
686
  actions = await createTier({
533
687
  currency: to.currency,
534
688
  interval: to.interval,
@@ -569,11 +723,11 @@ var require_subscription = __commonJS({
569
723
  items: {
570
724
  plan: {
571
725
  id: plan.id,
572
- price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
573
- 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
574
728
  },
575
729
  actions: {
576
- ...((_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) || {},
577
731
  ...actions
578
732
  }
579
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.6"
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
  }