@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
package/src/plugins/events.ts
CHANGED
|
@@ -1,40 +1,29 @@
|
|
|
1
1
|
import type Stripe from 'stripe'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
CheckoutPricingMode, PaygateError, PlanDuration, SubscriptionStatus,
|
|
5
|
-
} from '@owlmeans/payment'
|
|
2
|
+
import { CheckoutPricingMode, ENTITLING_STATUSES, PaygateError, SubscriptionStatus } from '@owlmeans/payment'
|
|
6
3
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
7
|
-
import { STRIPE_PAYGATE_ALIAS } from '../consts.js'
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
case 'unpaid': return SubscriptionStatus.Expired
|
|
31
|
-
case 'canceled': return SubscriptionStatus.Canceled
|
|
32
|
-
case 'incomplete':
|
|
33
|
-
case 'incomplete_expired':
|
|
34
|
-
case 'paused': return SubscriptionStatus.Suspended
|
|
35
|
-
default: return SubscriptionStatus.Created
|
|
36
|
-
}
|
|
37
|
-
}
|
|
4
|
+
import { GATEWAY_SERVICE, STRIPE_PAYGATE_ALIAS } from '../consts.js'
|
|
5
|
+
import type {
|
|
6
|
+
CommitResult, CheckoutOutcome, CheckoutPlugin, DisputeEvent, PaymentFulfillmentRecord, PaymentSubscriptionRecord,
|
|
7
|
+
RefundEvent, TopUpCompletion,
|
|
8
|
+
} from '../types.js'
|
|
9
|
+
import type { PaymentTarget } from './refunds/types.js'
|
|
10
|
+
import type { ApplyOptions } from './subscriptions/types.js'
|
|
11
|
+
import { log } from '../log.js'
|
|
12
|
+
import { DISPUTE_PHASES } from './consts.local.js'
|
|
13
|
+
import type { EventHandler, StripeEventHandler } from './events/types.js'
|
|
14
|
+
import { paymentAccessOf } from '../access.js'
|
|
15
|
+
import { paymentUtils } from '../utils.js'
|
|
16
|
+
import { checkoutPluginsOf } from './checkout-plugins.js'
|
|
17
|
+
import { paymentTargetOf } from './refunds.js'
|
|
18
|
+
import { customerLockHelper } from './customer-lock.js'
|
|
19
|
+
import { stripeSubscriptionsOf } from './subscriptions.js'
|
|
20
|
+
import { consumerRecordsOf } from '../consumer/records.js'
|
|
21
|
+
import { captureOf } from '../consumer/capture.js'
|
|
22
|
+
|
|
23
|
+
/** `invoice.subscription`, top-level on the pinned API version. */
|
|
24
|
+
const subscriptionIdOfInvoice = (invoice: Stripe.Invoice): string | undefined =>
|
|
25
|
+
paymentUtils.idOf((invoice as unknown as { subscription?: string | { id?: string } | null }).subscription)
|
|
26
|
+
|
|
38
27
|
const integerMetadata = (metadata: Stripe.Metadata, key: string): number => {
|
|
39
28
|
const raw = metadata[key]
|
|
40
29
|
const value = raw == null || raw.trim() === '' ? Number.NaN : Number(raw)
|
|
@@ -42,42 +31,105 @@ const integerMetadata = (metadata: Stripe.Metadata, key: string): number => {
|
|
|
42
31
|
return value
|
|
43
32
|
}
|
|
44
33
|
|
|
45
|
-
|
|
34
|
+
/**
|
|
35
|
+
* The Stripe event dispatch table. `process` runs the handler of an event type and ignores every
|
|
36
|
+
* other type; a throw escapes so Stripe delivers the event again.
|
|
37
|
+
*/
|
|
38
|
+
export const createEventHandler = (ctx: ApiContext, stripe: Stripe): StripeEventHandler => {
|
|
39
|
+
const access = paymentAccessOf(ctx)
|
|
40
|
+
const stripeSubscriptions = stripeSubscriptionsOf(ctx)
|
|
41
|
+
|
|
42
|
+
/** The registered checkout plugins — none in a context without a gateway service. */
|
|
43
|
+
const registeredPlugins = (): readonly CheckoutPlugin[] =>
|
|
44
|
+
(ctx as unknown as { hasService?: (alias: string) => boolean }).hasService?.(GATEWAY_SERVICE) === true
|
|
45
|
+
? access.gateway().checkoutPlugins?.() ?? [] : []
|
|
46
|
+
|
|
47
|
+
const upsertCustomer = async (customer: Stripe.Customer): Promise<void> => {
|
|
48
|
+
await customerLockHelper.withCustomerLock(customer.id, async () => {
|
|
49
|
+
const resource = access.paygateCustomers()
|
|
50
|
+
const existing = await resource.loadByPgId(customer.id, STRIPE_PAYGATE_ALIAS)
|
|
51
|
+
const data = paymentUtils.compact({
|
|
52
|
+
email: customer.email ?? undefined, name: customer.name ?? undefined,
|
|
53
|
+
taxId: customer.tax_ids?.data?.[0]?.value ?? undefined,
|
|
54
|
+
country: customer.address?.country?.toUpperCase() ?? undefined,
|
|
55
|
+
currency: customer.currency?.toLowerCase() ?? undefined,
|
|
56
|
+
entityId: customer.metadata?.entityId ?? existing?.entityId ?? undefined,
|
|
57
|
+
profileId: customer.metadata?.profileId ?? existing?.profileId ?? undefined,
|
|
58
|
+
})
|
|
59
|
+
if (existing == null) {
|
|
60
|
+
try {
|
|
61
|
+
await resource.create({ paygate: STRIPE_PAYGATE_ALIAS, externalId: customer.id, ...data })
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (!paymentUtils.isDuplicateKey(error)) throw error
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
const { deletedAt: _deleted, ...kept } = existing
|
|
67
|
+
await resource.update({ ...kept, ...data })
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const markCustomerDeleted = async (customer: Stripe.DeletedCustomer | Stripe.Customer): Promise<void> => {
|
|
73
|
+
const resource = access.paygateCustomers()
|
|
74
|
+
const existing = await resource.loadByPgId(customer.id, STRIPE_PAYGATE_ALIAS)
|
|
75
|
+
if (existing != null && existing.deletedAt == null) {
|
|
76
|
+
await resource.update({ ...existing, deletedAt: new Date() })
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const settle = async (session: Stripe.Checkout.Session, outcome: CheckoutOutcome, entityId?: string): Promise<void> => {
|
|
81
|
+
const metadata = session.metadata ?? {}
|
|
82
|
+
const owner = entityId ?? metadata.entityId
|
|
83
|
+
if (owner == null) return
|
|
84
|
+
const amount = Number(metadata.amountMinor)
|
|
85
|
+
await checkoutPluginsOf(ctx).settleCheckout(registeredPlugins(), paymentUtils.compact({
|
|
86
|
+
entityId: owner, productSku: metadata.productSku, planSku: metadata.planSku, sessionId: session.id, outcome,
|
|
87
|
+
amountMinor: Number.isSafeInteger(amount) && amount > 0 ? amount : undefined, at: new Date(),
|
|
88
|
+
}))
|
|
89
|
+
}
|
|
90
|
+
|
|
46
91
|
const fulfillPayment = async (session: Stripe.Checkout.Session): Promise<void> => {
|
|
92
|
+
if (session.mode === 'subscription') {
|
|
93
|
+
await completeSubscriptionCheckout(session)
|
|
94
|
+
return
|
|
95
|
+
}
|
|
47
96
|
if (session.mode !== 'payment' || session.payment_status !== 'paid') return
|
|
48
97
|
const metadata = session.metadata ?? {}
|
|
49
98
|
if (metadata.entityId == null || metadata.service == null || metadata.productSku == null) {
|
|
50
99
|
throw new PaygateError('metadata:owner')
|
|
51
100
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
let stored = await subscriptions(context).byExternalId(session.id, STRIPE_PAYGATE_ALIAS)
|
|
101
|
+
await customerLockHelper.withCustomerLock(paymentUtils.idOf(session.customer), async () => {
|
|
102
|
+
const ledger = access.fulfillments()
|
|
103
|
+
let stored = await ledger.byExternalId(session.id, STRIPE_PAYGATE_ALIAS)
|
|
56
104
|
if (stored?.fulfilledAt != null) return
|
|
57
105
|
|
|
58
106
|
const amountMode = metadata.pricingMode === CheckoutPricingMode.Amount
|
|
107
|
+
const base = {
|
|
108
|
+
entityId: metadata.entityId, productSku: metadata.productSku, planSku: metadata.planSku,
|
|
109
|
+
service: metadata.service, paygate: STRIPE_PAYGATE_ALIAS, externalId: session.id,
|
|
110
|
+
}
|
|
59
111
|
let completion: TopUpCompletion
|
|
60
|
-
let record:
|
|
112
|
+
let record: Partial<PaymentFulfillmentRecord>
|
|
61
113
|
if (amountMode) {
|
|
62
114
|
const amountMinor = integerMetadata(metadata, 'amountMinor')
|
|
63
115
|
const chargedMinor = integerMetadata(metadata, 'chargeAmountMinor')
|
|
64
116
|
const currency = metadata.currency?.toLowerCase()
|
|
117
|
+
const sourceChargedMinor = metadata.sourceChargeAmountMinor == null
|
|
118
|
+
? chargedMinor : integerMetadata(metadata, 'sourceChargeAmountMinor')
|
|
119
|
+
const amountCurrency = (metadata.amountCurrency ?? currency)?.toLowerCase()
|
|
65
120
|
if (currency == null || session.currency?.toLowerCase() !== currency
|
|
66
|
-
||
|
|
121
|
+
|| amountCurrency == null || session.amount_subtotal !== chargedMinor
|
|
122
|
+
|| amountMinor <= 0 || chargedMinor <= 0 || sourceChargedMinor < amountMinor
|
|
123
|
+
|| (amountCurrency === currency && sourceChargedMinor !== chargedMinor)) {
|
|
67
124
|
throw new PaygateError('amount-mismatch')
|
|
68
125
|
}
|
|
69
126
|
record = {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
externalId: session.id, status: SubscriptionStatus.Consumable, kind: 'consumable',
|
|
73
|
-
pricingMode: CheckoutPricingMode.Amount, amountMinor, chargeAmountMinor: chargedMinor,
|
|
74
|
-
currency, createdAt: new Date(),
|
|
127
|
+
mode: CheckoutPricingMode.Amount, amountMinor, sourceChargeAmountMinor: sourceChargedMinor,
|
|
128
|
+
amountCurrency, chargeAmountMinor: chargedMinor, currency,
|
|
75
129
|
}
|
|
76
130
|
completion = {
|
|
77
|
-
mode: 'amount',
|
|
78
|
-
chargeAmountMinor: chargedMinor, currency,
|
|
79
|
-
planSku: metadata.planSku, service: metadata.service,
|
|
80
|
-
paygate: STRIPE_PAYGATE_ALIAS, externalId: session.id,
|
|
131
|
+
...base, mode: 'amount', amountMinor, sourceChargeAmountMinor: sourceChargedMinor,
|
|
132
|
+
amountCurrency, chargeAmountMinor: chargedMinor, currency,
|
|
81
133
|
}
|
|
82
134
|
} else {
|
|
83
135
|
if (metadata.currency != null && session.currency?.toLowerCase() !== metadata.currency.toLowerCase()) {
|
|
@@ -86,96 +138,378 @@ export const createEventHandler = (context: ApiContext, stripe: Stripe) => {
|
|
|
86
138
|
const lineItems = await stripe.checkout.sessions.listLineItems(session.id, { limit: 100 })
|
|
87
139
|
const units = lineItems.data.reduce((sum, line) => sum + (line.quantity ?? 0), 0)
|
|
88
140
|
if (units <= 0) throw new PaygateError('units')
|
|
89
|
-
record = {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
141
|
+
record = { mode: CheckoutPricingMode.Quantity, units, ...(session.currency != null ? { currency: session.currency } : {}) }
|
|
142
|
+
completion = { ...base, mode: 'quantity', units }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// The purchase — the withdrawal window and the contract — exists BEFORE the credits do; the
|
|
146
|
+
// billing country is locked with it.
|
|
147
|
+
const captured = await captureOf(ctx).capturePaymentPurchase(stripe, session, paymentUtils.compact({
|
|
148
|
+
netAmountMinor: completion.mode === 'amount' ? completion.amountMinor : undefined,
|
|
149
|
+
amountCurrency: completion.mode === 'amount' ? completion.amountCurrency : undefined,
|
|
150
|
+
units: completion.mode === 'quantity' ? completion.units : undefined,
|
|
151
|
+
}))
|
|
152
|
+
const evidence = captureOf(ctx).sessionEvidenceOf(session)
|
|
153
|
+
const identifiers = paymentUtils.compact({
|
|
154
|
+
paymentIntentId: paymentUtils.idOf(session.payment_intent), invoiceId: paymentUtils.idOf(session.invoice),
|
|
155
|
+
})
|
|
156
|
+
const evidenceFields = paymentUtils.compact({
|
|
157
|
+
country: evidence.country, email: evidence.email, profileId: metadata.profileId,
|
|
158
|
+
amountTotalMinor: session.amount_total ?? undefined, amountTaxMinor: session.total_details?.amount_tax ?? undefined,
|
|
159
|
+
termsAccepted: evidence.termsAccepted, purchaseId: captured?.purchase.purchaseId,
|
|
160
|
+
})
|
|
161
|
+
if (stored == null) {
|
|
162
|
+
try {
|
|
163
|
+
stored = await ledger.create(paymentUtils.compact({ ...base, ...record, ...identifiers, ...evidenceFields, createdAt: new Date() }) as PaymentFulfillmentRecord)
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (!paymentUtils.isDuplicateKey(error)) throw error
|
|
166
|
+
stored = await ledger.byExternalId(session.id, STRIPE_PAYGATE_ALIAS)
|
|
167
|
+
if (stored == null || stored.fulfilledAt != null) return
|
|
96
168
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
169
|
+
} else if ((stored.paymentIntentId == null && identifiers.paymentIntentId != null)
|
|
170
|
+
|| (stored.purchaseId == null && evidenceFields.purchaseId != null)) {
|
|
171
|
+
stored = await ledger.update({ ...stored, ...identifiers, ...evidenceFields })
|
|
172
|
+
}
|
|
173
|
+
await access.observer().propagateTopUp(completion, ctx)
|
|
174
|
+
await ledger.update({ ...stored, fulfilledAt: new Date() })
|
|
175
|
+
log.info('Top-up fulfilled', paymentUtils.compact({
|
|
176
|
+
entityId: base.entityId, productSku: base.productSku, planSku: base.planSku, sessionId: session.id,
|
|
177
|
+
purchaseId: captured?.purchase.purchaseId, ...record,
|
|
178
|
+
}), { event: 'payment.topup' })
|
|
179
|
+
})
|
|
180
|
+
await settle(session, 'paid')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A completed SUBSCRIPTION checkout: the subscription is applied if no webhook applied it yet
|
|
185
|
+
* (its first commit writes the purchase before the `created` observers), then the purchase is
|
|
186
|
+
* refined with the buyer's own country and totals, the billing country is locked, the evidence is
|
|
187
|
+
* stored on the subscription row and the confirmation mailed.
|
|
188
|
+
*/
|
|
189
|
+
const completeSubscriptionCheckout = async (session: Stripe.Checkout.Session): Promise<void> => {
|
|
190
|
+
if (session.payment_status !== 'paid' && session.payment_status !== 'no_payment_required') return
|
|
191
|
+
const subscriptionId = paymentUtils.idOf(session.subscription)
|
|
192
|
+
if (subscriptionId == null) return
|
|
193
|
+
await customerLockHelper.withCustomerLock(paymentUtils.idOf(session.customer), async () => {
|
|
194
|
+
let row = await access.subscriptions().byExternalId(subscriptionId, STRIPE_PAYGATE_ALIAS)
|
|
195
|
+
let subscription: Stripe.Subscription | null = null
|
|
196
|
+
if (row?.propagated == null) {
|
|
197
|
+
subscription = await stripeSubscriptions.retrieveSubscription(stripe, subscriptionId)
|
|
198
|
+
if (subscription != null) {
|
|
199
|
+
row = (await stripeSubscriptions.applySubscription(subscription, { source: 'webhook', stripe })).record ?? row
|
|
101
200
|
}
|
|
102
201
|
}
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
202
|
+
if (row == null) {
|
|
203
|
+
log.warn('Completed checkout names an unknown subscription', { sessionId: session.id, subscriptionId })
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
let purchase = await access.purchases().byPurchaseId(consumerRecordsOf(ctx).purchaseIdOf(subscriptionId))
|
|
207
|
+
if (purchase == null && ENTITLING_STATUSES.includes(row.status)) {
|
|
208
|
+
subscription = subscription ?? await stripeSubscriptions.retrieveSubscription(stripe, subscriptionId)
|
|
209
|
+
purchase = subscription != null
|
|
210
|
+
? (await captureOf(ctx).captureSubscriptionPurchase(stripe, subscription, row))?.purchase ?? null : null
|
|
211
|
+
}
|
|
212
|
+
if (purchase != null) {
|
|
213
|
+
purchase = await captureOf(ctx).completeSubscriptionPurchase(stripe, session, purchase)
|
|
214
|
+
}
|
|
215
|
+
const evidence = captureOf(ctx).sessionEvidenceOf(session)
|
|
216
|
+
const metadata = session.metadata ?? {}
|
|
217
|
+
const current = await access.subscriptions().byExternalId(subscriptionId, STRIPE_PAYGATE_ALIAS) ?? row
|
|
218
|
+
await access.subscriptions().update(paymentUtils.compact({
|
|
219
|
+
...current,
|
|
220
|
+
checkoutSessionId: session.id,
|
|
221
|
+
purchaseId: purchase?.purchaseId ?? current.purchaseId,
|
|
222
|
+
firstInvoiceId: current.firstInvoiceId ?? paymentUtils.idOf(session.invoice),
|
|
223
|
+
currency: current.currency ?? (evidence.currency !== '' ? evidence.currency : undefined),
|
|
224
|
+
country: evidence.country ?? current.country,
|
|
225
|
+
email: evidence.email ?? current.email,
|
|
226
|
+
amountTotalMinor: evidence.totalMinor,
|
|
227
|
+
amountTaxMinor: evidence.taxMinor,
|
|
228
|
+
termsAccepted: evidence.termsAccepted ?? current.termsAccepted,
|
|
229
|
+
startRequestId: metadata.startRequestId ?? purchase?.startRequestId ?? current.startRequestId,
|
|
230
|
+
}))
|
|
231
|
+
log.info('Subscription checkout completed', paymentUtils.compact({
|
|
232
|
+
entityId: current.entityId, sessionId: session.id, subscriptionId, planSku: current.planSku,
|
|
233
|
+
purchaseId: purchase?.purchaseId, amountTotalMinor: evidence.totalMinor,
|
|
234
|
+
currency: current.currency ?? (evidence.currency !== '' ? evidence.currency : undefined),
|
|
235
|
+
}), { event: 'checkout.completed' })
|
|
236
|
+
})
|
|
237
|
+
await settle(session, 'paid')
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const checkoutFailed = async (session: Stripe.Checkout.Session): Promise<void> => {
|
|
241
|
+
const metadata = session.metadata ?? {}
|
|
242
|
+
const customerId = paymentUtils.idOf(session.customer)
|
|
243
|
+
const entityId = metadata.entityId
|
|
244
|
+
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId, STRIPE_PAYGATE_ALIAS))?.entityId : undefined)
|
|
245
|
+
if (entityId == null) {
|
|
246
|
+
log.warn('Failed checkout belongs to no known entity', { sessionId: session.id })
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
if (session.mode === 'payment' && metadata.productSku != null && metadata.service != null) {
|
|
250
|
+
const ledger = access.fulfillments()
|
|
251
|
+
const stored = await ledger.byExternalId(session.id, STRIPE_PAYGATE_ALIAS)
|
|
252
|
+
if (stored == null) {
|
|
253
|
+
try {
|
|
254
|
+
await ledger.create(paymentUtils.compact({
|
|
255
|
+
entityId, productSku: metadata.productSku, planSku: metadata.planSku, service: metadata.service,
|
|
256
|
+
paygate: STRIPE_PAYGATE_ALIAS, externalId: session.id,
|
|
257
|
+
mode: metadata.pricingMode === CheckoutPricingMode.Amount ? CheckoutPricingMode.Amount : CheckoutPricingMode.Quantity,
|
|
258
|
+
paymentIntentId: paymentUtils.idOf(session.payment_intent), createdAt: new Date(), failedAt: new Date(),
|
|
259
|
+
}) as PaymentFulfillmentRecord)
|
|
260
|
+
} catch (error) {
|
|
261
|
+
if (!paymentUtils.isDuplicateKey(error)) throw error
|
|
262
|
+
}
|
|
263
|
+
} else if (stored.failedAt == null && stored.fulfilledAt == null) {
|
|
264
|
+
await ledger.update({ ...stored, failedAt: new Date() })
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
await access.observer().propagatePaymentFailed(paymentUtils.compact({
|
|
268
|
+
entityId, kind: 'checkout' as const, externalId: session.id,
|
|
269
|
+
subscriptionId: paymentUtils.idOf(session.subscription), eventKey: `payment-failed:${session.id}:0`,
|
|
270
|
+
}), ctx)
|
|
271
|
+
log.info('Payment failed', paymentUtils.compact({
|
|
272
|
+
entityId, kind: 'checkout', sessionId: session.id, subscriptionId: paymentUtils.idOf(session.subscription),
|
|
273
|
+
}), { event: 'payment.failed' })
|
|
274
|
+
await settle(session, 'failed', entityId)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const purgeExpiredCheckout = async (session: Stripe.Checkout.Session): Promise<void> => {
|
|
278
|
+
await access.fulfillments().purge({ paygate: STRIPE_PAYGATE_ALIAS, externalId: session.id, fulfilledAt: null })
|
|
279
|
+
await settle(session, 'expired')
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const applyFromEvent = (opts: Partial<ApplyOptions> = {}): EventHandler => async event => {
|
|
283
|
+
const subscription = event.data.object as Stripe.Subscription
|
|
284
|
+
await customerLockHelper.withCustomerLock(paymentUtils.idOf(subscription.customer), async () => {
|
|
285
|
+
await stripeSubscriptions.applySubscription(subscription, {
|
|
286
|
+
source: 'webhook', eventId: event.id, eventCreated: event.created, stripe, ...opts,
|
|
287
|
+
})
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const reapplyInvoiceSubscription = async (
|
|
292
|
+
event: Stripe.Event, invoice: Stripe.Invoice, opts: Partial<ApplyOptions> = {},
|
|
293
|
+
): Promise<CommitResult | null> => {
|
|
294
|
+
const subscriptionId = subscriptionIdOfInvoice(invoice)
|
|
295
|
+
if (subscriptionId == null) return null
|
|
296
|
+
const subscription = await stripeSubscriptions.retrieveSubscription(stripe, subscriptionId)
|
|
297
|
+
if (subscription == null) {
|
|
298
|
+
return await customerLockHelper.withCustomerLock(paymentUtils.idOf(invoice.customer),
|
|
299
|
+
async () => await stripeSubscriptions.cancelMissing(subscriptionId))
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return await customerLockHelper.withCustomerLock(paymentUtils.idOf(subscription.customer), async () =>
|
|
303
|
+
await stripeSubscriptions.applySubscription(subscription, { source: 'webhook', eventId: event.id, stripe, ...opts }))
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const invoicePaid: EventHandler = async event => {
|
|
307
|
+
const invoice = event.data.object as Stripe.Invoice
|
|
308
|
+
if (invoice.billing_reason === 'subscription_cycle') {
|
|
309
|
+
await reapplyInvoiceSubscription(event, invoice, { renewal: true, invoiceId: invoice.id })
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
await refreshLatestInvoice(invoice, invoice.id)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const refreshLatestInvoice = async (invoice: Stripe.Invoice, latestInvoiceId?: string): Promise<void> => {
|
|
316
|
+
const subscriptionId = subscriptionIdOfInvoice(invoice)
|
|
317
|
+
if (subscriptionId == null) return
|
|
318
|
+
await customerLockHelper.withCustomerLock(paymentUtils.idOf(invoice.customer), async () => {
|
|
319
|
+
const row = await access.subscriptions().byExternalId(subscriptionId, STRIPE_PAYGATE_ALIAS)
|
|
320
|
+
if (row == null) return
|
|
321
|
+
let latest = latestInvoiceId
|
|
322
|
+
if (latest == null) {
|
|
323
|
+
const subscription = await stripeSubscriptions.retrieveSubscription(stripe, subscriptionId)
|
|
324
|
+
latest = paymentUtils.idOf(subscription?.latest_invoice)
|
|
325
|
+
}
|
|
326
|
+
if (latest != null && row.latestInvoiceId !== latest) {
|
|
327
|
+
await access.subscriptions().update({ ...row, latestInvoiceId: latest, updatedAt: new Date() })
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const invoiceFailed: EventHandler = async event => {
|
|
333
|
+
const invoice = event.data.object as Stripe.Invoice
|
|
334
|
+
const result = await reapplyInvoiceSubscription(event, invoice)
|
|
335
|
+
const customerId = paymentUtils.idOf(invoice.customer)
|
|
336
|
+
const entityId = result?.record?.entityId
|
|
337
|
+
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId, STRIPE_PAYGATE_ALIAS))?.entityId : undefined)
|
|
338
|
+
if (entityId == null) {
|
|
339
|
+
log.warn('Failed invoice belongs to no known entity', { invoiceId: invoice.id })
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
const attempt = invoice.attempt_count ?? 0
|
|
343
|
+
await access.observer().propagatePaymentFailed(paymentUtils.compact({
|
|
344
|
+
entityId, kind: 'invoice' as const, externalId: invoice.id, subscriptionId: subscriptionIdOfInvoice(invoice),
|
|
345
|
+
invoiceId: invoice.id, attempt, actionRequired: event.type === 'invoice.payment_action_required',
|
|
346
|
+
nextAttemptAt: paymentUtils.dateOf(invoice.next_payment_attempt), eventKey: `payment-failed:${invoice.id}:${attempt}`,
|
|
347
|
+
}), ctx)
|
|
348
|
+
log.info('Payment failed', paymentUtils.compact({
|
|
349
|
+
entityId, kind: 'invoice', invoiceId: invoice.id, subscriptionId: subscriptionIdOfInvoice(invoice), attempt,
|
|
350
|
+
actionRequired: event.type === 'invoice.payment_action_required',
|
|
351
|
+
}), { event: 'payment.failed' })
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const targetFields = (target: PaymentTarget) => target.kind === 'fulfillment'
|
|
355
|
+
? paymentUtils.compact({
|
|
356
|
+
entityId: target.record.entityId, target: target.kind, productSku: target.record.productSku,
|
|
357
|
+
planSku: target.record.planSku ?? undefined, externalId: target.record.externalId,
|
|
358
|
+
netAmountMinor: target.record.amountMinor ?? undefined,
|
|
359
|
+
chargeAmountMinor: target.record.chargeAmountMinor ?? undefined,
|
|
360
|
+
})
|
|
361
|
+
: paymentUtils.compact({
|
|
362
|
+
entityId: target.record.entityId, target: target.kind, productSku: target.record.productSku,
|
|
363
|
+
planSku: target.record.planSku, externalId: target.record.externalId,
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
/** A refund of a purchase's payment moves its refunded total; a whole refund closes its window. */
|
|
367
|
+
const refundPurchase = async (target: PaymentTarget, refundedTotal: number, paid: number | undefined): Promise<void> => {
|
|
368
|
+
const purchaseId = consumerRecordsOf(ctx).purchaseIdOf(target.record.externalId)
|
|
369
|
+
const purchase = await access.purchases().byPurchaseId(purchaseId)
|
|
370
|
+
if (purchase == null || (target.kind === 'subscription' && purchase.invoiceId != null && purchase.invoiceId !== target.invoiceId)) {
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
await consumerRecordsOf(ctx).patchPurchase(purchaseId, paymentUtils.compact({
|
|
374
|
+
refundedMinor: Math.max(purchase.refundedMinor ?? 0, refundedTotal),
|
|
375
|
+
refundedAt: purchase.refundedAt ?? (paid != null && refundedTotal >= paid ? new Date() : undefined),
|
|
376
|
+
}))
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const processRefund = async (refund: Stripe.Refund, charge?: Stripe.Charge | null): Promise<void> => {
|
|
380
|
+
if (refund.status !== 'succeeded') return
|
|
381
|
+
const target = await paymentTargetOf(ctx).resolvePaymentTarget(stripe, {
|
|
382
|
+
paymentIntentId: paymentUtils.idOf(refund.payment_intent), chargeId: paymentUtils.idOf(refund.charge), charge,
|
|
383
|
+
})
|
|
384
|
+
if (target == null) {
|
|
385
|
+
log.warn('Refund matches no fulfillment or subscription', { refundId: refund.id })
|
|
386
|
+
return
|
|
108
387
|
}
|
|
388
|
+
const chargeId = paymentUtils.idOf(refund.charge)
|
|
389
|
+
const refunded = target.charge ?? (chargeId != null ? await paymentTargetOf(ctx).retrieveCharge(stripe, chargeId) : null)
|
|
390
|
+
const refundedTotal = Math.max(refunded?.amount_refunded ?? 0, refund.amount)
|
|
391
|
+
const paid = refunded?.amount
|
|
392
|
+
if (target.kind === 'fulfillment') {
|
|
393
|
+
await access.fulfillments().update(paymentUtils.compact({
|
|
394
|
+
...target.record, refundedMinor: Math.max(target.record.refundedMinor ?? 0, refundedTotal),
|
|
395
|
+
refundedAt: new Date(), chargeId: target.record.chargeId ?? refunded?.id,
|
|
396
|
+
}))
|
|
397
|
+
}
|
|
398
|
+
await refundPurchase(target, refundedTotal, paid)
|
|
399
|
+
const metadata = { ...(refund.metadata ?? {}) } as Record<string, string>
|
|
400
|
+
await access.observer().propagateRefund(paymentUtils.compact({
|
|
401
|
+
...targetFields(target),
|
|
402
|
+
refundId: refund.id,
|
|
403
|
+
paymentIntentId: target.paymentIntentId ?? paymentUtils.idOf(refund.payment_intent),
|
|
404
|
+
invoiceId: target.kind === 'subscription' ? target.invoiceId : target.record.invoiceId ?? undefined,
|
|
405
|
+
amountMinor: refund.amount,
|
|
406
|
+
refundedTotalMinor: refundedTotal,
|
|
407
|
+
paidMinor: paid,
|
|
408
|
+
currency: refund.currency,
|
|
409
|
+
partial: paid != null ? refundedTotal < paid : false,
|
|
410
|
+
eventKey: `refund:${refund.id}`,
|
|
411
|
+
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
|
412
|
+
// Set by a withdrawal's own refund: its observer takes back the unused units, `onRefund` must not.
|
|
413
|
+
withdrawalId: metadata.withdrawalId != null && metadata.withdrawalId !== '' ? metadata.withdrawalId : undefined,
|
|
414
|
+
}) as RefundEvent, ctx)
|
|
415
|
+
log.info('Payment refunded', paymentUtils.compact({
|
|
416
|
+
entityId: target.record.entityId, target: target.kind, externalId: target.record.externalId, refundId: refund.id,
|
|
417
|
+
amountMinor: refund.amount, refundedTotalMinor: refundedTotal, paidMinor: paid, currency: refund.currency,
|
|
418
|
+
withdrawalId: metadata.withdrawalId != null && metadata.withdrawalId !== '' ? metadata.withdrawalId : undefined,
|
|
419
|
+
}), { event: 'payment.refunded' })
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const chargeRefunded: EventHandler = async event => {
|
|
423
|
+
const charge = event.data.object as Stripe.Charge
|
|
424
|
+
const refunds = await stripe.refunds.list({ charge: charge.id, limit: 100 })
|
|
425
|
+
for (const refund of refunds.data) {
|
|
426
|
+
await processRefund(refund, charge)
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const refundChanged: EventHandler = async event => {
|
|
431
|
+
await processRefund(event.data.object as Stripe.Refund)
|
|
109
432
|
}
|
|
110
433
|
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
434
|
+
const disputeChanged: EventHandler = async event => {
|
|
435
|
+
const dispute = event.data.object as Stripe.Dispute
|
|
436
|
+
const phase = DISPUTE_PHASES[event.type]
|
|
437
|
+
const target = await paymentTargetOf(ctx).resolvePaymentTarget(stripe, {
|
|
438
|
+
paymentIntentId: paymentUtils.idOf(dispute.payment_intent), chargeId: paymentUtils.idOf(dispute.charge),
|
|
439
|
+
})
|
|
440
|
+
if (target == null) {
|
|
441
|
+
log.warn('Dispute matches no fulfillment or subscription', { disputeId: dispute.id })
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
const marks = { disputedAt: target.record.disputedAt ?? new Date(), disputeStatus: dispute.status }
|
|
445
|
+
if (target.kind === 'fulfillment') {
|
|
446
|
+
await access.fulfillments().update({ ...target.record, ...marks })
|
|
447
|
+
} else {
|
|
448
|
+
await access.subscriptions().update({ ...(target.record as PaymentSubscriptionRecord), ...marks })
|
|
449
|
+
}
|
|
450
|
+
await access.observer().propagateDispute(paymentUtils.compact({
|
|
451
|
+
...targetFields(target), disputeId: dispute.id, phase, status: dispute.status,
|
|
452
|
+
amountMinor: dispute.amount, currency: dispute.currency, eventKey: `dispute:${dispute.id}:${phase}`,
|
|
453
|
+
}) as DisputeEvent, ctx)
|
|
454
|
+
log.info('Dispute changed', paymentUtils.compact({
|
|
455
|
+
entityId: target.record.entityId, target: target.kind, externalId: target.record.externalId,
|
|
456
|
+
disputeId: dispute.id, phase, status: dispute.status, amountMinor: dispute.amount, currency: dispute.currency,
|
|
457
|
+
}), { event: phase === 'opened' ? 'dispute.opened' : phase === 'closed' ? 'dispute.closed' : 'dispute.updated' })
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const handlers: Record<string, EventHandler> = {
|
|
461
|
+
'customer.created': async event => { await upsertCustomer(event.data.object as Stripe.Customer) },
|
|
462
|
+
'customer.updated': async event => { await upsertCustomer(event.data.object as Stripe.Customer) },
|
|
463
|
+
'customer.deleted': async event => { await markCustomerDeleted(event.data.object as Stripe.Customer) },
|
|
464
|
+
|
|
465
|
+
'checkout.session.completed': async event => { await fulfillPayment(event.data.object as Stripe.Checkout.Session) },
|
|
466
|
+
'checkout.session.async_payment_succeeded': async event => {
|
|
467
|
+
await fulfillPayment(event.data.object as Stripe.Checkout.Session)
|
|
114
468
|
},
|
|
115
|
-
'
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
'checkout.session.
|
|
119
|
-
|
|
120
|
-
'customer.subscription.created': async (event: Stripe.CustomerSubscriptionCreatedEvent) => handler._syncSubscription(event.data.object),
|
|
121
|
-
'customer.subscription.updated': async (event: Stripe.CustomerSubscriptionUpdatedEvent) => handler._syncSubscription(event.data.object),
|
|
122
|
-
'customer.subscription.deleted': async (event: Stripe.CustomerSubscriptionDeletedEvent) =>
|
|
123
|
-
handler._syncSubscription(event.data.object, SubscriptionStatus.Canceled),
|
|
124
|
-
|
|
125
|
-
_upsertCustomer: async (customer: Stripe.Customer) => {
|
|
126
|
-
const resource = paygateCustomers(context)
|
|
127
|
-
await lockOn(customer.id)
|
|
128
|
-
try {
|
|
129
|
-
const existing = await resource.loadByPgId(customer.id, STRIPE_PAYGATE_ALIAS)
|
|
130
|
-
const data = {
|
|
131
|
-
email: customer.email ?? undefined, name: customer.name ?? undefined,
|
|
132
|
-
taxId: customer.tax_ids?.data?.[0]?.value ?? undefined,
|
|
133
|
-
entityId: customer.metadata?.entityId ?? existing?.entityId,
|
|
134
|
-
profileId: customer.metadata?.profileId ?? existing?.profileId,
|
|
135
|
-
}
|
|
136
|
-
if (existing == null) await resource.create({ paygate: STRIPE_PAYGATE_ALIAS, externalId: customer.id, ...data })
|
|
137
|
-
else { Object.assign(existing, data); await resource.save(existing) }
|
|
138
|
-
} finally { unlockOn(customer.id) }
|
|
469
|
+
'checkout.session.async_payment_failed': async event => {
|
|
470
|
+
await checkoutFailed(event.data.object as Stripe.Checkout.Session)
|
|
471
|
+
},
|
|
472
|
+
'checkout.session.expired': async event => {
|
|
473
|
+
await purgeExpiredCheckout(event.data.object as Stripe.Checkout.Session)
|
|
139
474
|
},
|
|
140
475
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
476
|
+
'customer.subscription.created': applyFromEvent(),
|
|
477
|
+
'customer.subscription.updated': applyFromEvent(),
|
|
478
|
+
'customer.subscription.pending_update_applied': applyFromEvent(),
|
|
479
|
+
'customer.subscription.pending_update_expired': applyFromEvent(),
|
|
480
|
+
'customer.subscription.paused': applyFromEvent(),
|
|
481
|
+
'customer.subscription.resumed': applyFromEvent(),
|
|
482
|
+
'customer.subscription.deleted': applyFromEvent({ forced: SubscriptionStatus.Canceled }),
|
|
483
|
+
'customer.subscription.trial_will_end': applyFromEvent({ trialEnding: true }),
|
|
484
|
+
|
|
485
|
+
'invoice.paid': invoicePaid,
|
|
486
|
+
'invoice.payment_failed': invoiceFailed,
|
|
487
|
+
'invoice.payment_action_required': invoiceFailed,
|
|
488
|
+
// Enabled on the endpoint for an application's own use; it has no invoice id and nothing is due.
|
|
489
|
+
'invoice.upcoming': async () => undefined,
|
|
490
|
+
'invoice.marked_uncollectible': async event => {
|
|
491
|
+
await reapplyInvoiceSubscription(event, event.data.object as Stripe.Invoice)
|
|
492
|
+
},
|
|
493
|
+
'invoice.voided': async event => { await refreshLatestInvoice(event.data.object as Stripe.Invoice) },
|
|
494
|
+
|
|
495
|
+
'charge.refunded': chargeRefunded,
|
|
496
|
+
'refund.created': refundChanged,
|
|
497
|
+
'refund.updated': refundChanged,
|
|
498
|
+
'refund.failed': async () => undefined,
|
|
499
|
+
|
|
500
|
+
'charge.dispute.created': disputeChanged,
|
|
501
|
+
'charge.dispute.funds_withdrawn': disputeChanged,
|
|
502
|
+
'charge.dispute.funds_reinstated': disputeChanged,
|
|
503
|
+
'charge.dispute.closed': disputeChanged,
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return {
|
|
507
|
+
handlers,
|
|
508
|
+
process: async (event: Stripe.Event): Promise<void> => {
|
|
509
|
+
const handler = handlers[event.type]
|
|
510
|
+
if (handler != null) {
|
|
511
|
+
await handler(event)
|
|
512
|
+
}
|
|
178
513
|
},
|
|
179
514
|
}
|
|
180
|
-
return handler
|
|
181
515
|
}
|