@owlmeans/server-payment 0.1.18-rc.9 → 0.1.18
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 +157 -26
- package/agent-meta/manifest.json +30 -4
- package/agent-meta/skills/server-payment/SKILL.md +613 -54
- package/agent-meta/skills/server-payment/parts/reference.md +174 -0
- package/build/access/types.d.ts +46 -0
- package/build/access/types.d.ts.map +1 -0
- package/build/access/types.js +2 -0
- package/build/access/types.js.map +1 -0
- package/build/access.d.ts +6 -0
- package/build/access.d.ts.map +1 -0
- package/build/access.js +45 -0
- package/build/access.js.map +1 -0
- package/build/actions/index.d.ts +1 -0
- package/build/actions/index.d.ts.map +1 -1
- package/build/actions/index.js +1 -0
- package/build/actions/index.js.map +1 -1
- package/build/actions/resync-subscriptions.d.ts +3 -0
- package/build/actions/resync-subscriptions.d.ts.map +1 -0
- package/build/actions/resync-subscriptions.js +7 -0
- package/build/actions/resync-subscriptions.js.map +1 -0
- package/build/actions/resync.d.ts +6 -1
- package/build/actions/resync.d.ts.map +1 -1
- package/build/actions/resync.js +15 -5
- package/build/actions/resync.js.map +1 -1
- package/build/actions/webhook.d.ts +2 -1
- package/build/actions/webhook.d.ts.map +1 -1
- package/build/actions/webhook.js +10 -4
- package/build/actions/webhook.js.map +1 -1
- package/build/bootstrap/types.d.ts +16 -0
- package/build/bootstrap/types.d.ts.map +1 -0
- package/build/bootstrap/types.js +2 -0
- package/build/bootstrap/types.js.map +1 -0
- package/build/bootstrap.d.ts +6 -0
- package/build/bootstrap.d.ts.map +1 -0
- package/build/bootstrap.js +26 -0
- package/build/bootstrap.js.map +1 -0
- package/build/catalogue/types.d.ts +38 -0
- package/build/catalogue/types.d.ts.map +1 -0
- package/build/catalogue/types.js +2 -0
- package/build/catalogue/types.js.map +1 -0
- package/build/catalogue.d.ts +6 -0
- package/build/catalogue.d.ts.map +1 -0
- package/build/catalogue.js +110 -0
- package/build/catalogue.js.map +1 -0
- package/build/commit/types.d.ts +22 -0
- package/build/commit/types.d.ts.map +1 -0
- package/build/commit/types.js +2 -0
- package/build/commit/types.js.map +1 -0
- package/build/commit.d.ts +6 -0
- package/build/commit.d.ts.map +1 -0
- package/build/commit.js +125 -0
- package/build/commit.js.map +1 -0
- package/build/config.d.ts +44 -5
- package/build/config.d.ts.map +1 -1
- package/build/config.js +216 -10
- package/build/config.js.map +1 -1
- package/build/consts.d.ts +74 -16
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +86 -23
- package/build/consts.js.map +1 -1
- package/build/consts.local.d.ts +94 -0
- package/build/consts.local.d.ts.map +1 -0
- package/build/consts.local.js +44 -0
- package/build/consts.local.js.map +1 -0
- package/build/consumer/cancellation/types.d.ts +11 -0
- package/build/consumer/cancellation/types.d.ts.map +1 -0
- package/build/consumer/cancellation/types.js +2 -0
- package/build/consumer/cancellation/types.js.map +1 -0
- package/build/consumer/cancellation.d.ts +6 -0
- package/build/consumer/cancellation.d.ts.map +1 -0
- package/build/consumer/cancellation.js +33 -0
- package/build/consumer/cancellation.js.map +1 -0
- package/build/consumer/capture/types.d.ts +50 -0
- package/build/consumer/capture/types.d.ts.map +1 -0
- package/build/consumer/capture/types.js +2 -0
- package/build/consumer/capture/types.js.map +1 -0
- package/build/consumer/capture.d.ts +6 -0
- package/build/consumer/capture.d.ts.map +1 -0
- package/build/consumer/capture.js +284 -0
- package/build/consumer/capture.js.map +1 -0
- package/build/consumer/consts.local.d.ts +10 -0
- package/build/consumer/consts.local.d.ts.map +1 -0
- package/build/consumer/consts.local.js +11 -0
- package/build/consumer/consts.local.js.map +1 -0
- package/build/consumer/format/types.d.ts +30 -0
- package/build/consumer/format/types.d.ts.map +1 -0
- package/build/consumer/format/types.js +2 -0
- package/build/consumer/format/types.js.map +1 -0
- package/build/consumer/format.d.ts +4 -0
- package/build/consumer/format.d.ts.map +1 -0
- package/build/consumer/format.js +73 -0
- package/build/consumer/format.js.map +1 -0
- package/build/consumer/handlers.d.ts +21 -0
- package/build/consumer/handlers.d.ts.map +1 -0
- package/build/consumer/handlers.js +174 -0
- package/build/consumer/handlers.js.map +1 -0
- package/build/consumer/index.d.ts +10 -0
- package/build/consumer/index.d.ts.map +1 -0
- package/build/consumer/index.js +6 -0
- package/build/consumer/index.js.map +1 -0
- package/build/consumer/mail/types.d.ts +29 -0
- package/build/consumer/mail/types.d.ts.map +1 -0
- package/build/consumer/mail/types.js +2 -0
- package/build/consumer/mail/types.js.map +1 -0
- package/build/consumer/mail.d.ts +6 -0
- package/build/consumer/mail.d.ts.map +1 -0
- package/build/consumer/mail.js +313 -0
- package/build/consumer/mail.js.map +1 -0
- package/build/consumer/observers/types.d.ts +15 -0
- package/build/consumer/observers/types.d.ts.map +1 -0
- package/build/consumer/observers/types.js +2 -0
- package/build/consumer/observers/types.js.map +1 -0
- package/build/consumer/observers.d.ts +6 -0
- package/build/consumer/observers.d.ts.map +1 -0
- package/build/consumer/observers.js +74 -0
- package/build/consumer/observers.js.map +1 -0
- package/build/consumer/origin/types.d.ts +17 -0
- package/build/consumer/origin/types.d.ts.map +1 -0
- package/build/consumer/origin/types.js +2 -0
- package/build/consumer/origin/types.js.map +1 -0
- package/build/consumer/origin.d.ts +8 -0
- package/build/consumer/origin.d.ts.map +1 -0
- package/build/consumer/origin.js +43 -0
- package/build/consumer/origin.js.map +1 -0
- package/build/consumer/reconcile/types.d.ts +13 -0
- package/build/consumer/reconcile/types.d.ts.map +1 -0
- package/build/consumer/reconcile/types.js +2 -0
- package/build/consumer/reconcile/types.js.map +1 -0
- package/build/consumer/reconcile.d.ts +5 -0
- package/build/consumer/reconcile.d.ts.map +1 -0
- package/build/consumer/reconcile.js +320 -0
- package/build/consumer/reconcile.js.map +1 -0
- package/build/consumer/records/types.d.ts +66 -0
- package/build/consumer/records/types.d.ts.map +1 -0
- package/build/consumer/records/types.js +2 -0
- package/build/consumer/records/types.js.map +1 -0
- package/build/consumer/records.d.ts +6 -0
- package/build/consumer/records.d.ts.map +1 -0
- package/build/consumer/records.js +243 -0
- package/build/consumer/records.js.map +1 -0
- package/build/consumer/service.d.ts +28 -0
- package/build/consumer/service.d.ts.map +1 -0
- package/build/consumer/service.js +706 -0
- package/build/consumer/service.js.map +1 -0
- package/build/consumer/types.d.ts +84 -0
- package/build/consumer/types.d.ts.map +1 -0
- package/build/consumer/types.js +2 -0
- package/build/consumer/types.js.map +1 -0
- package/build/consumer/types.local.d.ts +26 -0
- package/build/consumer/types.local.d.ts.map +1 -0
- package/build/consumer/types.local.js +2 -0
- package/build/consumer/types.local.js.map +1 -0
- package/build/consumer/withdrawal/types.d.ts +36 -0
- package/build/consumer/withdrawal/types.d.ts.map +1 -0
- package/build/consumer/withdrawal/types.js +2 -0
- package/build/consumer/withdrawal/types.js.map +1 -0
- package/build/consumer/withdrawal.d.ts +6 -0
- package/build/consumer/withdrawal.d.ts.map +1 -0
- package/build/consumer/withdrawal.js +237 -0
- package/build/consumer/withdrawal.js.map +1 -0
- package/build/entitlement.d.ts +4 -0
- package/build/entitlement.d.ts.map +1 -0
- package/build/entitlement.js +72 -0
- package/build/entitlement.js.map +1 -0
- package/build/entrypoints.d.ts +19 -2
- package/build/entrypoints.d.ts.map +1 -1
- package/build/entrypoints.js +16 -4
- package/build/entrypoints.js.map +1 -1
- package/build/gate/request.d.ts +4 -0
- package/build/gate/request.d.ts.map +1 -0
- package/build/gate/request.js +17 -0
- package/build/gate/request.js.map +1 -0
- package/build/gate/types.d.ts +15 -0
- package/build/gate/types.d.ts.map +1 -0
- package/build/gate/types.js +2 -0
- package/build/gate/types.js.map +1 -0
- package/build/gate.d.ts +13 -9
- package/build/gate.d.ts.map +1 -1
- package/build/gate.js +36 -21
- package/build/gate.js.map +1 -1
- package/build/index.d.ts +50 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +29 -3
- package/build/index.js.map +1 -1
- package/build/limit.d.ts +11 -0
- package/build/limit.d.ts.map +1 -0
- package/build/limit.js +48 -0
- package/build/limit.js.map +1 -0
- package/build/log.d.ts +3 -0
- package/build/log.d.ts.map +1 -0
- package/build/log.js +4 -0
- package/build/log.js.map +1 -0
- package/build/models/declarations/types.d.ts +22 -0
- package/build/models/declarations/types.d.ts.map +1 -0
- package/build/models/declarations/types.js +2 -0
- package/build/models/declarations/types.js.map +1 -0
- package/build/models/declarations.d.ts +4 -0
- package/build/models/declarations.d.ts.map +1 -0
- package/build/models/declarations.js +26 -0
- package/build/models/declarations.js.map +1 -0
- package/build/models/purchase/types.d.ts +13 -0
- package/build/models/purchase/types.d.ts.map +1 -0
- package/build/models/purchase/types.js +2 -0
- package/build/models/purchase/types.js.map +1 -0
- package/build/models/purchase.d.ts +4 -0
- package/build/models/purchase.d.ts.map +1 -0
- package/build/models/purchase.js +37 -0
- package/build/models/purchase.js.map +1 -0
- package/build/observer.d.ts +9 -0
- package/build/observer.d.ts.map +1 -1
- package/build/observer.js +38 -12
- package/build/observer.js.map +1 -1
- package/build/plan/types.d.ts +21 -0
- package/build/plan/types.d.ts.map +1 -0
- package/build/plan/types.js +2 -0
- package/build/plan/types.js.map +1 -0
- package/build/plan.d.ts +7 -0
- package/build/plan.d.ts.map +1 -0
- package/build/plan.js +29 -0
- package/build/plan.js.map +1 -0
- package/build/plugins/checkout-plugins/types.d.ts +25 -0
- package/build/plugins/checkout-plugins/types.d.ts.map +1 -0
- package/build/plugins/checkout-plugins/types.js +2 -0
- package/build/plugins/checkout-plugins/types.js.map +1 -0
- package/build/plugins/checkout-plugins.d.ts +12 -0
- package/build/plugins/checkout-plugins.d.ts.map +1 -0
- package/build/plugins/checkout-plugins.js +72 -0
- package/build/plugins/checkout-plugins.js.map +1 -0
- package/build/plugins/checkout-policy/types.d.ts +18 -0
- package/build/plugins/checkout-policy/types.d.ts.map +1 -0
- package/build/plugins/checkout-policy/types.js +2 -0
- package/build/plugins/checkout-policy/types.js.map +1 -0
- package/build/plugins/checkout-policy.d.ts +4 -0
- package/build/plugins/checkout-policy.d.ts.map +1 -0
- package/build/plugins/checkout-policy.js +26 -0
- package/build/plugins/checkout-policy.js.map +1 -0
- package/build/plugins/checkout-registry/types.d.ts +7 -0
- package/build/plugins/checkout-registry/types.d.ts.map +1 -0
- package/build/plugins/checkout-registry/types.js +2 -0
- package/build/plugins/checkout-registry/types.js.map +1 -0
- package/build/plugins/checkout-registry.d.ts +8 -0
- package/build/plugins/checkout-registry.d.ts.map +1 -0
- package/build/plugins/checkout-registry.js +21 -0
- package/build/plugins/checkout-registry.js.map +1 -0
- package/build/plugins/consts.local.d.ts +13 -0
- package/build/plugins/consts.local.d.ts.map +1 -0
- package/build/plugins/consts.local.js +22 -0
- package/build/plugins/consts.local.js.map +1 -0
- package/build/plugins/customer-lock/types.d.ts +6 -0
- package/build/plugins/customer-lock/types.d.ts.map +1 -0
- package/build/plugins/customer-lock/types.js +2 -0
- package/build/plugins/customer-lock/types.js.map +1 -0
- package/build/plugins/customer-lock.d.ts +5 -0
- package/build/plugins/customer-lock.d.ts.map +1 -0
- package/build/plugins/customer-lock.js +22 -0
- package/build/plugins/customer-lock.js.map +1 -0
- package/build/plugins/estimate/cache.d.ts +4 -0
- package/build/plugins/estimate/cache.d.ts.map +1 -0
- package/build/plugins/estimate/cache.js +7 -0
- package/build/plugins/estimate/cache.js.map +1 -0
- package/build/plugins/estimate/types.d.ts +27 -0
- package/build/plugins/estimate/types.d.ts.map +1 -0
- package/build/plugins/estimate/types.js +2 -0
- package/build/plugins/estimate/types.js.map +1 -0
- package/build/plugins/estimate.d.ts +6 -0
- package/build/plugins/estimate.d.ts.map +1 -0
- package/build/plugins/estimate.js +271 -0
- package/build/plugins/estimate.js.map +1 -0
- package/build/plugins/events/types.d.ts +12 -0
- package/build/plugins/events/types.d.ts.map +1 -0
- package/build/plugins/events/types.js +2 -0
- package/build/plugins/events/types.js.map +1 -0
- package/build/plugins/events.d.ts +6 -13
- package/build/plugins/events.d.ts.map +1 -1
- package/build/plugins/events.js +441 -144
- package/build/plugins/events.js.map +1 -1
- package/build/plugins/fx/types.d.ts +33 -0
- package/build/plugins/fx/types.d.ts.map +1 -0
- package/build/plugins/fx/types.js +2 -0
- package/build/plugins/fx/types.js.map +1 -0
- package/build/plugins/fx.d.ts +6 -0
- package/build/plugins/fx.d.ts.map +1 -0
- package/build/plugins/fx.js +83 -0
- package/build/plugins/fx.js.map +1 -0
- package/build/plugins/portal/types.d.ts +44 -0
- package/build/plugins/portal/types.d.ts.map +1 -0
- package/build/plugins/portal/types.js +2 -0
- package/build/plugins/portal/types.js.map +1 -0
- package/build/plugins/portal.d.ts +6 -0
- package/build/plugins/portal.d.ts.map +1 -0
- package/build/plugins/portal.js +255 -0
- package/build/plugins/portal.js.map +1 -0
- package/build/plugins/refunds/types.d.ts +35 -0
- package/build/plugins/refunds/types.d.ts.map +1 -0
- package/build/plugins/refunds/types.js +2 -0
- package/build/plugins/refunds/types.js.map +1 -0
- package/build/plugins/refunds.d.ts +6 -0
- package/build/plugins/refunds.d.ts.map +1 -0
- package/build/plugins/refunds.js +81 -0
- package/build/plugins/refunds.js.map +1 -0
- package/build/plugins/session/types.d.ts +32 -0
- package/build/plugins/session/types.d.ts.map +1 -0
- package/build/plugins/session/types.js +2 -0
- package/build/plugins/session/types.js.map +1 -0
- package/build/plugins/session.d.ts +4 -0
- package/build/plugins/session.d.ts.map +1 -0
- package/build/plugins/session.js +58 -0
- package/build/plugins/session.js.map +1 -0
- package/build/plugins/status.d.ts +8 -0
- package/build/plugins/status.d.ts.map +1 -0
- package/build/plugins/status.js +21 -0
- package/build/plugins/status.js.map +1 -0
- package/build/plugins/stripe/types.d.ts +20 -0
- package/build/plugins/stripe/types.d.ts.map +1 -0
- package/build/plugins/stripe/types.js +2 -0
- package/build/plugins/stripe/types.js.map +1 -0
- package/build/plugins/stripe.d.ts +5 -13
- package/build/plugins/stripe.d.ts.map +1 -1
- package/build/plugins/stripe.js +481 -129
- package/build/plugins/stripe.js.map +1 -1
- package/build/plugins/subscriptions/types.d.ts +41 -0
- package/build/plugins/subscriptions/types.d.ts.map +1 -0
- package/build/plugins/subscriptions/types.js +2 -0
- package/build/plugins/subscriptions/types.js.map +1 -0
- package/build/plugins/subscriptions.d.ts +6 -0
- package/build/plugins/subscriptions.d.ts.map +1 -0
- package/build/plugins/subscriptions.js +169 -0
- package/build/plugins/subscriptions.js.map +1 -0
- package/build/plugins/types.d.ts +7 -0
- package/build/plugins/types.d.ts.map +1 -0
- package/build/plugins/types.js +2 -0
- package/build/plugins/types.js.map +1 -0
- package/build/plugins/types.local.d.ts +75 -0
- package/build/plugins/types.local.d.ts.map +1 -0
- package/build/plugins/types.local.js +2 -0
- package/build/plugins/types.local.js.map +1 -0
- package/build/plugins/webhook-manager/types.d.ts +62 -0
- package/build/plugins/webhook-manager/types.d.ts.map +1 -0
- package/build/plugins/webhook-manager/types.js +2 -0
- package/build/plugins/webhook-manager/types.js.map +1 -0
- package/build/plugins/webhook-manager.d.ts +8 -0
- package/build/plugins/webhook-manager.d.ts.map +1 -0
- package/build/plugins/webhook-manager.js +245 -0
- package/build/plugins/webhook-manager.js.map +1 -0
- package/build/protocol.d.ts +13 -0
- package/build/protocol.d.ts.map +1 -0
- package/build/protocol.js +17 -0
- package/build/protocol.js.map +1 -0
- package/build/reconcile/types.d.ts +17 -0
- package/build/reconcile/types.d.ts.map +1 -0
- package/build/reconcile/types.js +2 -0
- package/build/reconcile/types.js.map +1 -0
- package/build/reconcile.d.ts +12 -0
- package/build/reconcile.d.ts.map +1 -0
- package/build/reconcile.js +91 -0
- package/build/reconcile.js.map +1 -0
- package/build/resource.d.ts +12 -1
- package/build/resource.d.ts.map +1 -1
- package/build/resource.js +97 -6
- package/build/resource.js.map +1 -1
- package/build/schemas.d.ts +19 -0
- package/build/schemas.d.ts.map +1 -0
- package/build/schemas.js +244 -0
- package/build/schemas.js.map +1 -0
- package/build/service.d.ts +28 -3
- package/build/service.d.ts.map +1 -1
- package/build/service.js +140 -19
- package/build/service.js.map +1 -1
- package/build/subscription/types.d.ts +21 -0
- package/build/subscription/types.d.ts.map +1 -0
- package/build/subscription/types.js +2 -0
- package/build/subscription/types.js.map +1 -0
- package/build/subscription.d.ts +4 -0
- package/build/subscription.d.ts.map +1 -0
- package/build/subscription.js +84 -0
- package/build/subscription.js.map +1 -0
- package/build/sync/types.d.ts +25 -0
- package/build/sync/types.d.ts.map +1 -0
- package/build/sync/types.js +2 -0
- package/build/sync/types.js.map +1 -0
- package/build/sync.d.ts +4 -3
- package/build/sync.d.ts.map +1 -1
- package/build/sync.js +231 -79
- package/build/sync.js.map +1 -1
- package/build/types.d.ts +1258 -36
- package/build/types.d.ts.map +1 -1
- package/build/types.local.d.ts +22 -0
- package/build/types.local.d.ts.map +1 -0
- package/build/types.local.js +2 -0
- package/build/types.local.js.map +1 -0
- package/build/usage/types.d.ts +56 -0
- package/build/usage/types.d.ts.map +1 -0
- package/build/usage/types.js +2 -0
- package/build/usage/types.js.map +1 -0
- package/build/usage.d.ts +6 -0
- package/build/usage.d.ts.map +1 -0
- package/build/usage.js +365 -0
- package/build/usage.js.map +1 -0
- package/build/utils/types.d.ts +31 -0
- package/build/utils/types.d.ts.map +1 -0
- package/build/utils/types.js +2 -0
- package/build/utils/types.js.map +1 -0
- package/build/utils.d.ts +21 -7
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +56 -17
- package/build/utils.js.map +1 -1
- package/package.json +22 -17
- package/src/access/types.ts +52 -0
- package/src/access.ts +86 -0
- package/src/actions/index.ts +1 -0
- package/src/actions/resync-subscriptions.ts +10 -0
- package/src/actions/resync.ts +14 -5
- package/src/actions/webhook.ts +9 -4
- package/src/bootstrap/types.ts +16 -0
- package/src/bootstrap.ts +31 -0
- package/src/catalogue/types.ts +32 -0
- package/src/catalogue.ts +135 -0
- package/src/commit/types.ts +27 -0
- package/src/commit.ts +152 -0
- package/src/config.ts +234 -14
- package/src/consts.local.ts +62 -0
- package/src/consts.ts +100 -36
- package/src/consumer/cancellation/types.ts +13 -0
- package/src/consumer/cancellation.ts +43 -0
- package/src/consumer/capture/types.ts +53 -0
- package/src/consumer/capture.ts +321 -0
- package/src/consumer/consts.local.ts +18 -0
- package/src/consumer/format/types.ts +29 -0
- package/src/consumer/format.ts +85 -0
- package/src/consumer/handlers.ts +204 -0
- package/src/consumer/index.ts +9 -0
- package/src/consumer/mail/types.ts +31 -0
- package/src/consumer/mail.ts +353 -0
- package/src/consumer/observers/types.ts +22 -0
- package/src/consumer/observers.ts +97 -0
- package/src/consumer/origin/types.ts +17 -0
- package/src/consumer/origin.ts +61 -0
- package/src/consumer/reconcile/types.ts +13 -0
- package/src/consumer/reconcile.ts +330 -0
- package/src/consumer/records/types.ts +64 -0
- package/src/consumer/records.ts +288 -0
- package/src/consumer/service.ts +768 -0
- package/src/consumer/types.local.ts +22 -0
- package/src/consumer/types.ts +96 -0
- package/src/consumer/withdrawal/types.ts +37 -0
- package/src/consumer/withdrawal.ts +268 -0
- package/src/entitlement.ts +85 -0
- package/src/entrypoints.ts +17 -4
- package/src/gate/request.ts +24 -0
- package/src/gate/types.ts +13 -0
- package/src/gate.ts +47 -30
- package/src/index.ts +55 -3
- package/src/limit.ts +54 -0
- package/src/log.ts +4 -0
- package/src/models/declarations/types.ts +22 -0
- package/src/models/declarations.ts +30 -0
- package/src/models/purchase/types.ts +13 -0
- package/src/models/purchase.ts +43 -0
- package/src/observer.ts +44 -11
- package/src/plan/types.ts +21 -0
- package/src/plan.ts +46 -0
- package/src/plugins/checkout-plugins/types.ts +25 -0
- package/src/plugins/checkout-plugins.ts +90 -0
- package/src/plugins/checkout-policy/types.ts +18 -0
- package/src/plugins/checkout-policy.ts +34 -0
- package/src/plugins/checkout-registry/types.ts +7 -0
- package/src/plugins/checkout-registry.ts +23 -0
- package/src/plugins/consts.local.ts +30 -0
- package/src/plugins/customer-lock/types.ts +5 -0
- package/src/plugins/customer-lock.ts +25 -0
- package/src/plugins/estimate/cache.ts +12 -0
- package/src/plugins/estimate/types.ts +28 -0
- package/src/plugins/estimate.ts +305 -0
- package/src/plugins/events/types.ts +11 -0
- package/src/plugins/events.ts +466 -132
- package/src/plugins/fx/types.ts +40 -0
- package/src/plugins/fx.ts +101 -0
- package/src/plugins/portal/types.ts +45 -0
- package/src/plugins/portal.ts +284 -0
- package/src/plugins/refunds/types.ts +31 -0
- package/src/plugins/refunds.ts +95 -0
- package/src/plugins/session/types.ts +28 -0
- package/src/plugins/session.ts +73 -0
- package/src/plugins/status.ts +22 -0
- package/src/plugins/stripe/types.ts +20 -0
- package/src/plugins/stripe.ts +523 -135
- package/src/plugins/subscriptions/types.ts +39 -0
- package/src/plugins/subscriptions.ts +195 -0
- package/src/plugins/types.local.ts +61 -0
- package/src/plugins/types.ts +7 -0
- package/src/plugins/webhook-manager/types.ts +63 -0
- package/src/plugins/webhook-manager.ts +288 -0
- package/src/protocol.ts +29 -0
- package/src/reconcile/types.ts +17 -0
- package/src/reconcile.ts +110 -0
- package/src/resource.ts +158 -11
- package/src/schemas.ts +271 -0
- package/src/service.ts +161 -19
- package/src/subscription/types.ts +23 -0
- package/src/subscription.ts +104 -0
- package/src/sync/types.ts +25 -0
- package/src/sync.ts +255 -72
- package/src/types.local.ts +18 -0
- package/src/types.ts +1365 -32
- package/src/usage/types.ts +51 -0
- package/src/usage.ts +432 -0
- package/src/utils/types.ts +30 -0
- package/src/utils.ts +94 -29
- package/tests/checkout-consumer.spec.ts +366 -0
- package/tests/checkout-locks.spec.ts +161 -0
- package/tests/checkout-plugins.spec.ts +164 -0
- package/tests/checkout.spec.ts +209 -93
- package/tests/consumer-events.spec.ts +215 -0
- package/tests/consumer-fixtures.ts +134 -0
- package/tests/consumer-ops.spec.ts +350 -0
- package/tests/consumer-rights.integration.spec.ts +147 -0
- package/tests/consumer-rights.spec.ts +572 -0
- package/tests/context.ts +110 -0
- package/tests/entitlement.spec.ts +139 -0
- package/tests/estimate.spec.ts +350 -0
- package/tests/events.spec.ts +357 -0
- package/tests/fake-stripe.ts +981 -0
- package/tests/gate-handlers.spec.ts +120 -0
- package/tests/gate.spec.ts +63 -72
- package/tests/gateway-options.spec.ts +180 -0
- package/tests/limit-gate.spec.ts +66 -0
- package/tests/portal.spec.ts +202 -0
- package/tests/protocol.spec.ts +17 -3
- package/tests/sync.spec.ts +114 -0
- package/tests/usage.integration.spec.ts +133 -0
- package/tests/usage.spec.ts +169 -0
- package/tests/webhook-manager.spec.ts +149 -0
- package/build/model.d.ts +0 -6
- package/build/model.d.ts.map +0 -1
- package/build/model.js +0 -33
- package/build/model.js.map +0 -1
- package/src/model.ts +0 -37
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import type Stripe from 'stripe'
|
|
2
|
+
import { createLazyService } from '@owlmeans/context'
|
|
3
|
+
import { MAILER_SERVICE } from '@owlmeans/mailer'
|
|
4
|
+
import { cancellationEffectiveAt, CancellationKind, CancellationStatus, CancellationUnavailable, CancellationUnavailableReason, ConsentKind, CONSUMER_RIGHTS_COPY_VERSION, ConsumerRightsError, DeclarationChannel, DeclarationKind, ENTITLING_STATUSES, PaygateError, PerformanceConsentRequired, PurchaseKind, SubscriptionStartRequired, TERMINAL_STATUSES, UnknownPlan, WithdrawalStatus, WithdrawalUnavailable, WithdrawalUnavailableReason, type CancellationReceipt, type ConsumerRightsPolicy, type DeclarationReceipt, type PerformanceConsentView, type PurchaseView, type SubscriptionStartView, type WithdrawalCandidate, type WithdrawalReceipt, consumerCopyHelper, consumerRegionHelper, consumerRightsPolicyHelper } from '@owlmeans/payment'
|
|
5
|
+
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
6
|
+
import { CONSUMER_RIGHTS_SERVICE, STRIPE_PAYGATE_ALIAS } from '../consts.js'
|
|
7
|
+
import { CONSUMER_RIGHTS_RESOURCE_MAKERS } from '../resource.js'
|
|
8
|
+
import { makeConsumerReconcileHelper } from './reconcile.js'
|
|
9
|
+
import type {
|
|
10
|
+
WithdrawalComputation, WithdrawalExecution, ConsumerRightsInternals, ConsumerRightsRegistrar,
|
|
11
|
+
} from './types.js'
|
|
12
|
+
import type {
|
|
13
|
+
Config, ConsumerConsentRecord, ConsumerDeclarationRecord, ConsumerMailRenderer, ConsumerRightsOptions,
|
|
14
|
+
ConsumerRightsService, Context, PaymentSubscriptionRecord, PurchaseRecord, UsageMeter,
|
|
15
|
+
} from '../types.js'
|
|
16
|
+
import { log } from '../log.js'
|
|
17
|
+
import type { Plumbing } from './types.local.js'
|
|
18
|
+
import { paymentAccessOf } from '../access.js'
|
|
19
|
+
import { paymentUtils } from '../utils.js'
|
|
20
|
+
import { catalogueOf } from '../catalogue.js'
|
|
21
|
+
import { consumerFormatHelper } from './format.js'
|
|
22
|
+
import { originHelper } from './origin.js'
|
|
23
|
+
import { consumerRecordsOf } from './records.js'
|
|
24
|
+
import { makePurchaseModel } from '../models/purchase.js'
|
|
25
|
+
import { consumerMailOf } from './mail.js'
|
|
26
|
+
import { consumerObserversOf } from './observers.js'
|
|
27
|
+
import { cancellationOf } from './cancellation.js'
|
|
28
|
+
import { withdrawalOf } from './withdrawal.js'
|
|
29
|
+
|
|
30
|
+
const latestDeadline = (items: Array<{ deadline?: Date | null }>): Date | undefined => {
|
|
31
|
+
const times = items.map(item => item.deadline != null ? new Date(item.deadline).getTime() : Number.NaN)
|
|
32
|
+
.filter(time => !Number.isNaN(time))
|
|
33
|
+
|
|
34
|
+
return times.length > 0 ? new Date(Math.max(...times)) : undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Only what a person typed, echoed as the receipt's content — never enriched with a match. */
|
|
38
|
+
const contentOf = (fields: Record<string, string | undefined | null>): Record<string, string> =>
|
|
39
|
+
Object.fromEntries(Object.entries(fields).filter((entry): entry is [string, string] =>
|
|
40
|
+
typeof entry[1] === 'string' && entry[1].trim() !== ''))
|
|
41
|
+
|
|
42
|
+
const publicReceipt = (declaration: ConsumerDeclarationRecord, content: Record<string, string>, mailed: boolean): DeclarationReceipt => ({
|
|
43
|
+
declarationId: declaration.id as string,
|
|
44
|
+
receivedAt: new Date(declaration.receivedAt),
|
|
45
|
+
content,
|
|
46
|
+
mailed,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** An e-mail value safe to prefill a form with. */
|
|
50
|
+
const prefillEmail = (email: string | undefined): string | undefined =>
|
|
51
|
+
email != null && /^[^\s@]+@[^\s@]+$/.test(email) ? email : undefined
|
|
52
|
+
|
|
53
|
+
/** A withdrawal receipt from its declaration (in-app), `status` as executed so far. */
|
|
54
|
+
const receiptOf = (
|
|
55
|
+
declaration: ConsumerDeclarationRecord, content: Record<string, string>, mailed: boolean, status?: WithdrawalStatus,
|
|
56
|
+
): WithdrawalReceipt => paymentUtils.compact({
|
|
57
|
+
...publicReceipt(declaration, content, mailed),
|
|
58
|
+
status: status ?? declaration.status as WithdrawalStatus,
|
|
59
|
+
refundMinor: declaration.refundMinor ?? undefined,
|
|
60
|
+
currency: declaration.currency ?? undefined,
|
|
61
|
+
}) as WithdrawalReceipt
|
|
62
|
+
|
|
63
|
+
/** The late-configuration seam of every service `makeConsumerRightsService` made — module-private. */
|
|
64
|
+
const plumbers = new WeakMap<object, (plumbing: Plumbing, from: ConsumerRightsRegistrar) => void>()
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The consumer-rights service (`CONSUMER_RIGHTS_SERVICE`): the billing profile and its lock,
|
|
68
|
+
* purchases and their withdrawal windows, performance consent and subscription start requests,
|
|
69
|
+
* the withdrawal and cancellation functions with their paygate steps, the durable-medium mails,
|
|
70
|
+
* and `reconcile`. Unmanaged (`manage: false`) it still reads, records consents and asserts them;
|
|
71
|
+
* withdrawing and cancelling need the paygate.
|
|
72
|
+
*
|
|
73
|
+
* A lazy service, like the completion observer: `useMeter` / `useMailRenderer` work while the
|
|
74
|
+
* application is still being wired, before the context initializes.
|
|
75
|
+
*/
|
|
76
|
+
export const makeConsumerRightsService = (
|
|
77
|
+
alias: string = CONSUMER_RIGHTS_SERVICE, opts: ConsumerRightsOptions = {},
|
|
78
|
+
): ConsumerRightsService => {
|
|
79
|
+
// `manage` by precedence: the application's own, else the gateway's, else managed.
|
|
80
|
+
let manageOwn: boolean | undefined = opts.manage
|
|
81
|
+
let manageGateway: boolean | undefined
|
|
82
|
+
const isManaged = (): boolean => (manageOwn ?? manageGateway) !== false
|
|
83
|
+
let meter: UsageMeter | null = opts.usage ?? null
|
|
84
|
+
let stripeFactory = opts.stripe
|
|
85
|
+
let renderer: ConsumerMailRenderer | null = null
|
|
86
|
+
const stripeOf = async (ctx: ApiContext): Promise<Stripe> =>
|
|
87
|
+
stripeFactory != null ? await stripeFactory(ctx) : await paymentAccessOf(ctx).stripeClient()
|
|
88
|
+
const internals: ConsumerRightsInternals = {
|
|
89
|
+
get managed() { return isManaged() }, meter: () => meter, stripe: stripeOf,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const requirePolicy = async (ctx: ApiContext): Promise<ConsumerRightsPolicy> => {
|
|
93
|
+
const policy = await paymentAccessOf(ctx).payment().consumerRightsPolicy()
|
|
94
|
+
if (policy == null) {
|
|
95
|
+
throw new ConsumerRightsError('policy:none')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return policy
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A contract a person quoted — a contract reference, else an invoice number. */
|
|
102
|
+
const purchaseByContract = async (ctx: ApiContext, contract: string | undefined): Promise<PurchaseRecord | null> => {
|
|
103
|
+
const access = paymentAccessOf(ctx)
|
|
104
|
+
if (contract == null || contract.trim() === '') {
|
|
105
|
+
return null
|
|
106
|
+
}
|
|
107
|
+
const reference = consumerFormatHelper.normalizeContractRef(contract)
|
|
108
|
+
|
|
109
|
+
return await access.purchases().load({ contractRef: reference })
|
|
110
|
+
?? await access.purchases().load({ invoiceNumber: contract.trim() })
|
|
111
|
+
?? await access.purchases().load({ invoiceNumber: contract.trim().toUpperCase() })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Public matching: the quoted contract, and an e-mail that belongs to it. */
|
|
115
|
+
const matchPublicPurchase = async (ctx: ApiContext, contract: string | undefined, email: string): Promise<PurchaseRecord | null> => {
|
|
116
|
+
const purchase = await purchaseByContract(ctx, contract)
|
|
117
|
+
|
|
118
|
+
return purchase != null && await consumerRecordsOf(ctx).emailMatches(purchase, email) ? purchase : null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The one organization whose paygate customer uses this e-mail — none when several do. */
|
|
122
|
+
const entityByEmail = async (ctx: ApiContext, email: string): Promise<string | null> => {
|
|
123
|
+
const wanted = consumerFormatHelper.normalizeEmail(email)
|
|
124
|
+
if (wanted === '') {
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
// Case-insensitive (`$ilike`, its `%`/`_`/`\` wildcards escaped): a paygate keeps the e-mail as typed.
|
|
128
|
+
const pattern = wanted.replace(/[\\%_]/g, match => `\\${match}`)
|
|
129
|
+
const { items } = await paymentAccessOf(ctx).paygateCustomers().list({ paygate: STRIPE_PAYGATE_ALIAS, email: { $ilike: pattern } }, { size: 20 })
|
|
130
|
+
const entities = [...new Set(items.filter(item => item.deletedAt == null && item.entityId != null).map(item => item.entityId as string))]
|
|
131
|
+
|
|
132
|
+
return entities.length === 1 ? entities[0] : null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Where a withdrawal stands now: a `processing` declaration is `refunded` once its refund (and its
|
|
137
|
+
* subscription cancel) succeeded, `failed` after a failed attempt, else still `processing`.
|
|
138
|
+
*/
|
|
139
|
+
const executedStatusOf = async (ctx: ApiContext, declaration: ConsumerDeclarationRecord): Promise<WithdrawalStatus> => {
|
|
140
|
+
const access = paymentAccessOf(ctx)
|
|
141
|
+
if (declaration.status !== WithdrawalStatus.Processing) {
|
|
142
|
+
return declaration.status as WithdrawalStatus
|
|
143
|
+
}
|
|
144
|
+
const id = declaration.id as string
|
|
145
|
+
const refunded = (declaration.refundMinor ?? 0) <= 0 || await access.consumerEvents().load({ recordId: id, action: 'refund', ok: true }) != null
|
|
146
|
+
if (refunded) {
|
|
147
|
+
return WithdrawalStatus.Refunded
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return await access.consumerEvents().load({ recordId: id, action: 'refund', ok: false }) != null
|
|
151
|
+
? WithdrawalStatus.Failed : WithdrawalStatus.Processing
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const bootWarnings = async (ctx: ApiContext, managed: boolean, meter: () => UsageMeter | null): Promise<void> => {
|
|
155
|
+
const access = paymentAccessOf(ctx)
|
|
156
|
+
const policy = await access.payment().consumerRightsPolicy()
|
|
157
|
+
// An unmanaged process (a worker that asserts consent) neither mails nor refunds.
|
|
158
|
+
if (policy == null || !managed) {
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
const { mechanisms } = policy
|
|
162
|
+
const mailing = mechanisms.purchaseConfirmation || mechanisms.performanceConsent || mechanisms.subscriptionStart
|
|
163
|
+
|| mechanisms.withdrawal || mechanisms.cancellation
|
|
164
|
+
const mail = await access.consumerMailConfig()
|
|
165
|
+
if (mailing) {
|
|
166
|
+
const trader = mail?.trader
|
|
167
|
+
if (trader?.address == null || trader.email == null) {
|
|
168
|
+
log.warn('Consumer rights: the trader has no postal address or e-mail — the legal mails and the '
|
|
169
|
+
+ 'withdrawal information go out without them')
|
|
170
|
+
}
|
|
171
|
+
const alias = mail?.alias ?? MAILER_SERVICE
|
|
172
|
+
if ((ctx as unknown as { hasService?: (alias: string) => boolean }).hasService?.(alias) !== true) {
|
|
173
|
+
log.warn('Consumer rights: no mailer — no durable-medium mail is sent', { alias })
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (managed && mechanisms.withdrawal && mechanisms.automaticRefunds && meter() == null) {
|
|
177
|
+
log.warn('Consumer rights: no usage meter — every withdrawal is left to an operator (review)')
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const service: ConsumerRightsService = createLazyService<ConsumerRightsService>(alias, {
|
|
182
|
+
get managed() { return isManaged() },
|
|
183
|
+
|
|
184
|
+
policy: async () => await paymentAccessOf(service.assertCtx() as unknown as ApiContext).payment().consumerRightsPolicy(),
|
|
185
|
+
|
|
186
|
+
profile: async entityId => {
|
|
187
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
188
|
+
const access = paymentAccessOf(ctx)
|
|
189
|
+
const record = await access.billingProfiles().byEntity(entityId)
|
|
190
|
+
|
|
191
|
+
return record != null
|
|
192
|
+
? consumerRecordsOf(ctx).profileViewOf(record, await access.payment().consumerRightsPolicy()) : null
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
lock: async (entityId, country, source, lockOpts = {}) => {
|
|
196
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
197
|
+
const policy = await paymentAccessOf(ctx).payment().consumerRightsPolicy()
|
|
198
|
+
const { record } = await consumerRecordsOf(ctx).lockProfile(policy, { ...lockOpts, entityId, country, source })
|
|
199
|
+
|
|
200
|
+
return consumerRecordsOf(ctx).profileViewOf(record, policy)
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
unlock: async (entityId, unlockOpts = {}) => {
|
|
204
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
205
|
+
const record = await consumerRecordsOf(ctx).unlockProfile(entityId, unlockOpts)
|
|
206
|
+
|
|
207
|
+
return record != null
|
|
208
|
+
? consumerRecordsOf(ctx).profileViewOf(record, await paymentAccessOf(ctx).payment().consumerRightsPolicy()) : null
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
purchases: async (entityId, listOpts = {}) => {
|
|
212
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
213
|
+
const at = listOpts.at ?? new Date()
|
|
214
|
+
const { items } = await paymentAccessOf(ctx).purchases().list({ entityId }, {
|
|
215
|
+
size: 200, sort: [{ field: 'purchasedAt', order: 'desc' }],
|
|
216
|
+
})
|
|
217
|
+
const views: PurchaseView[] = []
|
|
218
|
+
for (const purchase of items) {
|
|
219
|
+
const open = makePurchaseModel(purchase).windowOpen(at)
|
|
220
|
+
if (listOpts.open === true && !open) continue
|
|
221
|
+
let withdrawable = open
|
|
222
|
+
if (open && meter != null && purchase.kind === PurchaseKind.TopUp) {
|
|
223
|
+
try {
|
|
224
|
+
withdrawable = (await withdrawalOf(ctx).computeWithdrawal(meter, purchase, at)).refundMinor > 0
|
|
225
|
+
} catch (error) {
|
|
226
|
+
log.warn('Purchase usage unreadable', { purchaseId: purchase.purchaseId, error })
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
views.push(makePurchaseModel(purchase).view(withdrawable))
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return views
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
consentView: async (entityId, at = new Date()) => {
|
|
236
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
237
|
+
const access = paymentAccessOf(ctx)
|
|
238
|
+
const policy = await requirePolicy(ctx)
|
|
239
|
+
const profile = await access.billingProfiles().byEntity(entityId)
|
|
240
|
+
const windows = policy.mechanisms.performanceConsent
|
|
241
|
+
? await consumerRecordsOf(ctx).unconsentedWindows(entityId, at) : []
|
|
242
|
+
const language = profile?.language ?? windows[0]?.language ?? policy.defaultLanguage
|
|
243
|
+
const deadline = latestDeadline(windows)
|
|
244
|
+
const view: PerformanceConsentView = {
|
|
245
|
+
required: windows.length > 0,
|
|
246
|
+
region: profile?.region ?? windows[0]?.region ?? null,
|
|
247
|
+
country: profile?.country ?? windows[0]?.country ?? null,
|
|
248
|
+
language,
|
|
249
|
+
trader: consumerMailOf(ctx).traderOf(await access.consumerMailConfig()).name,
|
|
250
|
+
...(policy.consentContext != null ? { context: policy.consentContext } : {}),
|
|
251
|
+
textVersion: policy.textVersion,
|
|
252
|
+
copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
253
|
+
links: consumerRightsPolicyHelper.linksOf(policy, language),
|
|
254
|
+
purchases: windows.map(window => makePurchaseModel(window).view(true)),
|
|
255
|
+
...(deadline != null ? { deadline } : {}),
|
|
256
|
+
at,
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return view
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
recordConsent: async (subject, body, origin) => {
|
|
263
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
264
|
+
const access = paymentAccessOf(ctx)
|
|
265
|
+
const policy = await requirePolicy(ctx)
|
|
266
|
+
if (!policy.mechanisms.performanceConsent) {
|
|
267
|
+
throw new ConsumerRightsError('mechanism:performance-consent')
|
|
268
|
+
}
|
|
269
|
+
const at = new Date()
|
|
270
|
+
const windows = await consumerRecordsOf(ctx).unconsentedWindows(subject.entityId, at)
|
|
271
|
+
const covered = windows.filter(window => body.purchaseIds.includes(window.purchaseId))
|
|
272
|
+
// A statement of another version, or one that saw none of what is open now, is asked again.
|
|
273
|
+
if (body.textVersion !== policy.textVersion || (covered.length === 0 && windows.length > 0)) {
|
|
274
|
+
throw new PerformanceConsentRequired({ pending: windows.length, ...(latestDeadline(windows) != null ? { deadline: latestDeadline(windows) } : {}) })
|
|
275
|
+
}
|
|
276
|
+
const trader = consumerMailOf(ctx).traderOf(await access.consumerMailConfig())
|
|
277
|
+
const deadline = latestDeadline(covered)
|
|
278
|
+
const consent = await access.consumerConsents().create(paymentUtils.compact({
|
|
279
|
+
kind: ConsentKind.Performance,
|
|
280
|
+
entityId: subject.entityId,
|
|
281
|
+
profileId: subject.profileId,
|
|
282
|
+
name: subject.name,
|
|
283
|
+
email: prefillEmail(subject.email),
|
|
284
|
+
purchaseIds: covered.map(window => window.purchaseId),
|
|
285
|
+
textVersion: policy.textVersion,
|
|
286
|
+
copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
287
|
+
language: body.language,
|
|
288
|
+
uiLanguage: body.uiLanguage,
|
|
289
|
+
trader: trader.name,
|
|
290
|
+
context: policy.consentContext,
|
|
291
|
+
text: consumerCopyHelper.consentStatementOf(body.language, ConsentKind.Performance, {
|
|
292
|
+
trader: trader.name, context: policy.consentContext,
|
|
293
|
+
}),
|
|
294
|
+
links: consumerRightsPolicyHelper.linksOf(policy, body.language),
|
|
295
|
+
deadline,
|
|
296
|
+
decidedAt: at,
|
|
297
|
+
...originHelper.originFields(origin),
|
|
298
|
+
}) as ConsumerConsentRecord)
|
|
299
|
+
for (const window of covered) {
|
|
300
|
+
await paymentUtils.conditionalSet(access.purchases(), { purchaseId: window.purchaseId, consentedAt: null }, {
|
|
301
|
+
consentedAt: at, consentId: consent.id, updatedAt: at,
|
|
302
|
+
})
|
|
303
|
+
}
|
|
304
|
+
const mailed = covered.length > 0 ? await consumerMailOf(ctx).sendConsumerMail(policy, 'consent', consent.id as string) : false
|
|
305
|
+
await consumerObserversOf(ctx).notifyConsent(consent)
|
|
306
|
+
|
|
307
|
+
return { consentId: consent.id as string, consentedAt: at, purchaseIds: [...consent.purchaseIds], mailed }
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
assertConsent: async (entityId, at = new Date()) => {
|
|
311
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
312
|
+
const policy = await paymentAccessOf(ctx).payment().consumerRightsPolicy()
|
|
313
|
+
if (policy?.mechanisms.performanceConsent !== true) {
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
const windows = await consumerRecordsOf(ctx).unconsentedWindows(entityId, at)
|
|
317
|
+
if (windows.length > 0) {
|
|
318
|
+
const deadline = latestDeadline(windows)
|
|
319
|
+
throw new PerformanceConsentRequired({ pending: windows.length, ...(deadline != null ? { deadline } : {}) })
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
startView: async (entityId, planSku, viewOpts = {}) => {
|
|
324
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
325
|
+
const access = paymentAccessOf(ctx)
|
|
326
|
+
const policy = await requirePolicy(ctx)
|
|
327
|
+
const profile = await access.billingProfiles().byEntity(entityId)
|
|
328
|
+
const language = viewOpts.language ?? profile?.language ?? policy.defaultLanguage
|
|
329
|
+
// The statement follows the plan's withdrawal arithmetic — the variant `recordStartRequest` records.
|
|
330
|
+
const context = consumerCopyHelper.startContextOf(await catalogueOf(ctx).findPlan(planSku))
|
|
331
|
+
const view: SubscriptionStartView = {
|
|
332
|
+
required: policy.mechanisms.subscriptionStart
|
|
333
|
+
&& (profile == null || consumerRegionHelper.inScope(profile.region, profile.country, policy)),
|
|
334
|
+
planSku,
|
|
335
|
+
language,
|
|
336
|
+
trader: consumerMailOf(ctx).traderOf(await access.consumerMailConfig()).name,
|
|
337
|
+
...(context != null ? { context } : {}),
|
|
338
|
+
textVersion: policy.textVersion,
|
|
339
|
+
copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
340
|
+
links: consumerRightsPolicyHelper.linksOf(policy, language),
|
|
341
|
+
region: profile?.region ?? null,
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return view
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
recordStartRequest: async (subject, body, origin, startOpts = {}) => {
|
|
348
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
349
|
+
const access = paymentAccessOf(ctx)
|
|
350
|
+
const policy = await requirePolicy(ctx)
|
|
351
|
+
if (!policy.mechanisms.subscriptionStart) {
|
|
352
|
+
throw new ConsumerRightsError('mechanism:subscription-start')
|
|
353
|
+
}
|
|
354
|
+
if (body.textVersion !== policy.textVersion) {
|
|
355
|
+
throw new SubscriptionStartRequired(body.planSku)
|
|
356
|
+
}
|
|
357
|
+
const plan = await catalogueOf(ctx).findPlan(body.planSku)
|
|
358
|
+
if (plan == null) {
|
|
359
|
+
throw new UnknownPlan(body.planSku)
|
|
360
|
+
}
|
|
361
|
+
const context = consumerCopyHelper.startContextOf(plan)
|
|
362
|
+
const at = new Date()
|
|
363
|
+
const expiresAt = new Date(at.getTime() + (policy.startRequestTtlSeconds ?? 3600) * 1000)
|
|
364
|
+
const trader = consumerMailOf(ctx).traderOf(await access.consumerMailConfig())
|
|
365
|
+
const planName = startOpts.plan ?? await consumerMailOf(ctx).planTitleOf(body.planSku, body.language)
|
|
366
|
+
const consent = await access.consumerConsents().create(paymentUtils.compact({
|
|
367
|
+
kind: ConsentKind.SubscriptionStart,
|
|
368
|
+
entityId: subject.entityId,
|
|
369
|
+
profileId: subject.profileId,
|
|
370
|
+
name: subject.name,
|
|
371
|
+
email: prefillEmail(subject.email),
|
|
372
|
+
purchaseIds: [],
|
|
373
|
+
planSku: body.planSku,
|
|
374
|
+
planName,
|
|
375
|
+
textVersion: policy.textVersion,
|
|
376
|
+
copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
377
|
+
language: body.language,
|
|
378
|
+
trader: trader.name,
|
|
379
|
+
context,
|
|
380
|
+
text: consumerCopyHelper.consentStatementOf(body.language, ConsentKind.SubscriptionStart, { trader: trader.name, plan: planName, context }),
|
|
381
|
+
links: consumerRightsPolicyHelper.linksOf(policy, body.language),
|
|
382
|
+
decidedAt: at,
|
|
383
|
+
expiresAt,
|
|
384
|
+
...originHelper.originFields(origin),
|
|
385
|
+
}) as ConsumerConsentRecord)
|
|
386
|
+
await consumerMailOf(ctx).sendConsumerMail(policy, 'start', consent.id as string)
|
|
387
|
+
await consumerObserversOf(ctx).notifyConsent(consent)
|
|
388
|
+
|
|
389
|
+
return { startRequestId: consent.id as string, requestedAt: at, expiresAt }
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
assertStartRequest: async (entityId, planSku, startRequestId) => {
|
|
393
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
394
|
+
const access = paymentAccessOf(ctx)
|
|
395
|
+
const policy = await access.payment().consumerRightsPolicy()
|
|
396
|
+
if (policy?.mechanisms.subscriptionStart !== true) {
|
|
397
|
+
return null
|
|
398
|
+
}
|
|
399
|
+
const profile = await access.billingProfiles().byEntity(entityId)
|
|
400
|
+
// Only an organization already locked outside the territories goes without one: a country
|
|
401
|
+
// picked before checkout may differ from the address typed at the paygate.
|
|
402
|
+
if (profile != null && !consumerRegionHelper.inScope(profile.region, profile.country, policy)) {
|
|
403
|
+
return null
|
|
404
|
+
}
|
|
405
|
+
const record = startRequestId != null && startRequestId !== ''
|
|
406
|
+
? await access.consumerConsents().load(startRequestId).catch(() => null) : null
|
|
407
|
+
if (record == null || record.kind !== ConsentKind.SubscriptionStart || record.entityId !== entityId
|
|
408
|
+
|| record.planSku !== planSku || record.textVersion !== policy.textVersion
|
|
409
|
+
|| record.expiresAt == null || new Date(record.expiresAt).getTime() <= Date.now()) {
|
|
410
|
+
throw new SubscriptionStartRequired(planSku)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return record
|
|
414
|
+
},
|
|
415
|
+
|
|
416
|
+
withdrawalCandidates: async (entityId, subject = {}) => {
|
|
417
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
418
|
+
const access = paymentAccessOf(ctx)
|
|
419
|
+
const policy = await requirePolicy(ctx)
|
|
420
|
+
const profile = await access.billingProfiles().byEntity(entityId)
|
|
421
|
+
const at = new Date()
|
|
422
|
+
const open = policy.mechanisms.withdrawal
|
|
423
|
+
? (await access.purchases().list({
|
|
424
|
+
entityId, inScope: true, withdrawnAt: null, refundedAt: null, deadline: { $gt: at },
|
|
425
|
+
}, { size: 100, sort: [{ field: 'purchasedAt', order: 'desc' }] })).items
|
|
426
|
+
: []
|
|
427
|
+
const candidates: WithdrawalCandidate[] = []
|
|
428
|
+
for (const purchase of open) {
|
|
429
|
+
let estimate: WithdrawalCandidate['estimate'] = null
|
|
430
|
+
if (meter != null) {
|
|
431
|
+
try {
|
|
432
|
+
estimate = (await withdrawalOf(ctx).computeWithdrawal(meter, purchase, at)).estimate
|
|
433
|
+
} catch (error) {
|
|
434
|
+
log.warn('Withdrawal estimate failed', { purchaseId: purchase.purchaseId, error })
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
// Credits fully used after consent: the right has expired, nothing would be reimbursed.
|
|
438
|
+
if (estimate != null && purchase.kind === PurchaseKind.TopUp && estimate.refundMinor === 0) continue
|
|
439
|
+
candidates.push({
|
|
440
|
+
purchaseId: purchase.purchaseId,
|
|
441
|
+
contractRef: purchase.contractRef,
|
|
442
|
+
kind: purchase.kind,
|
|
443
|
+
purchasedAt: new Date(purchase.purchasedAt),
|
|
444
|
+
deadline: new Date(purchase.deadline as Date),
|
|
445
|
+
amountTotalMinor: purchase.amountTotalMinor,
|
|
446
|
+
currency: purchase.currency,
|
|
447
|
+
estimate,
|
|
448
|
+
automatic: policy.mechanisms.automaticRefunds && meter != null && isManaged(),
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
const language = profile?.language ?? open[0]?.language ?? policy.defaultLanguage
|
|
452
|
+
|
|
453
|
+
return paymentUtils.compact({
|
|
454
|
+
candidates, language, links: consumerRightsPolicyHelper.linksOf(policy, language),
|
|
455
|
+
name: subject.name != null && subject.name.trim() !== '' ? subject.name : undefined,
|
|
456
|
+
email: prefillEmail(subject.email),
|
|
457
|
+
}) as Awaited<ReturnType<ConsumerRightsService['withdrawalCandidates']>>
|
|
458
|
+
},
|
|
459
|
+
|
|
460
|
+
withdraw: async (subject, body, origin) => {
|
|
461
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
462
|
+
const access = paymentAccessOf(ctx)
|
|
463
|
+
const policy = await requirePolicy(ctx)
|
|
464
|
+
if (!policy.mechanisms.withdrawal) {
|
|
465
|
+
throw new ConsumerRightsError('mechanism:withdrawal')
|
|
466
|
+
}
|
|
467
|
+
if (!isManaged()) {
|
|
468
|
+
throw new PaygateError('unmanaged')
|
|
469
|
+
}
|
|
470
|
+
const at = new Date()
|
|
471
|
+
const channel = subject == null ? DeclarationChannel.Public : subject.channel ?? DeclarationChannel.InApp
|
|
472
|
+
const disclose = channel === DeclarationChannel.InApp
|
|
473
|
+
|
|
474
|
+
let purchase: PurchaseRecord | null = null
|
|
475
|
+
if (subject != null) {
|
|
476
|
+
purchase = body.purchaseId != null ? await access.purchases().byPurchaseId(body.purchaseId)
|
|
477
|
+
: await purchaseByContract(ctx, body.contractRef)
|
|
478
|
+
if (purchase != null && purchase.entityId !== subject.entityId) purchase = null
|
|
479
|
+
} else {
|
|
480
|
+
purchase = await matchPublicPurchase(ctx, body.contractRef, body.email)
|
|
481
|
+
}
|
|
482
|
+
if (disclose) {
|
|
483
|
+
if (purchase == null) throw new WithdrawalUnavailable(WithdrawalUnavailableReason.Unknown)
|
|
484
|
+
if (!purchase.inScope) throw new WithdrawalUnavailable(WithdrawalUnavailableReason.NotInScope)
|
|
485
|
+
if (purchase.withdrawnAt == null && purchase.refundedAt != null) {
|
|
486
|
+
throw new WithdrawalUnavailable(WithdrawalUnavailableReason.Withdrawn)
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
const content = contentOf({
|
|
490
|
+
name: body.name, contract: body.contractRef ?? (disclose ? purchase?.contractRef : undefined), email: body.email,
|
|
491
|
+
})
|
|
492
|
+
const profile = purchase != null ? await access.billingProfiles().byEntity(purchase.entityId) : null
|
|
493
|
+
const language = body.language ?? purchase?.language ?? profile?.language ?? policy.defaultLanguage
|
|
494
|
+
const declarationBase = {
|
|
495
|
+
kind: DeclarationKind.Withdrawal, channel,
|
|
496
|
+
entityId: purchase?.entityId ?? subject?.entityId,
|
|
497
|
+
purchaseId: purchase?.purchaseId,
|
|
498
|
+
subscriptionId: purchase?.subscriptionId ?? undefined,
|
|
499
|
+
contractRef: body.contractRef ?? purchase?.contractRef,
|
|
500
|
+
name: body.name, email: body.email, language,
|
|
501
|
+
textVersion: policy.textVersion, copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
502
|
+
receivedAt: at, matched: purchase != null, profileId: subject?.profileId, ...originHelper.originFields(origin),
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// A repeated declaration of a contract already withdrawn from: recorded, answered with the original.
|
|
506
|
+
if (purchase?.withdrawnAt != null) {
|
|
507
|
+
const original = purchase.withdrawalId != null ? await access.consumerDeclarations().load(purchase.withdrawalId).catch(() => null) : null
|
|
508
|
+
const repeated = await access.consumerDeclarations().create(paymentUtils.compact({
|
|
509
|
+
...declarationBase, duplicateOf: original?.id, status: original?.status ?? WithdrawalStatus.Received,
|
|
510
|
+
}) as ConsumerDeclarationRecord)
|
|
511
|
+
if (!disclose) {
|
|
512
|
+
return publicReceipt(repeated, content, false) as WithdrawalReceipt
|
|
513
|
+
}
|
|
514
|
+
const answered = original ?? repeated
|
|
515
|
+
|
|
516
|
+
return receiptOf(answered, content, false, await executedStatusOf(ctx, answered))
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
let status: WithdrawalStatus
|
|
520
|
+
let computation: WithdrawalComputation | null = null
|
|
521
|
+
if (purchase == null || !purchase.inScope || purchase.refundedAt != null) {
|
|
522
|
+
status = WithdrawalStatus.Received
|
|
523
|
+
} else if (purchase.deadline == null || at.getTime() >= new Date(purchase.deadline).getTime()) {
|
|
524
|
+
status = WithdrawalStatus.Expired
|
|
525
|
+
} else if (meter != null && policy.mechanisms.automaticRefunds) {
|
|
526
|
+
try {
|
|
527
|
+
computation = await withdrawalOf(ctx).computeWithdrawal(meter, purchase, at)
|
|
528
|
+
} catch (error) {
|
|
529
|
+
await consumerRecordsOf(ctx).recordEvent({
|
|
530
|
+
recordId: purchase.purchaseId, recordKind: 'purchase', entityId: purchase.entityId, action: 'meter', ok: false,
|
|
531
|
+
error: paymentUtils.errorText(error),
|
|
532
|
+
})
|
|
533
|
+
}
|
|
534
|
+
status = computation != null ? WithdrawalStatus.Processing : WithdrawalStatus.Review
|
|
535
|
+
if (computation != null && purchase.kind === PurchaseKind.TopUp && computation.refundMinor === 0) {
|
|
536
|
+
if (disclose) throw new WithdrawalUnavailable(WithdrawalUnavailableReason.Performed)
|
|
537
|
+
computation = null
|
|
538
|
+
status = WithdrawalStatus.Review
|
|
539
|
+
}
|
|
540
|
+
} else {
|
|
541
|
+
status = WithdrawalStatus.Review
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const declaration = await access.consumerDeclarations().create(paymentUtils.compact({
|
|
545
|
+
...declarationBase, status,
|
|
546
|
+
refundMinor: computation?.refundMinor, currency: computation != null ? purchase?.currency : undefined,
|
|
547
|
+
}) as ConsumerDeclarationRecord)
|
|
548
|
+
const withdrawalId = declaration.id as string
|
|
549
|
+
|
|
550
|
+
if (purchase != null && (status === WithdrawalStatus.Processing || status === WithdrawalStatus.Review)) {
|
|
551
|
+
// The window closes now; only one declaration of a purchase wins it.
|
|
552
|
+
const won = await paymentUtils.conditionalSet(access.purchases(), { purchaseId: purchase.purchaseId, withdrawnAt: null }, {
|
|
553
|
+
withdrawnAt: at, withdrawalId, updatedAt: at,
|
|
554
|
+
})
|
|
555
|
+
if (!won) {
|
|
556
|
+
const winner = await access.purchases().byPurchaseId(purchase.purchaseId)
|
|
557
|
+
await consumerRecordsOf(ctx).recordEvent({
|
|
558
|
+
recordId: withdrawalId, recordKind: 'declaration', entityId: purchase.entityId, action: 'duplicate', ok: true,
|
|
559
|
+
detail: JSON.stringify({ of: winner?.withdrawalId }),
|
|
560
|
+
})
|
|
561
|
+
const original = winner?.withdrawalId != null ? await access.consumerDeclarations().load(winner.withdrawalId).catch(() => null) : null
|
|
562
|
+
|
|
563
|
+
const answered = original ?? declaration
|
|
564
|
+
|
|
565
|
+
return disclose
|
|
566
|
+
? receiptOf(answered, content, false, await executedStatusOf(ctx, answered))
|
|
567
|
+
: publicReceipt(declaration, content, false) as WithdrawalReceipt
|
|
568
|
+
}
|
|
569
|
+
purchase = { ...purchase, withdrawnAt: at, withdrawalId }
|
|
570
|
+
}
|
|
571
|
+
if (computation != null && purchase != null) {
|
|
572
|
+
await consumerRecordsOf(ctx).recordEvent({
|
|
573
|
+
recordId: withdrawalId, recordKind: 'declaration', entityId: purchase.entityId, action: 'computed', ok: true,
|
|
574
|
+
amountMinor: computation.refundMinor, currency: purchase.currency,
|
|
575
|
+
detail: JSON.stringify({
|
|
576
|
+
reading: computation.reading, deducted: computation.deducted, netMinor: computation.netMinor,
|
|
577
|
+
unitsReturned: computation.unitsReturned, estimate: computation.estimate,
|
|
578
|
+
}),
|
|
579
|
+
})
|
|
580
|
+
}
|
|
581
|
+
const mailed = await consumerMailOf(ctx).sendConsumerMail(policy, 'withdrawal', withdrawalId)
|
|
582
|
+
|
|
583
|
+
let final: WithdrawalStatus = status
|
|
584
|
+
let execution: WithdrawalExecution | null = null
|
|
585
|
+
if (status === WithdrawalStatus.Processing && purchase != null && computation != null) {
|
|
586
|
+
execution = await withdrawalOf(ctx).executeWithdrawal(await stripeOf(ctx), declaration, purchase, computation)
|
|
587
|
+
final = execution.needsReview ? WithdrawalStatus.Review : execution.ok ? WithdrawalStatus.Refunded : WithdrawalStatus.Failed
|
|
588
|
+
}
|
|
589
|
+
if (purchase != null && (final === WithdrawalStatus.Refunded || final === WithdrawalStatus.Review)
|
|
590
|
+
&& (status === WithdrawalStatus.Processing || status === WithdrawalStatus.Review)) {
|
|
591
|
+
await consumerObserversOf(ctx).notifyWithdrawal(declaration, purchase, final, computation, execution)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (!disclose) {
|
|
595
|
+
return publicReceipt(declaration, content, mailed) as WithdrawalReceipt
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
return paymentUtils.compact({
|
|
599
|
+
...receiptOf(declaration, content, mailed), status: final,
|
|
600
|
+
refundMinor: execution?.refundedMinor ?? computation?.refundMinor,
|
|
601
|
+
subscriptionCanceled: execution?.subscriptionCanceled,
|
|
602
|
+
}) as WithdrawalReceipt
|
|
603
|
+
},
|
|
604
|
+
|
|
605
|
+
cancel: async (subject, body, origin) => {
|
|
606
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
607
|
+
const access = paymentAccessOf(ctx)
|
|
608
|
+
const policy = await requirePolicy(ctx)
|
|
609
|
+
if (!policy.mechanisms.cancellation) {
|
|
610
|
+
throw new ConsumerRightsError('mechanism:cancellation')
|
|
611
|
+
}
|
|
612
|
+
if (!isManaged()) {
|
|
613
|
+
throw new PaygateError('unmanaged')
|
|
614
|
+
}
|
|
615
|
+
const at = new Date()
|
|
616
|
+
const channel = subject == null ? DeclarationChannel.Public : subject.channel ?? DeclarationChannel.InApp
|
|
617
|
+
const disclose = channel === DeclarationChannel.InApp
|
|
618
|
+
|
|
619
|
+
let purchase: PurchaseRecord | null = null
|
|
620
|
+
let row: PaymentSubscriptionRecord | null = null
|
|
621
|
+
if (subject != null) {
|
|
622
|
+
if (body.subscriptionId != null) {
|
|
623
|
+
row = await access.subscriptions().byExternalId(body.subscriptionId, STRIPE_PAYGATE_ALIAS)
|
|
624
|
+
} else if (body.contractRef != null) {
|
|
625
|
+
purchase = await purchaseByContract(ctx, body.contractRef)
|
|
626
|
+
if (purchase != null && purchase.entityId === subject.entityId && purchase.subscriptionId != null) {
|
|
627
|
+
row = await access.subscriptions().byExternalId(purchase.subscriptionId, STRIPE_PAYGATE_ALIAS)
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
row = row != null && row.entityId === subject.entityId
|
|
631
|
+
? row : await consumerRecordsOf(ctx).entitlingStripeSubscription(subject.entityId)
|
|
632
|
+
} else {
|
|
633
|
+
purchase = await matchPublicPurchase(ctx, body.contractRef, body.email)
|
|
634
|
+
if (purchase?.subscriptionId != null) {
|
|
635
|
+
row = await access.subscriptions().byExternalId(purchase.subscriptionId, STRIPE_PAYGATE_ALIAS)
|
|
636
|
+
} else if (purchase == null) {
|
|
637
|
+
const entityId = await entityByEmail(ctx, body.email)
|
|
638
|
+
row = entityId != null ? await consumerRecordsOf(ctx).entitlingStripeSubscription(entityId) : null
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
const live = row != null && !TERMINAL_STATUSES.includes(row.status) && ENTITLING_STATUSES.includes(row.status)
|
|
642
|
+
if (disclose && row == null) throw new CancellationUnavailable(CancellationUnavailableReason.NoSubscription)
|
|
643
|
+
if (disclose && !live) throw new CancellationUnavailable(CancellationUnavailableReason.Ended)
|
|
644
|
+
if (row != null && purchase == null) {
|
|
645
|
+
purchase = await access.purchases().load({ subscriptionId: row.externalId })
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const requested = body.effective === 'date' && body.date != null ? new Date(`${body.date}T00:00:00.000Z`) : null
|
|
649
|
+
const plan = row != null ? await catalogueOf(ctx).findPlan(row.planSku) : null
|
|
650
|
+
let status: CancellationStatus
|
|
651
|
+
let effectiveAt: Date | undefined
|
|
652
|
+
if (body.kind === CancellationKind.Extraordinary) {
|
|
653
|
+
// For cause: recorded and left to an operator — the paygate is not touched.
|
|
654
|
+
status = CancellationStatus.Review
|
|
655
|
+
} else if (row != null && live && row.periodEnd != null) {
|
|
656
|
+
effectiveAt = cancellationEffectiveAt(new Date(row.periodEnd), plan?.recurring?.interval ?? 'month',
|
|
657
|
+
requested != null && !Number.isNaN(requested.getTime()) ? requested : null)
|
|
658
|
+
status = row.cancelAtPeriodEnd === true && effectiveAt.getTime() === new Date(row.periodEnd).getTime()
|
|
659
|
+
? CancellationStatus.AlreadyScheduled : CancellationStatus.Scheduled
|
|
660
|
+
} else {
|
|
661
|
+
status = CancellationStatus.Received
|
|
662
|
+
}
|
|
663
|
+
const profile = row != null ? await access.billingProfiles().byEntity(row.entityId) : null
|
|
664
|
+
const language = body.language ?? purchase?.language ?? profile?.language ?? policy.defaultLanguage
|
|
665
|
+
const declaration = await access.consumerDeclarations().create(paymentUtils.compact({
|
|
666
|
+
kind: DeclarationKind.Cancellation, channel,
|
|
667
|
+
entityId: row?.entityId ?? subject?.entityId,
|
|
668
|
+
purchaseId: purchase?.purchaseId,
|
|
669
|
+
subscriptionId: row?.externalId,
|
|
670
|
+
contractRef: body.contractRef ?? purchase?.contractRef,
|
|
671
|
+
name: body.name, email: body.email,
|
|
672
|
+
cancellationKind: body.kind, reason: body.reason, effective: body.effective, requestedDate: body.date,
|
|
673
|
+
language, textVersion: policy.textVersion, copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
674
|
+
receivedAt: at, matched: row != null && live, profileId: subject?.profileId, status, effectiveAt,
|
|
675
|
+
...originHelper.originFields(origin),
|
|
676
|
+
}) as ConsumerDeclarationRecord)
|
|
677
|
+
let final = status
|
|
678
|
+
if (status === CancellationStatus.Scheduled && row != null) {
|
|
679
|
+
final = await cancellationOf(ctx).scheduleCancellation(await stripeOf(ctx), declaration, row)
|
|
680
|
+
? CancellationStatus.Scheduled : CancellationStatus.Received
|
|
681
|
+
}
|
|
682
|
+
if (purchase != null && effectiveAt != null) {
|
|
683
|
+
await consumerRecordsOf(ctx).patchPurchase(purchase.purchaseId, {
|
|
684
|
+
cancellationId: declaration.id as string, cancelEffectiveAt: effectiveAt,
|
|
685
|
+
})
|
|
686
|
+
}
|
|
687
|
+
const mailed = await consumerMailOf(ctx).sendConsumerMail(policy, 'cancellation', declaration.id as string)
|
|
688
|
+
await consumerObserversOf(ctx).notifyCancellation(declaration, final)
|
|
689
|
+
const content = contentOf({
|
|
690
|
+
name: body.name, contract: body.contractRef ?? (disclose ? purchase?.contractRef ?? row?.externalId : undefined),
|
|
691
|
+
email: body.email, kind: body.kind, reason: body.reason, date: body.effective === 'date' ? body.date : undefined,
|
|
692
|
+
})
|
|
693
|
+
if (!disclose) {
|
|
694
|
+
return publicReceipt(declaration, content, mailed) as CancellationReceipt
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return paymentUtils.compact({
|
|
698
|
+
...publicReceipt(declaration, content, mailed), status: final, effectiveAt,
|
|
699
|
+
}) as CancellationReceipt
|
|
700
|
+
},
|
|
701
|
+
|
|
702
|
+
useMeter: next => { meter = next },
|
|
703
|
+
useMailRenderer: next => { renderer = next },
|
|
704
|
+
usageMeter: () => meter,
|
|
705
|
+
mailRenderer: () => renderer,
|
|
706
|
+
|
|
707
|
+
reconcile: async (reconcileOpts = {}) =>
|
|
708
|
+
await makeConsumerReconcileHelper(service.assertCtx() as unknown as ApiContext, internals)
|
|
709
|
+
.reconcile(reconcileOpts),
|
|
710
|
+
}, service => async () => {
|
|
711
|
+
service.initialized = true
|
|
712
|
+
const ctx = service.assertCtx() as unknown as ApiContext
|
|
713
|
+
void ctx.waitForInitialized?.().then(async () => { await bootWarnings(ctx, isManaged(), () => meter) })
|
|
714
|
+
.catch(error => { log.error('Consumer-rights boot check failed', error) })
|
|
715
|
+
})
|
|
716
|
+
plumbers.set(service, (plumbing, from) => {
|
|
717
|
+
if (plumbing.manage != null) {
|
|
718
|
+
if (from === 'application') manageOwn = plumbing.manage
|
|
719
|
+
else manageGateway = plumbing.manage
|
|
720
|
+
}
|
|
721
|
+
if (plumbing.usage != null) meter = plumbing.usage
|
|
722
|
+
if (plumbing.stripe != null) stripeFactory = plumbing.stripe
|
|
723
|
+
})
|
|
724
|
+
|
|
725
|
+
return service
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Register the consumer-rights resources and service (each only when absent), or configure the
|
|
730
|
+
* service already registered — `from` says who asks. The order of the application's call and the
|
|
731
|
+
* gateway's is free: `usage` and `stripe` are installed whenever they come, and the application's
|
|
732
|
+
* explicit `manage` wins over the gateway's. The resources keep the first registration's aliases.
|
|
733
|
+
*/
|
|
734
|
+
export const registerConsumerRights = <C extends Config, T extends Context<C>>(
|
|
735
|
+
ctx: T, opts: ConsumerRightsOptions = {}, from: ConsumerRightsRegistrar = 'application',
|
|
736
|
+
): T => {
|
|
737
|
+
for (const [alias, maker] of CONSUMER_RIGHTS_RESOURCE_MAKERS) {
|
|
738
|
+
if (!ctx.hasResource(alias)) {
|
|
739
|
+
ctx.registerResource(maker(opts.dbAlias, opts.serviceAlias) as never)
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
const alias = opts.alias ?? CONSUMER_RIGHTS_SERVICE
|
|
743
|
+
// A lazy service: reachable before the context initializes.
|
|
744
|
+
let service = ctx.hasService(alias) ? ctx.service<ConsumerRightsService>(alias) : null
|
|
745
|
+
if (service == null) {
|
|
746
|
+
service = makeConsumerRightsService(alias, { ...opts, manage: undefined, usage: undefined, stripe: undefined })
|
|
747
|
+
ctx.registerService(service)
|
|
748
|
+
}
|
|
749
|
+
const plumb = plumbers.get(service)
|
|
750
|
+
if (plumb != null) {
|
|
751
|
+
plumb(paymentUtils.compact({ manage: opts.manage, usage: opts.usage, stripe: opts.stripe }), from)
|
|
752
|
+
} else if (opts.usage != null) {
|
|
753
|
+
// A service made elsewhere: only its public seam.
|
|
754
|
+
service.useMeter(opts.usage)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
return ctx
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Register the consumer-rights resources and service, each only when absent — the gateway
|
|
762
|
+
* registration does too. Called before or after the gateway, it installs `usage` and `stripe` on
|
|
763
|
+
* the service and its explicit `manage` wins over the gateway's; `useMeter` and `useMailRenderer`
|
|
764
|
+
* work on the service at any time.
|
|
765
|
+
*/
|
|
766
|
+
export const appendConsumerRights = <C extends Config, T extends Context<C>>(
|
|
767
|
+
ctx: T, opts: ConsumerRightsOptions = {},
|
|
768
|
+
): T => registerConsumerRights(ctx, opts, 'application')
|