@zonastery/bursar 1.0.0 → 2.0.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.
- package/README.md +189 -62
- package/dist/billing/auto-recharge-port.d.ts +3 -3
- package/dist/billing/auto-recharge-port.d.ts.map +1 -1
- package/dist/billing/auto-recharge-port.js.map +1 -1
- package/dist/billing/auto-recharge-service.d.ts +5 -4
- package/dist/billing/auto-recharge-service.d.ts.map +1 -1
- package/dist/billing/auto-recharge-service.js +64 -34
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +5 -5
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +2 -2
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +10 -9
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js.map +1 -1
- package/dist/billing/contracts.d.ts +31 -31
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +49 -10
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +1 -0
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +37 -10
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +69 -72
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +5 -5
- package/dist/billing/management.d.ts.map +1 -1
- package/dist/billing/management.js +2 -2
- package/dist/billing/management.js.map +1 -1
- package/dist/billing/policy-window.js.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +3 -3
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +205 -60
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/customer.js +19 -11
- package/dist/billing/postgres/repositories/customer.js.map +1 -1
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/dispute.js +3 -1
- package/dist/billing/postgres/repositories/dispute.js.map +1 -1
- package/dist/billing/postgres/repositories/event.d.ts +14 -7
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js +21 -20
- package/dist/billing/postgres/repositories/event.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +3 -3
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +53 -11
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts +20 -17
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +123 -57
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +23 -20
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +43 -37
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/preferences.d.ts +3 -9
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/preferences.js +45 -7
- package/dist/billing/postgres/repositories/preferences.js.map +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/refund.js +13 -9
- package/dist/billing/postgres/repositories/refund.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts +15 -0
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription-change.js +234 -0
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +38 -17
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +199 -91
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts +13 -15
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/topup.js +65 -34
- package/dist/billing/postgres/repositories/topup.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +23 -14
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +265 -228
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/service-types.d.ts +6 -5
- package/dist/billing/service-types.d.ts.map +1 -1
- package/dist/billing/service-types.js.map +1 -1
- package/dist/billing/types/auto-recharge.d.ts +6 -4
- package/dist/billing/types/auto-recharge.d.ts.map +1 -1
- package/dist/billing/types/auto-recharge.js.map +1 -1
- package/dist/billing/types/catalog.d.ts +18 -35
- package/dist/billing/types/catalog.d.ts.map +1 -1
- package/dist/billing/types/catalog.js.map +1 -1
- package/dist/billing/types/common.d.ts +0 -1
- package/dist/billing/types/common.d.ts.map +1 -1
- package/dist/billing/types/common.js.map +1 -1
- package/dist/billing/types/documents.d.ts +38 -12
- package/dist/billing/types/documents.d.ts.map +1 -1
- package/dist/billing/types/documents.js.map +1 -1
- package/dist/billing/types/events.d.ts +2 -0
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +238 -0
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/index.js.map +1 -1
- package/dist/billing/types/preferences.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +8 -8
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/bursar.d.ts +33 -11
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +58 -22
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts +1 -0
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +1 -0
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/errors.d.ts +4 -14
- package/dist/commerce/errors.d.ts.map +1 -1
- package/dist/commerce/errors.js +4 -21
- package/dist/commerce/errors.js.map +1 -1
- package/dist/commerce/index.d.ts +1 -0
- package/dist/commerce/index.d.ts.map +1 -1
- package/dist/commerce/index.js.map +1 -1
- package/dist/commerce/plan-change.js.map +1 -1
- package/dist/commerce/provider-registry.d.ts.map +1 -1
- package/dist/commerce/provider-registry.js +27 -2
- package/dist/commerce/provider-registry.js.map +1 -1
- package/dist/commerce/service.d.ts +15 -7
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +130 -77
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts +33 -13
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +30 -13
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +16 -5
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config/types.d.ts +14 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +7 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +64 -1
- package/dist/config.js.map +1 -1
- package/dist/credits/{pricing-runtime.d.ts → catalog-runtime.d.ts} +9 -8
- package/dist/credits/catalog-runtime.d.ts.map +1 -0
- package/dist/credits/{pricing-runtime.js → catalog-runtime.js} +58 -38
- package/dist/credits/catalog-runtime.js.map +1 -0
- package/dist/credits/events.d.ts +4 -4
- package/dist/credits/events.d.ts.map +1 -1
- package/dist/credits/events.js +1 -1
- package/dist/credits/events.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +14 -14
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +18 -17
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -2
- package/dist/credits/low-balance-monitor.d.ts.map +1 -1
- package/dist/credits/low-balance-monitor.js.map +1 -1
- package/dist/credits/postgres/repositories/analytics.d.ts +39 -35
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +55 -53
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts +28 -30
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +100 -59
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/bucket.d.ts +14 -8
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/bucket.js +22 -21
- package/dist/credits/postgres/repositories/bucket.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +31 -0
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/catalog.js +90 -0
- package/dist/credits/postgres/repositories/catalog.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +32 -21
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +171 -62
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/lease.d.ts +21 -18
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/lease.js +157 -65
- package/dist/credits/postgres/repositories/lease.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +94 -42
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js +134 -104
- package/dist/credits/postgres/repositories/plan.js.map +1 -1
- package/dist/credits/postgres/repositories/team.d.ts +36 -27
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/team.js +96 -52
- package/dist/credits/postgres/repositories/team.js.map +1 -1
- package/dist/credits/postgres/store.d.ts +31 -21
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +325 -235
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts +7 -9
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +42 -63
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/queries.d.ts +5 -5
- package/dist/credits/queries.d.ts.map +1 -1
- package/dist/credits/queries.js +4 -4
- package/dist/credits/queries.js.map +1 -1
- package/dist/credits/service-errors.js.map +1 -1
- package/dist/credits/service-types.d.ts +42 -7
- package/dist/credits/service-types.d.ts.map +1 -1
- package/dist/credits/service-types.js.map +1 -1
- package/dist/credits/service.d.ts +51 -58
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +178 -113
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +25 -22
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js +9 -5
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +50 -12
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/admission.d.ts +17 -6
- package/dist/credits/types/admission.d.ts.map +1 -1
- package/dist/credits/types/admission.js.map +1 -1
- package/dist/credits/types/analytics.d.ts +2 -2
- package/dist/credits/types/analytics.d.ts.map +1 -1
- package/dist/credits/types/analytics.js.map +1 -1
- package/dist/credits/types/catalog.d.ts +21 -14
- package/dist/credits/types/catalog.d.ts.map +1 -1
- package/dist/credits/types/catalog.js.map +1 -1
- package/dist/credits/types/grants.d.ts +22 -8
- package/dist/credits/types/grants.d.ts.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/index.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +19 -0
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/credits/types/quotas.js.map +1 -1
- package/dist/credits/types/teams.d.ts +18 -6
- package/dist/credits/types/teams.d.ts.map +1 -1
- package/dist/credits/types/teams.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +24 -10
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +105 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +137 -9
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts.map +1 -1
- package/dist/expr/ast.js.map +1 -1
- package/dist/expr/evaluator.d.ts.map +1 -1
- package/dist/expr/evaluator.js +29 -17
- package/dist/expr/evaluator.js.map +1 -1
- package/dist/expr/language.js.map +1 -1
- package/dist/expr/parser.d.ts.map +1 -1
- package/dist/expr/parser.js +11 -4
- package/dist/expr/parser.js.map +1 -1
- package/dist/expr/tokenizer.d.ts.map +1 -1
- package/dist/expr/tokenizer.js +17 -13
- package/dist/expr/tokenizer.js.map +1 -1
- package/dist/expr.d.ts +2 -2
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +7 -9
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +30 -9
- package/dist/generated/pricing-config.types.d.ts +14 -3
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/index.d.ts +11 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/integrations/better-auth.d.ts +23 -0
- package/dist/integrations/better-auth.d.ts.map +1 -0
- package/dist/integrations/better-auth.js +54 -0
- package/dist/integrations/better-auth.js.map +1 -0
- package/dist/{load-pricing-file.d.ts → load-config-file.d.ts} +4 -4
- package/dist/load-config-file.d.ts.map +1 -0
- package/dist/{load-pricing-file.js → load-config-file.js} +23 -14
- package/dist/load-config-file.js.map +1 -0
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +3 -3
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +5 -0
- package/dist/providers/_shared.d.ts.map +1 -1
- package/dist/providers/_shared.js +77 -14
- package/dist/providers/_shared.js.map +1 -1
- package/dist/providers/dodo/better-auth.d.ts +29 -0
- package/dist/providers/dodo/better-auth.d.ts.map +1 -0
- package/dist/providers/dodo/better-auth.js +23 -0
- package/dist/providers/dodo/better-auth.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +32 -70
- package/dist/providers/dodo/client-contract.d.ts.map +1 -1
- package/dist/providers/dodo/client-contract.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +3 -1
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +219 -148
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +5 -2
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js +2 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/nextjs.d.ts +16 -0
- package/dist/providers/dodo/nextjs.d.ts.map +1 -0
- package/dist/providers/dodo/nextjs.js +16 -0
- package/dist/providers/dodo/nextjs.js.map +1 -0
- package/dist/providers/dodo/provider.d.ts +30 -11
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +192 -122
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/react-native.d.ts +56 -0
- package/dist/providers/dodo/react-native.d.ts.map +1 -0
- package/dist/providers/dodo/react-native.js +57 -0
- package/dist/providers/dodo/react-native.js.map +1 -0
- package/dist/providers/dodo/webhook-bridge.d.ts +17 -0
- package/dist/providers/dodo/webhook-bridge.d.ts.map +1 -0
- package/dist/providers/dodo/webhook-bridge.js +21 -0
- package/dist/providers/dodo/webhook-bridge.js.map +1 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/index.d.ts +1 -0
- package/dist/providers/mock/index.d.ts.map +1 -1
- package/dist/providers/mock/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +8 -8
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +26 -19
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +214 -173
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/index.d.ts +1 -0
- package/dist/providers/stripe/index.d.ts.map +1 -1
- package/dist/providers/stripe/index.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +13 -11
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +287 -107
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +1 -2
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +26 -4
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +65 -23
- package/dist/retry.js.map +1 -1
- package/dist/shared/diagnostics.d.ts +7 -0
- package/dist/shared/diagnostics.d.ts.map +1 -0
- package/dist/shared/diagnostics.js +18 -0
- package/dist/shared/diagnostics.js.map +1 -0
- package/dist/shared/logger.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +43 -5
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +282 -30
- package/dist/shared/postgres-client.js.map +1 -1
- package/dist/shared/postgres-errors.d.ts +11 -0
- package/dist/shared/postgres-errors.d.ts.map +1 -0
- package/dist/shared/postgres-errors.js +105 -0
- package/dist/shared/postgres-errors.js.map +1 -0
- package/dist/shared/postgres-types.js.map +1 -1
- package/dist/shared/postgres-validation.d.ts +15 -3
- package/dist/shared/postgres-validation.d.ts.map +1 -1
- package/dist/shared/postgres-validation.js +58 -14
- package/dist/shared/postgres-validation.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +18 -3
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +0 -6
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +0 -2
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts +2 -1
- package/dist/storage/outbox-worker.d.ts.map +1 -1
- package/dist/storage/outbox-worker.js +17 -3
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +1 -1
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +1 -1
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +13 -3
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +35 -7
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +166 -53
- package/dist/storage/runtime.js.map +1 -1
- package/package.json +62 -7
- package/dist/credits/postgres/repositories/pricing.d.ts +0 -28
- package/dist/credits/postgres/repositories/pricing.d.ts.map +0 -1
- package/dist/credits/postgres/repositories/pricing.js +0 -70
- package/dist/credits/postgres/repositories/pricing.js.map +0 -1
- package/dist/credits/pricing-runtime.d.ts.map +0 -1
- package/dist/credits/pricing-runtime.js.map +0 -1
- package/dist/load-pricing-file.d.ts.map +0 -1
- package/dist/load-pricing-file.js.map +0 -1
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
import { z } from "zod";
|
|
1
3
|
import { BillingStore } from "../billing-store.js";
|
|
2
|
-
import { PostgresClient } from "../../shared/postgres-client.js";
|
|
4
|
+
import { PostgresClient, } from "../../shared/postgres-client.js";
|
|
5
|
+
import { StoreClosedError, StoreError } from "../../errors.js";
|
|
6
|
+
import { optionalBoundedDiagnosticMessage } from "../../shared/diagnostics.js";
|
|
7
|
+
import { optionalRecordRow, pgBoolean, postgresUuid, requireRecordRow, requireResultField, safeParse, } from "../../shared/postgres-validation.js";
|
|
3
8
|
import { BillingOfferRepository } from "./repositories/offer.js";
|
|
4
9
|
import { BillingTopupRepository } from "./repositories/topup.js";
|
|
5
10
|
import { BillingCustomerRepository } from "./repositories/customer.js";
|
|
6
|
-
import { BillingSubscriptionRepository } from "./repositories/subscription.js";
|
|
11
|
+
import { BillingSubscriptionRepository, } from "./repositories/subscription.js";
|
|
7
12
|
import { BillingEventRepository } from "./repositories/event.js";
|
|
8
13
|
import { BillingPaymentRepository } from "./repositories/payment.js";
|
|
9
14
|
import { BillingRefundRepository } from "./repositories/refund.js";
|
|
@@ -11,17 +16,58 @@ import { BillingInvoiceRepository } from "./repositories/invoice.js";
|
|
|
11
16
|
import { BillingDisputeRepository } from "./repositories/dispute.js";
|
|
12
17
|
import { BillingPreferencesRepository } from "./repositories/preferences.js";
|
|
13
18
|
import { BillingAutoRechargeRepository } from "./repositories/auto-recharge.js";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
import { BillingSubscriptionChangeRepository } from "./repositories/subscription-change.js";
|
|
20
|
+
const PgSafeMinorUnitsSchema = z
|
|
21
|
+
.union([z.string().regex(/^\d+$/), z.number().int().nonnegative().safe()])
|
|
22
|
+
.transform(Number)
|
|
23
|
+
.refine(Number.isSafeInteger, "minor-unit amount exceeds JavaScript's safe integer range");
|
|
24
|
+
const BillingCreditPostingRowSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
ledger_entry_id: postgresUuid.nullable(),
|
|
27
|
+
balance_after: z
|
|
28
|
+
.union([z.string(), z.number(), z.instanceof(Decimal)])
|
|
29
|
+
.nullable()
|
|
30
|
+
.transform((value) => (value === null ? null : new Decimal(value))),
|
|
31
|
+
replayed: pgBoolean,
|
|
32
|
+
error_code: z.string().min(1).nullable(),
|
|
33
|
+
})
|
|
34
|
+
.strict()
|
|
35
|
+
.superRefine((row, ctx) => {
|
|
36
|
+
if (row.error_code === null && (row.ledger_entry_id === null || row.balance_after === null)) {
|
|
37
|
+
ctx.addIssue({
|
|
38
|
+
code: z.ZodIssueCode.custom,
|
|
39
|
+
message: "successful posting requires ledger_entry_id and balance_after",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
const CheckoutIntentRowSchema = z
|
|
44
|
+
.object({
|
|
45
|
+
id: postgresUuid,
|
|
46
|
+
subject_id: postgresUuid,
|
|
47
|
+
provider: z.string().min(1),
|
|
48
|
+
checkout_kind: z.enum(["subscription", "credit_topup"]),
|
|
49
|
+
product_key: z.string().min(1),
|
|
50
|
+
request_digest: z.instanceof(Uint8Array).refine((value) => value.byteLength === 32),
|
|
51
|
+
status: z.enum(["open", "completed", "failed", "expired"]),
|
|
52
|
+
provider_session_id: z.string().min(1).nullable(),
|
|
53
|
+
checkout_url: z.string().min(1).nullable(),
|
|
54
|
+
expires_at: z.union([z.string().datetime({ offset: true }), z.date()]),
|
|
55
|
+
})
|
|
56
|
+
.strict();
|
|
57
|
+
function billingCreditPostingResult(rows, context) {
|
|
58
|
+
const raw = requireRecordRow(rows, context);
|
|
59
|
+
const row = safeParse(BillingCreditPostingRowSchema, {
|
|
60
|
+
ledger_entry_id: raw.ledger_entry_id,
|
|
61
|
+
balance_after: raw.balance_after,
|
|
62
|
+
replayed: raw.replayed,
|
|
63
|
+
error_code: raw.error_code,
|
|
64
|
+
}, context, { indeterminate: true });
|
|
65
|
+
return {
|
|
66
|
+
ledgerEntryId: row.ledger_entry_id,
|
|
67
|
+
balanceAfter: row.balance_after,
|
|
68
|
+
replayed: row.replayed,
|
|
69
|
+
errorCode: row.error_code,
|
|
70
|
+
};
|
|
25
71
|
}
|
|
26
72
|
export class PostgresBillingStore extends BillingStore {
|
|
27
73
|
postgres;
|
|
@@ -36,11 +82,27 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
36
82
|
_dispute = null;
|
|
37
83
|
_preferences = null;
|
|
38
84
|
_autoRecharge = null;
|
|
39
|
-
|
|
85
|
+
_subscriptionChange = null;
|
|
86
|
+
constructor(options) {
|
|
40
87
|
super();
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
88
|
+
if (typeof options !== "object" || options === null) {
|
|
89
|
+
throw new TypeError("PostgresBillingStore options are required");
|
|
90
|
+
}
|
|
91
|
+
if (typeof options.postgres !== "string" && options.poolConstructor !== undefined) {
|
|
92
|
+
throw new TypeError("poolConstructor cannot be used with an existing PostgreSQL pool");
|
|
93
|
+
}
|
|
94
|
+
this.postgres = new PostgresClient(options.postgres, {
|
|
95
|
+
tenantId: options.tenantId,
|
|
96
|
+
poolConstructor: options.poolConstructor,
|
|
97
|
+
billingPayloadBackend: options.billingPayloadBackend,
|
|
98
|
+
connectionTimeoutMs: options.connectionTimeoutMs,
|
|
99
|
+
statementTimeoutMs: options.statementTimeoutMs,
|
|
100
|
+
idleTransactionTimeoutMs: options.idleTransactionTimeoutMs,
|
|
101
|
+
idleTimeoutMs: options.idleTimeoutMs,
|
|
102
|
+
maxConnections: options.maxConnections,
|
|
103
|
+
applicationName: options.applicationName,
|
|
104
|
+
onPoolError: options.onPoolError,
|
|
105
|
+
closedError: () => new StoreClosedError("Billing store has been closed"),
|
|
44
106
|
});
|
|
45
107
|
}
|
|
46
108
|
async close() {
|
|
@@ -104,9 +166,15 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
104
166
|
this._autoRecharge = new BillingAutoRechargeRepository(this.queryFn);
|
|
105
167
|
return this._autoRecharge;
|
|
106
168
|
}
|
|
169
|
+
get billingSubscriptionChange() {
|
|
170
|
+
if (!this._subscriptionChange) {
|
|
171
|
+
this._subscriptionChange = new BillingSubscriptionChangeRepository(this.queryFn);
|
|
172
|
+
}
|
|
173
|
+
return this._subscriptionChange;
|
|
174
|
+
}
|
|
107
175
|
async createOrGetCheckoutIntent(input) {
|
|
108
176
|
if (!/^[0-9a-fA-F]{64}$/.test(input.requestDigest)) {
|
|
109
|
-
throw new
|
|
177
|
+
throw new TypeError("requestDigest must be a 32-byte hex string");
|
|
110
178
|
}
|
|
111
179
|
const rows = await this.queryFn(`SELECT bursar.create_checkout_intent($1::uuid, $2, $3, $4, decode($5, 'hex'), $6::timestamptz) AS id`, [
|
|
112
180
|
input.subjectId,
|
|
@@ -116,55 +184,79 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
116
184
|
input.requestDigest,
|
|
117
185
|
input.expiresAt,
|
|
118
186
|
]);
|
|
119
|
-
const id = rows
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
187
|
+
const id = requireResultField(rows, "id", postgresUuid, "PostgresBillingStore.createOrGetCheckoutIntent");
|
|
188
|
+
const intent = await this.getCheckoutIntent(id, input.subjectId);
|
|
189
|
+
if (!intent) {
|
|
190
|
+
throw new StoreError("checkout intent could not be read after creation", {
|
|
191
|
+
indeterminate: true,
|
|
192
|
+
details: { checkoutIntentId: id },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
125
195
|
return intent;
|
|
126
196
|
}
|
|
127
197
|
async updateCheckoutIntent(id, update) {
|
|
128
198
|
const rows = await this.queryFn(`SELECT bursar.advance_checkout_intent($1::uuid, $2, $3, $4) AS advanced`, [id, update.status ?? null, update.providerSessionId ?? null, update.checkoutUrl ?? null]);
|
|
129
|
-
if (!rows
|
|
130
|
-
throw new
|
|
199
|
+
if (!requireResultField(rows, "advanced", pgBoolean, "PostgresBillingStore.updateCheckoutIntent")) {
|
|
200
|
+
throw new StoreError(`checkout intent transition rejected: ${id}`, {
|
|
201
|
+
details: { checkoutIntentId: id },
|
|
202
|
+
});
|
|
131
203
|
}
|
|
132
204
|
}
|
|
133
205
|
async getCheckoutIntent(id, subjectId) {
|
|
134
206
|
const rows = await this.queryFn(`SELECT * FROM bursar.get_checkout_intent($1::uuid, $2::uuid)`, [id, subjectId]);
|
|
207
|
+
if (rows.length === 0)
|
|
208
|
+
return null;
|
|
209
|
+
if (rows.length !== 1) {
|
|
210
|
+
throw new StoreError("getCheckoutIntent: expected at most one row", {
|
|
211
|
+
details: { rowCount: rows.length, checkoutIntentId: id },
|
|
212
|
+
});
|
|
213
|
+
}
|
|
135
214
|
const row = rows[0];
|
|
136
|
-
return
|
|
215
|
+
return this.rowToCheckoutIntent(safeParse(CheckoutIntentRowSchema, {
|
|
216
|
+
id: row.id,
|
|
217
|
+
subject_id: row.subject_id,
|
|
218
|
+
provider: row.provider,
|
|
219
|
+
checkout_kind: row.checkout_kind,
|
|
220
|
+
product_key: row.product_key,
|
|
221
|
+
request_digest: row.request_digest,
|
|
222
|
+
status: row.status,
|
|
223
|
+
provider_session_id: row.provider_session_id,
|
|
224
|
+
checkout_url: row.checkout_url,
|
|
225
|
+
expires_at: row.expires_at,
|
|
226
|
+
}, "PostgresBillingStore.getCheckoutIntent"));
|
|
137
227
|
}
|
|
138
228
|
rowToCheckoutIntent(row) {
|
|
139
229
|
return {
|
|
140
|
-
id:
|
|
141
|
-
subjectId:
|
|
142
|
-
provider:
|
|
230
|
+
id: row.id,
|
|
231
|
+
subjectId: row.subject_id,
|
|
232
|
+
provider: row.provider,
|
|
143
233
|
checkoutKind: row.checkout_kind,
|
|
144
|
-
productKey:
|
|
234
|
+
productKey: row.product_key,
|
|
145
235
|
requestDigest: Buffer.from(row.request_digest).toString("hex"),
|
|
146
236
|
status: row.status,
|
|
147
|
-
providerSessionId: row.provider_session_id
|
|
148
|
-
checkoutUrl: row.checkout_url
|
|
149
|
-
expiresAt:
|
|
237
|
+
providerSessionId: row.provider_session_id,
|
|
238
|
+
checkoutUrl: row.checkout_url,
|
|
239
|
+
expiresAt: row.expires_at instanceof Date ? row.expires_at.toISOString() : row.expires_at,
|
|
150
240
|
};
|
|
151
241
|
}
|
|
152
242
|
rowToOffer(row) {
|
|
153
|
-
if (!row
|
|
243
|
+
if (!row)
|
|
154
244
|
return null;
|
|
155
245
|
return {
|
|
156
|
-
offerId:
|
|
157
|
-
offerKey:
|
|
158
|
-
planId: row.
|
|
159
|
-
plan: row.plan
|
|
160
|
-
interval: row.interval
|
|
161
|
-
intervalCount:
|
|
162
|
-
grant:
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
246
|
+
offerId: row.id,
|
|
247
|
+
offerKey: row.offerKey,
|
|
248
|
+
planId: row.planId,
|
|
249
|
+
plan: row.plan,
|
|
250
|
+
interval: row.interval,
|
|
251
|
+
intervalCount: row.intervalCount,
|
|
252
|
+
grant: row.grantCredits === null
|
|
253
|
+
? null
|
|
254
|
+
: {
|
|
255
|
+
mode: "cycle_grant",
|
|
256
|
+
credits: row.grantCredits,
|
|
257
|
+
bucket: row.grantBucket,
|
|
258
|
+
replacePrior: row.grantReplacePrior,
|
|
259
|
+
},
|
|
168
260
|
};
|
|
169
261
|
}
|
|
170
262
|
async resolveBillingOffer(provider, productId, priceId) {
|
|
@@ -177,28 +269,36 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
177
269
|
}
|
|
178
270
|
async claimBillingEvent(provider, eventId, eventType, envelope) {
|
|
179
271
|
const result = await this.billingEvent.claim(provider, eventId, eventType, JSON.stringify(envelope ?? { eventType }));
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
272
|
+
if (result.status === "claimed") {
|
|
273
|
+
if (result.claim_token === null || result.event_id === null) {
|
|
274
|
+
throw new StoreError("Billing event claim returned no claim identifiers", {
|
|
275
|
+
details: { provider, eventId },
|
|
276
|
+
});
|
|
277
|
+
}
|
|
185
278
|
return {
|
|
186
279
|
status: "claimed",
|
|
187
|
-
claimToken:
|
|
188
|
-
billingEventId:
|
|
280
|
+
claimToken: result.claim_token,
|
|
281
|
+
billingEventId: result.event_id,
|
|
189
282
|
};
|
|
190
283
|
}
|
|
191
|
-
if (
|
|
284
|
+
if (result.status === "duplicate")
|
|
192
285
|
return { status: "duplicate" };
|
|
193
|
-
if (
|
|
286
|
+
if (result.status === "busy")
|
|
194
287
|
return { status: "busy" };
|
|
195
|
-
|
|
288
|
+
if (result.status === "invalid_request" ||
|
|
289
|
+
result.status === "idempotency_conflict" ||
|
|
290
|
+
result.status === "max_retries_exceeded") {
|
|
291
|
+
return { status: "retry" };
|
|
292
|
+
}
|
|
293
|
+
throw new StoreError("Billing event claim returned an unsupported status", {
|
|
294
|
+
details: { provider, eventId, status: result.status },
|
|
295
|
+
});
|
|
196
296
|
}
|
|
197
297
|
async completeBillingEvent(provider, eventId, claimToken) {
|
|
198
|
-
|
|
298
|
+
return this.billingEvent.complete(provider, eventId, claimToken);
|
|
199
299
|
}
|
|
200
300
|
async failBillingEvent(provider, eventId, claimToken, error) {
|
|
201
|
-
|
|
301
|
+
return this.billingEvent.fail(provider, eventId, claimToken, optionalBoundedDiagnosticMessage(error) ?? undefined);
|
|
202
302
|
}
|
|
203
303
|
async upsertBillingCustomer(provider, providerCustomerId, userId, email) {
|
|
204
304
|
await this.billingCustomer.upsert(provider, providerCustomerId, userId, email ?? null);
|
|
@@ -217,31 +317,19 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
217
317
|
}
|
|
218
318
|
async createBillingSubscriptionChange(input) {
|
|
219
319
|
const subscription = await this.billingSubscription.get(input.provider, input.providerSubscriptionId);
|
|
220
|
-
if (!subscription?.id)
|
|
221
|
-
throw new
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
input.effective,
|
|
229
|
-
input.idempotencyKey,
|
|
230
|
-
input.prorationBehavior ?? "provider_default",
|
|
231
|
-
]);
|
|
232
|
-
const result = rows[0] ?? {};
|
|
233
|
-
if (result.error_code)
|
|
234
|
-
throw new Error(`subscription change: ${String(result.error_code)}`);
|
|
235
|
-
const changeRows = await this.queryFn(`SELECT * FROM bursar.get_billing_subscription_change($1::bigint)`, [result.change_id]);
|
|
236
|
-
if (changeRows[0]?.id == null) {
|
|
237
|
-
throw new Error("subscription change creation returned no row");
|
|
320
|
+
if (!subscription?.id) {
|
|
321
|
+
throw new StoreError("subscription change requires a persisted subscription", {
|
|
322
|
+
retryable: true,
|
|
323
|
+
details: {
|
|
324
|
+
provider: input.provider,
|
|
325
|
+
providerSubscriptionId: input.providerSubscriptionId,
|
|
326
|
+
},
|
|
327
|
+
});
|
|
238
328
|
}
|
|
239
|
-
return this.
|
|
329
|
+
return this.billingSubscriptionChange.create(subscription.id, input);
|
|
240
330
|
}
|
|
241
331
|
async getOpenBillingSubscriptionChange(provider, providerSubscriptionId) {
|
|
242
|
-
|
|
243
|
-
const row = rows[0];
|
|
244
|
-
return row?.id == null ? null : this.rowToSubscriptionChange(row);
|
|
332
|
+
return this.billingSubscriptionChange.getOpen(provider, providerSubscriptionId);
|
|
245
333
|
}
|
|
246
334
|
async listExpiredGraceSubscriptions(now, limit = 100) {
|
|
247
335
|
const rows = await this.billingSubscription.listExpiredGraceSubscriptions(now, limit);
|
|
@@ -251,12 +339,7 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
251
339
|
return this.billingSubscription.markGraceExpired(subscriptionId, expectedGraceEndsAt, expiredAt);
|
|
252
340
|
}
|
|
253
341
|
async updateBillingSubscriptionChange(id, update) {
|
|
254
|
-
|
|
255
|
-
return;
|
|
256
|
-
const rows = await this.queryFn(`SELECT bursar.advance_subscription_change($1::bigint, $2, $3, $4) AS advanced`, [id, update.state, update.providerOperationId ?? null, update.errorMessage ?? null]);
|
|
257
|
-
if (!rows[0]?.advanced) {
|
|
258
|
-
throw new Error(`subscription change transition rejected: ${id}`);
|
|
259
|
-
}
|
|
342
|
+
await this.billingSubscriptionChange.update(id, update);
|
|
260
343
|
}
|
|
261
344
|
async getUserSubscription(userId, statuses) {
|
|
262
345
|
const r = await this.billingSubscription.getUserSubscription(userId, statuses);
|
|
@@ -275,20 +358,22 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
275
358
|
return this.billingSubscription.selectEntitlementSource(userId, provider, providerSubscriptionId);
|
|
276
359
|
}
|
|
277
360
|
rowToTopup(r) {
|
|
278
|
-
if (!r
|
|
361
|
+
if (!r)
|
|
279
362
|
return null;
|
|
363
|
+
const minAmountMinor = safeParse(PgSafeMinorUnitsSchema, r.amount_minor * r.min_quantity, "PostgresBillingStore.rowToTopup.minAmountMinor");
|
|
364
|
+
const maxAmountMinor = safeParse(PgSafeMinorUnitsSchema, r.amount_minor * r.max_quantity, "PostgresBillingStore.rowToTopup.maxAmountMinor");
|
|
280
365
|
return {
|
|
281
|
-
topupId:
|
|
366
|
+
topupId: r.id,
|
|
282
367
|
topupKey: r.topup_key,
|
|
283
|
-
creditsPerUnit:
|
|
284
|
-
depositTo: r.bucket_key
|
|
285
|
-
amountMinor: r.amount_minor
|
|
286
|
-
currency: r.currency
|
|
287
|
-
minQuantity: r.min_quantity
|
|
288
|
-
maxQuantity: r.max_quantity
|
|
289
|
-
defaultQuantity: r.default_quantity
|
|
290
|
-
minAmountMinor
|
|
291
|
-
maxAmountMinor
|
|
368
|
+
creditsPerUnit: r.credits_per_unit,
|
|
369
|
+
depositTo: r.bucket_key,
|
|
370
|
+
amountMinor: r.amount_minor,
|
|
371
|
+
currency: r.currency,
|
|
372
|
+
minQuantity: r.min_quantity,
|
|
373
|
+
maxQuantity: r.max_quantity,
|
|
374
|
+
defaultQuantity: r.default_quantity,
|
|
375
|
+
minAmountMinor,
|
|
376
|
+
maxAmountMinor,
|
|
292
377
|
};
|
|
293
378
|
}
|
|
294
379
|
async resolveCreditTopup(provider, productId, priceId) {
|
|
@@ -300,201 +385,153 @@ export class PostgresBillingStore extends BillingStore {
|
|
|
300
385
|
return this.rowToTopup(r);
|
|
301
386
|
}
|
|
302
387
|
async computeTopupCredits(amountMinor, topupConfig) {
|
|
388
|
+
if (!Number.isSafeInteger(amountMinor) || amountMinor < 0) {
|
|
389
|
+
throw new TypeError("amountMinor must be a non-negative safe integer");
|
|
390
|
+
}
|
|
303
391
|
const unitAmount = topupConfig.amountMinor;
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
return 0;
|
|
392
|
+
if (unitAmount <= 0 || amountMinor % unitAmount !== 0)
|
|
393
|
+
return new Decimal(0);
|
|
307
394
|
const quantity = amountMinor / unitAmount;
|
|
308
|
-
if (quantity <
|
|
309
|
-
return 0;
|
|
395
|
+
if (quantity < topupConfig.minQuantity || quantity > topupConfig.maxQuantity) {
|
|
396
|
+
return new Decimal(0);
|
|
310
397
|
}
|
|
311
|
-
return quantity
|
|
398
|
+
return topupConfig.creditsPerUnit.mul(quantity);
|
|
312
399
|
}
|
|
313
400
|
async upsertBillingPayment(options) {
|
|
314
|
-
return this.billingPayment.upsert(options.provider, options.providerPaymentId, options.providerInvoiceId ?? null, options.userId
|
|
401
|
+
return this.billingPayment.upsert(options.provider, options.providerPaymentId, options.providerInvoiceId ?? null, options.userId, options.amountMinor, options.taxMinor, options.currency, options.purpose, options.metadata ? JSON.stringify(options.metadata) : null, options.status, options.providerUpdatedAt);
|
|
315
402
|
}
|
|
316
403
|
async createBillingCreditGrant(input) {
|
|
317
404
|
const rows = await this.queryFn("SELECT bursar.create_billing_credit_grant($1::uuid, $2::uuid, $3::uuid, $4, $5, $6::uuid) AS id", [
|
|
318
405
|
input.paymentId ?? null,
|
|
319
406
|
input.subscriptionId ?? null,
|
|
320
407
|
input.topupId ?? null,
|
|
321
|
-
input.configuredCredits,
|
|
322
|
-
input.quantity
|
|
408
|
+
input.configuredCredits.toString(),
|
|
409
|
+
input.quantity,
|
|
323
410
|
input.billingEventId ?? null,
|
|
324
411
|
]);
|
|
325
|
-
|
|
326
|
-
if (!id)
|
|
327
|
-
throw new Error("billing credit grant creation returned no ID");
|
|
328
|
-
return String(id);
|
|
412
|
+
return requireResultField(rows, "id", postgresUuid, "PostgresBillingStore.createBillingCreditGrant");
|
|
329
413
|
}
|
|
330
414
|
async grantBillingCredit(grantId, idempotencyKey) {
|
|
331
415
|
const rows = await this.queryFn("SELECT * FROM bursar.grant_billing_credit($1::uuid, $2)", [
|
|
332
416
|
grantId,
|
|
333
417
|
idempotencyKey,
|
|
334
418
|
]);
|
|
335
|
-
return rows
|
|
419
|
+
return billingCreditPostingResult(rows, "PostgresBillingStore.grantBillingCredit");
|
|
336
420
|
}
|
|
337
421
|
async getBillingCreditGrantByPayment(paymentId) {
|
|
338
422
|
const rows = await this.queryFn(`SELECT * FROM bursar.get_billing_credit_grant_by_payment($1::uuid)`, [paymentId]);
|
|
339
|
-
const
|
|
340
|
-
return
|
|
423
|
+
const row = optionalRecordRow(rows, "PostgresBillingStore.getBillingCreditGrantByPayment");
|
|
424
|
+
return row === null
|
|
425
|
+
? null
|
|
426
|
+
: safeParse(postgresUuid, row.id, "PostgresBillingStore.getBillingCreditGrantByPayment.id");
|
|
341
427
|
}
|
|
342
428
|
async postBillingRefund(refundId, grantId, amountMinor, idempotencyKey) {
|
|
343
429
|
const rows = await this.queryFn("SELECT * FROM bursar.post_billing_refund($1::uuid, $2::uuid, $3, $4)", [refundId, grantId, amountMinor, idempotencyKey]);
|
|
344
|
-
return rows
|
|
430
|
+
return billingCreditPostingResult(rows, "PostgresBillingStore.postBillingRefund");
|
|
345
431
|
}
|
|
346
432
|
async upsertBillingRefund(options) {
|
|
347
|
-
return this.billingRefund.upsert(options.provider, options.providerRefundId, options.providerPaymentId
|
|
433
|
+
return this.billingRefund.upsert(options.provider, options.providerRefundId, options.providerPaymentId, options.userId, options.amountMinor, options.currency, options.reason ?? null, options.metadata ? JSON.stringify(options.metadata) : null, options.status, options.providerUpdatedAt);
|
|
348
434
|
}
|
|
349
435
|
async upsertBillingInvoice(options) {
|
|
350
436
|
const subscription = options.providerSubscriptionId
|
|
351
437
|
? await this.billingSubscription.get(options.provider, options.providerSubscriptionId)
|
|
352
438
|
: null;
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
439
|
+
if (options.providerSubscriptionId && !subscription) {
|
|
440
|
+
throw new StoreError("invoice subscription is not available", {
|
|
441
|
+
retryable: true,
|
|
442
|
+
details: {
|
|
443
|
+
provider: options.provider,
|
|
444
|
+
providerInvoiceId: options.providerInvoiceId,
|
|
445
|
+
providerSubscriptionId: options.providerSubscriptionId ?? null,
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
await this.billingInvoice.upsert(options.userId, options.provider, options.providerInvoiceId, subscription?.id ? String(subscription.id) : null, options.status, options.amountDueMinor, options.amountPaidMinor, options.currency, options.periodStart ?? null, options.periodEnd ?? null, options.metadata ?? {}, options.providerUpdatedAt);
|
|
357
450
|
}
|
|
358
451
|
async listBillingInvoices(userId) {
|
|
359
452
|
return this.billingInvoice.listForUser(userId);
|
|
360
453
|
}
|
|
361
454
|
async upsertBillingDispute(options) {
|
|
362
|
-
const payment = options.providerPaymentId
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
455
|
+
const payment = await this.billingPayment.getForRefund(options.provider, options.providerPaymentId);
|
|
456
|
+
if (!payment?.id) {
|
|
457
|
+
throw new StoreError("dispute payment is required", {
|
|
458
|
+
retryable: true,
|
|
459
|
+
details: {
|
|
460
|
+
provider: options.provider,
|
|
461
|
+
providerDisputeId: options.providerDisputeId,
|
|
462
|
+
providerPaymentId: options.providerPaymentId ?? null,
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
await this.billingDispute.upsert(options.provider, options.providerDisputeId, String(payment.id), options.status, options.reason ?? null, options.metadata ?? {}, options.providerUpdatedAt);
|
|
368
467
|
}
|
|
369
468
|
async getBillingPayment(provider, providerPaymentId) {
|
|
370
|
-
const
|
|
371
|
-
if (!
|
|
469
|
+
const row = await this.billingPayment.getForRefund(provider, providerPaymentId);
|
|
470
|
+
if (!row)
|
|
372
471
|
return null;
|
|
373
|
-
const row = result;
|
|
374
472
|
return {
|
|
375
473
|
id: row.id,
|
|
376
474
|
provider: row.provider,
|
|
377
475
|
providerPaymentId: row.provider_payment_id,
|
|
378
476
|
providerInvoiceId: row.provider_invoice_id,
|
|
379
477
|
userId: row.subject_id,
|
|
380
|
-
amountMinor: row.amount_minor
|
|
381
|
-
taxMinor: row.tax_minor
|
|
478
|
+
amountMinor: safeParse(PgSafeMinorUnitsSchema, row.amount_minor, "PostgresBillingStore.getBillingPayment.amountMinor"),
|
|
479
|
+
taxMinor: safeParse(PgSafeMinorUnitsSchema, row.tax_minor, "PostgresBillingStore.getBillingPayment.taxMinor"),
|
|
382
480
|
currency: row.currency,
|
|
383
481
|
purpose: row.purpose,
|
|
384
482
|
status: row.status,
|
|
385
|
-
providerUpdatedAt:
|
|
483
|
+
providerUpdatedAt: row.provider_updated_at instanceof Date
|
|
484
|
+
? row.provider_updated_at.toISOString()
|
|
485
|
+
: row.provider_updated_at,
|
|
386
486
|
metadata: row.metadata,
|
|
387
487
|
};
|
|
388
488
|
}
|
|
389
489
|
rowToSubscriptionState(r) {
|
|
390
490
|
return {
|
|
391
|
-
subscriptionId: r.id
|
|
392
|
-
userId:
|
|
393
|
-
provider:
|
|
394
|
-
providerSubscriptionId:
|
|
395
|
-
providerCustomerId: r.provider_customer_id
|
|
396
|
-
offerId: r.offer_id
|
|
397
|
-
offerKey: r.offer_key
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
: null,
|
|
491
|
+
subscriptionId: r.id,
|
|
492
|
+
userId: r.user_id,
|
|
493
|
+
provider: r.provider,
|
|
494
|
+
providerSubscriptionId: r.provider_subscription_id,
|
|
495
|
+
providerCustomerId: r.provider_customer_id,
|
|
496
|
+
offerId: r.offer_id,
|
|
497
|
+
offerKey: r.offer_key,
|
|
498
|
+
planId: r.plan_id,
|
|
499
|
+
plan: r.plan,
|
|
500
|
+
status: r.status,
|
|
501
|
+
currentPeriodStart: r.current_period_start,
|
|
502
|
+
currentPeriodEnd: r.current_period_end,
|
|
503
|
+
trialEnd: r.trial_end,
|
|
504
|
+
cancelAt: r.cancel_at,
|
|
505
|
+
endedAt: r.ended_at,
|
|
506
|
+
graceEndsAt: r.grace_ends_at,
|
|
507
|
+
graceExpiredAt: r.grace_expired_at,
|
|
508
|
+
providerUpdatedAt: r.provider_updated_at,
|
|
509
|
+
cancelAtPeriodEnd: r.cancel_at_period_end,
|
|
510
|
+
interval: r.interval,
|
|
511
|
+
intervalCount: r.interval_count,
|
|
512
|
+
metadata: r.metadata,
|
|
414
513
|
};
|
|
415
514
|
}
|
|
416
|
-
async
|
|
417
|
-
const rows = await this.queryFn(`SELECT requested.side, requested.offer_id, context.*
|
|
418
|
-
FROM (
|
|
419
|
-
VALUES
|
|
420
|
-
('from', $1::uuid, $2::uuid),
|
|
421
|
-
('to', $3::uuid, $4::uuid)
|
|
422
|
-
) AS requested(side, offer_id, catalog_revision_id)
|
|
423
|
-
CROSS JOIN LATERAL bursar.get_catalog_offer_context(
|
|
424
|
-
requested.offer_id,
|
|
425
|
-
requested.catalog_revision_id
|
|
426
|
-
) AS context`, [r.from_offer_id, r.from_catalog_revision_id, r.to_offer_id, r.to_catalog_revision_id]);
|
|
427
|
-
const bySide = new Map(rows.map((row) => {
|
|
428
|
-
const context = row;
|
|
429
|
-
return [String(context.side), context];
|
|
430
|
-
}));
|
|
431
|
-
const mapContext = (side) => {
|
|
432
|
-
const context = bySide.get(side);
|
|
433
|
-
if (context?.offer_key == null) {
|
|
434
|
-
throw new Error(`subscription change ${side}-offer context not found`);
|
|
435
|
-
}
|
|
436
|
-
return {
|
|
437
|
-
offerId: String(context.offer_id),
|
|
438
|
-
offerKey: String(context.offer_key),
|
|
439
|
-
planId: context.plan_id ? String(context.plan_id) : null,
|
|
440
|
-
plan: context.plan_key ? String(context.plan_key) : null,
|
|
441
|
-
interval: context.billing_unit ? String(context.billing_unit) : null,
|
|
442
|
-
intervalCount: context.billing_count == null ? null : Number(context.billing_count),
|
|
443
|
-
};
|
|
444
|
-
};
|
|
445
|
-
return { fromOffer: mapContext("from"), toOffer: mapContext("to") };
|
|
446
|
-
}
|
|
447
|
-
async rowToSubscriptionChange(r) {
|
|
448
|
-
const { fromOffer, toOffer } = await this.getSubscriptionOfferContexts(r);
|
|
449
|
-
return {
|
|
450
|
-
id: String(r.id),
|
|
451
|
-
subscriptionId: String(r.subscription_id),
|
|
452
|
-
fromOfferId: String(r.from_offer_id),
|
|
453
|
-
toOfferId: String(r.to_offer_id),
|
|
454
|
-
fromOffer,
|
|
455
|
-
toOffer,
|
|
456
|
-
effectiveAt: toIso(r.effective_at),
|
|
457
|
-
effective: String(r.effective_behavior),
|
|
458
|
-
state: String(r.state),
|
|
459
|
-
prorationBehavior: String(r.proration_behavior),
|
|
460
|
-
idempotencyKey: String(r.idempotency_key),
|
|
461
|
-
providerOperationId: r.provider_operation_id ? String(r.provider_operation_id) : null,
|
|
462
|
-
errorMessage: r.error_message ? String(r.error_message) : null,
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
async getActiveBursarConfig() {
|
|
515
|
+
async getActiveCatalogDocument() {
|
|
466
516
|
const rows = await this.queryFn("SELECT * FROM bursar.active_catalog_revision()", []);
|
|
467
|
-
if (
|
|
517
|
+
if (rows.length === 0)
|
|
468
518
|
return null;
|
|
469
|
-
|
|
470
|
-
|
|
519
|
+
if (rows.length !== 1 || typeof rows[0] !== "object" || rows[0] === null) {
|
|
520
|
+
throw new StoreError("active catalog revision returned a malformed row", {
|
|
521
|
+
details: { rowCount: rows.length },
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
return safeParse(z.record(z.string(), z.unknown()), rows[0].source_document, "PostgresBillingStore.getActiveCatalogDocument");
|
|
471
525
|
}
|
|
472
526
|
async pseudonymizeFinancialSubject(userId) {
|
|
473
527
|
const rows = await this.queryFn(`SELECT bursar.pseudonymize_financial_subject($1::uuid) AS pseudonymized`, [userId]);
|
|
474
|
-
return rows
|
|
528
|
+
return requireResultField(rows, "pseudonymized", pgBoolean, "PostgresBillingStore.pseudonymizeFinancialSubject");
|
|
475
529
|
}
|
|
476
530
|
async getBillingPreferences(userId) {
|
|
477
|
-
|
|
478
|
-
if (!row || row.subject_id == null)
|
|
479
|
-
return null;
|
|
480
|
-
return {
|
|
481
|
-
userId: String(row.subject_id),
|
|
482
|
-
autoRecharge: Boolean(row.auto_recharge),
|
|
483
|
-
overageProtection: Boolean(row.overage_protection),
|
|
484
|
-
emailNotifications: Boolean(row.email_notifications),
|
|
485
|
-
usageAlerts: Boolean(row.usage_alerts),
|
|
486
|
-
invoiceReminders: Boolean(row.invoice_reminders),
|
|
487
|
-
};
|
|
531
|
+
return this.billingPreferences.get(userId);
|
|
488
532
|
}
|
|
489
533
|
async upsertBillingPreferences(prefs) {
|
|
490
|
-
await this.billingPreferences.upsert(
|
|
491
|
-
userId: prefs.userId,
|
|
492
|
-
autoRecharge: prefs.autoRecharge,
|
|
493
|
-
overageProtection: prefs.overageProtection,
|
|
494
|
-
emailNotifications: prefs.emailNotifications,
|
|
495
|
-
usageAlerts: prefs.usageAlerts,
|
|
496
|
-
invoiceReminders: prefs.invoiceReminders,
|
|
497
|
-
});
|
|
534
|
+
await this.billingPreferences.upsert(prefs);
|
|
498
535
|
}
|
|
499
536
|
async getAutoRechargeProfile(userId) {
|
|
500
537
|
return this.billingAutoRecharge.getProfile(userId);
|