@zonastery/bursar 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -62
- package/dist/billing/auto-recharge-port.d.ts +3 -3
- package/dist/billing/auto-recharge-port.d.ts.map +1 -1
- package/dist/billing/auto-recharge-port.js.map +1 -1
- package/dist/billing/auto-recharge-service.d.ts +5 -4
- package/dist/billing/auto-recharge-service.d.ts.map +1 -1
- package/dist/billing/auto-recharge-service.js +64 -34
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +5 -5
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +2 -2
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +10 -9
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js.map +1 -1
- package/dist/billing/contracts.d.ts +31 -31
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +49 -10
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +1 -0
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +37 -10
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +69 -72
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +5 -5
- package/dist/billing/management.d.ts.map +1 -1
- package/dist/billing/management.js +2 -2
- package/dist/billing/management.js.map +1 -1
- package/dist/billing/policy-window.js.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +3 -3
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +205 -60
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/customer.js +19 -11
- package/dist/billing/postgres/repositories/customer.js.map +1 -1
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/dispute.js +3 -1
- package/dist/billing/postgres/repositories/dispute.js.map +1 -1
- package/dist/billing/postgres/repositories/event.d.ts +14 -7
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js +21 -20
- package/dist/billing/postgres/repositories/event.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +3 -3
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +53 -11
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts +20 -17
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +123 -57
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +23 -20
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +43 -37
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/preferences.d.ts +3 -9
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/preferences.js +45 -7
- package/dist/billing/postgres/repositories/preferences.js.map +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/refund.js +13 -9
- package/dist/billing/postgres/repositories/refund.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts +15 -0
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription-change.js +234 -0
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +38 -17
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +199 -91
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts +13 -15
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/topup.js +65 -34
- package/dist/billing/postgres/repositories/topup.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +23 -14
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +265 -228
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/service-types.d.ts +6 -5
- package/dist/billing/service-types.d.ts.map +1 -1
- package/dist/billing/service-types.js.map +1 -1
- package/dist/billing/types/auto-recharge.d.ts +6 -4
- package/dist/billing/types/auto-recharge.d.ts.map +1 -1
- package/dist/billing/types/auto-recharge.js.map +1 -1
- package/dist/billing/types/catalog.d.ts +18 -35
- package/dist/billing/types/catalog.d.ts.map +1 -1
- package/dist/billing/types/catalog.js.map +1 -1
- package/dist/billing/types/common.d.ts +0 -1
- package/dist/billing/types/common.d.ts.map +1 -1
- package/dist/billing/types/common.js.map +1 -1
- package/dist/billing/types/documents.d.ts +38 -12
- package/dist/billing/types/documents.d.ts.map +1 -1
- package/dist/billing/types/documents.js.map +1 -1
- package/dist/billing/types/events.d.ts +2 -0
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +238 -0
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/index.js.map +1 -1
- package/dist/billing/types/preferences.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +8 -8
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/bursar.d.ts +33 -11
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +58 -22
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts +1 -0
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +1 -0
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/errors.d.ts +4 -14
- package/dist/commerce/errors.d.ts.map +1 -1
- package/dist/commerce/errors.js +4 -21
- package/dist/commerce/errors.js.map +1 -1
- package/dist/commerce/index.d.ts +1 -0
- package/dist/commerce/index.d.ts.map +1 -1
- package/dist/commerce/index.js.map +1 -1
- package/dist/commerce/plan-change.js.map +1 -1
- package/dist/commerce/provider-registry.d.ts.map +1 -1
- package/dist/commerce/provider-registry.js +27 -2
- package/dist/commerce/provider-registry.js.map +1 -1
- package/dist/commerce/service.d.ts +15 -7
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +130 -77
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts +33 -13
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +30 -13
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +16 -5
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config/types.d.ts +14 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +7 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +64 -1
- package/dist/config.js.map +1 -1
- package/dist/credits/{pricing-runtime.d.ts → catalog-runtime.d.ts} +9 -8
- package/dist/credits/catalog-runtime.d.ts.map +1 -0
- package/dist/credits/{pricing-runtime.js → catalog-runtime.js} +58 -38
- package/dist/credits/catalog-runtime.js.map +1 -0
- package/dist/credits/events.d.ts +4 -4
- package/dist/credits/events.d.ts.map +1 -1
- package/dist/credits/events.js +1 -1
- package/dist/credits/events.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +14 -14
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +18 -17
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -2
- package/dist/credits/low-balance-monitor.d.ts.map +1 -1
- package/dist/credits/low-balance-monitor.js.map +1 -1
- package/dist/credits/postgres/repositories/analytics.d.ts +39 -35
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +55 -53
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts +28 -30
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +100 -59
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/bucket.d.ts +14 -8
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/bucket.js +22 -21
- package/dist/credits/postgres/repositories/bucket.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +31 -0
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/catalog.js +90 -0
- package/dist/credits/postgres/repositories/catalog.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +32 -21
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +171 -62
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/lease.d.ts +21 -18
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/lease.js +157 -65
- package/dist/credits/postgres/repositories/lease.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +94 -42
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js +134 -104
- package/dist/credits/postgres/repositories/plan.js.map +1 -1
- package/dist/credits/postgres/repositories/team.d.ts +36 -27
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/team.js +96 -52
- package/dist/credits/postgres/repositories/team.js.map +1 -1
- package/dist/credits/postgres/store.d.ts +31 -21
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +325 -235
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts +7 -9
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +42 -63
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/queries.d.ts +5 -5
- package/dist/credits/queries.d.ts.map +1 -1
- package/dist/credits/queries.js +4 -4
- package/dist/credits/queries.js.map +1 -1
- package/dist/credits/service-errors.js.map +1 -1
- package/dist/credits/service-types.d.ts +42 -7
- package/dist/credits/service-types.d.ts.map +1 -1
- package/dist/credits/service-types.js.map +1 -1
- package/dist/credits/service.d.ts +51 -58
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +178 -113
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +25 -22
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js +9 -5
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +50 -12
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/admission.d.ts +17 -6
- package/dist/credits/types/admission.d.ts.map +1 -1
- package/dist/credits/types/admission.js.map +1 -1
- package/dist/credits/types/analytics.d.ts +2 -2
- package/dist/credits/types/analytics.d.ts.map +1 -1
- package/dist/credits/types/analytics.js.map +1 -1
- package/dist/credits/types/catalog.d.ts +21 -14
- package/dist/credits/types/catalog.d.ts.map +1 -1
- package/dist/credits/types/catalog.js.map +1 -1
- package/dist/credits/types/grants.d.ts +22 -8
- package/dist/credits/types/grants.d.ts.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/index.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +19 -0
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/credits/types/quotas.js.map +1 -1
- package/dist/credits/types/teams.d.ts +18 -6
- package/dist/credits/types/teams.d.ts.map +1 -1
- package/dist/credits/types/teams.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +24 -10
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +105 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +137 -9
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts.map +1 -1
- package/dist/expr/ast.js.map +1 -1
- package/dist/expr/evaluator.d.ts.map +1 -1
- package/dist/expr/evaluator.js +29 -17
- package/dist/expr/evaluator.js.map +1 -1
- package/dist/expr/language.js.map +1 -1
- package/dist/expr/parser.d.ts.map +1 -1
- package/dist/expr/parser.js +11 -4
- package/dist/expr/parser.js.map +1 -1
- package/dist/expr/tokenizer.d.ts.map +1 -1
- package/dist/expr/tokenizer.js +17 -13
- package/dist/expr/tokenizer.js.map +1 -1
- package/dist/expr.d.ts +2 -2
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +7 -9
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +30 -9
- package/dist/generated/pricing-config.types.d.ts +14 -3
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/index.d.ts +11 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/integrations/better-auth.d.ts +23 -0
- package/dist/integrations/better-auth.d.ts.map +1 -0
- package/dist/integrations/better-auth.js +54 -0
- package/dist/integrations/better-auth.js.map +1 -0
- package/dist/{load-pricing-file.d.ts → load-config-file.d.ts} +4 -4
- package/dist/load-config-file.d.ts.map +1 -0
- package/dist/{load-pricing-file.js → load-config-file.js} +23 -14
- package/dist/load-config-file.js.map +1 -0
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +3 -3
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +5 -0
- package/dist/providers/_shared.d.ts.map +1 -1
- package/dist/providers/_shared.js +77 -14
- package/dist/providers/_shared.js.map +1 -1
- package/dist/providers/dodo/better-auth.d.ts +29 -0
- package/dist/providers/dodo/better-auth.d.ts.map +1 -0
- package/dist/providers/dodo/better-auth.js +23 -0
- package/dist/providers/dodo/better-auth.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +32 -70
- package/dist/providers/dodo/client-contract.d.ts.map +1 -1
- package/dist/providers/dodo/client-contract.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +3 -1
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +219 -148
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +5 -2
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js +2 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/nextjs.d.ts +16 -0
- package/dist/providers/dodo/nextjs.d.ts.map +1 -0
- package/dist/providers/dodo/nextjs.js +16 -0
- package/dist/providers/dodo/nextjs.js.map +1 -0
- package/dist/providers/dodo/provider.d.ts +30 -11
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +192 -122
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/react-native.d.ts +56 -0
- package/dist/providers/dodo/react-native.d.ts.map +1 -0
- package/dist/providers/dodo/react-native.js +57 -0
- package/dist/providers/dodo/react-native.js.map +1 -0
- package/dist/providers/dodo/webhook-bridge.d.ts +17 -0
- package/dist/providers/dodo/webhook-bridge.d.ts.map +1 -0
- package/dist/providers/dodo/webhook-bridge.js +21 -0
- package/dist/providers/dodo/webhook-bridge.js.map +1 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/index.d.ts +1 -0
- package/dist/providers/mock/index.d.ts.map +1 -1
- package/dist/providers/mock/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +8 -8
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +26 -19
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +214 -173
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/index.d.ts +1 -0
- package/dist/providers/stripe/index.d.ts.map +1 -1
- package/dist/providers/stripe/index.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +13 -11
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +287 -107
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +1 -2
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +26 -4
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +65 -23
- package/dist/retry.js.map +1 -1
- package/dist/shared/diagnostics.d.ts +7 -0
- package/dist/shared/diagnostics.d.ts.map +1 -0
- package/dist/shared/diagnostics.js +18 -0
- package/dist/shared/diagnostics.js.map +1 -0
- package/dist/shared/logger.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +43 -5
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +282 -30
- package/dist/shared/postgres-client.js.map +1 -1
- package/dist/shared/postgres-errors.d.ts +11 -0
- package/dist/shared/postgres-errors.d.ts.map +1 -0
- package/dist/shared/postgres-errors.js +105 -0
- package/dist/shared/postgres-errors.js.map +1 -0
- package/dist/shared/postgres-types.js.map +1 -1
- package/dist/shared/postgres-validation.d.ts +15 -3
- package/dist/shared/postgres-validation.d.ts.map +1 -1
- package/dist/shared/postgres-validation.js +58 -14
- package/dist/shared/postgres-validation.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +18 -3
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +0 -6
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +0 -2
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts +2 -1
- package/dist/storage/outbox-worker.d.ts.map +1 -1
- package/dist/storage/outbox-worker.js +17 -3
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +1 -1
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +1 -1
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +13 -3
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +35 -7
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +166 -53
- package/dist/storage/runtime.js.map +1 -1
- package/package.json +62 -7
- package/dist/credits/postgres/repositories/pricing.d.ts +0 -28
- package/dist/credits/postgres/repositories/pricing.d.ts.map +0 -1
- package/dist/credits/postgres/repositories/pricing.js +0 -70
- package/dist/credits/postgres/repositories/pricing.js.map +0 -1
- package/dist/credits/pricing-runtime.d.ts.map +0 -1
- package/dist/credits/pricing-runtime.js.map +0 -1
- package/dist/load-pricing-file.d.ts.map +0 -1
- package/dist/load-pricing-file.js.map +0 -1
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import Stripe from "stripe";
|
|
2
2
|
import type { CheckoutPaymentStatus, PaymentProvider } from "../types.js";
|
|
3
3
|
import { type ProviderLogger } from "../types.js";
|
|
4
|
-
import type { CheckoutParams, PortalParams, UpdatePaymentMethodParams, PaymentMethodSetupParams, CreateCustomerParams, PaymentMethodInfo, WebhookRequest, ChangePlanParams, PreviewChangePlanParams, ChangePlanPreview, SavedPaymentChargeParams, SavedPaymentChargeResult, SavedPaymentChargeQuote, WebhookResult } from "../types.js";
|
|
4
|
+
import type { CheckoutParams, CheckoutSessionResult, PortalParams, UpdatePaymentMethodParams, PaymentMethodSetupParams, CreateCustomerParams, PaymentMethodInfo, WebhookRequest, ChangePlanParams, PreviewChangePlanParams, ChangePlanPreview, SavedPaymentChargeParams, SavedPaymentChargeResult, SavedPaymentChargeQuote, WebhookResult } from "../types.js";
|
|
5
5
|
import type { BillingEventSink } from "../../bursar.js";
|
|
6
|
+
export interface StripeProviderOptions {
|
|
7
|
+
getClient: () => Stripe;
|
|
8
|
+
webhookSecret: string;
|
|
9
|
+
eventSink: BillingEventSink;
|
|
10
|
+
logger?: ProviderLogger | null;
|
|
11
|
+
}
|
|
6
12
|
export declare class StripeProvider implements PaymentProvider {
|
|
7
|
-
private getStripe;
|
|
8
|
-
private sink;
|
|
9
|
-
private webhookSecret;
|
|
10
13
|
readonly provider: "stripe";
|
|
11
|
-
|
|
12
|
-
private
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
private readonly getStripe;
|
|
15
|
+
private readonly sink;
|
|
16
|
+
private readonly webhookSecret;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
constructor(options: StripeProviderOptions);
|
|
19
|
+
createCheckoutSession(params: CheckoutParams): Promise<CheckoutSessionResult>;
|
|
17
20
|
getCheckoutSessionStatus(providerSessionId: string): Promise<{
|
|
18
21
|
paymentStatus: CheckoutPaymentStatus;
|
|
19
22
|
} | null>;
|
|
@@ -31,7 +34,6 @@ export declare class StripeProvider implements PaymentProvider {
|
|
|
31
34
|
reactivateSubscription(subscriptionId: string, idempotencyKey?: string): Promise<void>;
|
|
32
35
|
cancelScheduledPlanChange(_subscriptionId: string, providerOperationId?: string | null, idempotencyKey?: string): Promise<void>;
|
|
33
36
|
listPaymentMethods(customerId: string): Promise<PaymentMethodInfo[]>;
|
|
34
|
-
getDefaultPaymentMethod(customerId: string): Promise<PaymentMethodInfo | null>;
|
|
35
37
|
previewSavedPaymentCharge(params: SavedPaymentChargeParams): Promise<SavedPaymentChargeQuote>;
|
|
36
38
|
chargeSavedPaymentMethod(params: SavedPaymentChargeParams): Promise<SavedPaymentChargeResult>;
|
|
37
39
|
createCustomer(params: CreateCustomerParams): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../src/providers/stripe/provider.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAEL,KAAK,cAAc,EAEpB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,yBAAyB,EACzB,wBAAwB,EACxB,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,uBAAuB,EACvB,iBAAiB,EACjB,wBAAwB,EACxB,wBAAwB,EACxB,uBAAuB,EACvB,aAAa,EACd,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../src/providers/stripe/provider.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,18 +1,132 @@
|
|
|
1
1
|
import Stripe from "stripe";
|
|
2
2
|
import { deduplicatePaymentMethods, normalizeProviderLogger, } from "../types.js";
|
|
3
|
+
import { ProviderResponseError } from "../../errors.js";
|
|
3
4
|
import { handleStripeWebhook } from "./event-mapper.js";
|
|
5
|
+
function requireStripeText(value, operation, field) {
|
|
6
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
7
|
+
throw new ProviderResponseError("stripe", operation, { details: { field } });
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
function requireStripeInteger(value, operation, field, minimum = 0) {
|
|
12
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum) {
|
|
13
|
+
throw new ProviderResponseError("stripe", operation, { details: { field } });
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function requireStripeNumber(value, operation, field) {
|
|
18
|
+
const parsed = typeof value === "number"
|
|
19
|
+
? value
|
|
20
|
+
: typeof value === "string" && /^-?\d+(?:\.\d+)?$/.test(value)
|
|
21
|
+
? Number(value)
|
|
22
|
+
: Number.NaN;
|
|
23
|
+
if (!Number.isFinite(parsed)) {
|
|
24
|
+
throw new ProviderResponseError("stripe", operation, { details: { field } });
|
|
25
|
+
}
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
function savedPaymentStatus(status) {
|
|
29
|
+
switch (status) {
|
|
30
|
+
case "succeeded":
|
|
31
|
+
return "succeeded";
|
|
32
|
+
case "processing":
|
|
33
|
+
return "processing";
|
|
34
|
+
case "requires_action":
|
|
35
|
+
return "requires_customer_action";
|
|
36
|
+
case "requires_payment_method":
|
|
37
|
+
return "requires_payment_method";
|
|
38
|
+
case "requires_confirmation":
|
|
39
|
+
return "requires_confirmation";
|
|
40
|
+
case "requires_capture":
|
|
41
|
+
return "requires_capture";
|
|
42
|
+
case "canceled":
|
|
43
|
+
return "cancelled";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function scopedIdempotencyKey(key, scope) {
|
|
47
|
+
if (!key)
|
|
48
|
+
return undefined;
|
|
49
|
+
const suffix = `:${scope}`;
|
|
50
|
+
return `${key.slice(0, 255 - suffix.length)}${suffix}`;
|
|
51
|
+
}
|
|
52
|
+
function requestOptions(idempotencyKey) {
|
|
53
|
+
return idempotencyKey ? { idempotencyKey } : undefined;
|
|
54
|
+
}
|
|
55
|
+
function expandableId(value) {
|
|
56
|
+
if (!value)
|
|
57
|
+
return undefined;
|
|
58
|
+
return typeof value === "string" ? value : value.id;
|
|
59
|
+
}
|
|
60
|
+
function mapPaymentIntentStatus(intent) {
|
|
61
|
+
switch (intent?.status) {
|
|
62
|
+
case "succeeded":
|
|
63
|
+
return "succeeded";
|
|
64
|
+
case "processing":
|
|
65
|
+
return "processing";
|
|
66
|
+
case "requires_action":
|
|
67
|
+
return "requires_customer_action";
|
|
68
|
+
case "requires_payment_method":
|
|
69
|
+
return "requires_payment_method";
|
|
70
|
+
case "requires_confirmation":
|
|
71
|
+
return "requires_confirmation";
|
|
72
|
+
case "requires_capture":
|
|
73
|
+
return "requires_capture";
|
|
74
|
+
case "canceled":
|
|
75
|
+
return "cancelled";
|
|
76
|
+
default:
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function schedulePhaseParams(phase) {
|
|
81
|
+
const items = phase.items.map((item) => ({
|
|
82
|
+
price: requireStripeText(expandableId(item.price), "changePlan", "schedule.phases.items.price"),
|
|
83
|
+
quantity: item.quantity,
|
|
84
|
+
...(item.metadata ? { metadata: item.metadata } : {}),
|
|
85
|
+
...(item.tax_rates
|
|
86
|
+
? {
|
|
87
|
+
tax_rates: item.tax_rates.map((taxRate) => requireStripeText(expandableId(taxRate), "changePlan", "schedule.phases.items.tax_rates")),
|
|
88
|
+
}
|
|
89
|
+
: {}),
|
|
90
|
+
}));
|
|
91
|
+
if (items.length === 0) {
|
|
92
|
+
throw new ProviderResponseError("stripe", "changePlan", {
|
|
93
|
+
details: { field: "schedule.phases.items" },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
items,
|
|
98
|
+
start_date: phase.start_date,
|
|
99
|
+
end_date: phase.end_date,
|
|
100
|
+
...(phase.automatic_tax ? { automatic_tax: { enabled: phase.automatic_tax.enabled } } : {}),
|
|
101
|
+
...(phase.billing_cycle_anchor ? { billing_cycle_anchor: phase.billing_cycle_anchor } : {}),
|
|
102
|
+
...(phase.collection_method ? { collection_method: phase.collection_method } : {}),
|
|
103
|
+
...(phase.currency ? { currency: phase.currency } : {}),
|
|
104
|
+
...(expandableId(phase.default_payment_method)
|
|
105
|
+
? { default_payment_method: expandableId(phase.default_payment_method) }
|
|
106
|
+
: {}),
|
|
107
|
+
...(phase.description != null ? { description: phase.description } : {}),
|
|
108
|
+
...(phase.metadata ? { metadata: phase.metadata } : {}),
|
|
109
|
+
...(phase.proration_behavior ? { proration_behavior: phase.proration_behavior } : {}),
|
|
110
|
+
...(phase.trial_end != null ? { trial_end: phase.trial_end } : {}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function stripeProrationBehavior(mode) {
|
|
114
|
+
return mode === "do_not_bill" ? "none" : "always_invoice";
|
|
115
|
+
}
|
|
4
116
|
export class StripeProvider {
|
|
117
|
+
provider = "stripe";
|
|
5
118
|
getStripe;
|
|
6
119
|
sink;
|
|
7
120
|
webhookSecret;
|
|
8
|
-
provider = "stripe";
|
|
9
|
-
constructor(getStripe, sink, webhookSecret, logger) {
|
|
10
|
-
this.getStripe = getStripe;
|
|
11
|
-
this.sink = sink;
|
|
12
|
-
this.webhookSecret = webhookSecret;
|
|
13
|
-
this.logger = normalizeProviderLogger(logger);
|
|
14
|
-
}
|
|
15
121
|
logger;
|
|
122
|
+
constructor(options) {
|
|
123
|
+
if (!options.webhookSecret.trim())
|
|
124
|
+
throw new TypeError("webhookSecret must not be empty");
|
|
125
|
+
this.getStripe = options.getClient;
|
|
126
|
+
this.sink = options.eventSink;
|
|
127
|
+
this.webhookSecret = options.webhookSecret;
|
|
128
|
+
this.logger = normalizeProviderLogger(options.logger);
|
|
129
|
+
}
|
|
16
130
|
async createCheckoutSession(params) {
|
|
17
131
|
this.logger.info("[StripeProvider] createCheckoutSession", {
|
|
18
132
|
productId: params.productId,
|
|
@@ -20,13 +134,14 @@ export class StripeProvider {
|
|
|
20
134
|
hasUserId: Boolean(params.userId),
|
|
21
135
|
});
|
|
22
136
|
if (!params.userId)
|
|
23
|
-
throw new
|
|
137
|
+
throw new TypeError("Authentication required for checkout");
|
|
24
138
|
const stripe = this.getStripe();
|
|
25
139
|
let customerId = params.customerId;
|
|
26
140
|
if (!customerId) {
|
|
27
141
|
const customer = await stripe.customers.create({
|
|
142
|
+
...(params.email ? { email: params.email } : {}),
|
|
28
143
|
metadata: { userId: params.userId },
|
|
29
|
-
});
|
|
144
|
+
}, requestOptions(scopedIdempotencyKey(params.idempotencyKey, "customer")));
|
|
30
145
|
customerId = customer.id;
|
|
31
146
|
}
|
|
32
147
|
const sessionOpts = {
|
|
@@ -42,21 +157,38 @@ export class StripeProvider {
|
|
|
42
157
|
? { subscription_data: { metadata: { userId: params.userId, ...params.metadata } } }
|
|
43
158
|
: { payment_intent_data: { metadata: { userId: params.userId, ...params.metadata } } }),
|
|
44
159
|
};
|
|
45
|
-
const session = await stripe.checkout.sessions.create(sessionOpts, params.idempotencyKey
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
160
|
+
const session = await stripe.checkout.sessions.create(sessionOpts, requestOptions(params.idempotencyKey));
|
|
161
|
+
return {
|
|
162
|
+
url: requireStripeText(session.url, "createCheckoutSession", "url"),
|
|
163
|
+
customerId: requireStripeText(customerId, "createCheckoutSession", "customer"),
|
|
164
|
+
providerSessionId: requireStripeText(session.id, "createCheckoutSession", "id"),
|
|
165
|
+
};
|
|
49
166
|
}
|
|
50
167
|
async getCheckoutSessionStatus(providerSessionId) {
|
|
51
|
-
|
|
168
|
+
let session;
|
|
169
|
+
try {
|
|
170
|
+
session = await this.getStripe().checkout.sessions.retrieve(providerSessionId, {
|
|
171
|
+
expand: ["payment_intent"],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (error instanceof Stripe.errors.StripeInvalidRequestError &&
|
|
176
|
+
error.code === "resource_missing") {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
52
181
|
if (session.status === "expired")
|
|
53
182
|
return { paymentStatus: "cancelled" };
|
|
54
183
|
if (session.payment_status === "paid" || session.payment_status === "no_payment_required") {
|
|
55
184
|
return { paymentStatus: "succeeded" };
|
|
56
185
|
}
|
|
57
|
-
if (session.
|
|
58
|
-
return { paymentStatus: "
|
|
59
|
-
|
|
186
|
+
if (session.status === "open")
|
|
187
|
+
return { paymentStatus: "processing" };
|
|
188
|
+
const intent = session.payment_intent && typeof session.payment_intent !== "string"
|
|
189
|
+
? session.payment_intent
|
|
190
|
+
: null;
|
|
191
|
+
return { paymentStatus: mapPaymentIntentStatus(intent) };
|
|
60
192
|
}
|
|
61
193
|
async createCustomerPortalSession(params) {
|
|
62
194
|
const stripe = this.getStripe();
|
|
@@ -64,9 +196,7 @@ export class StripeProvider {
|
|
|
64
196
|
customer: params.customerId,
|
|
65
197
|
return_url: params.returnUrl,
|
|
66
198
|
});
|
|
67
|
-
|
|
68
|
-
throw new Error("Stripe portal session returned no URL");
|
|
69
|
-
return { url: session.url };
|
|
199
|
+
return { url: requireStripeText(session.url, "createCustomerPortalSession", "url") };
|
|
70
200
|
}
|
|
71
201
|
async createUpdatePaymentMethodSession(params) {
|
|
72
202
|
const stripe = this.getStripe();
|
|
@@ -75,9 +205,7 @@ export class StripeProvider {
|
|
|
75
205
|
return_url: params.returnUrl,
|
|
76
206
|
flow_data: { type: "payment_method_update" },
|
|
77
207
|
});
|
|
78
|
-
|
|
79
|
-
throw new Error("Stripe portal session returned no URL");
|
|
80
|
-
return { url: session.url };
|
|
208
|
+
return { url: requireStripeText(session.url, "createUpdatePaymentMethodSession", "url") };
|
|
81
209
|
}
|
|
82
210
|
async createPaymentMethodSetupSession(params) {
|
|
83
211
|
const stripe = this.getStripe();
|
|
@@ -88,9 +216,7 @@ export class StripeProvider {
|
|
|
88
216
|
cancel_url: params.cancelUrl ?? params.returnUrl,
|
|
89
217
|
payment_method_types: ["card"],
|
|
90
218
|
});
|
|
91
|
-
|
|
92
|
-
throw new Error("Stripe setup session returned no URL");
|
|
93
|
-
return { url: session.url };
|
|
219
|
+
return { url: requireStripeText(session.url, "createPaymentMethodSetupSession", "url") };
|
|
94
220
|
}
|
|
95
221
|
async handleWebhook(req) {
|
|
96
222
|
const stripe = this.getStripe();
|
|
@@ -139,45 +265,66 @@ export class StripeProvider {
|
|
|
139
265
|
}
|
|
140
266
|
async cancelScheduledPlanChange(_subscriptionId, providerOperationId, idempotencyKey) {
|
|
141
267
|
if (!providerOperationId)
|
|
142
|
-
throw new
|
|
268
|
+
throw new TypeError("Stripe scheduled change has no schedule ID");
|
|
143
269
|
await this.getStripe().subscriptionSchedules.release(providerOperationId, {}, idempotencyKey ? { idempotencyKey } : undefined);
|
|
144
270
|
}
|
|
145
271
|
async listPaymentMethods(customerId) {
|
|
146
272
|
const stripe = this.getStripe();
|
|
147
|
-
const methods = await
|
|
148
|
-
|
|
149
|
-
type: "card",
|
|
150
|
-
|
|
151
|
-
return deduplicatePaymentMethods(methods.data.map((pm) => ({
|
|
152
|
-
id: pm.id,
|
|
153
|
-
last4: pm.card?.last4 ?? "",
|
|
154
|
-
brand: pm.card?.brand ?? "unknown",
|
|
155
|
-
expiryMonth: pm.card?.exp_month ?? 0,
|
|
156
|
-
expiryYear: pm.card?.exp_year ?? 0,
|
|
157
|
-
})));
|
|
158
|
-
}
|
|
159
|
-
async getDefaultPaymentMethod(customerId) {
|
|
160
|
-
const customer = await this.getStripe().customers.retrieve(customerId);
|
|
273
|
+
const [customer, methods] = await Promise.all([
|
|
274
|
+
stripe.customers.retrieve(customerId),
|
|
275
|
+
stripe.customers.listPaymentMethods(customerId, { type: "card" }),
|
|
276
|
+
]);
|
|
161
277
|
if (customer.deleted)
|
|
162
|
-
return
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
278
|
+
return [];
|
|
279
|
+
const defaultPaymentMethod = customer.invoice_settings.default_payment_method;
|
|
280
|
+
const defaultId = typeof defaultPaymentMethod === "string"
|
|
281
|
+
? defaultPaymentMethod
|
|
282
|
+
: (defaultPaymentMethod?.id ?? null);
|
|
283
|
+
return deduplicatePaymentMethods(methods.data.map((method) => {
|
|
284
|
+
const card = method.card;
|
|
285
|
+
if (card === null || card === undefined) {
|
|
286
|
+
throw new ProviderResponseError("stripe", "listPaymentMethods", {
|
|
287
|
+
details: { field: "card" },
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const last4 = requireStripeText(card.last4, "listPaymentMethods", "card.last4");
|
|
291
|
+
if (!/^\d{4}$/.test(last4)) {
|
|
292
|
+
throw new ProviderResponseError("stripe", "listPaymentMethods", {
|
|
293
|
+
details: { field: "card.last4" },
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const id = requireStripeText(method.id, "listPaymentMethods", "id");
|
|
297
|
+
return {
|
|
298
|
+
id,
|
|
299
|
+
last4,
|
|
300
|
+
brand: requireStripeText(card.brand, "listPaymentMethods", "card.brand"),
|
|
301
|
+
expiryMonth: requireStripeInteger(card.exp_month, "listPaymentMethods", "card.exp_month", 1),
|
|
302
|
+
expiryYear: requireStripeInteger(card.exp_year, "listPaymentMethods", "card.exp_year", 1),
|
|
303
|
+
isDefault: id === defaultId,
|
|
304
|
+
};
|
|
305
|
+
}));
|
|
169
306
|
}
|
|
170
307
|
async previewSavedPaymentCharge(params) {
|
|
171
308
|
const price = await this.getStripe().prices.retrieve(params.productId);
|
|
172
|
-
if (price.unit_amount == null)
|
|
173
|
-
throw new
|
|
174
|
-
|
|
309
|
+
if (price.unit_amount == null) {
|
|
310
|
+
throw new ProviderResponseError("stripe", "previewSavedPaymentCharge", {
|
|
311
|
+
details: { field: "unit_amount" },
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
const amountMinor = price.unit_amount * params.quantity;
|
|
315
|
+
return {
|
|
316
|
+
amountMinor: requireStripeInteger(amountMinor, "previewSavedPaymentCharge", "amount"),
|
|
317
|
+
currency: requireStripeText(price.currency, "previewSavedPaymentCharge", "currency"),
|
|
318
|
+
};
|
|
175
319
|
}
|
|
176
320
|
async chargeSavedPaymentMethod(params) {
|
|
177
321
|
const stripe = this.getStripe();
|
|
178
322
|
const price = await stripe.prices.retrieve(params.productId);
|
|
179
|
-
if (price.unit_amount == null)
|
|
180
|
-
throw new
|
|
323
|
+
if (price.unit_amount == null) {
|
|
324
|
+
throw new ProviderResponseError("stripe", "chargeSavedPaymentMethod", {
|
|
325
|
+
details: { field: "unit_amount" },
|
|
326
|
+
});
|
|
327
|
+
}
|
|
181
328
|
const intent = await stripe.paymentIntents.create({
|
|
182
329
|
amount: price.unit_amount * params.quantity,
|
|
183
330
|
currency: price.currency,
|
|
@@ -185,22 +332,13 @@ export class StripeProvider {
|
|
|
185
332
|
payment_method: params.paymentMethodId,
|
|
186
333
|
confirm: true,
|
|
187
334
|
off_session: true,
|
|
188
|
-
metadata: params.metadata,
|
|
335
|
+
metadata: { ...params.metadata, price_id: params.productId },
|
|
189
336
|
}, { idempotencyKey: params.idempotencyKey });
|
|
190
|
-
const status = intent.status === "succeeded"
|
|
191
|
-
? "succeeded"
|
|
192
|
-
: intent.status === "processing"
|
|
193
|
-
? "processing"
|
|
194
|
-
: intent.status === "requires_action"
|
|
195
|
-
? "requires_customer_action"
|
|
196
|
-
: intent.status === "requires_payment_method"
|
|
197
|
-
? "requires_payment_method"
|
|
198
|
-
: "failed";
|
|
199
337
|
return {
|
|
200
|
-
providerPaymentId: intent.id,
|
|
201
|
-
status,
|
|
202
|
-
amountMinor: intent.amount,
|
|
203
|
-
currency: intent.currency,
|
|
338
|
+
providerPaymentId: requireStripeText(intent.id, "chargeSavedPaymentMethod", "id"),
|
|
339
|
+
status: savedPaymentStatus(intent.status),
|
|
340
|
+
amountMinor: requireStripeInteger(intent.amount, "chargeSavedPaymentMethod", "amount"),
|
|
341
|
+
currency: requireStripeText(intent.currency, "chargeSavedPaymentMethod", "currency"),
|
|
204
342
|
};
|
|
205
343
|
}
|
|
206
344
|
async createCustomer(params) {
|
|
@@ -210,79 +348,121 @@ export class StripeProvider {
|
|
|
210
348
|
name: params.name,
|
|
211
349
|
metadata: params.metadata,
|
|
212
350
|
});
|
|
213
|
-
return { customerId: customer.id };
|
|
351
|
+
return { customerId: requireStripeText(customer.id, "createCustomer", "id") };
|
|
214
352
|
}
|
|
215
353
|
async getInvoiceUrl(providerPaymentId) {
|
|
216
354
|
const stripe = this.getStripe();
|
|
217
355
|
const invoice = await stripe.invoices.retrieve(providerPaymentId);
|
|
218
|
-
return invoice.hosted_invoice_url
|
|
356
|
+
return invoice.hosted_invoice_url === null
|
|
357
|
+
? null
|
|
358
|
+
: {
|
|
359
|
+
url: requireStripeText(invoice.hosted_invoice_url, "getInvoiceUrl", "hosted_invoice_url"),
|
|
360
|
+
};
|
|
219
361
|
}
|
|
220
362
|
async changePlan(params) {
|
|
221
363
|
const stripe = this.getStripe();
|
|
222
364
|
const subscription = await stripe.subscriptions.retrieve(params.providerSubscriptionId);
|
|
223
365
|
const item = subscription.items.data[0];
|
|
224
|
-
if (!item)
|
|
225
|
-
throw new
|
|
226
|
-
|
|
227
|
-
const schedule = await stripe.subscriptionSchedules.create({
|
|
228
|
-
from_subscription: params.providerSubscriptionId,
|
|
366
|
+
if (!item) {
|
|
367
|
+
throw new ProviderResponseError("stripe", "changePlan", {
|
|
368
|
+
details: { field: "subscription.items" },
|
|
229
369
|
});
|
|
370
|
+
}
|
|
371
|
+
if (params.effectiveAt === "next_billing_date") {
|
|
372
|
+
const schedule = await stripe.subscriptionSchedules.create({ from_subscription: params.providerSubscriptionId }, requestOptions(scopedIdempotencyKey(params.idempotencyKey, "schedule-create")));
|
|
373
|
+
const currentPhase = schedule.phases[0];
|
|
374
|
+
if (!currentPhase) {
|
|
375
|
+
throw new ProviderResponseError("stripe", "changePlan", {
|
|
376
|
+
details: { field: "schedule.phases" },
|
|
377
|
+
});
|
|
378
|
+
}
|
|
230
379
|
await stripe.subscriptionSchedules.update(schedule.id, {
|
|
231
380
|
phases: [
|
|
381
|
+
schedulePhaseParams(currentPhase),
|
|
232
382
|
{
|
|
233
|
-
items: [{ price:
|
|
234
|
-
start_date:
|
|
235
|
-
|
|
383
|
+
items: [{ price: params.productId, quantity: params.quantity ?? 1 }],
|
|
384
|
+
start_date: currentPhase.end_date,
|
|
385
|
+
proration_behavior: "none",
|
|
386
|
+
...(params.metadata ? { metadata: params.metadata } : {}),
|
|
236
387
|
},
|
|
237
|
-
{ items: [{ price: params.productId, quantity: params.quantity ?? 1 }] },
|
|
238
388
|
],
|
|
239
|
-
|
|
240
|
-
|
|
389
|
+
proration_behavior: "none",
|
|
390
|
+
}, requestOptions(scopedIdempotencyKey(params.idempotencyKey, "schedule-update")));
|
|
391
|
+
return {
|
|
392
|
+
providerOperationId: requireStripeText(schedule.id, "changePlan", "schedule.id"),
|
|
393
|
+
};
|
|
241
394
|
}
|
|
242
395
|
const updated = await stripe.subscriptions.update(params.providerSubscriptionId, {
|
|
243
396
|
items: [{ id: item.id, price: params.productId, quantity: params.quantity ?? 1 }],
|
|
244
|
-
proration_behavior:
|
|
245
|
-
payment_behavior: "pending_if_incomplete",
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
};
|
|
397
|
+
proration_behavior: stripeProrationBehavior(params.prorationBillingMode),
|
|
398
|
+
payment_behavior: params.onPaymentFailure === "apply_change" ? "allow_incomplete" : "pending_if_incomplete",
|
|
399
|
+
...(params.metadata ? { metadata: params.metadata } : {}),
|
|
400
|
+
}, requestOptions(scopedIdempotencyKey(params.idempotencyKey, "subscription-update")));
|
|
401
|
+
const latestInvoiceId = expandableId(updated.latest_invoice);
|
|
402
|
+
return latestInvoiceId === undefined ? {} : { providerOperationId: latestInvoiceId };
|
|
250
403
|
}
|
|
251
404
|
async previewChangePlan(params) {
|
|
252
405
|
const stripe = this.getStripe();
|
|
253
406
|
const subscription = await stripe.subscriptions.retrieve(params.providerSubscriptionId);
|
|
254
407
|
const item = subscription.items.data[0];
|
|
255
|
-
if (!item)
|
|
256
|
-
throw new
|
|
408
|
+
if (!item) {
|
|
409
|
+
throw new ProviderResponseError("stripe", "previewChangePlan", {
|
|
410
|
+
details: { field: "subscription.items" },
|
|
411
|
+
});
|
|
412
|
+
}
|
|
257
413
|
const invoice = await stripe.invoices.createPreview({
|
|
258
414
|
customer: String(subscription.customer),
|
|
259
415
|
subscription: params.providerSubscriptionId,
|
|
260
416
|
subscription_details: {
|
|
261
417
|
items: [{ id: item.id, price: params.productId, quantity: params.quantity ?? 1 }],
|
|
262
|
-
proration_behavior: params.effectiveAt === "next_billing_date"
|
|
418
|
+
proration_behavior: params.effectiveAt === "next_billing_date"
|
|
419
|
+
? "none"
|
|
420
|
+
: stripeProrationBehavior(params.prorationBillingMode),
|
|
263
421
|
},
|
|
264
422
|
});
|
|
265
423
|
const price = await stripe.prices.retrieve(params.productId);
|
|
424
|
+
const currentPeriodEnd = requireStripeInteger(item.current_period_end, "previewChangePlan", "subscription.items.current_period_end", 1);
|
|
425
|
+
const lineItems = invoice.lines.data
|
|
426
|
+
.filter((line) => line.parent !== null && line.parent.subscription_item_details !== null)
|
|
427
|
+
.map((line) => {
|
|
428
|
+
const operation = "previewChangePlan";
|
|
429
|
+
const priceDetails = line.pricing?.price_details;
|
|
430
|
+
if (!priceDetails) {
|
|
431
|
+
throw new ProviderResponseError("stripe", operation, {
|
|
432
|
+
details: { field: "invoice.lines.pricing.price_details" },
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
const quantity = line.quantity === null
|
|
436
|
+
? 1
|
|
437
|
+
: requireStripeInteger(line.quantity, operation, "invoice.lines.quantity", 1);
|
|
438
|
+
const unitPrice = requireStripeNumber(line.pricing?.unit_amount_decimal, operation, "invoice.lines.pricing.unit_amount_decimal");
|
|
439
|
+
const subtotal = requireStripeInteger(line.subtotal, operation, "invoice.lines.subtotal", Number.MIN_SAFE_INTEGER);
|
|
440
|
+
const expectedSubtotal = unitPrice * quantity;
|
|
441
|
+
const tax = line.taxes?.reduce((total, taxItem) => total + requireStripeInteger(taxItem.amount, operation, "invoice.lines.taxes.amount"), 0) ?? 0;
|
|
442
|
+
return {
|
|
443
|
+
productId: requireStripeText(expandableId(priceDetails.price), operation, "invoice.lines.pricing.price_details.price"),
|
|
444
|
+
name: requireStripeText(line.description, operation, "invoice.lines.description"),
|
|
445
|
+
unitPrice,
|
|
446
|
+
quantity,
|
|
447
|
+
prorationFactor: expectedSubtotal === 0 ? 1 : subtotal / expectedSubtotal,
|
|
448
|
+
currency: requireStripeText(line.currency, operation, "invoice.lines.currency"),
|
|
449
|
+
tax,
|
|
450
|
+
subtotal,
|
|
451
|
+
};
|
|
452
|
+
});
|
|
266
453
|
return {
|
|
267
|
-
totalAmount: invoice.total
|
|
268
|
-
settlementAmount: invoice.amount_due
|
|
269
|
-
currency: invoice.currency,
|
|
270
|
-
lineItems
|
|
271
|
-
productId: params.productId,
|
|
272
|
-
name: line.description ?? "Subscription change",
|
|
273
|
-
unitPrice: line.amount ?? 0,
|
|
274
|
-
quantity: line.quantity ?? 1,
|
|
275
|
-
prorationFactor: 1,
|
|
276
|
-
currency: line.currency ?? invoice.currency,
|
|
277
|
-
tax: 0,
|
|
278
|
-
subtotal: line.amount ?? 0,
|
|
279
|
-
})),
|
|
454
|
+
totalAmount: requireStripeInteger(invoice.total, "previewChangePlan", "invoice.total"),
|
|
455
|
+
settlementAmount: requireStripeInteger(invoice.amount_due, "previewChangePlan", "invoice.amount_due"),
|
|
456
|
+
currency: requireStripeText(invoice.currency, "previewChangePlan", "invoice.currency"),
|
|
457
|
+
lineItems,
|
|
280
458
|
effectiveAt: params.effectiveAt === "next_billing_date"
|
|
281
|
-
? new Date(
|
|
282
|
-
: new Date().toISOString(),
|
|
283
|
-
recurringAmount: price.unit_amount
|
|
284
|
-
recurringCurrency: price.currency,
|
|
285
|
-
nextBillingDate: new Date(
|
|
459
|
+
? new Date(currentPeriodEnd * 1000).toISOString()
|
|
460
|
+
: new Date(invoice.created * 1000).toISOString(),
|
|
461
|
+
...(price.unit_amount === null ? {} : { recurringAmount: price.unit_amount }),
|
|
462
|
+
recurringCurrency: requireStripeText(price.currency, "previewChangePlan", "price.currency"),
|
|
463
|
+
nextBillingDate: new Date(currentPeriodEnd * 1000).toISOString(),
|
|
464
|
+
taxAmount: invoice.total_taxes?.reduce((total, taxItem) => total +
|
|
465
|
+
requireStripeInteger(taxItem.amount, "previewChangePlan", "invoice.total_taxes.amount"), 0) ?? 0,
|
|
286
466
|
};
|
|
287
467
|
}
|
|
288
468
|
}
|