@wopr-network/platform-core 1.45.0 → 1.47.0

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 (50) hide show
  1. package/dist/billing/stripe/billing-period-summary-repository.d.ts +22 -0
  2. package/dist/billing/stripe/billing-period-summary-repository.js +14 -0
  3. package/dist/billing/stripe/index.d.ts +10 -0
  4. package/dist/billing/stripe/index.js +5 -0
  5. package/dist/billing/stripe/metered-price-map.d.ts +26 -0
  6. package/dist/billing/stripe/metered-price-map.js +75 -0
  7. package/dist/billing/stripe/stripe-payment-processor.test.js +1 -0
  8. package/dist/billing/stripe/stripe-usage-reconciliation.d.ts +31 -0
  9. package/dist/billing/stripe/stripe-usage-reconciliation.js +84 -0
  10. package/dist/billing/stripe/stripe-usage-reconciliation.test.d.ts +1 -0
  11. package/dist/billing/stripe/stripe-usage-reconciliation.test.js +109 -0
  12. package/dist/billing/stripe/tenant-store.d.ts +9 -0
  13. package/dist/billing/stripe/tenant-store.js +7 -0
  14. package/dist/billing/stripe/usage-report-repository.d.ts +39 -0
  15. package/dist/billing/stripe/usage-report-repository.js +30 -0
  16. package/dist/billing/stripe/usage-report-repository.test.d.ts +1 -0
  17. package/dist/billing/stripe/usage-report-repository.test.js +77 -0
  18. package/dist/billing/stripe/usage-report-writer.d.ts +41 -0
  19. package/dist/billing/stripe/usage-report-writer.js +95 -0
  20. package/dist/billing/stripe/usage-report-writer.test.d.ts +1 -0
  21. package/dist/billing/stripe/usage-report-writer.test.js +167 -0
  22. package/dist/gateway/credit-gate.js +5 -0
  23. package/dist/gateway/credit-gate.test.js +53 -0
  24. package/dist/gateway/types.d.ts +2 -0
  25. package/dist/monetization/stripe/stripe-payment-processor.test.js +1 -0
  26. package/dist/trpc/index.d.ts +1 -0
  27. package/dist/trpc/index.js +1 -0
  28. package/dist/trpc/org-remove-payment-method-router.d.ts +23 -0
  29. package/dist/trpc/org-remove-payment-method-router.js +61 -0
  30. package/dist/trpc/org-remove-payment-method-router.test.d.ts +1 -0
  31. package/dist/trpc/org-remove-payment-method-router.test.js +166 -0
  32. package/package.json +1 -1
  33. package/src/billing/stripe/billing-period-summary-repository.ts +32 -0
  34. package/src/billing/stripe/index.ts +17 -0
  35. package/src/billing/stripe/metered-price-map.ts +95 -0
  36. package/src/billing/stripe/stripe-payment-processor.test.ts +1 -0
  37. package/src/billing/stripe/stripe-usage-reconciliation.test.ts +127 -0
  38. package/src/billing/stripe/stripe-usage-reconciliation.ts +129 -0
  39. package/src/billing/stripe/tenant-store.ts +9 -0
  40. package/src/billing/stripe/usage-report-repository.test.ts +87 -0
  41. package/src/billing/stripe/usage-report-repository.ts +77 -0
  42. package/src/billing/stripe/usage-report-writer.test.ts +194 -0
  43. package/src/billing/stripe/usage-report-writer.ts +139 -0
  44. package/src/gateway/credit-gate.test.ts +62 -0
  45. package/src/gateway/credit-gate.ts +6 -0
  46. package/src/gateway/types.ts +2 -0
  47. package/src/monetization/stripe/stripe-payment-processor.test.ts +1 -0
  48. package/src/trpc/index.ts +4 -0
  49. package/src/trpc/org-remove-payment-method-router.test.ts +188 -0
  50. package/src/trpc/org-remove-payment-method-router.ts +80 -0
@@ -0,0 +1,80 @@
1
+ import { TRPCError } from "@trpc/server";
2
+ import { z } from "zod";
3
+ import type { IPaymentProcessor } from "../billing/payment-processor.js";
4
+ import { PaymentMethodOwnershipError } from "../billing/payment-processor.js";
5
+ import type { IAutoTopupSettingsRepository } from "../credits/auto-topup-settings-repository.js";
6
+ import { orgAdminProcedure, router } from "./init.js";
7
+
8
+ export interface OrgRemovePaymentMethodDeps {
9
+ processor: IPaymentProcessor;
10
+ autoTopupSettingsStore?: IAutoTopupSettingsRepository;
11
+ }
12
+
13
+ export function createOrgRemovePaymentMethodRouter(getDeps: () => OrgRemovePaymentMethodDeps) {
14
+ return router({
15
+ orgRemovePaymentMethod: orgAdminProcedure
16
+ .input(
17
+ z.object({
18
+ orgId: z.string().min(1),
19
+ paymentMethodId: z.string().min(1),
20
+ }),
21
+ )
22
+ .mutation(async ({ input }) => {
23
+ const { processor, autoTopupSettingsStore } = getDeps();
24
+
25
+ if (!autoTopupSettingsStore) {
26
+ console.warn(
27
+ "orgRemovePaymentMethod: autoTopupSettingsStore not provided — last-payment-method guard is inactive",
28
+ );
29
+ }
30
+
31
+ // Guard: prevent removing the last payment method when auto-topup is enabled
32
+ if (autoTopupSettingsStore) {
33
+ const methods = await processor.listPaymentMethods(input.orgId);
34
+ if (methods.length <= 1) {
35
+ const settings = await autoTopupSettingsStore.getByTenant(input.orgId);
36
+ if (settings && (settings.usageEnabled || settings.scheduleEnabled)) {
37
+ throw new TRPCError({
38
+ code: "FORBIDDEN",
39
+ message: "Cannot remove last payment method while auto-topup is enabled. Disable auto-topup first.",
40
+ });
41
+ }
42
+ }
43
+ }
44
+
45
+ try {
46
+ await processor.detachPaymentMethod(input.orgId, input.paymentMethodId);
47
+ } catch (err) {
48
+ if (err instanceof PaymentMethodOwnershipError) {
49
+ throw new TRPCError({
50
+ code: "FORBIDDEN",
51
+ message: "Payment method does not belong to this organization",
52
+ });
53
+ }
54
+ throw new TRPCError({
55
+ code: "INTERNAL_SERVER_ERROR",
56
+ message: "Failed to remove payment method. Please try again.",
57
+ });
58
+ }
59
+
60
+ // TOCTOU guard: re-check count after detach. A concurrent request
61
+ // may have already removed another payment method between our pre-check
62
+ // and this detach, leaving the org with 0 methods while auto-topup is
63
+ // still enabled. Warn operators — the method is already gone.
64
+ if (autoTopupSettingsStore) {
65
+ const remaining = await processor.listPaymentMethods(input.orgId);
66
+ if (remaining.length === 0) {
67
+ const settings = await autoTopupSettingsStore.getByTenant(input.orgId);
68
+ if (settings && (settings.usageEnabled || settings.scheduleEnabled)) {
69
+ console.warn(
70
+ "orgRemovePaymentMethod: TOCTOU — org %s now has 0 payment methods with auto-topup enabled. Operator must add a payment method or disable auto-topup.",
71
+ input.orgId,
72
+ );
73
+ }
74
+ }
75
+ }
76
+
77
+ return { removed: true };
78
+ }),
79
+ });
80
+ }