@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
package/README.md
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
1
|
# Bursar for JavaScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@zonastery/bursar)
|
|
4
|
+
[](https://www.npmjs.com/package/@zonastery/bursar)
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
<img
|
|
8
|
+
src="https://raw.githubusercontent.com/zonastery/bursar/main/docs/static/img/logo.png"
|
|
9
|
+
alt="Bursar logo"
|
|
10
|
+
width="192"
|
|
11
|
+
height="192"
|
|
12
|
+
/>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
The TypeScript SDK for [Bursar](https://github.com/Zonastery/bursar) — a
|
|
16
|
+
behavioral mirror of the Python SDK. It meters usage, prices operations, and
|
|
17
|
+
manages balances against the shared canonical PostgreSQL schema and the same
|
|
18
|
+
versioned configuration document, with identical rounding. `@zonastery/bursar`
|
|
19
|
+
is ESM-only and requires Node.js 22 or newer.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
4
22
|
|
|
5
|
-
|
|
6
|
-
|
|
23
|
+
```bash
|
|
24
|
+
npm install @zonastery/bursar
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Optional peer dependencies: `pg` (PostgresStore), `stripe` and `dodopayments`
|
|
28
|
+
(provider billing), `js-yaml` (YAML config loading), plus framework adapters
|
|
29
|
+
such as `@dodopayments/nextjs`, `@dodopayments/better-auth`, `next`, and
|
|
30
|
+
`better-auth`, and `@dodopayments/react-native-checkout`. No peer is required
|
|
31
|
+
for core credits.
|
|
32
|
+
|
|
33
|
+
## Database setup
|
|
34
|
+
|
|
35
|
+
Database installation belongs to the SQL baseline, not to the SDK. Apply it
|
|
36
|
+
with the Python migration CLI:
|
|
7
37
|
|
|
8
38
|
```bash
|
|
9
|
-
|
|
39
|
+
pip install bursar[postgres]
|
|
40
|
+
export DATABASE_URL=postgresql://...
|
|
41
|
+
bursar migrate
|
|
10
42
|
```
|
|
11
43
|
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
12
46
|
```ts
|
|
13
47
|
import { Bursar, PostgresStore } from "@zonastery/bursar";
|
|
14
48
|
|
|
15
|
-
const
|
|
16
|
-
|
|
49
|
+
const store = new PostgresStore({
|
|
50
|
+
postgres: process.env.DATABASE_URL!,
|
|
51
|
+
tenantId,
|
|
52
|
+
connectionTimeoutMs: 10_000,
|
|
53
|
+
statementTimeoutMs: 30_000,
|
|
54
|
+
onPoolError: (error) => console.error("Bursar PostgreSQL pool error", error),
|
|
55
|
+
});
|
|
17
56
|
const bursar = new Bursar({ creditStore: store });
|
|
18
57
|
|
|
19
58
|
const grant = await bursar.credits.addCredits(userId, 500, {
|
|
@@ -34,83 +73,171 @@ while (page.nextCursor) {
|
|
|
34
73
|
}
|
|
35
74
|
```
|
|
36
75
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
76
|
+
`LedgerEntry`, `LedgerCursor`, and `LedgerPage` are the public history
|
|
77
|
+
contract; pagination is cursor-only. Publish one versioned configuration
|
|
78
|
+
document through the facade — billing and auto-recharge read the same active
|
|
79
|
+
document:
|
|
40
80
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
that active configuration; there is no separate billing configuration.
|
|
81
|
+
```ts
|
|
82
|
+
await bursar.catalog.publishAndActivate(config);
|
|
83
|
+
```
|
|
45
84
|
|
|
46
|
-
|
|
47
|
-
applications. Database installation is deliberately not a store or `Bursar`
|
|
48
|
-
method.
|
|
85
|
+
## Framework integrations
|
|
49
86
|
|
|
50
|
-
|
|
87
|
+
Framework integrations are optional subpaths; the credits, metering, and
|
|
88
|
+
billing core does not import them. For Next.js, Dodo's official adapter verifies
|
|
89
|
+
and validates the request before Bursar maps the event:
|
|
51
90
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
91
|
+
```ts
|
|
92
|
+
// app/api/dodo/webhook/route.ts
|
|
93
|
+
import { createDodoNextWebhookHandler } from "@zonastery/bursar/providers/dodo/nextjs";
|
|
94
|
+
|
|
95
|
+
export const POST = createDodoNextWebhookHandler({
|
|
96
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
|
|
97
|
+
getEventSink: () => bursar.requireBilling(),
|
|
98
|
+
resolveUser: async ({ customerId, email, metadata }) => {
|
|
99
|
+
return metadata.userId ?? lookupAccount(customerId, email);
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
```
|
|
55
103
|
|
|
56
|
-
|
|
57
|
-
|
|
104
|
+
React Native apps can compose a Bursar-created checkout intent with Dodo's
|
|
105
|
+
official native SDK. The app receives the checkout URL and persists only the
|
|
106
|
+
Bursar intent needed for recovery; API keys and payment confirmation remain on
|
|
107
|
+
the backend:
|
|
58
108
|
|
|
59
109
|
```ts
|
|
60
|
-
import {
|
|
110
|
+
import { createDodoReactNativeCheckout } from "@zonastery/bursar/providers/dodo/react-native";
|
|
61
111
|
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
112
|
+
const checkout = createDodoReactNativeCheckout({
|
|
113
|
+
returnUrl: "myappcheckout://return",
|
|
114
|
+
store: pendingCheckoutStore,
|
|
115
|
+
getCheckoutStatus: async (intentId) => (await api.checkoutStatus(intentId)).status,
|
|
65
116
|
});
|
|
66
|
-
await runtime.start();
|
|
67
117
|
|
|
68
|
-
const
|
|
118
|
+
const session = await api.createCheckout(input);
|
|
119
|
+
const result = await checkout.start(session); // UI hint only
|
|
120
|
+
await checkout.reconcile(); // authoritative Bursar status
|
|
69
121
|
```
|
|
70
122
|
|
|
71
|
-
|
|
72
|
-
|
|
123
|
+
Install `@dodopayments/react-native-checkout` directly in the native app, use
|
|
124
|
+
its Expo/native callback-scheme configuration, and forward React Native
|
|
125
|
+
`Linking` events to `checkout.handleOpenURL`. The adapter also exposes Dodo's
|
|
126
|
+
abandoned-session recovery without moving provider secrets into the app.
|
|
127
|
+
|
|
128
|
+
Better Auth users can compose Bursar with Dodo's official plugin. The Bursar
|
|
129
|
+
plugin provisions accounts and adopts the `dodoCustomerId` maintained by the
|
|
130
|
+
official adapter, avoiding two customer owners:
|
|
73
131
|
|
|
74
132
|
```ts
|
|
75
|
-
import {
|
|
76
|
-
import {
|
|
133
|
+
import { dodopayments, portal } from "@dodopayments/better-auth";
|
|
134
|
+
import { bursarBetterAuth } from "@zonastery/bursar/integrations/better-auth";
|
|
135
|
+
import {
|
|
136
|
+
dodoBetterAuthCustomer,
|
|
137
|
+
dodoBetterAuthWebhooks,
|
|
138
|
+
} from "@zonastery/bursar/providers/dodo/better-auth";
|
|
139
|
+
|
|
140
|
+
plugins: [
|
|
141
|
+
dodopayments({
|
|
142
|
+
client: dodoClient,
|
|
143
|
+
createCustomerOnSignUp: true,
|
|
144
|
+
use: [
|
|
145
|
+
portal(),
|
|
146
|
+
dodoBetterAuthWebhooks({
|
|
147
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
|
|
148
|
+
getEventSink: () => bursar.requireBilling(),
|
|
149
|
+
}),
|
|
150
|
+
],
|
|
151
|
+
}),
|
|
152
|
+
bursarBetterAuth({
|
|
153
|
+
getBursar: () => bursar,
|
|
154
|
+
resolveProviderCustomer: dodoBetterAuthCustomer,
|
|
155
|
+
}),
|
|
156
|
+
];
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Run Better Auth's schema generator after enabling the official Dodo plugin; it
|
|
160
|
+
adds the optional `dodoCustomerId` user field. Checkout and portal operations
|
|
161
|
+
may still call `bursar.requireCommerce()` when the application needs Bursar's
|
|
162
|
+
checkout intents, provider selection, or idempotency guarantees.
|
|
163
|
+
|
|
164
|
+
## Errors, deadlines, and retries
|
|
77
165
|
|
|
78
|
-
|
|
166
|
+
All Bursar-classified failures extend `BursarError` and carry stable `code`,
|
|
167
|
+
`category`, and `retryable` fields. Native `cause` chains retain the underlying
|
|
168
|
+
driver error without exposing it from
|
|
169
|
+
`bursarErrorPublicMessage()` or the serialized `toJSON()` shape. PostgreSQL
|
|
170
|
+
failures are classified primarily from SQLSTATE and network error codes, with
|
|
171
|
+
narrow fallbacks for the stable timeout messages emitted by `pg` itself.
|
|
172
|
+
|
|
173
|
+
`PostgresStore` and `PostgresBillingStore` use a 10-second connection deadline,
|
|
174
|
+
a 30-second server-side statement deadline, and a 30-second idle-transaction
|
|
175
|
+
deadline by default. All are configurable. An SDK-owned pool also installs an
|
|
176
|
+
idle-client error listener so a network partition cannot become an uncaught
|
|
177
|
+
EventEmitter error.
|
|
178
|
+
|
|
179
|
+
Use `retryBursarOperation()` only for reads or idempotent mutations. It uses
|
|
180
|
+
bounded exponential backoff with jitter, an elapsed-time budget, and optional
|
|
181
|
+
`AbortSignal` cancellation:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { isBursarError, retryBursarOperation } from "@zonastery/bursar";
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const balance = await retryBursarOperation(() => bursar.credits.getBalance(userId), {
|
|
188
|
+
maxAttempts: 3,
|
|
189
|
+
signal: request.signal,
|
|
190
|
+
});
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (isBursarError(error)) {
|
|
193
|
+
console.error("Bursar request failed", error.toJSON());
|
|
194
|
+
}
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`StoreError.indeterminate` means a transport failure occurred when a mutation
|
|
200
|
+
may have reached PostgreSQL. Retry only with the same idempotency key. A plain
|
|
201
|
+
`StoreError` is deliberately non-retryable; custom stores should throw
|
|
202
|
+
`StoreUnavailableError` or `StoreTimeoutError` only for classified transient
|
|
203
|
+
failures.
|
|
204
|
+
|
|
205
|
+
## Optional S3 and ClickHouse storage
|
|
206
|
+
|
|
207
|
+
PostgreSQL remains authoritative. S3 and ClickHouse are optional delivery
|
|
208
|
+
targets, managed by `createBursarRuntime` from `@zonastery/bursar/node`:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { createBursarRuntime } from "@zonastery/bursar/node";
|
|
79
212
|
|
|
80
213
|
const runtime = await createBursarRuntime({
|
|
81
214
|
postgres: process.env.DATABASE_URL!,
|
|
82
215
|
tenantId: process.env.BURSAR_TENANT_ID!,
|
|
83
|
-
s3: {
|
|
84
|
-
bucket: process.env.BURSAR_S3_BUCKET!,
|
|
85
|
-
region: process.env.BURSAR_S3_REGION!,
|
|
86
|
-
endpoint: process.env.BURSAR_S3_ENDPOINT,
|
|
87
|
-
forcePathStyle: process.env.BURSAR_S3_FORCE_PATH_STYLE === "true",
|
|
88
|
-
credentials: {
|
|
89
|
-
accessKeyId: process.env.BURSAR_S3_ACCESS_KEY_ID!,
|
|
90
|
-
secretAccessKey: process.env.BURSAR_S3_SECRET_ACCESS_KEY!,
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
clickhouse: {
|
|
94
|
-
client: clickhouseClient,
|
|
95
|
-
tenantId: process.env.BURSAR_TENANT_ID!,
|
|
96
|
-
// Optional; omit to keep the ClickHouse projection indefinitely.
|
|
97
|
-
retentionDays: 730,
|
|
98
|
-
},
|
|
99
216
|
});
|
|
100
|
-
|
|
101
217
|
await runtime.start();
|
|
102
|
-
|
|
103
|
-
// On graceful shutdown:
|
|
104
|
-
await runtime.close();
|
|
218
|
+
const bursar = runtime.bursar;
|
|
105
219
|
```
|
|
106
220
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
221
|
+
With no S3/ClickHouse configuration the runtime creates no background worker
|
|
222
|
+
and analytics query PostgreSQL directly. See the
|
|
223
|
+
[storage guide](https://zonastery.github.io/bursar/docs/guides/storage-backends)
|
|
224
|
+
for the full S3 and ClickHouse setup.
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
cd javascript
|
|
230
|
+
bun ci # Bun 1.3.14; installs the committed bun.lock
|
|
231
|
+
bun run typecheck
|
|
232
|
+
bun run test # integration tests need Postgres (DATABASE_URL or testcontainer)
|
|
233
|
+
bun run lint
|
|
234
|
+
bun run build
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Bun manages development dependencies and scripts; consumers of the published
|
|
238
|
+
package do not need it. See
|
|
239
|
+
[CONTRIBUTING.md](https://github.com/Zonastery/bursar/blob/main/CONTRIBUTING.md).
|
|
240
|
+
|
|
241
|
+
## License
|
|
112
242
|
|
|
113
|
-
|
|
114
|
-
Database retention maintenance remains independent: schedule
|
|
115
|
-
`bursar.maybe_run_storage_maintenance()` and partition maintenance with
|
|
116
|
-
`pg_cron` as described in the SQL README.
|
|
243
|
+
AGPL-3.0.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { BillingAutoRechargeAttempt, BillingAutoRechargeProfile, BillingCustomerRecord, BillingTopupResult } from "./types/index.js";
|
|
1
|
+
import type { BillingAutoRechargeAttempt, BillingAutoRechargeAttemptState, BillingAutoRechargeProfile, BillingCustomerRecord, BillingTopupResult } from "./types/index.js";
|
|
2
2
|
/** Narrow billing capability required by AutoRechargeService. */
|
|
3
3
|
export interface AutoRechargeBillingPort {
|
|
4
|
-
|
|
4
|
+
getActiveCatalogDocument(): Promise<Record<string, unknown> | null>;
|
|
5
5
|
resolveTopup(provider: string, productId?: string | null, priceId?: string | null): Promise<BillingTopupResult | null>;
|
|
6
6
|
resolveTopupByLookup(provider: string, lookupKey: string): Promise<BillingTopupResult | null>;
|
|
7
7
|
getCustomerByUserId(userId: string, provider?: string | null): Promise<BillingCustomerRecord | null>;
|
|
@@ -15,7 +15,7 @@ export interface AutoRechargeBillingPort {
|
|
|
15
15
|
}): Promise<BillingAutoRechargeAttempt | null>;
|
|
16
16
|
updateAutoRechargeAttempt(input: {
|
|
17
17
|
id: string;
|
|
18
|
-
state:
|
|
18
|
+
state: BillingAutoRechargeAttemptState;
|
|
19
19
|
providerAttemptId?: string | null;
|
|
20
20
|
failureCode?: string | null;
|
|
21
21
|
failureMessage?: string | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-port.d.ts","sourceRoot":"","sources":["../../src/billing/auto-recharge-port.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAE1B,iEAAiE;AACjE,MAAM,WAAW,uBAAuB;IACtC,
|
|
1
|
+
{"version":3,"file":"auto-recharge-port.d.ts","sourceRoot":"","sources":["../../src/billing/auto-recharge-port.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAE1B,iEAAiE;AACjE,MAAM,WAAW,uBAAuB;IACtC,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;IACpE,YAAY,CACV,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,EACzB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;IACtC,oBAAoB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;IAC9F,mBAAmB,CACjB,MAAM,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CAAC;IACzC,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,0BAA0B,GAAG,IAAI,CAAC,CAAC;IACnF,yBAAyB,CACvB,OAAO,EAAE,0BAA0B,EACnC,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,OAAO,CAAA;KAAE,GACpC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,wBAAwB,CAAC,KAAK,EAAE;QAC9B,MAAM,EAAE,MAAM,CAAC;QACf,cAAc,EAAE,MAAM,CAAC;KACxB,GAAG,OAAO,CAAC,0BAA0B,GAAG,IAAI,CAAC,CAAC;IAC/C,yBAAyB,CAAC,KAAK,EAAE;QAC/B,EAAE,EAAE,MAAM,CAAC;QACX,KAAK,EAAE,+BAA+B,CAAC;QACvC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAClC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC5B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACpC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAClB,6CAA6C;IAC7C,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAClF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-port.js","sourceRoot":"","sources":["../../src/billing/auto-recharge-port.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"auto-recharge-port.js","sourceRoot":"","sources":["../../src/billing/auto-recharge-port.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n BillingAutoRechargeAttempt,\n BillingAutoRechargeAttemptState,\n BillingAutoRechargeProfile,\n BillingCustomerRecord,\n BillingTopupResult,\n} from \"./types/index.js\";\n\n/** Narrow billing capability required by AutoRechargeService. */\nexport interface AutoRechargeBillingPort {\n getActiveCatalogDocument(): Promise<Record<string, unknown> | null>;\n resolveTopup(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingTopupResult | null>;\n resolveTopupByLookup(provider: string, lookupKey: string): Promise<BillingTopupResult | null>;\n getCustomerByUserId(\n userId: string,\n provider?: string | null,\n ): Promise<BillingCustomerRecord | null>;\n getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null>;\n upsertAutoRechargeProfile(\n profile: BillingAutoRechargeProfile,\n options?: { resetCooldown?: boolean },\n ): Promise<void>;\n claimAutoRechargeAttempt(input: {\n userId: string;\n idempotencyKey: string;\n }): Promise<BillingAutoRechargeAttempt | null>;\n updateAutoRechargeAttempt(input: {\n id: string;\n state: BillingAutoRechargeAttemptState;\n providerAttemptId?: string | null;\n failureCode?: string | null;\n failureMessage?: string | null;\n metadata?: Record<string, unknown>;\n }): Promise<void>;\n /** Count attempts since an exact instant. */\n countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number>;\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
1
2
|
import type { BillingAutoRechargeStatus } from "./types/index.js";
|
|
2
3
|
import type { PaymentProvider, SavedPaymentChargeQuote, SavedPaymentChargeResult } from "../providers/types.js";
|
|
3
4
|
import type { AutoRechargeBillingPort } from "./auto-recharge-port.js";
|
|
@@ -11,6 +12,7 @@ export declare class AutoRechargeService {
|
|
|
11
12
|
constructor(billing: AutoRechargeBillingPort);
|
|
12
13
|
private policy;
|
|
13
14
|
private paymentMethod;
|
|
15
|
+
private preview;
|
|
14
16
|
quote(input: {
|
|
15
17
|
userId: string;
|
|
16
18
|
provider: PaymentProvider;
|
|
@@ -22,21 +24,20 @@ export declare class AutoRechargeService {
|
|
|
22
24
|
enable(input: {
|
|
23
25
|
userId: string;
|
|
24
26
|
provider: PaymentProvider;
|
|
25
|
-
balance:
|
|
27
|
+
balance: Decimal;
|
|
26
28
|
returnUrl?: string;
|
|
27
|
-
consentReference?: string;
|
|
28
29
|
}): Promise<BillingAutoRechargeStatus | null>;
|
|
29
30
|
disable(userId: string): Promise<void>;
|
|
30
31
|
retry(input: {
|
|
31
32
|
userId: string;
|
|
32
33
|
provider: PaymentProvider;
|
|
33
|
-
balance:
|
|
34
|
+
balance: Decimal;
|
|
34
35
|
returnUrl?: string;
|
|
35
36
|
}): Promise<AutoRechargeProcessResult>;
|
|
36
37
|
processIfNeeded(input: {
|
|
37
38
|
userId: string;
|
|
38
39
|
provider: PaymentProvider;
|
|
39
|
-
balance:
|
|
40
|
+
balance: Decimal;
|
|
40
41
|
returnUrl?: string;
|
|
41
42
|
}): Promise<AutoRechargeProcessResult>;
|
|
42
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-service.d.ts","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auto-recharge-service.d.ts","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,YAAY,CAAC;AAQjC,OAAO,KAAK,EAA8B,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,KAAK,EAEV,eAAe,EACf,uBAAuB,EACvB,wBAAwB,EACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAGvE,MAAM,MAAM,mBAAmB,GAC3B,gBAAgB,GAChB,UAAU,GACV,iBAAiB,GACjB,oBAAoB,GACpB,eAAe,GACf,WAAW,GACX,iBAAiB,GACjB,QAAQ,CAAC;AAEb,MAAM,WAAW,yBAAyB;IACxC,OAAO,EAAE,mBAAmB,CAAC;IAC7B,MAAM,CAAC,EAAE,wBAAwB,GAAG,IAAI,CAAC;CAC1C;AAkBD,qBAAa,mBAAmB;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,uBAAuB;YAE/C,MAAM;YA8CN,aAAa;IAgB3B,OAAO,CAAC,OAAO;IAgBT,KAAK,CAAC,KAAK,EAAE;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;KAC3B,GAAG,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC;IAQrC,SAAS,CAAC,KAAK,EAAE;QACrB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;KAC3B,GAAG,OAAO,CAAC,yBAAyB,GAAG,IAAI,CAAC;IA+BvC,MAAM,CAAC,KAAK,EAAE;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;QAC1B,OAAO,EAAE,OAAO,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,yBAAyB,GAAG,IAAI,CAAC;IA0BvC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWtC,KAAK,CAAC,KAAK,EAAE;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;QAC1B,OAAO,EAAE,OAAO,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,yBAAyB,CAAC;IAchC,eAAe,CAAC,KAAK,EAAE;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;QAC1B,OAAO,EAAE,OAAO,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,yBAAyB,CAAC;CAoFvC"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import Decimal from "decimal.js";
|
|
2
3
|
import { loadConfigFromDict } from "../config.js";
|
|
4
|
+
import { AutoRechargeDisabledError, AutoRechargeNotConfiguredError, PaymentMethodRequiredError, ProviderCapabilityNotSupportedError, } from "../errors.js";
|
|
3
5
|
import { resolveAutoRechargeWindow } from "./policy-window.js";
|
|
4
6
|
export class AutoRechargeService {
|
|
5
7
|
billing;
|
|
@@ -7,7 +9,7 @@ export class AutoRechargeService {
|
|
|
7
9
|
this.billing = billing;
|
|
8
10
|
}
|
|
9
11
|
async policy(provider) {
|
|
10
|
-
const raw = await this.billing.
|
|
12
|
+
const raw = await this.billing.getActiveCatalogDocument();
|
|
11
13
|
if (!raw)
|
|
12
14
|
return null;
|
|
13
15
|
const config = loadConfigFromDict(raw);
|
|
@@ -15,6 +17,8 @@ export class AutoRechargeService {
|
|
|
15
17
|
if (!auto)
|
|
16
18
|
return null;
|
|
17
19
|
const topupKey = auto.eligibleTopups[0];
|
|
20
|
+
if (topupKey === undefined)
|
|
21
|
+
return null;
|
|
18
22
|
const topup = config.commerce.offers[topupKey];
|
|
19
23
|
if (!topup || topup.type !== "topup")
|
|
20
24
|
return null;
|
|
@@ -36,7 +40,7 @@ export class AutoRechargeService {
|
|
|
36
40
|
const period = resolveAutoRechargeWindow(auto.limits.window);
|
|
37
41
|
return {
|
|
38
42
|
enabled: true,
|
|
39
|
-
threshold:
|
|
43
|
+
threshold: auto.balanceBelow.default,
|
|
40
44
|
topupKey,
|
|
41
45
|
topupId: resolvedTopup.topupId,
|
|
42
46
|
quantity: auto.quantity.default,
|
|
@@ -55,22 +59,17 @@ export class AutoRechargeService {
|
|
|
55
59
|
if (!customer)
|
|
56
60
|
return null;
|
|
57
61
|
if (!provider.listPaymentMethods) {
|
|
58
|
-
throw new
|
|
62
|
+
throw new ProviderCapabilityNotSupportedError(provider.provider, "listPaymentMethods");
|
|
59
63
|
}
|
|
60
64
|
const methods = await provider.listPaymentMethods(customer.providerCustomerId);
|
|
61
|
-
const method = (
|
|
62
|
-
methods.find((candidate) => candidate.isDefault) ??
|
|
65
|
+
const method = methods.find((candidate) => candidate.isDefault) ??
|
|
63
66
|
(methods.length === 1 ? methods[0] : null);
|
|
64
67
|
return method ? { customerId: customer.providerCustomerId, method } : null;
|
|
65
68
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!policy || !input.provider.previewSavedPaymentCharge)
|
|
69
|
+
preview(provider, policy, payment) {
|
|
70
|
+
if (!provider.previewSavedPaymentCharge)
|
|
69
71
|
return null;
|
|
70
|
-
|
|
71
|
-
if (!payment)
|
|
72
|
-
return null;
|
|
73
|
-
return input.provider.previewSavedPaymentCharge({
|
|
72
|
+
return provider.previewSavedPaymentCharge({
|
|
74
73
|
customerId: payment.customerId,
|
|
75
74
|
paymentMethodId: payment.method.id,
|
|
76
75
|
productId: policy.productId,
|
|
@@ -79,6 +78,15 @@ export class AutoRechargeService {
|
|
|
79
78
|
idempotencyKey: "auto-recharge-preview",
|
|
80
79
|
});
|
|
81
80
|
}
|
|
81
|
+
async quote(input) {
|
|
82
|
+
const policy = await this.policy(input.provider);
|
|
83
|
+
if (!policy)
|
|
84
|
+
return null;
|
|
85
|
+
const payment = await this.paymentMethod(input.userId, input.provider);
|
|
86
|
+
if (!payment)
|
|
87
|
+
return null;
|
|
88
|
+
return this.preview(input.provider, policy, payment);
|
|
89
|
+
}
|
|
82
90
|
async getStatus(input) {
|
|
83
91
|
const policy = await this.policy(input.provider);
|
|
84
92
|
if (!policy)
|
|
@@ -87,7 +95,7 @@ export class AutoRechargeService {
|
|
|
87
95
|
const payment = profile?.enabled
|
|
88
96
|
? await this.paymentMethod(input.userId, input.provider)
|
|
89
97
|
: null;
|
|
90
|
-
const quote = await this.
|
|
98
|
+
const quote = payment ? await this.preview(input.provider, policy, payment) : null;
|
|
91
99
|
return {
|
|
92
100
|
enabled: Boolean(profile?.enabled),
|
|
93
101
|
state: profile?.enabled ? profile.state : "disabled",
|
|
@@ -110,10 +118,10 @@ export class AutoRechargeService {
|
|
|
110
118
|
async enable(input) {
|
|
111
119
|
const policy = await this.policy(input.provider);
|
|
112
120
|
if (!policy)
|
|
113
|
-
throw new
|
|
121
|
+
throw new AutoRechargeNotConfiguredError();
|
|
114
122
|
const payment = await this.paymentMethod(input.userId, input.provider);
|
|
115
123
|
if (!payment)
|
|
116
|
-
throw new
|
|
124
|
+
throw new PaymentMethodRequiredError();
|
|
117
125
|
const profile = {
|
|
118
126
|
userId: input.userId,
|
|
119
127
|
enabled: true,
|
|
@@ -147,7 +155,7 @@ export class AutoRechargeService {
|
|
|
147
155
|
async retry(input) {
|
|
148
156
|
const profile = await this.billing.getAutoRechargeProfile(input.userId);
|
|
149
157
|
if (!profile?.enabled)
|
|
150
|
-
throw new
|
|
158
|
+
throw new AutoRechargeDisabledError();
|
|
151
159
|
await this.billing.upsertAutoRechargeProfile({
|
|
152
160
|
...profile,
|
|
153
161
|
state: "active",
|
|
@@ -162,34 +170,50 @@ export class AutoRechargeService {
|
|
|
162
170
|
const profile = await this.billing.getAutoRechargeProfile(input.userId);
|
|
163
171
|
if (!profile?.enabled || profile.state !== "active")
|
|
164
172
|
return { outcome: "disabled" };
|
|
165
|
-
if (input.balance
|
|
173
|
+
if (input.balance.gte(policy.threshold)) {
|
|
166
174
|
return { outcome: "above_threshold" };
|
|
167
175
|
}
|
|
168
176
|
const payment = await this.paymentMethod(input.userId, input.provider);
|
|
169
177
|
if (!payment)
|
|
170
178
|
return { outcome: "failed" };
|
|
179
|
+
if (!input.provider.chargeSavedPaymentMethod) {
|
|
180
|
+
throw new ProviderCapabilityNotSupportedError(input.provider.provider, "chargeSavedPaymentMethod");
|
|
181
|
+
}
|
|
182
|
+
const idempotencyKey = `auto-recharge:${input.userId}:${randomUUID()}`;
|
|
171
183
|
const attempt = await this.billing.claimAutoRechargeAttempt({
|
|
172
184
|
userId: input.userId,
|
|
173
|
-
idempotencyKey
|
|
185
|
+
idempotencyKey,
|
|
174
186
|
});
|
|
175
187
|
if (!attempt)
|
|
176
188
|
return { outcome: "limit_reached" };
|
|
177
|
-
if (
|
|
178
|
-
|
|
189
|
+
if (attempt.idempotencyKey !== idempotencyKey) {
|
|
190
|
+
return { outcome: "already_processing" };
|
|
191
|
+
}
|
|
192
|
+
let charge;
|
|
193
|
+
try {
|
|
194
|
+
charge = await input.provider.chargeSavedPaymentMethod({
|
|
195
|
+
customerId: payment.customerId,
|
|
196
|
+
paymentMethodId: payment.method.id,
|
|
197
|
+
productId: policy.productId,
|
|
198
|
+
quantity: policy.quantity,
|
|
199
|
+
returnUrl: input.returnUrl,
|
|
200
|
+
idempotencyKey: attempt.idempotencyKey,
|
|
201
|
+
metadata: {
|
|
202
|
+
auto_recharge_attempt_id: attempt.id,
|
|
203
|
+
purpose: "credit_topup",
|
|
204
|
+
userId: input.userId,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
await this.billing.updateAutoRechargeAttempt({
|
|
210
|
+
id: attempt.id,
|
|
211
|
+
state: "unknown",
|
|
212
|
+
failureCode: "provider_request_failed",
|
|
213
|
+
failureMessage: error instanceof Error ? error.message : String(error),
|
|
214
|
+
});
|
|
215
|
+
throw error;
|
|
179
216
|
}
|
|
180
|
-
const charge = await input.provider.chargeSavedPaymentMethod({
|
|
181
|
-
customerId: payment.customerId,
|
|
182
|
-
paymentMethodId: payment.method.id,
|
|
183
|
-
productId: policy.productId,
|
|
184
|
-
quantity: policy.quantity,
|
|
185
|
-
returnUrl: input.returnUrl,
|
|
186
|
-
idempotencyKey: attempt.idempotencyKey,
|
|
187
|
-
metadata: {
|
|
188
|
-
auto_recharge_attempt_id: attempt.id,
|
|
189
|
-
purpose: "credit_topup",
|
|
190
|
-
userId: input.userId,
|
|
191
|
-
},
|
|
192
|
-
});
|
|
193
217
|
if (charge.status === "requires_customer_action") {
|
|
194
218
|
await this.billing.updateAutoRechargeAttempt({
|
|
195
219
|
id: attempt.id,
|
|
@@ -198,7 +222,13 @@ export class AutoRechargeService {
|
|
|
198
222
|
});
|
|
199
223
|
return { outcome: "action_required", charge };
|
|
200
224
|
}
|
|
201
|
-
if (charge.status === "succeeded" ||
|
|
225
|
+
if (charge.status === "succeeded" ||
|
|
226
|
+
charge.status === "processing" ||
|
|
227
|
+
charge.status === "requires_merchant_action" ||
|
|
228
|
+
charge.status === "requires_confirmation" ||
|
|
229
|
+
charge.status === "requires_capture" ||
|
|
230
|
+
charge.status === "partially_captured" ||
|
|
231
|
+
charge.status === "partially_captured_and_capturable") {
|
|
202
232
|
await this.billing.updateAutoRechargeAttempt({
|
|
203
233
|
id: attempt.id,
|
|
204
234
|
state: "processing",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-service.js","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AASlD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAiC/D,MAAM,OAAO,mBAAmB;IACD;IAA7B,YAA6B,OAAgC;QAAhC,YAAO,GAAP,OAAO,CAAyB;IAAG,CAAC;IAEzD,KAAK,CAAC,MAAM,CAAC,QAAyB;QAC5C,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAC;QACvD,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC;QACtB,MAAM,MAAM,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;QACvC,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;QAC1C,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAEvB,MAAM,QAAQ,GAAG,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;QACxC,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/C,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;YAAE,OAAO,IAAI,CAAC;QAClD,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACrD,IAAI,CAAC,SAAS;YAAE,OAAO,IAAI,CAAC;QAE5B,MAAM,SAAS,GACb,SAAS,CAAC,IAAI,KAAK,cAAc;YAC/B,CAAC,CAAC,SAAS,CAAC,OAAO;YACnB,CAAC,CAAC,SA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,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,EAAE,CAAC;QAChD,CAAC;QACD,IACE,MAAM,CAAC,MAAM,KAAK,WAAW;YAC7B,MAAM,CAAC,MAAM,KAAK,YAAY;YAC9B,MAAM,CAAC,MAAM,KAAK,0BAA0B;YAC5C,MAAM,CAAC,MAAM,KAAK,uBAAuB;YACzC,MAAM,CAAC,MAAM,KAAK,kBAAkB;YACpC,MAAM,CAAC,MAAM,KAAK,oBAAoB;YACtC,MAAM,CAAC,MAAM,KAAK,mCAAmC,EACrD,CAAC;YACD,MAAM,IAAI,CAAC,OAAO,CAAC,yBAAyB,CAAC;gBAC3C,EAAE,EAAE,OAAO,CAAC,EAAE;gBACd,KAAK,EAAE,YAAY;gBACnB,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,IAAI,IAAI;aACpD,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC;QAC1C,CAAC;QAED,MAAM,IAAI,CAAC,OAAO,CAAC,yBAAyB,CAAC;YAC3C,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,KAAK,EAAE,QAAQ;YACf,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,IAAI,IAAI;YACnD,WAAW,EAAE,gBAAgB;SAC9B,CAAC,CAAC;QACH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;IACvC,CAAC;CACF","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport Decimal from \"decimal.js\";\nimport { loadConfigFromDict } from \"../config.js\";\nimport {\n AutoRechargeDisabledError,\n AutoRechargeNotConfiguredError,\n PaymentMethodRequiredError,\n ProviderCapabilityNotSupportedError,\n} from \"../errors.js\";\nimport type { BillingAutoRechargeProfile, BillingAutoRechargeStatus } from \"./types/index.js\";\nimport type {\n PaymentMethodInfo,\n PaymentProvider,\n SavedPaymentChargeQuote,\n SavedPaymentChargeResult,\n} from \"../providers/types.js\";\nimport type { AutoRechargeBillingPort } from \"./auto-recharge-port.js\";\nimport { resolveAutoRechargeWindow } from \"./policy-window.js\";\n\nexport type AutoRechargeOutcome =\n | \"not_configured\"\n | \"disabled\"\n | \"above_threshold\"\n | \"already_processing\"\n | \"limit_reached\"\n | \"submitted\"\n | \"action_required\"\n | \"failed\";\n\nexport interface AutoRechargeProcessResult {\n outcome: AutoRechargeOutcome;\n charge?: SavedPaymentChargeResult | null;\n}\n\ninterface ResolvedAutoRechargePolicy {\n enabled: true;\n threshold: Decimal;\n topupKey: string;\n topupId: string;\n quantity: number;\n maxChargesPerWindow: number;\n windowUnit: \"second\" | \"minute\" | \"hour\" | \"day\" | \"week\" | \"month\" | \"year\";\n windowCount: number;\n windowAnchor: \"calendar\" | \"rolling\";\n windowTimezone: string;\n windowStart: string;\n windowEnd: string;\n productId: string;\n}\n\nexport class AutoRechargeService {\n constructor(private readonly billing: AutoRechargeBillingPort) {}\n\n private async policy(provider: PaymentProvider): Promise<ResolvedAutoRechargePolicy | null> {\n const raw = await this.billing.getActiveCatalogDocument();\n if (!raw) return null;\n const config = loadConfigFromDict(raw);\n const auto = config.commerce.autoRecharge;\n if (!auto) return null;\n\n const topupKey = auto.eligibleTopups[0];\n if (topupKey === undefined) return null;\n const topup = config.commerce.offers[topupKey];\n if (!topup || topup.type !== \"topup\") return null;\n const reference = topup.providers[provider.provider];\n if (!reference) return null;\n\n const productId =\n reference.type === \"stripe_price\"\n ? reference.priceId\n : reference.type === \"dodo_product\"\n ? reference.productId\n : reference.externalId;\n const resolvedTopup =\n reference.type === \"stripe_price\"\n ? await this.billing.resolveTopup(provider.provider, null, productId)\n : reference.type === \"dodo_product\"\n ? await this.billing.resolveTopup(provider.provider, productId, null)\n : await this.billing.resolveTopupByLookup(provider.provider, productId);\n if (!resolvedTopup) return null;\n\n const period = resolveAutoRechargeWindow(auto.limits.window);\n return {\n enabled: true,\n threshold: auto.balanceBelow.default,\n topupKey,\n topupId: resolvedTopup.topupId,\n quantity: auto.quantity.default,\n maxChargesPerWindow: auto.limits.maxPurchases,\n windowUnit: period.unit,\n windowCount: period.count,\n windowAnchor: period.anchor,\n windowTimezone: period.timezone,\n windowStart: period.start,\n windowEnd: period.end,\n productId,\n };\n }\n\n private async paymentMethod(\n userId: string,\n provider: PaymentProvider,\n ): Promise<{ customerId: string; method: PaymentMethodInfo } | null> {\n const customer = await this.billing.getCustomerByUserId(userId, provider.provider);\n if (!customer) return null;\n if (!provider.listPaymentMethods) {\n throw new ProviderCapabilityNotSupportedError(provider.provider, \"listPaymentMethods\");\n }\n const methods = await provider.listPaymentMethods(customer.providerCustomerId);\n const method =\n methods.find((candidate) => candidate.isDefault) ??\n (methods.length === 1 ? methods[0] : null);\n return method ? { customerId: customer.providerCustomerId, method } : null;\n }\n\n private preview(\n provider: PaymentProvider,\n policy: ResolvedAutoRechargePolicy,\n payment: { customerId: string; method: PaymentMethodInfo },\n ): Promise<SavedPaymentChargeQuote> | null {\n if (!provider.previewSavedPaymentCharge) return null;\n return provider.previewSavedPaymentCharge({\n customerId: payment.customerId,\n paymentMethodId: payment.method.id,\n productId: policy.productId,\n quantity: policy.quantity,\n metadata: {},\n idempotencyKey: \"auto-recharge-preview\",\n });\n }\n\n async quote(input: {\n userId: string;\n provider: PaymentProvider;\n }): Promise<SavedPaymentChargeQuote | null> {\n const policy = await this.policy(input.provider);\n if (!policy) return null;\n const payment = await this.paymentMethod(input.userId, input.provider);\n if (!payment) return null;\n return this.preview(input.provider, policy, payment);\n }\n\n async getStatus(input: {\n userId: string;\n provider: PaymentProvider;\n }): Promise<BillingAutoRechargeStatus | null> {\n const policy = await this.policy(input.provider);\n if (!policy) return null;\n const profile = await this.billing.getAutoRechargeProfile(input.userId);\n const payment = profile?.enabled\n ? await this.paymentMethod(input.userId, input.provider)\n : null;\n const quote = payment ? await this.preview(input.provider, policy, payment) : null;\n return {\n enabled: Boolean(profile?.enabled),\n state: profile?.enabled ? profile.state : \"disabled\",\n thresholdCredits: policy.threshold,\n topupKey: policy.topupKey,\n quantity: policy.quantity,\n maxRecharges: policy.maxChargesPerWindow,\n windowStart: policy.windowStart,\n windowEnd: policy.windowEnd,\n rechargesInWindow: await this.billing.countAutoRechargeAttempts(\n input.userId,\n policy.windowStart,\n ),\n paymentMethodId: payment?.method.id ?? null,\n paymentMethodLast4: payment?.method.last4 ?? null,\n paymentMethodBrand: payment?.method.brand ?? null,\n suspendedReason: profile?.state === \"paused\" ? \"auto_recharge_paused\" : null,\n pendingAttemptId: null,\n quoteAmountMinor: quote?.amountMinor ?? null,\n quoteCurrency: quote?.currency ?? null,\n };\n }\n\n async enable(input: {\n userId: string;\n provider: PaymentProvider;\n balance: Decimal;\n returnUrl?: string;\n }): Promise<BillingAutoRechargeStatus | null> {\n const policy = await this.policy(input.provider);\n if (!policy) throw new AutoRechargeNotConfiguredError();\n const payment = await this.paymentMethod(input.userId, input.provider);\n if (!payment) throw new PaymentMethodRequiredError();\n\n const profile: BillingAutoRechargeProfile = {\n userId: input.userId,\n enabled: true,\n state: \"active\",\n armed: true,\n provider: input.provider.provider,\n topupId: policy.topupId,\n quantity: policy.quantity,\n threshold: policy.threshold,\n maxChargesPerWindow: policy.maxChargesPerWindow,\n windowUnit: policy.windowUnit,\n windowCount: policy.windowCount,\n windowAnchor: policy.windowAnchor,\n windowTimezone: policy.windowTimezone,\n };\n await this.billing.upsertAutoRechargeProfile(profile);\n await this.processIfNeeded(input);\n return this.getStatus(input);\n }\n\n async disable(userId: string): Promise<void> {\n const profile = await this.billing.getAutoRechargeProfile(userId);\n if (!profile) return;\n await this.billing.upsertAutoRechargeProfile({\n ...profile,\n enabled: false,\n state: \"disabled\",\n armed: true,\n });\n }\n\n async retry(input: {\n userId: string;\n provider: PaymentProvider;\n balance: Decimal;\n returnUrl?: string;\n }): Promise<AutoRechargeProcessResult> {\n const profile = await this.billing.getAutoRechargeProfile(input.userId);\n if (!profile?.enabled) throw new AutoRechargeDisabledError();\n await this.billing.upsertAutoRechargeProfile(\n {\n ...profile,\n state: \"active\",\n armed: true,\n },\n { resetCooldown: true },\n );\n return this.processIfNeeded(input);\n }\n\n async processIfNeeded(input: {\n userId: string;\n provider: PaymentProvider;\n balance: Decimal;\n returnUrl?: string;\n }): Promise<AutoRechargeProcessResult> {\n const policy = await this.policy(input.provider);\n if (!policy) return { outcome: \"not_configured\" };\n const profile = await this.billing.getAutoRechargeProfile(input.userId);\n if (!profile?.enabled || profile.state !== \"active\") return { outcome: \"disabled\" };\n if (input.balance.gte(policy.threshold)) {\n return { outcome: \"above_threshold\" };\n }\n\n const payment = await this.paymentMethod(input.userId, input.provider);\n if (!payment) return { outcome: \"failed\" };\n if (!input.provider.chargeSavedPaymentMethod) {\n throw new ProviderCapabilityNotSupportedError(\n input.provider.provider,\n \"chargeSavedPaymentMethod\",\n );\n }\n const idempotencyKey = `auto-recharge:${input.userId}:${randomUUID()}`;\n const attempt = await this.billing.claimAutoRechargeAttempt({\n userId: input.userId,\n idempotencyKey,\n });\n if (!attempt) return { outcome: \"limit_reached\" };\n if (attempt.idempotencyKey !== idempotencyKey) {\n return { outcome: \"already_processing\" };\n }\n\n let charge: SavedPaymentChargeResult;\n try {\n charge = await input.provider.chargeSavedPaymentMethod({\n customerId: payment.customerId,\n paymentMethodId: payment.method.id,\n productId: policy.productId,\n quantity: policy.quantity,\n returnUrl: input.returnUrl,\n idempotencyKey: attempt.idempotencyKey,\n metadata: {\n auto_recharge_attempt_id: attempt.id,\n purpose: \"credit_topup\",\n userId: input.userId,\n },\n });\n } catch (error) {\n await this.billing.updateAutoRechargeAttempt({\n id: attempt.id,\n state: \"unknown\",\n failureCode: \"provider_request_failed\",\n failureMessage: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n if (charge.status === \"requires_customer_action\") {\n await this.billing.updateAutoRechargeAttempt({\n id: attempt.id,\n state: \"action_required\",\n providerAttemptId: charge.providerPaymentId ?? null,\n });\n return { outcome: \"action_required\", charge };\n }\n if (\n charge.status === \"succeeded\" ||\n charge.status === \"processing\" ||\n charge.status === \"requires_merchant_action\" ||\n charge.status === \"requires_confirmation\" ||\n charge.status === \"requires_capture\" ||\n charge.status === \"partially_captured\" ||\n charge.status === \"partially_captured_and_capturable\"\n ) {\n await this.billing.updateAutoRechargeAttempt({\n id: attempt.id,\n state: \"processing\",\n providerAttemptId: charge.providerPaymentId ?? null,\n });\n return { outcome: \"submitted\", charge };\n }\n\n await this.billing.updateAutoRechargeAttempt({\n id: attempt.id,\n state: \"failed\",\n providerAttemptId: charge.providerPaymentId ?? null,\n failureCode: \"payment_failed\",\n });\n return { outcome: \"failed\", charge };\n }\n}\n"]}
|