@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,51 @@
|
|
|
1
|
+
import type { LimitView } from '@owlmeans/payment'
|
|
2
|
+
import type {
|
|
3
|
+
ConsumeRequest, CounterReconciliation, LimitOutcome, OccupancyOutcome, PaymentUsageRecord, ReleaseRequest,
|
|
4
|
+
} from '../types.js'
|
|
5
|
+
|
|
6
|
+
/** One usage counter: an entity's limit in one window. */
|
|
7
|
+
export interface CounterKey { entityId: string; limitKey: string; window: string }
|
|
8
|
+
|
|
9
|
+
export interface LedgerSum { entityId: string; limitKey: string; window: string; used: number }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The usage ledger of a context and its counters. `payment-usage` events are the source of truth;
|
|
13
|
+
* `payment-usage-counter` is a projection kept for synchronous admission — the counter may
|
|
14
|
+
* over-count, never over-admit.
|
|
15
|
+
*/
|
|
16
|
+
export interface UsageHelper {
|
|
17
|
+
/** @throws LimitExhausted | LimitUnknown */
|
|
18
|
+
consumeLimit: (req: ConsumeRequest) => Promise<LimitOutcome>
|
|
19
|
+
/** Idempotent: the release event is appended first, the counter decremented only by that append. */
|
|
20
|
+
releaseLimit: (req: ReleaseRequest) => Promise<LimitOutcome>
|
|
21
|
+
/** The active consume event of a key, or `null` (never consumed, or released). */
|
|
22
|
+
consumptionOf: (entityId: string, limitKey: string, eventKey: string) => Promise<PaymentUsageRecord | null>
|
|
23
|
+
/**
|
|
24
|
+
* The newest active consume event (not released) that pays for `ref`, or `null`. For a unit held
|
|
25
|
+
* per record under a fresh event key each time — an occupancy acquired again after a release —
|
|
26
|
+
* this answers "does the record hold a unit, and under which key".
|
|
27
|
+
*/
|
|
28
|
+
consumptionByRefOf: (entityId: string, limitKey: string, ref: string) => Promise<PaymentUsageRecord | null>
|
|
29
|
+
/** One limit of the effective plan, read against its current window. @throws LimitUnknown */
|
|
30
|
+
limitStateOf: (entityId: string, limitKey: string) => Promise<LimitView>
|
|
31
|
+
/** Sum of every event's `delta`, per (entity, limit, window). */
|
|
32
|
+
ledgerSums: (match: Partial<CounterKey>) => Promise<LedgerSum[]>
|
|
33
|
+
/**
|
|
34
|
+
* Bring an occupancy limit's ledger and counter to the live count. Appends at most one adjusting
|
|
35
|
+
* event per limit per UTC day (`reconcile:<key>:<YYYY-MM-DD>`), adjusted in place by a later run of
|
|
36
|
+
* the same day, so the ledger sum always equals what was last observed. Flags `overSince` while the
|
|
37
|
+
* count exceeds the limit. Never stops anything.
|
|
38
|
+
*
|
|
39
|
+
* @throws LimitUnknown | LimitMisdeclared
|
|
40
|
+
*/
|
|
41
|
+
reconcileOccupancyOf: (entityId: string, limitKey: string, actual: number) => Promise<OccupancyOutcome>
|
|
42
|
+
/**
|
|
43
|
+
* Recompute counters from the ledger — one entity, or every entity the ledger knows.
|
|
44
|
+
*
|
|
45
|
+
* Every (entity, limit, window) with events gets `used = max(0, sum)` and the limit of the
|
|
46
|
+
* entity's current plan; a counter of a past day/month with no events is deleted (lifetime and
|
|
47
|
+
* occupancy counters never are); a counter with no events otherwise reads `0`; and the current
|
|
48
|
+
* window counter of every declared window limit exists.
|
|
49
|
+
*/
|
|
50
|
+
reconcileLedgerCounters: (entityId?: string) => Promise<CounterReconciliation>
|
|
51
|
+
}
|
package/src/usage.ts
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { LimitExhausted, LimitKind, LimitMisdeclared, LimitUnknown, LimitWindow, OCCUPANCY_WINDOW, SUBSCRIPTION_WEEK_MS, SUBSCRIPTION_WEEK_PREFIX, type LimitDeclaration, type LimitView, entitlementViewHelper, planLimitHelper, promoHelper } from '@owlmeans/payment'
|
|
2
|
+
import { UnsupportedArgumentError } from '@owlmeans/resource'
|
|
3
|
+
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
4
|
+
import { memoHelper } from '@owlmeans/context'
|
|
5
|
+
import type {
|
|
6
|
+
ConsumeRequest, CounterReconciliation, EffectivePlan, LimitOutcome, OccupancyOutcome, PaymentUsageCounterRecord,
|
|
7
|
+
PaymentUsageRecord, ReleaseRequest,
|
|
8
|
+
} from './types.js'
|
|
9
|
+
import { log } from './log.js'
|
|
10
|
+
import { DAY_WINDOW, MONTH_WINDOW } from './consts.local.js'
|
|
11
|
+
import type { CounterKey, LedgerSum, UsageHelper } from './usage/types.js'
|
|
12
|
+
import { paymentAccessOf } from './access.js'
|
|
13
|
+
import { paymentUtils } from './utils.js'
|
|
14
|
+
import { catalogueOf } from './catalogue.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The usage ledger and its counters.
|
|
18
|
+
*
|
|
19
|
+
* `payment-usage` events are the source of truth; `payment-usage-counter` is a projection kept
|
|
20
|
+
* for synchronous admission. Consuming increments the counter FIRST, with one atomic conditional
|
|
21
|
+
* update that only matches while there is room, and appends the event after. Invariant: **the
|
|
22
|
+
* counter may over-count, never over-admit.** A crash between the two leaves a unit counted that no
|
|
23
|
+
* event spent, which `reconcileCounters` repairs from the ledger; the opposite order would let two
|
|
24
|
+
* concurrent requests both read "room left" and both spend it.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** When a stored window renews: the first instant of the next day, month or subscription week. */
|
|
28
|
+
const resetsAtOfWindow = (window: string): Date | undefined => {
|
|
29
|
+
if (window.startsWith(SUBSCRIPTION_WEEK_PREFIX)) {
|
|
30
|
+
const start = new Date(window.slice(SUBSCRIPTION_WEEK_PREFIX.length)).getTime()
|
|
31
|
+
return Number.isFinite(start) ? new Date(start + SUBSCRIPTION_WEEK_MS) : undefined
|
|
32
|
+
}
|
|
33
|
+
const day = DAY_WINDOW.exec(window)
|
|
34
|
+
if (day != null) {
|
|
35
|
+
return new Date(Date.UTC(Number(day[1]), Number(day[2]) - 1, Number(day[3]) + 1))
|
|
36
|
+
}
|
|
37
|
+
const month = MONTH_WINDOW.exec(window)
|
|
38
|
+
if (month != null) {
|
|
39
|
+
return new Date(Date.UTC(Number(month[1]), Number(month[2]), 1))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ceilingOf = (declaration: LimitDeclaration, effective: EffectivePlan, at: Date): number =>
|
|
46
|
+
promoHelper.promoActive(declaration.promo, effective.subscription?.createdAt, at) ? declaration.limit : 0
|
|
47
|
+
|
|
48
|
+
const declarationOf = (effective: EffectivePlan, limitKey: string): LimitDeclaration => {
|
|
49
|
+
const declaration = effective.plan.limits?.[limitKey]
|
|
50
|
+
if (declaration == null) {
|
|
51
|
+
throw new LimitUnknown(limitKey)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return declaration
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const isActiveConsumption = (event: PaymentUsageRecord | null): boolean =>
|
|
58
|
+
event != null && event.delta > 0 && event.releasedAt == null
|
|
59
|
+
|
|
60
|
+
const isPastWindow = (window: string, at: Date): boolean => {
|
|
61
|
+
if (window.startsWith(SUBSCRIPTION_WEEK_PREFIX)) {
|
|
62
|
+
const reset = resetsAtOfWindow(window)
|
|
63
|
+
return reset != null && reset.getTime() <= at.getTime()
|
|
64
|
+
}
|
|
65
|
+
if (DAY_WINDOW.test(window)) {
|
|
66
|
+
return window < planLimitHelper.windowKeyOf(LimitKind.Window, LimitWindow.Day, at)
|
|
67
|
+
}
|
|
68
|
+
if (MONTH_WINDOW.test(window)) {
|
|
69
|
+
return window < planLimitHelper.windowKeyOf(LimitKind.Window, LimitWindow.Month, at)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return false
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const makeUsageHelper = (ctx: ApiContext): UsageHelper => {
|
|
76
|
+
const access = paymentAccessOf(ctx)
|
|
77
|
+
|
|
78
|
+
const readCounter = async (key: CounterKey): Promise<PaymentUsageCounterRecord | null> =>
|
|
79
|
+
await access.usageCounters().load({ entityId: key.entityId, limitKey: key.limitKey, window: key.window })
|
|
80
|
+
|
|
81
|
+
const outcomeOf = async (
|
|
82
|
+
key: CounterKey, eventKey: string, flags: { admitted: boolean, replayed: boolean }, limit?: number,
|
|
83
|
+
): Promise<LimitOutcome> => {
|
|
84
|
+
const counter = await readCounter(key)
|
|
85
|
+
const used = Math.max(0, counter?.used ?? 0)
|
|
86
|
+
const ceiling = limit ?? counter?.limit ?? 0
|
|
87
|
+
const resetsAt = resetsAtOfWindow(key.window)
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
...flags, limitKey: key.limitKey, window: key.window, used, limit: ceiling,
|
|
91
|
+
remaining: Math.max(0, ceiling - used), ...(resetsAt != null ? { resetsAt } : {}), eventKey,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The counter increment that IS the admission decision. `null` when there is no room. */
|
|
96
|
+
const admit = async (
|
|
97
|
+
key: CounterKey, amount: number, limit: number, planSku: string, at: Date,
|
|
98
|
+
): Promise<PaymentUsageCounterRecord | null> => {
|
|
99
|
+
if (amount > limit) {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
102
|
+
const filter = { ...key, used: { $lte: limit - amount } }
|
|
103
|
+
const update = {
|
|
104
|
+
$inc: { used: amount },
|
|
105
|
+
$set: { limit, planSku, updatedAt: at },
|
|
106
|
+
$setOnInsert: { ...key },
|
|
107
|
+
}
|
|
108
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
109
|
+
try {
|
|
110
|
+
const doc = await access.usageCounters().collection.findOneAndUpdate(
|
|
111
|
+
filter, update, { upsert: true, returnDocument: 'after' },
|
|
112
|
+
)
|
|
113
|
+
return doc as unknown as PaymentUsageCounterRecord | null
|
|
114
|
+
} catch (error) {
|
|
115
|
+
// A duplicate here is either a racing insert of the same counter — the retry then matches
|
|
116
|
+
// the inserted document — or an existing counter without room, whose upsert collides again.
|
|
117
|
+
if (!paymentUtils.isDuplicateKey(error)) {
|
|
118
|
+
throw error
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Take `amount` back off a counter, never below zero. */
|
|
127
|
+
const decrement = async (key: CounterKey, amount: number, at: Date): Promise<void> => {
|
|
128
|
+
const counters = access.usageCounters().collection
|
|
129
|
+
const doc = await counters.findOneAndUpdate(
|
|
130
|
+
{ ...key, used: { $gte: amount } }, { $inc: { used: -amount }, $set: { updatedAt: at } },
|
|
131
|
+
)
|
|
132
|
+
if (doc == null) {
|
|
133
|
+
await counters.updateOne({ ...key }, { $set: { used: 0, updatedAt: at } })
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const consumeLimit = async (req: ConsumeRequest): Promise<LimitOutcome> => {
|
|
138
|
+
const amount = req.amount ?? 1
|
|
139
|
+
if (!Number.isSafeInteger(amount) || amount < 1) {
|
|
140
|
+
throw new UnsupportedArgumentError('amount')
|
|
141
|
+
}
|
|
142
|
+
const at = new Date()
|
|
143
|
+
const effective = await catalogueOf(ctx).resolveEffectivePlan(req.entityId, at)
|
|
144
|
+
const declaration = declarationOf(effective, req.limitKey)
|
|
145
|
+
const limit = ceilingOf(declaration, effective, at)
|
|
146
|
+
const key: CounterKey = {
|
|
147
|
+
entityId: req.entityId, limitKey: req.limitKey, window: planLimitHelper.windowKeyOf(declaration.kind, declaration.window, at, effective?.subscription?.createdAt),
|
|
148
|
+
}
|
|
149
|
+
const ledger = access.usageEvents()
|
|
150
|
+
|
|
151
|
+
// A retry of an event already written answers from the ledger before admission is asked: at the
|
|
152
|
+
// ceiling, admission would refuse the very unit this key already holds. A released key is final.
|
|
153
|
+
const earlier = await ledger.load({ entityId: req.entityId, limitKey: req.limitKey, eventKey: req.eventKey })
|
|
154
|
+
if (earlier != null) {
|
|
155
|
+
return await outcomeOf({ ...key, window: earlier.window }, req.eventKey, {
|
|
156
|
+
admitted: isActiveConsumption(earlier), replayed: true,
|
|
157
|
+
}, earlier.window === key.window ? limit : undefined)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const counter = await admit(key, amount, limit, effective.plan.sku, at)
|
|
161
|
+
if (counter == null) {
|
|
162
|
+
// A concurrent consume of this very key may hold the last unit: that is a replay, not a refusal.
|
|
163
|
+
const concurrent = await ledger.load({ entityId: req.entityId, limitKey: req.limitKey, eventKey: req.eventKey })
|
|
164
|
+
if (concurrent != null) {
|
|
165
|
+
return await outcomeOf({ ...key, window: concurrent.window }, req.eventKey, {
|
|
166
|
+
admitted: isActiveConsumption(concurrent), replayed: true,
|
|
167
|
+
}, concurrent.window === key.window ? limit : undefined)
|
|
168
|
+
}
|
|
169
|
+
const current = await readCounter(key)
|
|
170
|
+
const resetsAt = declaration.kind === LimitKind.Window && declaration.window != null
|
|
171
|
+
? planLimitHelper.windowBoundsOf(declaration.window, at, effective?.subscription?.createdAt).resetsAt : undefined
|
|
172
|
+
throw new LimitExhausted({ key: req.limitKey, used: current?.used ?? 0, limit, resetsAt })
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
await ledger.create({
|
|
177
|
+
...key, delta: amount, eventKey: req.eventKey, ref: req.ref, reason: req.reason,
|
|
178
|
+
planSku: effective.plan.sku, createdAt: at,
|
|
179
|
+
})
|
|
180
|
+
} catch (error) {
|
|
181
|
+
// Undo the admission. Should the undo itself fail, the counter over-counts — never over-admits.
|
|
182
|
+
await decrement(key, amount, at).catch(undo => { log.error('Usage undo failed', undo) })
|
|
183
|
+
if (!paymentUtils.isDuplicateKey(error)) {
|
|
184
|
+
throw error
|
|
185
|
+
}
|
|
186
|
+
// A concurrent consume of the same key won the event: replay its outcome.
|
|
187
|
+
const winner = await ledger.load({ entityId: req.entityId, limitKey: req.limitKey, eventKey: req.eventKey })
|
|
188
|
+
return await outcomeOf({ ...key, window: winner?.window ?? key.window }, req.eventKey, {
|
|
189
|
+
admitted: isActiveConsumption(winner), replayed: true,
|
|
190
|
+
}, limit)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const used = Math.max(0, counter.used)
|
|
194
|
+
const resetsAt = resetsAtOfWindow(key.window)
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
admitted: true, replayed: false, limitKey: req.limitKey, window: key.window, used, limit,
|
|
198
|
+
remaining: Math.max(0, limit - used), ...(resetsAt != null ? { resetsAt } : {}), eventKey: req.eventKey,
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const releaseLimit = async (req: ReleaseRequest): Promise<LimitOutcome> => {
|
|
203
|
+
const ledger = access.usageEvents()
|
|
204
|
+
const consumed = await ledger.load({ entityId: req.entityId, limitKey: req.limitKey, eventKey: req.eventKey })
|
|
205
|
+
if (consumed == null || consumed.delta <= 0) {
|
|
206
|
+
return {
|
|
207
|
+
admitted: false, replayed: false, limitKey: req.limitKey, window: consumed?.window ?? '', used: 0,
|
|
208
|
+
limit: 0, remaining: 0, eventKey: req.eventKey,
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const key: CounterKey = { entityId: req.entityId, limitKey: req.limitKey, window: consumed.window }
|
|
212
|
+
if (consumed.releasedAt != null) {
|
|
213
|
+
return await outcomeOf(key, req.eventKey, { admitted: true, replayed: true })
|
|
214
|
+
}
|
|
215
|
+
const amount = Math.min(req.amount ?? consumed.delta, consumed.delta)
|
|
216
|
+
if (!Number.isSafeInteger(amount) || amount < 1) {
|
|
217
|
+
throw new UnsupportedArgumentError('amount')
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const at = new Date()
|
|
221
|
+
let appended = true
|
|
222
|
+
try {
|
|
223
|
+
await ledger.create({
|
|
224
|
+
...key, delta: -amount, eventKey: `release:${req.eventKey}`, ref: consumed.ref ?? undefined,
|
|
225
|
+
reason: 'release', planSku: consumed.planSku ?? undefined, createdAt: at,
|
|
226
|
+
})
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (!paymentUtils.isDuplicateKey(error)) {
|
|
229
|
+
throw error
|
|
230
|
+
}
|
|
231
|
+
// An earlier attempt appended the release and decremented; only the stamp is missing.
|
|
232
|
+
appended = false
|
|
233
|
+
}
|
|
234
|
+
if (appended) {
|
|
235
|
+
await decrement(key, amount, at)
|
|
236
|
+
}
|
|
237
|
+
await ledger.update({ ...consumed, releasedAt: at })
|
|
238
|
+
|
|
239
|
+
return await outcomeOf(key, req.eventKey, { admitted: true, replayed: false })
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const consumptionOf = async (
|
|
243
|
+
entityId: string, limitKey: string, eventKey: string,
|
|
244
|
+
): Promise<PaymentUsageRecord | null> => {
|
|
245
|
+
const event = await access.usageEvents().load({ entityId, limitKey, eventKey })
|
|
246
|
+
|
|
247
|
+
return event != null && isActiveConsumption(event) ? event : null
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const consumptionByRefOf = async (
|
|
251
|
+
entityId: string, limitKey: string, ref: string,
|
|
252
|
+
): Promise<PaymentUsageRecord | null> => await access.usageEvents().load(
|
|
253
|
+
{ entityId, limitKey, ref, delta: { $gt: 0 }, releasedAt: null },
|
|
254
|
+
{ sort: [{ field: 'createdAt', order: 'desc' }] },
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
const limitStateOf = async (entityId: string, limitKey: string): Promise<LimitView> => {
|
|
258
|
+
const at = new Date()
|
|
259
|
+
const effective = await catalogueOf(ctx).resolveEffectivePlan(entityId, at)
|
|
260
|
+
const declaration = declarationOf(effective, limitKey)
|
|
261
|
+
const window = planLimitHelper.windowKeyOf(declaration.kind, declaration.window, at, effective?.subscription?.createdAt)
|
|
262
|
+
const counter = await readCounter({ entityId, limitKey, window })
|
|
263
|
+
const [view] = entitlementViewHelper.limitViewsOf({ limits: { [limitKey]: declaration } }, counter != null ? [{ key: limitKey, window, used: counter.used }] : [], effective.subscription?.createdAt, at)
|
|
264
|
+
|
|
265
|
+
return view
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const ledgerSums = async (match: Partial<CounterKey>): Promise<LedgerSum[]> => {
|
|
269
|
+
const filter = Object.fromEntries(Object.entries(match).filter(([, value]) => value != null))
|
|
270
|
+
const rows = await access.usageEvents().collection.aggregate([
|
|
271
|
+
{ $match: filter },
|
|
272
|
+
{
|
|
273
|
+
$group: {
|
|
274
|
+
_id: { entityId: '$entityId', limitKey: '$limitKey', window: '$window' },
|
|
275
|
+
used: { $sum: '$delta' },
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
], { allowDiskUse: true }).toArray()
|
|
279
|
+
|
|
280
|
+
return rows.map(row => ({ ...(row._id as CounterKey), used: row.used as number }))
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Set a counter to a value, creating it when absent. */
|
|
284
|
+
const setCounter = async (
|
|
285
|
+
key: CounterKey, set: Partial<PaymentUsageCounterRecord>, unset: string[] = [],
|
|
286
|
+
): Promise<void> => {
|
|
287
|
+
const update = {
|
|
288
|
+
$set: set,
|
|
289
|
+
$setOnInsert: { ...key },
|
|
290
|
+
...(unset.length > 0 ? { $unset: Object.fromEntries(unset.map(field => [field, ''])) } : {}),
|
|
291
|
+
}
|
|
292
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
293
|
+
try {
|
|
294
|
+
await access.usageCounters().collection.updateOne({ ...key }, update, { upsert: true })
|
|
295
|
+
return
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (!paymentUtils.isDuplicateKey(error) || attempt > 0) {
|
|
298
|
+
throw error
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const reconcileOccupancyOf = async (
|
|
305
|
+
entityId: string, limitKey: string, actual: number,
|
|
306
|
+
): Promise<OccupancyOutcome> => {
|
|
307
|
+
if (!Number.isSafeInteger(actual) || actual < 0) {
|
|
308
|
+
throw new UnsupportedArgumentError('actual')
|
|
309
|
+
}
|
|
310
|
+
const at = new Date()
|
|
311
|
+
const effective = await catalogueOf(ctx).resolveEffectivePlan(entityId, at)
|
|
312
|
+
const declaration = declarationOf(effective, limitKey)
|
|
313
|
+
if (declaration.kind !== LimitKind.Occupancy) {
|
|
314
|
+
throw new LimitMisdeclared(`${limitKey}:not-occupancy`)
|
|
315
|
+
}
|
|
316
|
+
const limit = ceilingOf(declaration, effective, at)
|
|
317
|
+
const key: CounterKey = { entityId, limitKey, window: OCCUPANCY_WINDOW }
|
|
318
|
+
|
|
319
|
+
const [sum] = await ledgerSums(key)
|
|
320
|
+
const delta = actual - (sum?.used ?? 0)
|
|
321
|
+
if (delta !== 0) {
|
|
322
|
+
const eventKey = `reconcile:${limitKey}:${planLimitHelper.windowKeyOf(LimitKind.Window, LimitWindow.Day, at)}`
|
|
323
|
+
try {
|
|
324
|
+
await access.usageEvents().create({
|
|
325
|
+
...key, delta, eventKey, reason: 'reconcile', planSku: effective.plan.sku, createdAt: at,
|
|
326
|
+
})
|
|
327
|
+
} catch (error) {
|
|
328
|
+
if (!paymentUtils.isDuplicateKey(error)) {
|
|
329
|
+
throw error
|
|
330
|
+
}
|
|
331
|
+
await access.usageEvents().collection.updateOne({ entityId, limitKey, eventKey }, { $inc: { delta } })
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const counter = await readCounter(key)
|
|
336
|
+
const over = Math.max(0, actual - limit)
|
|
337
|
+
const overSince = over > 0 ? (counter?.overSince != null ? new Date(counter.overSince) : at) : undefined
|
|
338
|
+
await setCounter(key, {
|
|
339
|
+
used: actual, limit, planSku: effective.plan.sku, updatedAt: at, reconciledAt: at,
|
|
340
|
+
...(overSince != null ? { overSince } : {}),
|
|
341
|
+
}, overSince == null ? ['overSince'] : [])
|
|
342
|
+
|
|
343
|
+
return { limitKey, used: actual, limit, over, ...(overSince != null ? { overSince } : {}) }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const reconcileLedgerCounters = async (entityId?: string): Promise<CounterReconciliation> => {
|
|
347
|
+
const at = new Date()
|
|
348
|
+
const sums = await ledgerSums(entityId != null ? { entityId } : {})
|
|
349
|
+
const entities = new Set(sums.map(sum => sum.entityId))
|
|
350
|
+
if (entityId != null) {
|
|
351
|
+
entities.add(entityId)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
let counters = 0
|
|
355
|
+
let repaired = 0
|
|
356
|
+
for (const entity of entities) {
|
|
357
|
+
let effective: EffectivePlan | null = null
|
|
358
|
+
try {
|
|
359
|
+
effective = await catalogueOf(ctx).resolveEffectivePlan(entity, at)
|
|
360
|
+
} catch (error) {
|
|
361
|
+
log.warn('Reconcile: no plan for an entity', { entityId: entity, error })
|
|
362
|
+
}
|
|
363
|
+
const limitOf = (limitKey: string, fallback: number): number => {
|
|
364
|
+
const declaration = effective?.plan.limits?.[limitKey]
|
|
365
|
+
return declaration != null && effective != null ? ceilingOf(declaration, effective, at) : fallback
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const ledger = new Map(sums.filter(sum => sum.entityId === entity)
|
|
369
|
+
.map(sum => [`${sum.limitKey}${sum.window}`, sum.used]))
|
|
370
|
+
const { items: stored } = await access.usageCounters().list({ entityId: entity }, { size: 0 })
|
|
371
|
+
const seen = new Set<string>()
|
|
372
|
+
|
|
373
|
+
for (const counter of stored) {
|
|
374
|
+
const id = `${counter.limitKey}${counter.window}`
|
|
375
|
+
seen.add(id)
|
|
376
|
+
const sum = ledger.get(id)
|
|
377
|
+
if (sum == null && isPastWindow(counter.window, at)) {
|
|
378
|
+
await access.usageCounters().collection.deleteOne({
|
|
379
|
+
entityId: entity, limitKey: counter.limitKey, window: counter.window,
|
|
380
|
+
})
|
|
381
|
+
continue
|
|
382
|
+
}
|
|
383
|
+
const used = Math.max(0, sum ?? 0)
|
|
384
|
+
const limit = limitOf(counter.limitKey, counter.limit)
|
|
385
|
+
counters++
|
|
386
|
+
if (used !== counter.used) {
|
|
387
|
+
repaired++
|
|
388
|
+
}
|
|
389
|
+
await setCounter({ entityId: entity, limitKey: counter.limitKey, window: counter.window }, {
|
|
390
|
+
used, limit, updatedAt: at, reconciledAt: at,
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (const [id, sum] of ledger) {
|
|
395
|
+
if (seen.has(id)) {
|
|
396
|
+
continue
|
|
397
|
+
}
|
|
398
|
+
const [limitKey, window] = id.split('')
|
|
399
|
+
counters++
|
|
400
|
+
repaired++
|
|
401
|
+
await setCounter({ entityId: entity, limitKey, window }, {
|
|
402
|
+
used: Math.max(0, sum), limit: limitOf(limitKey, 0), updatedAt: at, reconciledAt: at,
|
|
403
|
+
})
|
|
404
|
+
seen.add(id)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
for (const [limitKey, declaration] of Object.entries(effective?.plan.limits ?? {})) {
|
|
408
|
+
if (declaration.kind !== LimitKind.Window) {
|
|
409
|
+
continue
|
|
410
|
+
}
|
|
411
|
+
const window = planLimitHelper.windowKeyOf(declaration.kind, declaration.window, at, effective?.subscription?.createdAt)
|
|
412
|
+
if (seen.has(`${limitKey}${window}`)) {
|
|
413
|
+
continue
|
|
414
|
+
}
|
|
415
|
+
counters++
|
|
416
|
+
await setCounter({ entityId: entity, limitKey, window }, {
|
|
417
|
+
used: 0, limit: limitOf(limitKey, 0), planSku: effective?.plan.sku, updatedAt: at, reconciledAt: at,
|
|
418
|
+
})
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return { counters, repaired }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
consumeLimit, releaseLimit, consumptionOf, consumptionByRefOf, limitStateOf, ledgerSums, reconcileOccupancyOf,
|
|
427
|
+
reconcileLedgerCounters,
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** The usage ledger of a context — one per context. */
|
|
432
|
+
export const usageOf = memoHelper.oncePer(makeUsageHelper)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type Stripe from 'stripe'
|
|
2
|
+
|
|
3
|
+
/** A resource whose Mongo collection is reached directly — a conditional write's target. */
|
|
4
|
+
export interface CollectionHolder { collection: unknown }
|
|
5
|
+
|
|
6
|
+
/** The package's pure primitives: paygate payload readers, store guards and audit text. */
|
|
7
|
+
export interface PaymentUtils {
|
|
8
|
+
/**
|
|
9
|
+
* The API version a client was built with — read back from the client, never written as a literal,
|
|
10
|
+
* so a webhook endpoint always receives the payload shapes the client itself parses.
|
|
11
|
+
*/
|
|
12
|
+
apiVersionOf: (stripe: Stripe) => string
|
|
13
|
+
isDuplicateKey: (error: unknown) => boolean
|
|
14
|
+
/** A paygate answer meaning the object does not exist (deleted, or never did). */
|
|
15
|
+
isMissingObject: (error: unknown) => boolean
|
|
16
|
+
/** A paygate object reference, expanded or not, as its id. */
|
|
17
|
+
idOf: (value: string | { id?: string } | null | undefined) => string | undefined
|
|
18
|
+
/** Epoch seconds → `Date`. */
|
|
19
|
+
dateOf: (seconds: number | null | undefined) => Date | undefined
|
|
20
|
+
/** Drop `null` and `undefined` properties — a stored record reads an absent field back as `null`. */
|
|
21
|
+
compact: <T extends object>(value: T) => T
|
|
22
|
+
/** A conditional single-document `$set` — `true` when the filter matched (the guard held). */
|
|
23
|
+
conditionalSet: (
|
|
24
|
+
resource: CollectionHolder, filter: Record<string, unknown>, set: Record<string, unknown>,
|
|
25
|
+
) => Promise<boolean>
|
|
26
|
+
/** A conditional single-document delete — `true` when the filter matched (the guard held). */
|
|
27
|
+
conditionalDelete: (resource: CollectionHolder, filter: Record<string, unknown>) => Promise<boolean>
|
|
28
|
+
/** An error's message for an audit record, never a stack. */
|
|
29
|
+
errorText: (error: unknown) => string
|
|
30
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,33 +1,98 @@
|
|
|
1
|
-
import Stripe from 'stripe'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { PaymentService } from '@owlmeans/payment'
|
|
1
|
+
import type Stripe from 'stripe'
|
|
2
|
+
import { MONGO_DUPLICATE_KEY } from '@owlmeans/mongo-resource'
|
|
3
|
+
import { WebhookSetupError, type PaymentService } from '@owlmeans/payment'
|
|
5
4
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
RES_PAYMENT_SUBSCRIPTION, STRIPE_PLUGIN_CONFIG,
|
|
9
|
-
} from './consts.js'
|
|
5
|
+
import { CONSUMER_RIGHTS_SERVICE } from './consts.js'
|
|
6
|
+
import { paymentAccessOf } from './access.js'
|
|
10
7
|
import type {
|
|
11
|
-
CompletionObserver,
|
|
12
|
-
|
|
8
|
+
CompletionObserver, ConsumerConsentResource, ConsumerRightsService, EntitlementService, GatewayService,
|
|
9
|
+
PaymentFulfillmentResource, PaymentSubscriptionResource, PurchaseResource,
|
|
13
10
|
} from './types.js'
|
|
11
|
+
import type { CollectionHolder, PaymentUtils } from './utils/types.js'
|
|
14
12
|
|
|
15
|
-
export const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
})
|
|
13
|
+
export const createPaymentUtils = (): PaymentUtils => {
|
|
14
|
+
const apiVersionOf = (stripe: Stripe): string => {
|
|
15
|
+
const version = (stripe as unknown as { getApiField?: (key: string) => unknown }).getApiField?.('version')
|
|
16
|
+
if (typeof version !== 'string' || version === '') {
|
|
17
|
+
throw new WebhookSetupError('api-version')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return version
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const isDuplicateKey = (error: unknown): boolean =>
|
|
24
|
+
(error as { code?: unknown } | null)?.code === MONGO_DUPLICATE_KEY
|
|
25
|
+
|
|
26
|
+
const isMissingObject = (error: unknown): boolean => {
|
|
27
|
+
const typed = error as { code?: unknown; statusCode?: unknown; raw?: { code?: unknown } } | null
|
|
28
|
+
return typed?.code === 'resource_missing' || typed?.raw?.code === 'resource_missing' || typed?.statusCode === 404
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const idOf = (value: string | { id?: string } | null | undefined): string | undefined =>
|
|
32
|
+
value == null ? undefined : typeof value === 'string' ? value : value.id
|
|
33
|
+
|
|
34
|
+
const dateOf = (seconds: number | null | undefined): Date | undefined =>
|
|
35
|
+
seconds == null ? undefined : new Date(seconds * 1000)
|
|
36
|
+
|
|
37
|
+
const compact = <T extends object>(value: T): T =>
|
|
38
|
+
Object.fromEntries(Object.entries(value).filter(([, entry]) => entry != null)) as T
|
|
39
|
+
|
|
40
|
+
const conditionalSet = async (
|
|
41
|
+
resource: CollectionHolder, filter: Record<string, unknown>, set: Record<string, unknown>,
|
|
42
|
+
): Promise<boolean> => {
|
|
43
|
+
const collection = resource.collection as {
|
|
44
|
+
updateOne: (filter: object, update: object) => Promise<{ matchedCount?: number, modifiedCount?: number }>
|
|
45
|
+
}
|
|
46
|
+
const result = await collection.updateOne(filter, { $set: set })
|
|
47
|
+
|
|
48
|
+
return (result.matchedCount ?? result.modifiedCount ?? 0) > 0
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const conditionalDelete = async (resource: CollectionHolder, filter: Record<string, unknown>): Promise<boolean> => {
|
|
52
|
+
const collection = resource.collection as { deleteOne: (filter: object) => Promise<{ deletedCount?: number }> }
|
|
53
|
+
const result = await collection.deleteOne(filter)
|
|
54
|
+
|
|
55
|
+
return (result.deletedCount ?? 0) > 0
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const errorText = (error: unknown): string =>
|
|
59
|
+
(error instanceof Error ? error.message : String(error)).slice(0, 1000)
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
apiVersionOf, isDuplicateKey, isMissingObject, idOf, dateOf, compact, conditionalSet, conditionalDelete, errorText,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const paymentUtils = createPaymentUtils()
|
|
67
|
+
|
|
68
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).payment()` */
|
|
69
|
+
export const payment = (ctx: ApiContext): PaymentService => paymentAccessOf(ctx).payment()
|
|
70
|
+
|
|
71
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).gateway()` */
|
|
72
|
+
export const gateway = (ctx: ApiContext): GatewayService => paymentAccessOf(ctx).gateway()
|
|
73
|
+
|
|
74
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).observer()` */
|
|
75
|
+
export const observer = (ctx: ApiContext): CompletionObserver => paymentAccessOf(ctx).observer()
|
|
76
|
+
|
|
77
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).entitlements()` */
|
|
78
|
+
export const entitlements = (ctx: ApiContext): EntitlementService => paymentAccessOf(ctx).entitlements()
|
|
79
|
+
|
|
80
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).subscriptions()` */
|
|
81
|
+
export const subscriptions = (ctx: ApiContext): PaymentSubscriptionResource => paymentAccessOf(ctx).subscriptions()
|
|
82
|
+
|
|
83
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).fulfillments()` */
|
|
84
|
+
export const fulfillments = (ctx: ApiContext): PaymentFulfillmentResource => paymentAccessOf(ctx).fulfillments()
|
|
85
|
+
|
|
86
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).purchases()` */
|
|
87
|
+
export const purchases = (ctx: ApiContext): PurchaseResource => paymentAccessOf(ctx).purchases()
|
|
88
|
+
|
|
89
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).consumerConsents()` */
|
|
90
|
+
export const consumerConsents = (ctx: ApiContext): ConsumerConsentResource => paymentAccessOf(ctx).consumerConsents()
|
|
91
|
+
|
|
92
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).consumerRightsOf(…)` */
|
|
93
|
+
export const consumerRightsOf = (
|
|
94
|
+
ctx: ApiContext, alias: string = CONSUMER_RIGHTS_SERVICE,
|
|
95
|
+
): ConsumerRightsService | null => paymentAccessOf(ctx).consumerRightsOf(alias)
|
|
96
|
+
|
|
97
|
+
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).stripeClient()` */
|
|
98
|
+
export const stripeClient = async (ctx: ApiContext): Promise<Stripe> => await paymentAccessOf(ctx).stripeClient()
|