@drawbridge/drawbridge-stripe 0.1.12 → 0.1.13

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 +106 -56
  2. package/dist/index.mjs +106 -56
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -288,18 +288,28 @@ var require_subscription = __commonJS({
288
288
  "Stripe-Version": "2026-03-25.preview"
289
289
  }
290
290
  };
291
+ const idem = (base, suffix) => {
292
+ if (!base) return void 0;
293
+ return {
294
+ idempotencyKey: base + ":" + suffix
295
+ };
296
+ };
291
297
  const createMeter = async ({
298
+ idempotencyKey,
292
299
  metadata,
293
300
  name
294
301
  }) => {
295
302
  const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
296
- const meter = await stripe2.billing.meters.create({
297
- default_aggregation: {
298
- formula: "sum"
303
+ const meter = await stripe2.billing.meters.create(
304
+ {
305
+ default_aggregation: {
306
+ formula: "sum"
307
+ },
308
+ display_name: usage,
309
+ event_name: usage
299
310
  },
300
- display_name: usage,
301
- event_name: usage
302
- });
311
+ idem(idempotencyKey, "meter.create." + name)
312
+ );
303
313
  return {
304
314
  meter: meter == null ? void 0 : meter.id,
305
315
  usage
@@ -307,6 +317,7 @@ var require_subscription = __commonJS({
307
317
  };
308
318
  const createTier = async ({
309
319
  currency,
320
+ idempotencyKey,
310
321
  interval,
311
322
  limit,
312
323
  metadata,
@@ -315,42 +326,49 @@ var require_subscription = __commonJS({
315
326
  overage
316
327
  }) => {
317
328
  const response = {};
318
- const product = await stripe2.products.create({
319
- metadata,
320
- name
321
- });
322
- response["product"] = product == null ? void 0 : product.id;
323
- const price = await stripe2.prices.create({
324
- billing_scheme: "tiered",
325
- currency,
326
- metadata,
327
- nickname: name,
328
- product: product == null ? void 0 : product.id,
329
- recurring: {
330
- interval,
331
- meter,
332
- usage_type: "metered"
329
+ const product = await stripe2.products.create(
330
+ {
331
+ metadata,
332
+ name
333
333
  },
334
- tax_behavior: "exclusive",
335
- tiers: [
336
- {
337
- flat_amount: "0",
338
- // included in the plan. Should not be a value.
339
- up_to: limit
334
+ idem(idempotencyKey, "product.create." + name)
335
+ );
336
+ response["product"] = product == null ? void 0 : product.id;
337
+ const price = await stripe2.prices.create(
338
+ {
339
+ billing_scheme: "tiered",
340
+ currency,
341
+ metadata,
342
+ nickname: name,
343
+ product: product == null ? void 0 : product.id,
344
+ recurring: {
345
+ interval,
346
+ meter,
347
+ usage_type: "metered"
340
348
  },
341
- {
342
- unit_amount_decimal: overage,
343
- up_to: "inf"
344
- }
345
- ],
346
- tiers_mode: "graduated"
347
- });
349
+ tax_behavior: "exclusive",
350
+ tiers: [
351
+ {
352
+ flat_amount: "0",
353
+ // included in the plan. Should not be a value.
354
+ up_to: limit
355
+ },
356
+ {
357
+ unit_amount_decimal: overage,
358
+ up_to: "inf"
359
+ }
360
+ ],
361
+ tiers_mode: "graduated"
362
+ },
363
+ idem(idempotencyKey, "price.create." + name)
364
+ );
348
365
  response["price"] = price == null ? void 0 : price.id;
349
366
  return response;
350
367
  ;
351
368
  };
352
369
  const createItem = async ({
353
370
  currency,
371
+ idempotencyKey,
354
372
  interval,
355
373
  limit,
356
374
  metadata,
@@ -361,6 +379,7 @@ var require_subscription = __commonJS({
361
379
  }) => {
362
380
  const response = {};
363
381
  const { meter, usage } = await createMeter({
382
+ idempotencyKey,
364
383
  metadata,
365
384
  name
366
385
  });
@@ -368,6 +387,7 @@ var require_subscription = __commonJS({
368
387
  response["usage"] = usage;
369
388
  const { price, product } = await createTier({
370
389
  currency,
390
+ idempotencyKey,
371
391
  interval,
372
392
  limit,
373
393
  metadata,
@@ -378,12 +398,15 @@ var require_subscription = __commonJS({
378
398
  response["price"] = price;
379
399
  response["product"] = product;
380
400
  if (subscription) {
381
- const { id } = await stripe2.subscriptionItems.create({
382
- metadata,
383
- price,
384
- proration_behavior,
385
- subscription
386
- });
401
+ const { id } = await stripe2.subscriptionItems.create(
402
+ {
403
+ metadata,
404
+ price,
405
+ proration_behavior,
406
+ subscription
407
+ },
408
+ idem(idempotencyKey, "subscriptionItem.create." + name)
409
+ );
387
410
  response["id"] = id;
388
411
  }
389
412
  ;
@@ -392,6 +415,7 @@ var require_subscription = __commonJS({
392
415
  return {
393
416
  change: async ({
394
417
  direction,
418
+ idempotencyKey,
395
419
  items,
396
420
  metadata,
397
421
  stripeSubscriptionId,
@@ -444,19 +468,26 @@ var require_subscription = __commonJS({
444
468
  proration_behavior: "none"
445
469
  }
446
470
  ];
447
- const schedule = await stripe2.subscriptionSchedules.create({
448
- from_subscription: stripeSubscriptionId
449
- });
471
+ const schedule = await stripe2.subscriptionSchedules.create(
472
+ {
473
+ from_subscription: stripeSubscriptionId
474
+ },
475
+ idem(idempotencyKey, "schedule.create")
476
+ );
450
477
  try {
451
478
  await stripe2.subscriptionSchedules.update(
452
479
  schedule.id,
453
480
  {
454
481
  end_behavior: "release",
455
482
  phases
456
- }
483
+ },
484
+ idem(idempotencyKey, "schedule.update")
457
485
  );
458
486
  } catch (error) {
459
- await stripe2.subscriptionSchedules.release(schedule.id).catch(() => {
487
+ await stripe2.subscriptionSchedules.release(
488
+ schedule.id,
489
+ idem(idempotencyKey, "schedule.release")
490
+ ).catch(() => {
460
491
  });
461
492
  throw error;
462
493
  }
@@ -479,7 +510,8 @@ var require_subscription = __commonJS({
479
510
  ...props,
480
511
  price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
481
512
  proration_behavior: "always_invoice"
482
- }
513
+ },
514
+ idem(idempotencyKey, "plan.update")
483
515
  );
484
516
  let actions = (items == null ? void 0 : items.actions) || {};
485
517
  if ((actions == null ? void 0 : actions.id) && (actions == null ? void 0 : actions.price)) {
@@ -489,11 +521,13 @@ var require_subscription = __commonJS({
489
521
  ...props,
490
522
  price: actions.price,
491
523
  proration_behavior: "none"
492
- }
524
+ },
525
+ idem(idempotencyKey, "actions.update")
493
526
  ));
494
527
  } else {
495
528
  actions = await createItem({
496
529
  currency: to.currency,
530
+ idempotencyKey,
497
531
  interval: to.interval,
498
532
  limit: to.limits.organization.actions,
499
533
  metadata,
@@ -507,7 +541,8 @@ var require_subscription = __commonJS({
507
541
  stripeSubscriptionId,
508
542
  {
509
543
  metadata
510
- }
544
+ },
545
+ idem(idempotencyKey, "subscription.update")
511
546
  );
512
547
  return {
513
548
  items: {
@@ -626,6 +661,7 @@ var require_subscription = __commonJS({
626
661
  update: async ({
627
662
  current,
628
663
  direction,
664
+ idempotencyKey,
629
665
  metadata,
630
666
  stripeSubscriptionId,
631
667
  to
@@ -638,6 +674,7 @@ var require_subscription = __commonJS({
638
674
  if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
639
675
  actions2 = await createTier({
640
676
  currency: to.currency,
677
+ idempotencyKey,
641
678
  interval: to.interval,
642
679
  limit: to.limits.organization.actions,
643
680
  metadata,
@@ -648,6 +685,7 @@ var require_subscription = __commonJS({
648
685
  } else {
649
686
  const created = await createItem({
650
687
  currency: to.currency,
688
+ idempotencyKey,
651
689
  interval: to.interval,
652
690
  limit: to.limits.organization.actions,
653
691
  metadata,
@@ -701,19 +739,26 @@ var require_subscription = __commonJS({
701
739
  proration_behavior: "none"
702
740
  }
703
741
  ];
704
- const schedule = await stripe2.subscriptionSchedules.create({
705
- from_subscription: stripeSubscriptionId
706
- });
742
+ const schedule = await stripe2.subscriptionSchedules.create(
743
+ {
744
+ from_subscription: stripeSubscriptionId
745
+ },
746
+ idem(idempotencyKey, "schedule.create")
747
+ );
707
748
  try {
708
749
  await stripe2.subscriptionSchedules.update(
709
750
  schedule.id,
710
751
  {
711
752
  end_behavior: "release",
712
753
  phases
713
- }
754
+ },
755
+ idem(idempotencyKey, "schedule.update")
714
756
  );
715
757
  } catch (error) {
716
- await stripe2.subscriptionSchedules.release(schedule.id).catch(() => {
758
+ await stripe2.subscriptionSchedules.release(
759
+ schedule.id,
760
+ idem(idempotencyKey, "schedule.release")
761
+ ).catch(() => {
717
762
  });
718
763
  throw error;
719
764
  }
@@ -742,12 +787,14 @@ var require_subscription = __commonJS({
742
787
  ...props,
743
788
  price: to.stripePriceId,
744
789
  proration_behavior: "always_invoice"
745
- }
790
+ },
791
+ idem(idempotencyKey, "plan.update")
746
792
  );
747
793
  let actions;
748
794
  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)) {
749
795
  actions = await createTier({
750
796
  currency: to.currency,
797
+ idempotencyKey,
751
798
  interval: to.interval,
752
799
  limit: to.limits.organization.actions,
753
800
  metadata,
@@ -761,11 +808,13 @@ var require_subscription = __commonJS({
761
808
  ...props,
762
809
  price: actions == null ? void 0 : actions.price,
763
810
  proration_behavior: "none"
764
- }
811
+ },
812
+ idem(idempotencyKey, "actions.update")
765
813
  ));
766
814
  } else {
767
815
  actions = await createItem({
768
816
  currency: to.currency,
817
+ idempotencyKey,
769
818
  interval: to.interval,
770
819
  limit: to.limits.organization.actions,
771
820
  metadata,
@@ -780,7 +829,8 @@ var require_subscription = __commonJS({
780
829
  stripeSubscriptionId,
781
830
  {
782
831
  metadata
783
- }
832
+ },
833
+ idem(idempotencyKey, "subscription.update")
784
834
  );
785
835
  return {
786
836
  items: {
package/dist/index.mjs CHANGED
@@ -294,18 +294,28 @@ var require_subscription = __commonJS({
294
294
  "Stripe-Version": "2026-03-25.preview"
295
295
  }
296
296
  };
297
+ const idem = (base, suffix) => {
298
+ if (!base) return void 0;
299
+ return {
300
+ idempotencyKey: base + ":" + suffix
301
+ };
302
+ };
297
303
  const createMeter = async ({
304
+ idempotencyKey,
298
305
  metadata,
299
306
  name
300
307
  }) => {
301
308
  const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
302
- const meter = await stripe.billing.meters.create({
303
- default_aggregation: {
304
- formula: "sum"
309
+ const meter = await stripe.billing.meters.create(
310
+ {
311
+ default_aggregation: {
312
+ formula: "sum"
313
+ },
314
+ display_name: usage,
315
+ event_name: usage
305
316
  },
306
- display_name: usage,
307
- event_name: usage
308
- });
317
+ idem(idempotencyKey, "meter.create." + name)
318
+ );
309
319
  return {
310
320
  meter: meter == null ? void 0 : meter.id,
311
321
  usage
@@ -313,6 +323,7 @@ var require_subscription = __commonJS({
313
323
  };
314
324
  const createTier = async ({
315
325
  currency,
326
+ idempotencyKey,
316
327
  interval,
317
328
  limit,
318
329
  metadata,
@@ -321,42 +332,49 @@ var require_subscription = __commonJS({
321
332
  overage
322
333
  }) => {
323
334
  const response = {};
324
- const product = await stripe.products.create({
325
- metadata,
326
- name
327
- });
328
- response["product"] = product == null ? void 0 : product.id;
329
- const price = await stripe.prices.create({
330
- billing_scheme: "tiered",
331
- currency,
332
- metadata,
333
- nickname: name,
334
- product: product == null ? void 0 : product.id,
335
- recurring: {
336
- interval,
337
- meter,
338
- usage_type: "metered"
335
+ const product = await stripe.products.create(
336
+ {
337
+ metadata,
338
+ name
339
339
  },
340
- tax_behavior: "exclusive",
341
- tiers: [
342
- {
343
- flat_amount: "0",
344
- // included in the plan. Should not be a value.
345
- up_to: limit
340
+ idem(idempotencyKey, "product.create." + name)
341
+ );
342
+ response["product"] = product == null ? void 0 : product.id;
343
+ const price = await stripe.prices.create(
344
+ {
345
+ billing_scheme: "tiered",
346
+ currency,
347
+ metadata,
348
+ nickname: name,
349
+ product: product == null ? void 0 : product.id,
350
+ recurring: {
351
+ interval,
352
+ meter,
353
+ usage_type: "metered"
346
354
  },
347
- {
348
- unit_amount_decimal: overage,
349
- up_to: "inf"
350
- }
351
- ],
352
- tiers_mode: "graduated"
353
- });
355
+ tax_behavior: "exclusive",
356
+ tiers: [
357
+ {
358
+ flat_amount: "0",
359
+ // included in the plan. Should not be a value.
360
+ up_to: limit
361
+ },
362
+ {
363
+ unit_amount_decimal: overage,
364
+ up_to: "inf"
365
+ }
366
+ ],
367
+ tiers_mode: "graduated"
368
+ },
369
+ idem(idempotencyKey, "price.create." + name)
370
+ );
354
371
  response["price"] = price == null ? void 0 : price.id;
355
372
  return response;
356
373
  ;
357
374
  };
358
375
  const createItem = async ({
359
376
  currency,
377
+ idempotencyKey,
360
378
  interval,
361
379
  limit,
362
380
  metadata,
@@ -367,6 +385,7 @@ var require_subscription = __commonJS({
367
385
  }) => {
368
386
  const response = {};
369
387
  const { meter, usage } = await createMeter({
388
+ idempotencyKey,
370
389
  metadata,
371
390
  name
372
391
  });
@@ -374,6 +393,7 @@ var require_subscription = __commonJS({
374
393
  response["usage"] = usage;
375
394
  const { price, product } = await createTier({
376
395
  currency,
396
+ idempotencyKey,
377
397
  interval,
378
398
  limit,
379
399
  metadata,
@@ -384,12 +404,15 @@ var require_subscription = __commonJS({
384
404
  response["price"] = price;
385
405
  response["product"] = product;
386
406
  if (subscription) {
387
- const { id } = await stripe.subscriptionItems.create({
388
- metadata,
389
- price,
390
- proration_behavior,
391
- subscription
392
- });
407
+ const { id } = await stripe.subscriptionItems.create(
408
+ {
409
+ metadata,
410
+ price,
411
+ proration_behavior,
412
+ subscription
413
+ },
414
+ idem(idempotencyKey, "subscriptionItem.create." + name)
415
+ );
393
416
  response["id"] = id;
394
417
  }
395
418
  ;
@@ -398,6 +421,7 @@ var require_subscription = __commonJS({
398
421
  return {
399
422
  change: async ({
400
423
  direction,
424
+ idempotencyKey,
401
425
  items,
402
426
  metadata,
403
427
  stripeSubscriptionId,
@@ -450,19 +474,26 @@ var require_subscription = __commonJS({
450
474
  proration_behavior: "none"
451
475
  }
452
476
  ];
453
- const schedule = await stripe.subscriptionSchedules.create({
454
- from_subscription: stripeSubscriptionId
455
- });
477
+ const schedule = await stripe.subscriptionSchedules.create(
478
+ {
479
+ from_subscription: stripeSubscriptionId
480
+ },
481
+ idem(idempotencyKey, "schedule.create")
482
+ );
456
483
  try {
457
484
  await stripe.subscriptionSchedules.update(
458
485
  schedule.id,
459
486
  {
460
487
  end_behavior: "release",
461
488
  phases
462
- }
489
+ },
490
+ idem(idempotencyKey, "schedule.update")
463
491
  );
464
492
  } catch (error) {
465
- await stripe.subscriptionSchedules.release(schedule.id).catch(() => {
493
+ await stripe.subscriptionSchedules.release(
494
+ schedule.id,
495
+ idem(idempotencyKey, "schedule.release")
496
+ ).catch(() => {
466
497
  });
467
498
  throw error;
468
499
  }
@@ -485,7 +516,8 @@ var require_subscription = __commonJS({
485
516
  ...props,
486
517
  price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
487
518
  proration_behavior: "always_invoice"
488
- }
519
+ },
520
+ idem(idempotencyKey, "plan.update")
489
521
  );
490
522
  let actions = (items == null ? void 0 : items.actions) || {};
491
523
  if ((actions == null ? void 0 : actions.id) && (actions == null ? void 0 : actions.price)) {
@@ -495,11 +527,13 @@ var require_subscription = __commonJS({
495
527
  ...props,
496
528
  price: actions.price,
497
529
  proration_behavior: "none"
498
- }
530
+ },
531
+ idem(idempotencyKey, "actions.update")
499
532
  ));
500
533
  } else {
501
534
  actions = await createItem({
502
535
  currency: to.currency,
536
+ idempotencyKey,
503
537
  interval: to.interval,
504
538
  limit: to.limits.organization.actions,
505
539
  metadata,
@@ -513,7 +547,8 @@ var require_subscription = __commonJS({
513
547
  stripeSubscriptionId,
514
548
  {
515
549
  metadata
516
- }
550
+ },
551
+ idem(idempotencyKey, "subscription.update")
517
552
  );
518
553
  return {
519
554
  items: {
@@ -632,6 +667,7 @@ var require_subscription = __commonJS({
632
667
  update: async ({
633
668
  current,
634
669
  direction,
670
+ idempotencyKey,
635
671
  metadata,
636
672
  stripeSubscriptionId,
637
673
  to
@@ -644,6 +680,7 @@ var require_subscription = __commonJS({
644
680
  if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
645
681
  actions2 = await createTier({
646
682
  currency: to.currency,
683
+ idempotencyKey,
647
684
  interval: to.interval,
648
685
  limit: to.limits.organization.actions,
649
686
  metadata,
@@ -654,6 +691,7 @@ var require_subscription = __commonJS({
654
691
  } else {
655
692
  const created = await createItem({
656
693
  currency: to.currency,
694
+ idempotencyKey,
657
695
  interval: to.interval,
658
696
  limit: to.limits.organization.actions,
659
697
  metadata,
@@ -707,19 +745,26 @@ var require_subscription = __commonJS({
707
745
  proration_behavior: "none"
708
746
  }
709
747
  ];
710
- const schedule = await stripe.subscriptionSchedules.create({
711
- from_subscription: stripeSubscriptionId
712
- });
748
+ const schedule = await stripe.subscriptionSchedules.create(
749
+ {
750
+ from_subscription: stripeSubscriptionId
751
+ },
752
+ idem(idempotencyKey, "schedule.create")
753
+ );
713
754
  try {
714
755
  await stripe.subscriptionSchedules.update(
715
756
  schedule.id,
716
757
  {
717
758
  end_behavior: "release",
718
759
  phases
719
- }
760
+ },
761
+ idem(idempotencyKey, "schedule.update")
720
762
  );
721
763
  } catch (error) {
722
- await stripe.subscriptionSchedules.release(schedule.id).catch(() => {
764
+ await stripe.subscriptionSchedules.release(
765
+ schedule.id,
766
+ idem(idempotencyKey, "schedule.release")
767
+ ).catch(() => {
723
768
  });
724
769
  throw error;
725
770
  }
@@ -748,12 +793,14 @@ var require_subscription = __commonJS({
748
793
  ...props,
749
794
  price: to.stripePriceId,
750
795
  proration_behavior: "always_invoice"
751
- }
796
+ },
797
+ idem(idempotencyKey, "plan.update")
752
798
  );
753
799
  let actions;
754
800
  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)) {
755
801
  actions = await createTier({
756
802
  currency: to.currency,
803
+ idempotencyKey,
757
804
  interval: to.interval,
758
805
  limit: to.limits.organization.actions,
759
806
  metadata,
@@ -767,11 +814,13 @@ var require_subscription = __commonJS({
767
814
  ...props,
768
815
  price: actions == null ? void 0 : actions.price,
769
816
  proration_behavior: "none"
770
- }
817
+ },
818
+ idem(idempotencyKey, "actions.update")
771
819
  ));
772
820
  } else {
773
821
  actions = await createItem({
774
822
  currency: to.currency,
823
+ idempotencyKey,
775
824
  interval: to.interval,
776
825
  limit: to.limits.organization.actions,
777
826
  metadata,
@@ -786,7 +835,8 @@ var require_subscription = __commonJS({
786
835
  stripeSubscriptionId,
787
836
  {
788
837
  metadata
789
- }
838
+ },
839
+ idem(idempotencyKey, "subscription.update")
790
840
  );
791
841
  return {
792
842
  items: {
package/package.json CHANGED
@@ -24,5 +24,5 @@
24
24
  "build": "tsup ./index.js && npm publish"
25
25
  },
26
26
  "types": "dist/index.d.ts",
27
- "version": "0.1.12"
27
+ "version": "0.1.13"
28
28
  }