@zonastery/bursar 2.0.3 → 2.0.5
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 +1 -1
- package/dist/billing/auto-recharge-port.d.ts +3 -2
- 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.map +1 -1
- package/dist/billing/auto-recharge-service.js +3 -3
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +3 -2
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +2 -10
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +6 -6
- 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 +8 -7
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts +5 -9
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +191 -285
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +1 -1
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +32 -23
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts +3 -9
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +10 -12
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +1 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +3 -2
- package/dist/billing/management.d.ts.map +1 -1
- 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 +2 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +58 -40
- 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.map +1 -1
- package/dist/billing/postgres/repositories/dispute.d.ts +2 -1
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/dispute.js.map +1 -1
- package/dist/billing/postgres/repositories/event.d.ts +2 -2
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +2 -1
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +8 -12
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +5 -0
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +4 -4
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +1 -1
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/preferences.js.map +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/refund.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.js +1 -9
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription.d.ts +11 -11
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +29 -30
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/topup.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +6 -5
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +15 -13
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/types/auto-recharge.d.ts +2 -1
- 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/documents.d.ts +2 -1
- 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 +3 -2
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +73 -39
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +4 -1
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.d.ts +3 -2
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +4 -3
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +45 -42
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/errors.d.ts.map +1 -1
- package/dist/commerce/errors.js.map +1 -1
- package/dist/commerce/provider-registry.d.ts.map +1 -1
- package/dist/commerce/provider-registry.js +11 -6
- package/dist/commerce/provider-registry.js.map +1 -1
- package/dist/commerce/service.d.ts +1 -1
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +16 -22
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-access.d.ts +5 -2
- package/dist/config/parse-access.d.ts.map +1 -1
- package/dist/config/parse-access.js +60 -41
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.d.ts +2 -1
- package/dist/config/parse-commerce.d.ts.map +1 -1
- package/dist/config/parse-commerce.js +49 -29
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.d.ts +2 -1
- package/dist/config/parse-credits.d.ts.map +1 -1
- package/dist/config/parse-credits.js +39 -23
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts +7 -2
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +49 -22
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts +2 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +41 -31
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.d.ts +15 -11
- package/dist/config/parse-utils.d.ts.map +1 -1
- package/dist/config/parse-utils.js +90 -27
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +6 -5
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +65 -35
- package/dist/config.js.map +1 -1
- package/dist/credits/amount.d.ts +1 -1
- package/dist/credits/amount.d.ts.map +1 -1
- package/dist/credits/amount.js +5 -4
- package/dist/credits/amount.js.map +1 -1
- package/dist/credits/catalog-runtime.d.ts.map +1 -1
- package/dist/credits/catalog-runtime.js +2 -3
- package/dist/credits/catalog-runtime.js.map +1 -1
- package/dist/credits/events.d.ts +12 -1
- package/dist/credits/events.d.ts.map +1 -1
- package/dist/credits/events.js +35 -21
- package/dist/credits/events.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +2 -1
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +3 -2
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -1
- 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 +2 -2
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +14 -6
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +6 -6
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/bucket.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +5 -4
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/catalog.js +4 -3
- package/dist/credits/postgres/repositories/catalog.js.map +1 -1
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +2 -2
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/lease.d.ts +2 -2
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/lease.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +11 -11
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js.map +1 -1
- package/dist/credits/postgres/repositories/team.d.ts +2 -2
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/team.js.map +1 -1
- package/dist/credits/postgres/store.d.ts +3 -2
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +17 -10
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts +4 -3
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +10 -11
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/queries.d.ts.map +1 -1
- package/dist/credits/queries.js.map +1 -1
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +10 -5
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +5 -4
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +5 -4
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/catalog.d.ts +2 -1
- 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.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +3 -2
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/engine.d.ts +2 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +39 -10
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +7 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +18 -11
- 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.js.map +1 -1
- package/dist/expr/parser.d.ts.map +1 -1
- package/dist/expr/parser.js.map +1 -1
- package/dist/expr/tokenizer.d.ts.map +1 -1
- package/dist/expr/tokenizer.js +34 -12
- package/dist/expr/tokenizer.js.map +1 -1
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +12 -6
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +0 -12
- package/dist/generated/pricing-config.types.d.ts +2 -8
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/integrations/better-auth.d.ts +4 -1
- package/dist/integrations/better-auth.d.ts.map +1 -1
- package/dist/integrations/better-auth.js +7 -4
- package/dist/integrations/better-auth.js.map +1 -1
- package/dist/load-config-file.d.ts +2 -1
- package/dist/load-config-file.d.ts.map +1 -1
- package/dist/load-config-file.js +19 -13
- package/dist/load-config-file.js.map +1 -1
- package/dist/metrics.d.ts +2 -1
- package/dist/metrics.d.ts.map +1 -1
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +8 -5
- package/dist/providers/_shared.d.ts.map +1 -1
- package/dist/providers/_shared.js +15 -11
- package/dist/providers/_shared.js.map +1 -1
- package/dist/providers/dodo/better-auth-core.d.ts +30 -0
- package/dist/providers/dodo/better-auth-core.d.ts.map +1 -0
- package/dist/providers/dodo/better-auth-core.js +14 -0
- package/dist/providers/dodo/better-auth-core.js.map +1 -0
- package/dist/providers/dodo/better-auth.d.ts +5 -9
- package/dist/providers/dodo/better-auth.d.ts.map +1 -1
- package/dist/providers/dodo/better-auth.js +16 -13
- package/dist/providers/dodo/better-auth.js.map +1 -1
- package/dist/providers/dodo/client-contract.d.ts +12 -10
- 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 +7 -4
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +121 -113
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/nextjs-core.d.ts +22 -0
- package/dist/providers/dodo/nextjs-core.d.ts.map +1 -0
- package/dist/providers/dodo/nextjs-core.js +14 -0
- package/dist/providers/dodo/nextjs-core.js.map +1 -0
- package/dist/providers/dodo/nextjs.d.ts +5 -7
- package/dist/providers/dodo/nextjs.d.ts.map +1 -1
- package/dist/providers/dodo/nextjs.js +11 -10
- package/dist/providers/dodo/nextjs.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +3 -3
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +42 -35
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/react-native-core.d.ts +56 -0
- package/dist/providers/dodo/react-native-core.d.ts.map +1 -0
- package/dist/providers/dodo/react-native-core.js +56 -0
- package/dist/providers/dodo/react-native-core.js.map +1 -0
- package/dist/providers/dodo/react-native.d.ts +6 -50
- package/dist/providers/dodo/react-native.d.ts.map +1 -1
- package/dist/providers/dodo/react-native.js +5 -49
- package/dist/providers/dodo/react-native.js.map +1 -1
- package/dist/providers/dodo/webhook-bridge.d.ts +4 -2
- package/dist/providers/dodo/webhook-bridge.d.ts.map +1 -1
- package/dist/providers/dodo/webhook-bridge.js.map +1 -1
- package/dist/providers/environment.d.ts +1 -1
- package/dist/providers/environment.d.ts.map +1 -1
- package/dist/providers/environment.js +4 -2
- package/dist/providers/environment.js.map +1 -1
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +5 -10
- 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 +33 -9
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +101 -61
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +16 -9
- package/dist/retry.js.map +1 -1
- package/dist/shared/diagnostics.d.ts +5 -5
- package/dist/shared/diagnostics.d.ts.map +1 -1
- package/dist/shared/diagnostics.js +32 -22
- package/dist/shared/diagnostics.js.map +1 -1
- package/dist/shared/idempotency.d.ts +4 -3
- package/dist/shared/idempotency.d.ts.map +1 -1
- package/dist/shared/idempotency.js +21 -10
- package/dist/shared/idempotency.js.map +1 -1
- package/dist/shared/json.d.ts +28 -0
- package/dist/shared/json.d.ts.map +1 -0
- package/dist/shared/json.js +41 -0
- package/dist/shared/json.js.map +1 -0
- package/dist/shared/logger.d.ts +10 -4
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/logger.js +28 -4
- package/dist/shared/logger.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +8 -6
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +69 -47
- package/dist/shared/postgres-client.js.map +1 -1
- package/dist/shared/postgres-errors.d.ts +1 -1
- package/dist/shared/postgres-errors.d.ts.map +1 -1
- package/dist/shared/postgres-errors.js +32 -31
- package/dist/shared/postgres-errors.js.map +1 -1
- package/dist/shared/postgres-types.d.ts +3 -2
- package/dist/shared/postgres-types.d.ts.map +1 -1
- package/dist/shared/postgres-types.js.map +1 -1
- package/dist/shared/postgres-validation.d.ts +7 -6
- package/dist/shared/postgres-validation.d.ts.map +1 -1
- package/dist/shared/postgres-validation.js +17 -10
- package/dist/shared/postgres-validation.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts +11 -4
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +8 -6
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +28 -5
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +48 -33
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/diagnostics.d.ts +1 -1
- package/dist/storage/diagnostics.d.ts.map +1 -1
- package/dist/storage/diagnostics.js +13 -12
- package/dist/storage/diagnostics.js.map +1 -1
- package/dist/storage/maintenance.d.ts.map +1 -1
- package/dist/storage/maintenance.js +31 -25
- package/dist/storage/maintenance.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts +1 -1
- package/dist/storage/outbox-worker.d.ts.map +1 -1
- package/dist/storage/outbox-worker.js +18 -18
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +10 -7
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +3 -1
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +10 -4
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +1 -1
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +165 -182
- package/dist/storage/runtime.js.map +1 -1
- package/dist/telemetry/index.d.ts +10 -6
- package/dist/telemetry/index.d.ts.map +1 -1
- package/dist/telemetry/index.js +26 -23
- package/dist/telemetry/index.js.map +1 -1
- package/dist/telemetry/opentelemetry.d.ts.map +1 -1
- package/dist/telemetry/opentelemetry.js +3 -3
- package/dist/telemetry/opentelemetry.js.map +1 -1
- package/package.json +40 -40
package/README.md
CHANGED
|
@@ -233,7 +233,7 @@ for the full S3 and ClickHouse setup.
|
|
|
233
233
|
|
|
234
234
|
```bash
|
|
235
235
|
cd javascript
|
|
236
|
-
bun ci # Bun 1.
|
|
236
|
+
bun ci # Bun 1.4.0; installs the committed bun.lock
|
|
237
237
|
bun run typecheck
|
|
238
238
|
bun run test # integration tests use a disposable testcontainer by default
|
|
239
239
|
bun run lint
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { BillingAutoRechargeAttempt, BillingAutoRechargeAttemptState, BillingAutoRechargeProfile, BillingCustomerRecord, BillingTopupResult } from "./types/index.js";
|
|
2
|
+
import type { JsonObject } from "../shared/json.js";
|
|
2
3
|
/** Narrow billing capability required by AutoRechargeService. */
|
|
3
4
|
export interface AutoRechargeBillingPort {
|
|
4
|
-
getActiveCatalogDocument(): Promise<
|
|
5
|
+
getActiveCatalogDocument(): Promise<JsonObject | null>;
|
|
5
6
|
resolveTopup(provider: string, productId?: string | null, priceId?: string | null): Promise<BillingTopupResult | null>;
|
|
6
7
|
resolveTopupByLookup(provider: string, lookupKey: string): Promise<BillingTopupResult | null>;
|
|
7
8
|
getCustomerByUserId(userId: string, provider?: string | null): Promise<BillingCustomerRecord | null>;
|
|
@@ -19,7 +20,7 @@ export interface AutoRechargeBillingPort {
|
|
|
19
20
|
providerAttemptId?: string | null;
|
|
20
21
|
failureCode?: string | null;
|
|
21
22
|
failureMessage?: string | null;
|
|
22
|
-
metadata?:
|
|
23
|
+
metadata?: JsonObject;
|
|
23
24
|
}): Promise<void>;
|
|
24
25
|
/** Count attempts since an exact instant. */
|
|
25
26
|
countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number>;
|
|
@@ -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,+BAA+B,EAC/B,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;
|
|
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;AAC1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,iEAAiE;AACjE,MAAM,WAAW,uBAAuB;IACtC,wBAAwB,IAAI,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IACvD,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,UAAU,CAAC;KACvB,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":"","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<
|
|
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\";\nimport type { JsonObject } from \"../shared/json.js\";\n\n/** Narrow billing capability required by AutoRechargeService. */\nexport interface AutoRechargeBillingPort {\n getActiveCatalogDocument(): Promise<JsonObject | 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?: JsonObject;\n }): Promise<void>;\n /** Count attempts since an exact instant. */\n countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number>;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-service.d.ts","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAQrC,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;AAIvE,MAAM,MAAM,mBAAmB,GAC3B,gBAAg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,MAAM,CAAC;QACf,QAAQ,EAAE,eAAe,CAAC;KAC3B,GAAG,OAAO,CAAC,yBAAyB,GAAG,IAAI,CAAC,CA6B5C;IAEK,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,CAwB5C;IAEK,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAS3C;IAEK,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,CAYrC;IAEK,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,CAsFrC;CACF"}
|
|
@@ -208,14 +208,14 @@ export class AutoRechargeService {
|
|
|
208
208
|
},
|
|
209
209
|
});
|
|
210
210
|
}
|
|
211
|
-
catch (
|
|
211
|
+
catch (cause) {
|
|
212
212
|
await this.billing.updateAutoRechargeAttempt({
|
|
213
213
|
id: attempt.id,
|
|
214
214
|
state: "unknown",
|
|
215
215
|
failureCode: "provider_request_failed",
|
|
216
|
-
failureMessage: persistedDiagnosticSummary(
|
|
216
|
+
failureMessage: persistedDiagnosticSummary(cause, "auto_recharge_provider_failed"),
|
|
217
217
|
});
|
|
218
|
-
throw
|
|
218
|
+
throw cause;
|
|
219
219
|
}
|
|
220
220
|
if (charge.status === "requires_customer_action") {
|
|
221
221
|
await this.billing.updateAutoRechargeAttempt({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-recharge-service.js","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,yBAAyB,EACzB,8BAA8B,EAC9B,0BAA0B,EAC1B,mCAAmC,GACpC,MAAM,cAAc,CAAC;AAStB,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAiCtE,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,wBAAwB,EAAE,CAAC;QAC1D,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,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAO,IAAI,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,CA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,CAAC,yBAAyB,CAAC;gBAC3C,EAAE,EAAE,OAAO,CAAC,EAAE;gBACd,KAAK,EAAE,iBAAiB;gBACxB,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,IAAI,IAAI;aACpD,CAAC,CAAC;YACH,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 { 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\";\nimport { persistedDiagnosticSummary } from \"../shared/diagnostics.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}:${globalThis.crypto.randomUUID()}`;\n const attempt = await this.billing.claimAutoRechargeAttempt({\n userId: input.userId,\n idempotencyKey,\n });\n if (!attempt) return { outcome: \"limit_reached\" };\n // A transport error leaves the provider outcome unknown. The database\n // returns that in-flight attempt on the next claim; replay the provider\n // request with its original key so a committed charge cannot be duplicated.\n if (attempt.idempotencyKey !== idempotencyKey && attempt.state !== \"unknown\") {\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 bursar_account_id: 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: persistedDiagnosticSummary(error, \"auto_recharge_provider_failed\"),\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"]}
|
|
1
|
+
{"version":3,"file":"auto-recharge-service.js","sourceRoot":"","sources":["../../src/billing/auto-recharge-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,yBAAyB,EACzB,8BAA8B,EAC9B,0BAA0B,EAC1B,mCAAmC,GACpC,MAAM,cAAc,CAAC;AAStB,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAiCtE,MAAM,OAAO,mBAAmB;IACD,OAAO;IAApC,YAA6B,OAAgC;uBAAhC,OAAO;IAA4B,CAAC;IAEzD,KAAK,CAAC,MAAM,CAAC,QAAyB;QAC5C,MAAM,GAAG,GAAG,M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AE,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 { 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\";\nimport { persistedDiagnosticSummary } from \"../shared/diagnostics.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}:${globalThis.crypto.randomUUID()}`;\n const attempt = await this.billing.claimAutoRechargeAttempt({\n userId: input.userId,\n idempotencyKey,\n });\n if (!attempt) return { outcome: \"limit_reached\" };\n // A transport error leaves the provider outcome unknown. The database\n // returns that in-flight attempt on the next claim; replay the provider\n // request with its original key so a committed charge cannot be duplicated.\n if (attempt.idempotencyKey !== idempotencyKey && attempt.state !== \"unknown\") {\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 bursar_account_id: input.userId,\n },\n });\n } catch (cause) {\n await this.billing.updateAutoRechargeAttempt({\n id: attempt.id,\n state: \"unknown\",\n failureCode: \"provider_request_failed\",\n failureMessage: persistedDiagnosticSummary(cause, \"auto_recharge_provider_failed\"),\n });\n throw cause;\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"]}
|
|
@@ -2,6 +2,7 @@ import type { BillingStore } from "./billing-store.js";
|
|
|
2
2
|
import type { BillingAutoRechargeAttempt, BillingAutoRechargeAttemptState, BillingAutoRechargeProfile, BillingCustomerRecord, BillingEvent, BillingEventResult, BillingOfferResult, BillingPreferences, BillingSubscriptionChange, BillingSubscriptionChangeInput, BillingSubscriptionState, BillingTopupResult, CheckoutIntent } from "./types/index.js";
|
|
3
3
|
import { AutoRechargeService } from "./auto-recharge-service.js";
|
|
4
4
|
import type { BillingServiceOptions } from "./service-types.js";
|
|
5
|
+
import type { JsonObject } from "../shared/json.js";
|
|
5
6
|
export type { BillingProvisioningPort, BillingServiceOptions } from "./service-types.js";
|
|
6
7
|
/**
|
|
7
8
|
* Provider-agnostic billing lifecycle state machine.
|
|
@@ -27,7 +28,7 @@ export declare class BillingService {
|
|
|
27
28
|
recordSubscriptionConflict(input: Parameters<BillingStore["recordSubscriptionConflict"]>[0]): Promise<void>;
|
|
28
29
|
updateUserPreferences(preferences: BillingPreferences): Promise<void>;
|
|
29
30
|
getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null>;
|
|
30
|
-
getActiveCatalogDocument(): Promise<
|
|
31
|
+
getActiveCatalogDocument(): Promise<JsonObject | null>;
|
|
31
32
|
listCancellableProviderSubscriptionIds(userId: string): Promise<string[]>;
|
|
32
33
|
listCancellableSubscriptions(userId: string): Promise<BillingSubscriptionState[]>;
|
|
33
34
|
pseudonymizeFinancialSubject(userId: string): Promise<void>;
|
|
@@ -44,7 +45,7 @@ export declare class BillingService {
|
|
|
44
45
|
providerAttemptId?: string | null;
|
|
45
46
|
failureCode?: string | null;
|
|
46
47
|
failureMessage?: string | null;
|
|
47
|
-
metadata?:
|
|
48
|
+
metadata?: JsonObject;
|
|
48
49
|
}): Promise<void>;
|
|
49
50
|
updateAutoRechargeAttemptByProviderPayment(input: {
|
|
50
51
|
provider: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing-service.d.ts","sourceRoot":"","sources":["../../src/billing/billing-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC1B,qBAAqB,EACrB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,yBAAyB,EACzB,8BAA8B,EAC9B,wBAAwB,EACxB,kBAAkB,EAClB,cAAc,EACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGjE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,YAAY,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEzF;;;GAGG;AACH,qBAAa,cAAc;IACzB,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAAC;IAC3C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAC/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwB;IAE/C,IAAI,eAAe,IAAI,OAAO,CAE7B;
|
|
1
|
+
{"version":3,"file":"billing-service.d.ts","sourceRoot":"","sources":["../../src/billing/billing-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC1B,qBAAqB,EACrB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,yBAAyB,EACzB,8BAA8B,EAC9B,wBAAwB,EACxB,kBAAkB,EAClB,cAAc,EACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE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GAAG,IAAI,EACzB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAEpC;IAEK,oBAAoB,CACxB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAEpC;IAEK,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,kBAAkB,EAAE,MAAM,EAC1B,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GACpB,OAAO,CAAC,IAAI,CAAC,CAEf;IAEK,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC,CAUlF;IAEK,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC,CAQtF;IAEK,yBAAyB,CAAC,GAAG,OAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAoBjE;YAEa,mBAAmB;IA0BjC,mDAAmD;IACnD,oBAAoB,IAAI,IAAI,CAE3B;IAEK,kBAAkB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAEzE;CACF"}
|
|
@@ -133,14 +133,7 @@ export class BillingService {
|
|
|
133
133
|
for (const candidate of candidates) {
|
|
134
134
|
if (!candidate.subscriptionId || !candidate.graceEndsAt)
|
|
135
135
|
continue;
|
|
136
|
-
|
|
137
|
-
if (current?.status !== "past_due" ||
|
|
138
|
-
current.graceEndsAt !== candidate.graceEndsAt ||
|
|
139
|
-
current.graceExpiredAt) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
await this.events.revokeIfCurrentSubscription(candidate.userId, candidate.providerSubscriptionId);
|
|
143
|
-
if (await this.store.markSubscriptionGraceExpired(candidate.subscriptionId, candidate.graceEndsAt, asOf)) {
|
|
136
|
+
if (await this.store.expireSubscriptionGracePeriod(candidate.userId, candidate.subscriptionId, candidate.graceEndsAt, asOf, this.events.terminalPlanKey)) {
|
|
144
137
|
expiredCount += 1;
|
|
145
138
|
}
|
|
146
139
|
}
|
|
@@ -156,9 +149,8 @@ export class BillingService {
|
|
|
156
149
|
!subscription.subscriptionId) {
|
|
157
150
|
return subscription;
|
|
158
151
|
}
|
|
159
|
-
await this.events.revokeIfCurrentSubscription(subscription.userId, subscription.providerSubscriptionId);
|
|
160
152
|
const expiredAt = new Date().toISOString();
|
|
161
|
-
const marked = await this.store.
|
|
153
|
+
const marked = await this.store.expireSubscriptionGracePeriod(subscription.userId, subscription.subscriptionId, subscription.graceEndsAt, expiredAt, this.events.terminalPlanKey);
|
|
162
154
|
return marked ? { ...subscription, graceExpiredAt: expiredAt } : subscription;
|
|
163
155
|
}
|
|
164
156
|
/** Invalidate cached provider offer resolution. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing-service.js","sourceRoot":"","sources":["../../src/billing/billing-service.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAIpD;;;GAGG;AACH,MAAM,OAAO,cAAc;IAChB,YAAY,CAAsB;IAC1B,KAAK,CAAe;IACpB,UAAU,CAAoB;IAC9B,MAAM,CAAwB;IAE/C,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC;IACrC,CAAC;IAED,YAAY,KAAmB,EAAE,OAA+B;QAC9D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG,IAAI,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,qBAAq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BAA4B,CAC1D,YAAY,CAAC,cAAc,EAC3B,YAAY,CAAC,WAAW,EACxB,SAAS,CACV,CAAC;QACF,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;IAChF,CAAC;IAED,mDAAmD;IACnD,oBAAoB;QAClB,IAAI,CAAC,MAAM,CAAC,oBAAoB,EAAE,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,kBAAkB,CAAC,KAAmB;QAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;CACF","sourcesContent":["import type { BillingStore } from \"./billing-store.js\";\nimport type {\n BillingAutoRechargeAttempt,\n BillingAutoRechargeAttemptState,\n BillingAutoRechargeProfile,\n BillingCustomerRecord,\n BillingEvent,\n BillingEventResult,\n BillingOfferResult,\n BillingPreferences,\n BillingSubscriptionChange,\n BillingSubscriptionChangeInput,\n BillingSubscriptionState,\n BillingTopupResult,\n CheckoutIntent,\n} from \"./types/index.js\";\nimport { SUBSCRIPTION_STATUS } from \"./types/index.js\";\nimport { AutoRechargeService } from \"./auto-recharge-service.js\";\nimport { BillingEventProcessor } from \"./event-processor.js\";\nimport { BillingManagement } from \"./management.js\";\nimport type { BillingServiceOptions } from \"./service-types.js\";\nexport type { BillingProvisioningPort, BillingServiceOptions } from \"./service-types.js\";\n\n/**\n * Provider-agnostic billing lifecycle state machine.\n * Mirrors the Python billing service implementation.\n */\nexport class BillingService {\n readonly autoRecharge: AutoRechargeService;\n private readonly store: BillingStore;\n private readonly management: BillingManagement;\n private readonly events: BillingEventProcessor;\n\n get hasProvisioning(): boolean {\n return this.events.hasProvisioning;\n }\n\n constructor(store: BillingStore, options?: BillingServiceOptions) {\n this.store = store;\n this.management = new BillingManagement(store);\n this.events = new BillingEventProcessor(store, options);\n this.autoRecharge = new AutoRechargeService(this);\n }\n\n async createOrGetCheckoutIntent(\n input: Parameters<BillingStore[\"createOrGetCheckoutIntent\"]>[0],\n ): Promise<CheckoutIntent> {\n return this.management.createOrGetCheckoutIntent(input);\n }\n\n async updateCheckoutIntent(\n id: string,\n update: Parameters<BillingStore[\"updateCheckoutIntent\"]>[1],\n ): Promise<void> {\n await this.management.updateCheckoutIntent(id, update);\n }\n\n async getCheckoutIntent(id: string, subjectId: string): Promise<CheckoutIntent | null> {\n return this.management.getCheckoutIntent(id, subjectId);\n }\n\n async getActiveSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n return this.management.getActiveSubscription(userId);\n }\n\n async getUserPreferences(userId: string): Promise<BillingPreferences | null> {\n return this.management.getUserPreferences(userId);\n }\n\n async listBillingInvoices(userId: string) {\n return this.management.listBillingInvoices(userId);\n }\n\n async upsertBillingSubscription(state: BillingSubscriptionState): Promise<void> {\n await this.management.upsertBillingSubscription(state);\n }\n\n async createBillingSubscriptionChange(\n input: BillingSubscriptionChangeInput,\n ): Promise<BillingSubscriptionChange> {\n return this.management.createBillingSubscriptionChange(input);\n }\n\n async getOpenBillingSubscriptionChange(\n provider: string,\n providerSubscriptionId: string,\n ): Promise<BillingSubscriptionChange | null> {\n return this.management.getOpenBillingSubscriptionChange(provider, providerSubscriptionId);\n }\n\n async updateBillingSubscriptionChange(\n id: string,\n update: Parameters<BillingStore[\"updateBillingSubscriptionChange\"]>[1],\n ): Promise<void> {\n await this.management.updateBillingSubscriptionChange(id, update);\n }\n\n async recordSubscriptionConflict(\n input: Parameters<BillingStore[\"recordSubscriptionConflict\"]>[0],\n ): Promise<void> {\n await this.management.recordSubscriptionConflict(input);\n }\n\n async updateUserPreferences(preferences: BillingPreferences): Promise<void> {\n await this.management.updateUserPreferences(preferences);\n }\n\n async getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null> {\n return this.management.getAutoRechargeProfile(userId);\n }\n\n async getActiveCatalogDocument(): Promise<Record<string, unknown> | null> {\n return this.management.getActiveCatalogDocument();\n }\n\n async listCancellableProviderSubscriptionIds(userId: string): Promise<string[]> {\n return this.management.listCancellableProviderSubscriptionIds(userId);\n }\n\n async listCancellableSubscriptions(userId: string): Promise<BillingSubscriptionState[]> {\n return this.management.listCancellableSubscriptions(userId);\n }\n\n async pseudonymizeFinancialSubject(userId: string): Promise<void> {\n await this.management.pseudonymizeFinancialSubject(userId);\n }\n\n async upsertAutoRechargeProfile(\n profile: BillingAutoRechargeProfile,\n options?: { resetCooldown?: boolean },\n ): Promise<void> {\n await this.management.upsertAutoRechargeProfile(profile, options);\n }\n\n async claimAutoRechargeAttempt(input: {\n userId: string;\n idempotencyKey: string;\n }): Promise<BillingAutoRechargeAttempt | null> {\n return this.management.claimAutoRechargeAttempt(input);\n }\n\n async 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 await this.management.updateAutoRechargeAttempt(input);\n }\n\n async updateAutoRechargeAttemptByProviderPayment(input: {\n provider: string;\n providerPaymentId: string;\n state: BillingAutoRechargeAttemptState;\n failureCode?: string | null;\n failureMessage?: string | null;\n }): Promise<void> {\n await this.management.updateAutoRechargeAttemptByProviderPayment(input);\n }\n\n async countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number> {\n return this.management.countAutoRechargeAttempts(userId, since);\n }\n\n async getCustomerByUserId(\n userId: string,\n provider?: string | null,\n ): Promise<BillingCustomerRecord | null> {\n return this.management.getCustomerByUserId(userId, provider);\n }\n\n async resolveOffer(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingOfferResult | null> {\n return this.management.resolveOffer(provider, productId, priceId);\n }\n\n async resolveOfferByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingOfferResult | null> {\n return this.management.resolveOfferByLookup(provider, lookupKey);\n }\n\n async resolveTopup(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingTopupResult | null> {\n return this.management.resolveTopup(provider, productId, priceId);\n }\n\n async resolveTopupByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingTopupResult | null> {\n return this.management.resolveTopupByLookup(provider, lookupKey);\n }\n\n async upsertCustomer(\n provider: string,\n providerCustomerId: string,\n userId: string,\n email?: string | null,\n ): Promise<void> {\n await this.management.upsertCustomer(provider, providerCustomerId, userId, email);\n }\n\n async getUserSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n const subscription = await this.store.getUserSubscription(userId, [\n SUBSCRIPTION_STATUS.ACTIVE,\n SUBSCRIPTION_STATUS.TRIALING,\n SUBSCRIPTION_STATUS.CANCELED,\n SUBSCRIPTION_STATUS.PAST_DUE,\n SUBSCRIPTION_STATUS.INCOMPLETE,\n // EXPIRED excluded — expired subscriptions are not \"current\" for billing purposes.\n ]);\n return this.expireGraceIfNeeded(subscription);\n }\n\n async getBlockingSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n const subscription = await this.store.getUserSubscription(userId, [\n SUBSCRIPTION_STATUS.ACTIVE,\n SUBSCRIPTION_STATUS.TRIALING,\n SUBSCRIPTION_STATUS.PAST_DUE,\n SUBSCRIPTION_STATUS.INCOMPLETE,\n ]);\n return this.expireGraceIfNeeded(subscription);\n }\n\n async expirePastDueGracePeriods(now = new Date()): Promise<number> {\n if (!this.events.hasProvisioning) return 0;\n const asOf = now.toISOString();\n const candidates = await this.store.listExpiredGraceSubscriptions(asOf);\n let expiredCount = 0;\n for (const candidate of candidates) {\n if (!candidate.subscriptionId || !candidate.graceEndsAt) continue;\n const current = await this.store.getBillingSubscription(\n candidate.provider,\n candidate.providerSubscriptionId,\n );\n if (\n current?.status !== \"past_due\" ||\n current.graceEndsAt !== candidate.graceEndsAt ||\n current.graceExpiredAt\n ) {\n continue;\n }\n await this.events.revokeIfCurrentSubscription(\n candidate.userId,\n candidate.providerSubscriptionId,\n );\n if (\n await this.store.markSubscriptionGraceExpired(\n candidate.subscriptionId,\n candidate.graceEndsAt,\n asOf,\n )\n ) {\n expiredCount += 1;\n }\n }\n return expiredCount;\n }\n\n private async expireGraceIfNeeded(\n subscription: BillingSubscriptionState | null,\n ): Promise<BillingSubscriptionState | null> {\n if (\n !subscription ||\n !this.events.hasProvisioning ||\n subscription.status !== \"past_due\" ||\n subscription.graceExpiredAt ||\n !subscription.graceEndsAt ||\n new Date(subscription.graceEndsAt).getTime() > Date.now() ||\n !subscription.subscriptionId\n ) {\n return subscription;\n }\n\n await this.events.revokeIfCurrentSubscription(\n subscription.userId,\n subscription.providerSubscriptionId,\n );\n const expiredAt = new Date().toISOString();\n const marked = await this.store.markSubscriptionGraceExpired(\n subscription.subscriptionId,\n subscription.graceEndsAt,\n expiredAt,\n );\n return marked ? { ...subscription, graceExpiredAt: expiredAt } : subscription;\n }\n\n /** Invalidate cached provider offer resolution. */\n invalidateOfferCache(): void {\n this.events.invalidateOfferCache();\n }\n\n async ingestBillingEvent(event: BillingEvent): Promise<BillingEventResult> {\n return this.events.ingestBillingEvent(event);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"billing-service.js","sourceRoot":"","sources":["../../src/billing/billing-service.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAKpD;;;GAGG;AACH,MAAM,OAAO,cAAc;IAChB,YAAY,CAAsB;IAC1B,KAAK,CAAe;IACpB,UAAU,CAAoB;IAC9B,MAAM,CAAwB;IAE/C,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC;IACrC,CAAC;IAED,YAAY,KAAmB,EAAE,OAA+B;QAC9D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG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IACrC,CAAC;IAED,KAAK,CAAC,kBAAkB,CAAC,KAAmB;QAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;CACF","sourcesContent":["import type { BillingStore } from \"./billing-store.js\";\nimport type {\n BillingAutoRechargeAttempt,\n BillingAutoRechargeAttemptState,\n BillingAutoRechargeProfile,\n BillingCustomerRecord,\n BillingEvent,\n BillingEventResult,\n BillingOfferResult,\n BillingPreferences,\n BillingSubscriptionChange,\n BillingSubscriptionChangeInput,\n BillingSubscriptionState,\n BillingTopupResult,\n CheckoutIntent,\n} from \"./types/index.js\";\nimport { SUBSCRIPTION_STATUS } from \"./types/index.js\";\nimport { AutoRechargeService } from \"./auto-recharge-service.js\";\nimport { BillingEventProcessor } from \"./event-processor.js\";\nimport { BillingManagement } from \"./management.js\";\nimport type { BillingServiceOptions } from \"./service-types.js\";\nimport type { JsonObject } from \"../shared/json.js\";\nexport type { BillingProvisioningPort, BillingServiceOptions } from \"./service-types.js\";\n\n/**\n * Provider-agnostic billing lifecycle state machine.\n * Mirrors the Python billing service implementation.\n */\nexport class BillingService {\n readonly autoRecharge: AutoRechargeService;\n private readonly store: BillingStore;\n private readonly management: BillingManagement;\n private readonly events: BillingEventProcessor;\n\n get hasProvisioning(): boolean {\n return this.events.hasProvisioning;\n }\n\n constructor(store: BillingStore, options?: BillingServiceOptions) {\n this.store = store;\n this.management = new BillingManagement(store);\n this.events = new BillingEventProcessor(store, options);\n this.autoRecharge = new AutoRechargeService(this);\n }\n\n async createOrGetCheckoutIntent(\n input: Parameters<BillingStore[\"createOrGetCheckoutIntent\"]>[0],\n ): Promise<CheckoutIntent> {\n return this.management.createOrGetCheckoutIntent(input);\n }\n\n async updateCheckoutIntent(\n id: string,\n update: Parameters<BillingStore[\"updateCheckoutIntent\"]>[1],\n ): Promise<void> {\n await this.management.updateCheckoutIntent(id, update);\n }\n\n async getCheckoutIntent(id: string, subjectId: string): Promise<CheckoutIntent | null> {\n return this.management.getCheckoutIntent(id, subjectId);\n }\n\n async getActiveSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n return this.management.getActiveSubscription(userId);\n }\n\n async getUserPreferences(userId: string): Promise<BillingPreferences | null> {\n return this.management.getUserPreferences(userId);\n }\n\n async listBillingInvoices(userId: string) {\n return this.management.listBillingInvoices(userId);\n }\n\n async upsertBillingSubscription(state: BillingSubscriptionState): Promise<void> {\n await this.management.upsertBillingSubscription(state);\n }\n\n async createBillingSubscriptionChange(\n input: BillingSubscriptionChangeInput,\n ): Promise<BillingSubscriptionChange> {\n return this.management.createBillingSubscriptionChange(input);\n }\n\n async getOpenBillingSubscriptionChange(\n provider: string,\n providerSubscriptionId: string,\n ): Promise<BillingSubscriptionChange | null> {\n return this.management.getOpenBillingSubscriptionChange(provider, providerSubscriptionId);\n }\n\n async updateBillingSubscriptionChange(\n id: string,\n update: Parameters<BillingStore[\"updateBillingSubscriptionChange\"]>[1],\n ): Promise<void> {\n await this.management.updateBillingSubscriptionChange(id, update);\n }\n\n async recordSubscriptionConflict(\n input: Parameters<BillingStore[\"recordSubscriptionConflict\"]>[0],\n ): Promise<void> {\n await this.management.recordSubscriptionConflict(input);\n }\n\n async updateUserPreferences(preferences: BillingPreferences): Promise<void> {\n await this.management.updateUserPreferences(preferences);\n }\n\n async getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null> {\n return this.management.getAutoRechargeProfile(userId);\n }\n\n async getActiveCatalogDocument(): Promise<JsonObject | null> {\n return this.management.getActiveCatalogDocument();\n }\n\n async listCancellableProviderSubscriptionIds(userId: string): Promise<string[]> {\n return this.management.listCancellableProviderSubscriptionIds(userId);\n }\n\n async listCancellableSubscriptions(userId: string): Promise<BillingSubscriptionState[]> {\n return this.management.listCancellableSubscriptions(userId);\n }\n\n async pseudonymizeFinancialSubject(userId: string): Promise<void> {\n await this.management.pseudonymizeFinancialSubject(userId);\n }\n\n async upsertAutoRechargeProfile(\n profile: BillingAutoRechargeProfile,\n options?: { resetCooldown?: boolean },\n ): Promise<void> {\n await this.management.upsertAutoRechargeProfile(profile, options);\n }\n\n async claimAutoRechargeAttempt(input: {\n userId: string;\n idempotencyKey: string;\n }): Promise<BillingAutoRechargeAttempt | null> {\n return this.management.claimAutoRechargeAttempt(input);\n }\n\n async updateAutoRechargeAttempt(input: {\n id: string;\n state: BillingAutoRechargeAttemptState;\n providerAttemptId?: string | null;\n failureCode?: string | null;\n failureMessage?: string | null;\n metadata?: JsonObject;\n }): Promise<void> {\n await this.management.updateAutoRechargeAttempt(input);\n }\n\n async updateAutoRechargeAttemptByProviderPayment(input: {\n provider: string;\n providerPaymentId: string;\n state: BillingAutoRechargeAttemptState;\n failureCode?: string | null;\n failureMessage?: string | null;\n }): Promise<void> {\n await this.management.updateAutoRechargeAttemptByProviderPayment(input);\n }\n\n async countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number> {\n return this.management.countAutoRechargeAttempts(userId, since);\n }\n\n async getCustomerByUserId(\n userId: string,\n provider?: string | null,\n ): Promise<BillingCustomerRecord | null> {\n return this.management.getCustomerByUserId(userId, provider);\n }\n\n async resolveOffer(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingOfferResult | null> {\n return this.management.resolveOffer(provider, productId, priceId);\n }\n\n async resolveOfferByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingOfferResult | null> {\n return this.management.resolveOfferByLookup(provider, lookupKey);\n }\n\n async resolveTopup(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingTopupResult | null> {\n return this.management.resolveTopup(provider, productId, priceId);\n }\n\n async resolveTopupByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingTopupResult | null> {\n return this.management.resolveTopupByLookup(provider, lookupKey);\n }\n\n async upsertCustomer(\n provider: string,\n providerCustomerId: string,\n userId: string,\n email?: string | null,\n ): Promise<void> {\n await this.management.upsertCustomer(provider, providerCustomerId, userId, email);\n }\n\n async getUserSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n const subscription = await this.store.getUserSubscription(userId, [\n SUBSCRIPTION_STATUS.ACTIVE,\n SUBSCRIPTION_STATUS.TRIALING,\n SUBSCRIPTION_STATUS.CANCELED,\n SUBSCRIPTION_STATUS.PAST_DUE,\n SUBSCRIPTION_STATUS.INCOMPLETE,\n // EXPIRED excluded — expired subscriptions are not \"current\" for billing purposes.\n ]);\n return this.expireGraceIfNeeded(subscription);\n }\n\n async getBlockingSubscription(userId: string): Promise<BillingSubscriptionState | null> {\n const subscription = await this.store.getUserSubscription(userId, [\n SUBSCRIPTION_STATUS.ACTIVE,\n SUBSCRIPTION_STATUS.TRIALING,\n SUBSCRIPTION_STATUS.PAST_DUE,\n SUBSCRIPTION_STATUS.INCOMPLETE,\n ]);\n return this.expireGraceIfNeeded(subscription);\n }\n\n async expirePastDueGracePeriods(now = new Date()): Promise<number> {\n if (!this.events.hasProvisioning) return 0;\n const asOf = now.toISOString();\n const candidates = await this.store.listExpiredGraceSubscriptions(asOf);\n let expiredCount = 0;\n for (const candidate of candidates) {\n if (!candidate.subscriptionId || !candidate.graceEndsAt) continue;\n if (\n await this.store.expireSubscriptionGracePeriod(\n candidate.userId,\n candidate.subscriptionId,\n candidate.graceEndsAt,\n asOf,\n this.events.terminalPlanKey,\n )\n ) {\n expiredCount += 1;\n }\n }\n return expiredCount;\n }\n\n private async expireGraceIfNeeded(\n subscription: BillingSubscriptionState | null,\n ): Promise<BillingSubscriptionState | null> {\n if (\n !subscription ||\n !this.events.hasProvisioning ||\n subscription.status !== \"past_due\" ||\n subscription.graceExpiredAt ||\n !subscription.graceEndsAt ||\n new Date(subscription.graceEndsAt).getTime() > Date.now() ||\n !subscription.subscriptionId\n ) {\n return subscription;\n }\n\n const expiredAt = new Date().toISOString();\n const marked = await this.store.expireSubscriptionGracePeriod(\n subscription.userId,\n subscription.subscriptionId,\n subscription.graceEndsAt,\n expiredAt,\n this.events.terminalPlanKey,\n );\n return marked ? { ...subscription, graceExpiredAt: expiredAt } : subscription;\n }\n\n /** Invalidate cached provider offer resolution. */\n invalidateOfferCache(): void {\n this.events.invalidateOfferCache();\n }\n\n async ingestBillingEvent(event: BillingEvent): Promise<BillingEventResult> {\n return this.events.ingestBillingEvent(event);\n }\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { BillingAutoRechargeAttempt, BillingAutoRechargeProfile, BillingCustomerRecord, BillingCreditPostingResult, BillingEventClaim, BillingOfferResult, BillingPaymentRecord, BillingPreferences, BillingSubscriptionChange, BillingSubscriptionChangeInput, BillingSubscriptionState, CheckoutIntent, BillingTopupResult, BillingInvoiceRecord } from "./types/index.js";
|
|
1
|
+
import type { BillingAutoRechargeAttempt, BillingAutoRechargeProfile, BillingCustomerRecord, BillingCreditPostingResult, BillingEventClaim, BillingOfferResult, BillingPaymentRecord, BillingPreferences, BillingSubscriptionChange, BillingSubscriptionChangeInput, BillingSubscriptionState, BillingSubscriptionStatus, SubscriptionEntitlementOutcome, CheckoutIntent, BillingTopupResult, BillingInvoiceRecord } from "./types/index.js";
|
|
2
2
|
import type { AutoRechargeAttemptClaim, AutoRechargeAttemptUpdate, AutoRechargeProviderPaymentUpdate, BillingCreditGrantCreate, BillingDisputeUpsert, BillingInvoiceUpsert, BillingPaymentUpsert, BillingRefundUpsert, BillingSubscriptionChangeUpdate, BillingSubscriptionConflictCreate, CheckoutIntentCreate, CheckoutIntentUpdate } from "./contracts.js";
|
|
3
3
|
import type { Decimal } from "decimal.js";
|
|
4
4
|
import type { ProviderEnvironment } from "../providers/environment.js";
|
|
5
|
+
import type { JsonObject } from "../shared/json.js";
|
|
5
6
|
/**
|
|
6
7
|
* Abstract billing store — provider-agnostic persistence layer for
|
|
7
8
|
* subscription lifecycle state.
|
|
@@ -15,7 +16,7 @@ export declare abstract class BillingStore {
|
|
|
15
16
|
abstract updateCheckoutIntent(id: string, update: CheckoutIntentUpdate): Promise<void>;
|
|
16
17
|
abstract getCheckoutIntent(id: string, subjectId: string): Promise<CheckoutIntent | null>;
|
|
17
18
|
abstract resolveBillingOffer(provider: string, productId?: string | null, priceId?: string | null): Promise<BillingOfferResult | null>;
|
|
18
|
-
abstract claimBillingEvent(provider: string, eventId: string, eventType: string, envelope?:
|
|
19
|
+
abstract claimBillingEvent(provider: string, eventId: string, eventType: string, envelope?: JsonObject): Promise<BillingEventClaim>;
|
|
19
20
|
abstract completeBillingEvent(provider: string, eventId: string, claimToken: string): Promise<boolean>;
|
|
20
21
|
abstract failBillingEvent(provider: string, eventId: string, claimToken: string, error?: string): Promise<boolean>;
|
|
21
22
|
abstract upsertBillingCustomer(provider: string, providerCustomerId: string, userId: string, email?: string | null): Promise<void>;
|
|
@@ -25,7 +26,8 @@ export declare abstract class BillingStore {
|
|
|
25
26
|
abstract createBillingSubscriptionChange(input: BillingSubscriptionChangeInput): Promise<BillingSubscriptionChange>;
|
|
26
27
|
abstract getOpenBillingSubscriptionChange(provider: string, providerSubscriptionId: string): Promise<BillingSubscriptionChange | null>;
|
|
27
28
|
abstract listExpiredGraceSubscriptions(now: string, limit?: number): Promise<BillingSubscriptionState[]>;
|
|
28
|
-
abstract
|
|
29
|
+
abstract reconcileSubscriptionEntitlement(subjectId: string, subscriptionId: string, billingEventId: string, expectedStatus: BillingSubscriptionStatus, expectedProviderUpdatedAt: string, planAssignedAt: Date | string | null, applyEntitlement: boolean, terminalPlanKey: string | null, reason: string): Promise<SubscriptionEntitlementOutcome>;
|
|
30
|
+
abstract expireSubscriptionGracePeriod(subjectId: string, subscriptionId: string, expectedGraceEndsAt: string, expiredAt: string, terminalPlanKey: string | null): Promise<boolean>;
|
|
29
31
|
abstract updateBillingSubscriptionChange(id: string, update: BillingSubscriptionChangeUpdate): Promise<void>;
|
|
30
32
|
abstract getUserSubscription(userId: string, statuses?: string[]): Promise<BillingSubscriptionState | null>;
|
|
31
33
|
abstract resolveCreditTopup(provider: string, productId?: string | null, priceId?: string | null): Promise<BillingTopupResult | null>;
|
|
@@ -42,12 +44,10 @@ export declare abstract class BillingStore {
|
|
|
42
44
|
abstract upsertBillingInvoice(options: BillingInvoiceUpsert): Promise<void>;
|
|
43
45
|
abstract upsertBillingDispute(options: BillingDisputeUpsert): Promise<void>;
|
|
44
46
|
abstract getBillingPayment(provider: string, providerPaymentId: string): Promise<BillingPaymentRecord | null>;
|
|
45
|
-
abstract getActiveCatalogDocument(): Promise<
|
|
47
|
+
abstract getActiveCatalogDocument(): Promise<JsonObject | null>;
|
|
46
48
|
abstract pseudonymizeFinancialSubject(userId: string): Promise<boolean>;
|
|
47
49
|
abstract getUserSubscriptions(userId: string): Promise<BillingSubscriptionState[]>;
|
|
48
50
|
abstract recordSubscriptionConflict(input: BillingSubscriptionConflictCreate): Promise<void>;
|
|
49
|
-
/** Select a current provider subscription as the subject's entitlement source. */
|
|
50
|
-
abstract selectSubscriptionEntitlementSource(userId: string, provider: string, providerSubscriptionId?: string | null): Promise<boolean>;
|
|
51
51
|
abstract getBillingPreferences(userId: string): Promise<BillingPreferences | null>;
|
|
52
52
|
abstract upsertBillingPreferences(prefs: BillingPreferences): Promise<void>;
|
|
53
53
|
abstract getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing-store.d.ts","sourceRoot":"","sources":["../../src/billing/billing-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,8BAA8B,EAC9B,wBAAwB,EACxB,cAAc,EACd,kBAAkB,EAClB,oBAAoB,EACrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,iCAAiC,EACjC,wBAAwB,EACxB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB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,QAAQ,CAAC,wBAAwB,CAC/B,KAAK,EAAE,wBAAwB,GAC9B,OAAO,CAAC,0BAA0B,GAAG,IAAI,CAAC,CAAC;IAC9C,QAAQ,CAAC,yBAAyB,CAAC,KAAK,EAAE,yBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpF,QAAQ,CAAC,0CAA0C,CACjD,KAAK,EAAE,iCAAiC,GACvC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,QAAQ,CAAC,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE1F,QAAQ,CAAC,0BAA0B,CACjC,MAAM,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CAAC;CAC1C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing-store.js","sourceRoot":"","sources":["../../src/billing/billing-store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"billing-store.js","sourceRoot":"","sources":["../../src/billing/billing-store.ts"],"names":[],"mappings":"AAoCA;;;;;GAKG;AACH,MAAM,OAAgB,YAAY;IAChC,iFAAiF;IACxE,mBAAmB,CAAuB;CA0KpD","sourcesContent":["import type {\n BillingAutoRechargeAttempt,\n BillingAutoRechargeProfile,\n BillingCustomerRecord,\n BillingCreditPostingResult,\n BillingEventClaim,\n BillingOfferResult,\n BillingPaymentRecord,\n BillingPreferences,\n BillingSubscriptionChange,\n BillingSubscriptionChangeInput,\n BillingSubscriptionState,\n BillingSubscriptionStatus,\n SubscriptionEntitlementOutcome,\n CheckoutIntent,\n BillingTopupResult,\n BillingInvoiceRecord,\n} from \"./types/index.js\";\nimport type {\n AutoRechargeAttemptClaim,\n AutoRechargeAttemptUpdate,\n AutoRechargeProviderPaymentUpdate,\n BillingCreditGrantCreate,\n BillingDisputeUpsert,\n BillingInvoiceUpsert,\n BillingPaymentUpsert,\n BillingRefundUpsert,\n BillingSubscriptionChangeUpdate,\n BillingSubscriptionConflictCreate,\n CheckoutIntentCreate,\n CheckoutIntentUpdate,\n} from \"./contracts.js\";\nimport type { Decimal } from \"decimal.js\";\nimport type { ProviderEnvironment } from \"../providers/environment.js\";\nimport type { JsonObject } from \"../shared/json.js\";\n\n/**\n * Abstract billing store — provider-agnostic persistence layer for\n * subscription lifecycle state.\n *\n * Mirrors Python bursar/billing/store.py.\n */\nexport abstract class BillingStore {\n /** Exposed by environment-partitioned stores so composition can reject drift. */\n readonly providerEnvironment?: ProviderEnvironment;\n\n abstract createOrGetCheckoutIntent(input: CheckoutIntentCreate): Promise<CheckoutIntent>;\n\n abstract updateCheckoutIntent(id: string, update: CheckoutIntentUpdate): Promise<void>;\n\n abstract getCheckoutIntent(id: string, subjectId: string): Promise<CheckoutIntent | null>;\n\n abstract resolveBillingOffer(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingOfferResult | null>;\n\n abstract claimBillingEvent(\n provider: string,\n eventId: string,\n eventType: string,\n envelope?: JsonObject,\n ): Promise<BillingEventClaim>;\n\n abstract completeBillingEvent(\n provider: string,\n eventId: string,\n claimToken: string,\n ): Promise<boolean>;\n\n abstract failBillingEvent(\n provider: string,\n eventId: string,\n claimToken: string,\n error?: string,\n ): Promise<boolean>;\n\n abstract upsertBillingCustomer(\n provider: string,\n providerCustomerId: string,\n userId: string,\n email?: string | null,\n ): Promise<void>;\n\n abstract upsertBillingSubscription(state: BillingSubscriptionState): Promise<void>;\n\n abstract getBillingCustomer(provider: string, providerCustomerId: string): Promise<string | null>;\n\n abstract getBillingSubscription(\n provider: string,\n providerSubscriptionId: string,\n ): Promise<BillingSubscriptionState | null>;\n\n abstract createBillingSubscriptionChange(\n input: BillingSubscriptionChangeInput,\n ): Promise<BillingSubscriptionChange>;\n\n abstract getOpenBillingSubscriptionChange(\n provider: string,\n providerSubscriptionId: string,\n ): Promise<BillingSubscriptionChange | null>;\n\n abstract listExpiredGraceSubscriptions(\n now: string,\n limit?: number,\n ): Promise<BillingSubscriptionState[]>;\n\n abstract reconcileSubscriptionEntitlement(\n subjectId: string,\n subscriptionId: string,\n billingEventId: string,\n expectedStatus: BillingSubscriptionStatus,\n expectedProviderUpdatedAt: string,\n planAssignedAt: Date | string | null,\n applyEntitlement: boolean,\n terminalPlanKey: string | null,\n reason: string,\n ): Promise<SubscriptionEntitlementOutcome>;\n\n abstract expireSubscriptionGracePeriod(\n subjectId: string,\n subscriptionId: string,\n expectedGraceEndsAt: string,\n expiredAt: string,\n terminalPlanKey: string | null,\n ): Promise<boolean>;\n\n abstract updateBillingSubscriptionChange(\n id: string,\n update: BillingSubscriptionChangeUpdate,\n ): Promise<void>;\n\n abstract getUserSubscription(\n userId: string,\n statuses?: string[],\n ): Promise<BillingSubscriptionState | null>;\n\n abstract resolveCreditTopup(\n provider: string,\n productId?: string | null,\n priceId?: string | null,\n ): Promise<BillingTopupResult | null>;\n abstract resolveCreditTopupByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingTopupResult | null>;\n\n abstract resolveBillingOfferByLookup(\n provider: string,\n lookupKey: string,\n ): Promise<BillingOfferResult | null>;\n\n abstract computeTopupCredits(\n amountMinor: number,\n topupConfig: BillingTopupResult,\n ): Promise<Decimal>;\n\n abstract upsertBillingPayment(options: BillingPaymentUpsert): Promise<string>;\n abstract createBillingCreditGrant(input: BillingCreditGrantCreate): Promise<string>;\n abstract grantBillingCredit(\n grantId: string,\n idempotencyKey: string,\n ): Promise<BillingCreditPostingResult>;\n abstract getBillingCreditGrantByPayment(paymentId: string): Promise<string | null>;\n abstract postBillingRefund(\n refundId: string,\n grantId: string,\n amountMinor: number,\n idempotencyKey: string,\n ): Promise<BillingCreditPostingResult>;\n\n abstract listBillingInvoices(userId: string): Promise<BillingInvoiceRecord[]>;\n\n abstract upsertBillingRefund(options: BillingRefundUpsert): Promise<string>;\n\n abstract upsertBillingInvoice(options: BillingInvoiceUpsert): Promise<void>;\n\n abstract upsertBillingDispute(options: BillingDisputeUpsert): Promise<void>;\n\n abstract getBillingPayment(\n provider: string,\n providerPaymentId: string,\n ): Promise<BillingPaymentRecord | null>;\n\n abstract getActiveCatalogDocument(): Promise<JsonObject | null>;\n\n abstract pseudonymizeFinancialSubject(userId: string): Promise<boolean>;\n\n abstract getUserSubscriptions(userId: string): Promise<BillingSubscriptionState[]>;\n\n abstract recordSubscriptionConflict(input: BillingSubscriptionConflictCreate): Promise<void>;\n\n abstract getBillingPreferences(userId: string): Promise<BillingPreferences | null>;\n\n abstract upsertBillingPreferences(prefs: BillingPreferences): Promise<void>;\n abstract getAutoRechargeProfile(userId: string): Promise<BillingAutoRechargeProfile | null>;\n abstract upsertAutoRechargeProfile(\n profile: BillingAutoRechargeProfile,\n options?: { resetCooldown?: boolean },\n ): Promise<void>;\n abstract claimAutoRechargeAttempt(\n input: AutoRechargeAttemptClaim,\n ): Promise<BillingAutoRechargeAttempt | null>;\n abstract updateAutoRechargeAttempt(input: AutoRechargeAttemptUpdate): Promise<void>;\n abstract updateAutoRechargeAttemptByProviderPayment(\n input: AutoRechargeProviderPaymentUpdate,\n ): Promise<void>;\n abstract countAutoRechargeAttempts(userId: string, since: string | Date): Promise<number>;\n\n abstract getBillingCustomerByUserId(\n userId: string,\n provider?: string | null,\n ): Promise<BillingCustomerRecord | null>;\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AutoRechargeService } from "./auto-recharge-service.js";
|
|
2
2
|
import type { Decimal } from "decimal.js";
|
|
3
|
+
import type { JsonObject } from "../shared/json.js";
|
|
3
4
|
import type { BillingAutoRechargeAttempt, BillingAutoRechargeAttemptState, BillingAutoRechargeProfile, BillingCustomerRecord, BillingDisputeInfo, BillingEvent, BillingEventResult, BillingInvoiceInfo, BillingInvoiceRecord, BillingOfferResult, BillingPaymentInfo, BillingPreferences, BillingRefundInfo, BillingSubscriptionChange, BillingSubscriptionChangeInput, BillingSubscriptionState, BillingTopupResult, CheckoutIntent } from "./types/index.js";
|
|
4
5
|
export interface CheckoutIntentCreate {
|
|
5
6
|
subjectId: string;
|
|
@@ -26,7 +27,7 @@ export interface BillingSubscriptionConflictCreate {
|
|
|
26
27
|
duplicateSubscriptionId: string;
|
|
27
28
|
existingSubscriptionId?: string | null;
|
|
28
29
|
eventId?: string | null;
|
|
29
|
-
metadata?:
|
|
30
|
+
metadata?: JsonObject;
|
|
30
31
|
}
|
|
31
32
|
export interface BillingPaymentUpsert {
|
|
32
33
|
provider: string;
|
|
@@ -39,7 +40,7 @@ export interface BillingPaymentUpsert {
|
|
|
39
40
|
purpose: "subscription" | "credit_topup";
|
|
40
41
|
status: "pending" | "succeeded" | "failed" | "canceled";
|
|
41
42
|
providerUpdatedAt: string;
|
|
42
|
-
metadata?:
|
|
43
|
+
metadata?: JsonObject | null;
|
|
43
44
|
}
|
|
44
45
|
export interface BillingCreditGrantCreate {
|
|
45
46
|
paymentId?: string | null;
|
|
@@ -59,7 +60,7 @@ export interface BillingRefundUpsert {
|
|
|
59
60
|
reason?: string | null;
|
|
60
61
|
status: "pending" | "succeeded" | "failed" | "canceled";
|
|
61
62
|
providerUpdatedAt: string;
|
|
62
|
-
metadata?:
|
|
63
|
+
metadata?: JsonObject | null;
|
|
63
64
|
}
|
|
64
65
|
export interface BillingInvoiceUpsert {
|
|
65
66
|
provider: string;
|
|
@@ -72,7 +73,7 @@ export interface BillingInvoiceUpsert {
|
|
|
72
73
|
currency: string;
|
|
73
74
|
periodStart?: string | null;
|
|
74
75
|
periodEnd?: string | null;
|
|
75
|
-
metadata?:
|
|
76
|
+
metadata?: JsonObject | null;
|
|
76
77
|
providerUpdatedAt: string;
|
|
77
78
|
}
|
|
78
79
|
export interface BillingDisputeUpsert {
|
|
@@ -81,7 +82,7 @@ export interface BillingDisputeUpsert {
|
|
|
81
82
|
providerPaymentId: string;
|
|
82
83
|
status: "needs_response" | "under_review" | "won" | "lost" | "closed";
|
|
83
84
|
reason?: string | null;
|
|
84
|
-
metadata?:
|
|
85
|
+
metadata?: JsonObject | null;
|
|
85
86
|
providerUpdatedAt: string;
|
|
86
87
|
}
|
|
87
88
|
export interface AutoRechargeAttemptClaim {
|
|
@@ -94,7 +95,7 @@ export interface AutoRechargeAttemptUpdate {
|
|
|
94
95
|
providerAttemptId?: string | null;
|
|
95
96
|
failureCode?: string | null;
|
|
96
97
|
failureMessage?: string | null;
|
|
97
|
-
metadata?:
|
|
98
|
+
metadata?: JsonObject;
|
|
98
99
|
}
|
|
99
100
|
export interface AutoRechargeProviderPaymentUpdate {
|
|
100
101
|
provider: string;
|
|
@@ -118,7 +119,7 @@ export interface BillingCapability extends BillingEventSink {
|
|
|
118
119
|
getActiveSubscription(userId: string): Promise<BillingSubscriptionState | null>;
|
|
119
120
|
getBlockingSubscription(userId: string): Promise<BillingSubscriptionState | null>;
|
|
120
121
|
getUserPreferences(userId: string): Promise<BillingPreferences | null>;
|
|
121
|
-
getActiveCatalogDocument(): Promise<
|
|
122
|
+
getActiveCatalogDocument(): Promise<JsonObject | null>;
|
|
122
123
|
listCancellableProviderSubscriptionIds(userId: string): Promise<string[]>;
|
|
123
124
|
listCancellableSubscriptions(userId: string): Promise<BillingSubscriptionState[]>;
|
|
124
125
|
listBillingInvoices(userId: string): Promise<BillingInvoiceRecord[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contracts.d.ts","sourceRoot":"","sources":["../../src/billing/contracts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,KAAK,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EAClB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,EAClB,kBAAk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