@owlmeans/server-payment 0.1.22-rc.0 → 0.2.0-alpha.1
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 +52 -54
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/server-payment/SKILL.md +272 -330
- package/agent-meta/skills/server-payment/parts/reference.md +3 -3
- package/build/access/types.d.ts +7 -16
- package/build/access/types.d.ts.map +1 -1
- package/build/access.d.ts.map +1 -1
- package/build/access.js +6 -8
- package/build/access.js.map +1 -1
- package/build/actions/resync.d.ts +3 -3
- package/build/actions/resync.js +5 -5
- package/build/actions/resync.js.map +1 -1
- package/build/actions/webhook.d.ts +4 -1
- package/build/actions/webhook.d.ts.map +1 -1
- package/build/actions/webhook.js +8 -4
- package/build/actions/webhook.js.map +1 -1
- package/build/bootstrap/types.d.ts +13 -7
- package/build/bootstrap/types.d.ts.map +1 -1
- package/build/bootstrap.d.ts +4 -4
- package/build/bootstrap.d.ts.map +1 -1
- package/build/bootstrap.js +19 -10
- package/build/bootstrap.js.map +1 -1
- package/build/catalogue/types.d.ts +3 -3
- package/build/catalogue/types.d.ts.map +1 -1
- package/build/catalogue.d.ts.map +1 -1
- package/build/catalogue.js +4 -8
- package/build/catalogue.js.map +1 -1
- package/build/checkout/types.d.ts +21 -0
- package/build/checkout/types.d.ts.map +1 -0
- package/build/{plugins/stripe → checkout}/types.js.map +1 -1
- package/build/checkout.d.ts +6 -0
- package/build/checkout.d.ts.map +1 -0
- package/build/checkout.js +374 -0
- package/build/checkout.js.map +1 -0
- package/build/config.d.ts +10 -16
- package/build/config.d.ts.map +1 -1
- package/build/config.js +18 -59
- package/build/config.js.map +1 -1
- package/build/consts.d.ts +3 -49
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +3 -61
- package/build/consts.js.map +1 -1
- package/build/consumer/cancellation/types.d.ts +6 -4
- package/build/consumer/cancellation/types.d.ts.map +1 -1
- package/build/consumer/cancellation.d.ts.map +1 -1
- package/build/consumer/cancellation.js +11 -5
- package/build/consumer/cancellation.js.map +1 -1
- package/build/consumer/capture/types.d.ts +8 -12
- package/build/consumer/capture/types.d.ts.map +1 -1
- package/build/consumer/capture.d.ts.map +1 -1
- package/build/consumer/capture.js +48 -53
- package/build/consumer/capture.js.map +1 -1
- package/build/consumer/index.d.ts +4 -0
- package/build/consumer/index.d.ts.map +1 -1
- package/build/consumer/index.js +2 -0
- package/build/consumer/index.js.map +1 -1
- package/build/consumer/reconcile.d.ts.map +1 -1
- package/build/consumer/reconcile.js +82 -72
- package/build/consumer/reconcile.js.map +1 -1
- package/build/consumer/records/types.d.ts +4 -4
- package/build/consumer/records/types.d.ts.map +1 -1
- package/build/consumer/records.d.ts.map +1 -1
- package/build/consumer/records.js +16 -13
- package/build/consumer/records.js.map +1 -1
- package/build/consumer/service.d.ts +2 -2
- package/build/consumer/service.d.ts.map +1 -1
- package/build/consumer/service.js +28 -21
- package/build/consumer/service.js.map +1 -1
- package/build/consumer/types.d.ts +1 -4
- package/build/consumer/types.d.ts.map +1 -1
- package/build/consumer/types.local.d.ts +1 -9
- package/build/consumer/types.local.d.ts.map +1 -1
- package/build/consumer/withdrawal/types.d.ts +14 -12
- package/build/consumer/withdrawal/types.d.ts.map +1 -1
- package/build/consumer/withdrawal.d.ts.map +1 -1
- package/build/consumer/withdrawal.js +55 -71
- package/build/consumer/withdrawal.js.map +1 -1
- package/build/index.d.ts +12 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -4
- package/build/index.js.map +1 -1
- package/build/paygate/providers.d.ts +13 -0
- package/build/paygate/providers.d.ts.map +1 -0
- package/build/paygate/providers.js +38 -0
- package/build/paygate/providers.js.map +1 -0
- package/build/paygate/registry.d.ts +6 -0
- package/build/paygate/registry.d.ts.map +1 -0
- package/build/paygate/registry.js +37 -0
- package/build/paygate/registry.js.map +1 -0
- package/build/paygate/types.d.ts +577 -0
- package/build/paygate/types.d.ts.map +1 -0
- package/build/{plugins/session → paygate}/types.js.map +1 -1
- package/build/plan/types.d.ts +6 -1
- package/build/plan/types.d.ts.map +1 -1
- package/build/plan.d.ts.map +1 -1
- package/build/plan.js +16 -2
- package/build/plan.js.map +1 -1
- package/build/plugins/checkout-policy/types.d.ts +5 -3
- package/build/plugins/checkout-policy/types.d.ts.map +1 -1
- package/build/plugins/checkout-policy.d.ts.map +1 -1
- package/build/plugins/checkout-policy.js +5 -3
- package/build/plugins/checkout-policy.js.map +1 -1
- package/build/plugins/consts.local.d.ts +8 -10
- package/build/plugins/consts.local.d.ts.map +1 -1
- package/build/plugins/consts.local.js +2 -19
- package/build/plugins/consts.local.js.map +1 -1
- package/build/plugins/customer-lock/types.d.ts +2 -2
- package/build/plugins/customer-lock/types.d.ts.map +1 -1
- package/build/plugins/customer-lock.js +2 -2
- package/build/plugins/customer-lock.js.map +1 -1
- package/build/plugins/estimate/cache.js +1 -1
- package/build/plugins/estimate/cache.js.map +1 -1
- package/build/plugins/estimate/types.d.ts +10 -10
- package/build/plugins/estimate/types.d.ts.map +1 -1
- package/build/plugins/estimate.d.ts.map +1 -1
- package/build/plugins/estimate.js +48 -122
- package/build/plugins/estimate.js.map +1 -1
- package/build/plugins/events/types.d.ts +17 -10
- package/build/plugins/events/types.d.ts.map +1 -1
- package/build/plugins/events.d.ts +6 -5
- package/build/plugins/events.d.ts.map +1 -1
- package/build/plugins/events.js +162 -173
- package/build/plugins/events.js.map +1 -1
- package/build/plugins/fx/types.d.ts +10 -21
- package/build/plugins/fx/types.d.ts.map +1 -1
- package/build/plugins/fx.d.ts +1 -1
- package/build/plugins/fx.d.ts.map +1 -1
- package/build/plugins/fx.js +17 -54
- package/build/plugins/fx.js.map +1 -1
- package/build/plugins/portal/types.d.ts +8 -37
- package/build/plugins/portal/types.d.ts.map +1 -1
- package/build/plugins/portal.d.ts.map +1 -1
- package/build/plugins/portal.js +38 -208
- package/build/plugins/portal.js.map +1 -1
- package/build/plugins/refunds/types.d.ts +11 -11
- package/build/plugins/refunds/types.d.ts.map +1 -1
- package/build/plugins/refunds.d.ts.map +1 -1
- package/build/plugins/refunds.js +14 -39
- package/build/plugins/refunds.js.map +1 -1
- package/build/plugins/snapshot/types.d.ts +11 -0
- package/build/plugins/snapshot/types.d.ts.map +1 -0
- package/build/plugins/snapshot/types.js +2 -0
- package/build/plugins/snapshot/types.js.map +1 -0
- package/build/plugins/snapshot.d.ts +4 -0
- package/build/plugins/snapshot.d.ts.map +1 -0
- package/build/plugins/snapshot.js +19 -0
- package/build/plugins/snapshot.js.map +1 -0
- package/build/plugins/subscriptions/types.d.ts +17 -17
- package/build/plugins/subscriptions/types.d.ts.map +1 -1
- package/build/plugins/subscriptions.d.ts +4 -4
- package/build/plugins/subscriptions.d.ts.map +1 -1
- package/build/plugins/subscriptions.js +69 -77
- package/build/plugins/subscriptions.js.map +1 -1
- package/build/plugins/types.local.d.ts +9 -43
- package/build/plugins/types.local.d.ts.map +1 -1
- package/build/plugins/webhook-manager/types.d.ts +15 -43
- package/build/plugins/webhook-manager/types.d.ts.map +1 -1
- package/build/plugins/webhook-manager.d.ts +3 -3
- package/build/plugins/webhook-manager.d.ts.map +1 -1
- package/build/plugins/webhook-manager.js +21 -199
- package/build/plugins/webhook-manager.js.map +1 -1
- package/build/protocol.d.ts +1 -1
- package/build/protocol.d.ts.map +1 -1
- package/build/protocol.js +1 -1
- package/build/protocol.js.map +1 -1
- package/build/service.d.ts +11 -11
- package/build/service.d.ts.map +1 -1
- package/build/service.js +59 -23
- package/build/service.js.map +1 -1
- package/build/sync/types.d.ts +17 -13
- package/build/sync/types.d.ts.map +1 -1
- package/build/sync.d.ts.map +1 -1
- package/build/sync.js +22 -122
- package/build/sync.js.map +1 -1
- package/build/types.d.ts +46 -101
- package/build/types.d.ts.map +1 -1
- package/build/utils/types.d.ts +0 -8
- package/build/utils/types.d.ts.map +1 -1
- package/build/utils.d.ts +1 -4
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +1 -15
- package/build/utils.js.map +1 -1
- package/package.json +20 -22
- package/src/access/types.ts +7 -16
- package/src/access.ts +7 -15
- package/src/actions/resync.ts +5 -5
- package/src/actions/webhook.ts +8 -4
- package/src/bootstrap/types.ts +13 -7
- package/src/bootstrap.ts +23 -13
- package/src/catalogue/types.ts +3 -3
- package/src/catalogue.ts +4 -8
- package/src/checkout/types.ts +21 -0
- package/src/checkout.ts +414 -0
- package/src/config.ts +19 -66
- package/src/consts.ts +3 -69
- package/src/consumer/cancellation/types.ts +6 -4
- package/src/consumer/cancellation.ts +13 -7
- package/src/consumer/capture/types.ts +8 -12
- package/src/consumer/capture.ts +50 -59
- package/src/consumer/index.ts +4 -0
- package/src/consumer/reconcile.ts +69 -68
- package/src/consumer/records/types.ts +4 -4
- package/src/consumer/records.ts +19 -14
- package/src/consumer/service.ts +35 -22
- package/src/consumer/types.local.ts +1 -8
- package/src/consumer/types.ts +1 -4
- package/src/consumer/withdrawal/types.ts +14 -12
- package/src/consumer/withdrawal.ts +61 -77
- package/src/index.ts +12 -6
- package/src/paygate/providers.ts +48 -0
- package/src/paygate/registry.ts +53 -0
- package/src/paygate/types.ts +640 -0
- package/src/plan/types.ts +6 -1
- package/src/plan.ts +21 -3
- package/src/plugins/checkout-policy/types.ts +5 -3
- package/src/plugins/checkout-policy.ts +6 -5
- package/src/plugins/consts.local.ts +2 -26
- package/src/plugins/customer-lock/types.ts +2 -2
- package/src/plugins/customer-lock.ts +2 -2
- package/src/plugins/estimate/cache.ts +1 -1
- package/src/plugins/estimate/types.ts +10 -10
- package/src/plugins/estimate.ts +52 -128
- package/src/plugins/events/types.ts +17 -9
- package/src/plugins/events.ts +172 -190
- package/src/plugins/fx/types.ts +10 -23
- package/src/plugins/fx.ts +28 -64
- package/src/plugins/portal/types.ts +8 -38
- package/src/plugins/portal.ts +48 -229
- package/src/plugins/refunds/types.ts +11 -11
- package/src/plugins/refunds.ts +16 -40
- package/src/plugins/snapshot/types.ts +11 -0
- package/src/plugins/snapshot.ts +22 -0
- package/src/plugins/subscriptions/types.ts +17 -17
- package/src/plugins/subscriptions.ts +81 -81
- package/src/plugins/types.local.ts +9 -33
- package/src/plugins/webhook-manager/types.ts +15 -43
- package/src/plugins/webhook-manager.ts +24 -220
- package/src/protocol.ts +1 -1
- package/src/service.ts +66 -30
- package/src/sync/types.ts +17 -13
- package/src/sync.ts +25 -143
- package/src/types.ts +47 -97
- package/src/utils/types.ts +0 -9
- package/src/utils.ts +2 -20
- package/tests/capabilities.spec.ts +219 -0
- package/tests/entitlement.spec.ts +4 -4
- package/tests/gate-handlers.spec.ts +6 -6
- package/tests/gate.spec.ts +2 -2
- package/tests/grants.spec.ts +2 -2
- package/tests/internal-plan.spec.ts +2 -2
- package/tests/limit-gate.spec.ts +2 -2
- package/tests/paygates.spec.ts +132 -0
- package/tests/protocol.spec.ts +4 -5
- package/tests/usage.spec.ts +2 -2
- package/tests/webhook.spec.ts +137 -0
- package/build/plugins/session/types.d.ts +0 -34
- package/build/plugins/session/types.d.ts.map +0 -1
- package/build/plugins/session.d.ts +0 -4
- package/build/plugins/session.d.ts.map +0 -1
- package/build/plugins/session.js +0 -71
- package/build/plugins/session.js.map +0 -1
- package/build/plugins/status.d.ts +0 -8
- package/build/plugins/status.d.ts.map +0 -1
- package/build/plugins/status.js +0 -21
- package/build/plugins/status.js.map +0 -1
- package/build/plugins/stripe/types.d.ts +0 -20
- package/build/plugins/stripe/types.d.ts.map +0 -1
- package/build/plugins/stripe.d.ts +0 -6
- package/build/plugins/stripe.d.ts.map +0 -1
- package/build/plugins/stripe.js +0 -518
- package/build/plugins/stripe.js.map +0 -1
- package/src/plugins/session/types.ts +0 -30
- package/src/plugins/session.ts +0 -86
- package/src/plugins/status.ts +0 -22
- package/src/plugins/stripe/types.ts +0 -20
- package/src/plugins/stripe.ts +0 -571
- package/tests/checkout-consumer.spec.ts +0 -366
- package/tests/checkout-locks.spec.ts +0 -161
- package/tests/checkout-plugins.spec.ts +0 -164
- package/tests/checkout.spec.ts +0 -313
- package/tests/consumer-events.spec.ts +0 -215
- package/tests/consumer-fixtures.ts +0 -141
- package/tests/consumer-ops.spec.ts +0 -361
- package/tests/consumer-rights.integration.spec.ts +0 -147
- package/tests/consumer-rights.spec.ts +0 -606
- package/tests/context.ts +0 -110
- package/tests/estimate.spec.ts +0 -350
- package/tests/events.spec.ts +0 -367
- package/tests/fake-stripe.ts +0 -1004
- package/tests/gateway-options.spec.ts +0 -180
- package/tests/portal.spec.ts +0 -202
- package/tests/sync.spec.ts +0 -114
- package/tests/usage.integration.spec.ts +0 -165
- package/tests/webhook-manager.spec.ts +0 -149
- /package/build/{plugins/session → checkout}/types.js +0 -0
- /package/build/{plugins/stripe → paygate}/types.js +0 -0
package/src/sync.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto'
|
|
2
|
-
import type
|
|
3
|
-
import { CheckoutPricingMode, ProductType, TaxBehavior, type PlanPriceView } from '@owlmeans/payment'
|
|
2
|
+
import { CheckoutPricingMode, TaxBehavior, type PlanPriceView } from '@owlmeans/payment'
|
|
4
3
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
5
4
|
import { memoHelper } from '@owlmeans/context'
|
|
6
5
|
import type { PaymentPlan, PaymentProduct, SyncedPrice, SyncedPriceOption } from './types.js'
|
|
7
|
-
import type {
|
|
6
|
+
import type { FxQuoteCache } from './plugins/types.local.js'
|
|
7
|
+
import type { Paygate } from './paygate/types.js'
|
|
8
|
+
import { paygatesOf } from './paygate/registry.js'
|
|
8
9
|
import { log } from './log.js'
|
|
9
10
|
import type { ResolvedPlan } from './types.local.js'
|
|
10
11
|
import { fxOf } from './plugins/fx.js'
|
|
@@ -67,144 +68,21 @@ const fingerprintOf = (
|
|
|
67
68
|
})).sort((a, b) => a.sku.localeCompare(b.sku)),
|
|
68
69
|
})).digest('hex')
|
|
69
70
|
|
|
70
|
-
/** A price's `currency_options` besides its default currency (read only once expanded). */
|
|
71
|
-
const priceOptionsOf = (price: Stripe.Price): Record<string, { unit_amount?: number | null, tax_behavior?: string | null }> =>
|
|
72
|
-
Object.fromEntries(Object.entries(price.currency_options ?? {}).filter(([code]) => code !== price.currency))
|
|
73
|
-
|
|
74
|
-
/** Whether a price carries exactly these options (and, with a declared behavior, with that behavior). */
|
|
75
|
-
const optionsMatch = (price: Stripe.Price, options: SyncedPriceOption[], behavior: TaxBehavior | null): boolean => {
|
|
76
|
-
const current = priceOptionsOf(price)
|
|
77
|
-
if (Object.keys(current).length !== options.length) {
|
|
78
|
-
return false
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return options.every(option => current[option.currency]?.unit_amount === option.unitAmount
|
|
82
|
-
&& (behavior == null || current[option.currency]?.tax_behavior == null
|
|
83
|
-
|| current[option.currency]?.tax_behavior === 'unspecified' || current[option.currency]?.tax_behavior === behavior))
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Whether `price` already carries the OPPOSITE of `behavior` — never `unspecified`, which is not a conflict. */
|
|
87
|
-
const opposesBehavior = (price: Stripe.Price, behavior: TaxBehavior | null): boolean =>
|
|
88
|
-
behavior != null && price.tax_behavior !== 'unspecified' && price.tax_behavior !== behavior
|
|
89
|
-
|
|
90
71
|
export const makeProductSyncHelper = (ctx: ApiContext): ProductSyncHelper => {
|
|
91
72
|
const access = paymentAccessOf(ctx)
|
|
92
73
|
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
metadata: { sku: product.sku, ...(product.services && { services: product.services.join(',') }) },
|
|
98
|
-
}
|
|
99
|
-
if (product.type === ProductType.Consumable && product.unitLabel != null) params.unit_label = product.unitLabel
|
|
100
|
-
try {
|
|
101
|
-
await stripe.products.retrieve(product.sku)
|
|
102
|
-
return await stripe.products.update(product.sku, params)
|
|
103
|
-
} catch {
|
|
104
|
-
return await stripe.products.create({ id: product.sku, ...params })
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const activePrices = async (stripe: Stripe, product: PaymentProduct): Promise<Stripe.Price[]> =>
|
|
109
|
-
(await stripe.prices.list({ product: product.sku, active: true, limit: 100, expand: ['data.currency_options'] })).data
|
|
110
|
-
|
|
111
|
-
const deactivateAmountPrice = async (stripe: Stripe, product: PaymentProduct, plan: PaymentPlan): Promise<void> => {
|
|
112
|
-
const lookup = planHelper.planLookupKey(product, plan)
|
|
113
|
-
for (const price of (await activePrices(stripe, product)).filter(item => item.lookup_key === lookup)) {
|
|
114
|
-
await stripe.prices.update(price.id, { active: false })
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The behavior the Stripe account's own Tax Settings resolve to for `currency`, or `null` when no
|
|
120
|
-
* default is configured yet (nothing established to disrupt). `inferred_by_currency` follows
|
|
121
|
-
* Stripe's own rule: exclusive for USD/CAD, inclusive otherwise.
|
|
122
|
-
*/
|
|
123
|
-
const accountDefaultBehavior = async (stripe: Stripe, currency: string): Promise<TaxBehavior | null> => {
|
|
124
|
-
const { defaults } = await stripe.tax.settings.retrieve()
|
|
125
|
-
if (defaults.tax_behavior === 'inferred_by_currency') {
|
|
126
|
-
return ['usd', 'cad'].includes(currency.toLowerCase()) ? TaxBehavior.Exclusive : TaxBehavior.Inclusive
|
|
74
|
+
const syncProducts = async (paygate: Paygate): Promise<void> => {
|
|
75
|
+
const catalogue = paygate.catalogue
|
|
76
|
+
if (catalogue == null) {
|
|
77
|
+
return
|
|
127
78
|
}
|
|
128
|
-
if (defaults.tax_behavior === 'exclusive') return TaxBehavior.Exclusive
|
|
129
|
-
if (defaults.tax_behavior === 'inclusive') return TaxBehavior.Inclusive
|
|
130
|
-
return null
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Give `price` the declared `behavior` while it is still `unspecified` (the only state Stripe lets
|
|
135
|
-
* an existing price's `tax_behavior` be set from). Skipped, with a `log.error`, when the
|
|
136
|
-
* account's own default resolves to the opposite behavior — applying ours would then change what an
|
|
137
|
-
* existing renewal actually charges — unless `migrateUnspecifiedPrices` opts into that migration.
|
|
138
|
-
*/
|
|
139
|
-
const applyUnspecifiedBehavior = async (
|
|
140
|
-
stripe: Stripe, price: Stripe.Price, lookupKey: string, behavior: TaxBehavior, migrateUnspecifiedPrices: boolean,
|
|
141
|
-
): Promise<void> => {
|
|
142
|
-
if (!migrateUnspecifiedPrices) {
|
|
143
|
-
const resolved = await accountDefaultBehavior(stripe, price.currency)
|
|
144
|
-
if (resolved != null && resolved !== behavior) {
|
|
145
|
-
log.error(
|
|
146
|
-
'Price left unspecified: the Stripe account\'s default tax behavior differs from the declared one — '
|
|
147
|
-
+ 'applying it would change existing renewal amounts; set `stripe.migrateUnspecifiedPrices` to override',
|
|
148
|
-
{ lookupKey, currency: price.currency, accountBehavior: resolved, declaredBehavior: behavior },
|
|
149
|
-
)
|
|
150
|
-
return
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
await stripe.prices.update(price.id, { tax_behavior: behavior })
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const ensureStripePrice = async (
|
|
157
|
-
stripe: Stripe, product: PaymentProduct, resolved: ResolvedPlan,
|
|
158
|
-
behavior: TaxBehavior | null, migrateUnspecifiedPrices: boolean,
|
|
159
|
-
): Promise<Stripe.Price | null> => {
|
|
160
|
-
const { plan, unitAmount, currency, options } = resolved
|
|
161
|
-
if (plan.pricingMode === CheckoutPricingMode.Amount) {
|
|
162
|
-
await deactivateAmountPrice(stripe, product, plan)
|
|
163
|
-
return null
|
|
164
|
-
}
|
|
165
|
-
const lookupKey = planHelper.planLookupKey(product, plan)
|
|
166
|
-
const recurring = plan.recurring != null
|
|
167
|
-
? { interval: plan.recurring.interval } as Stripe.PriceCreateParams.Recurring : undefined
|
|
168
|
-
const existing = await activePrices(stripe, product)
|
|
169
|
-
const candidate = existing.find(price => price.lookup_key === lookupKey && price.unit_amount === unitAmount
|
|
170
|
-
&& price.currency === currency
|
|
171
|
-
&& ((price.recurring?.interval ?? null) === (recurring?.interval ?? null))
|
|
172
|
-
&& optionsMatch(price, options, behavior))
|
|
173
|
-
if (candidate != null && !opposesBehavior(candidate, behavior)) {
|
|
174
|
-
if (behavior != null && candidate.tax_behavior === 'unspecified') {
|
|
175
|
-
await applyUnspecifiedBehavior(stripe, candidate, lookupKey, behavior, migrateUnspecifiedPrices)
|
|
176
|
-
}
|
|
177
|
-
return candidate
|
|
178
|
-
}
|
|
179
|
-
// A changed option replaces the Price like any other change — options are never edited in place,
|
|
180
|
-
// so a subscriber keeps exactly the Price (and currency amounts) they accepted.
|
|
181
|
-
for (const price of existing.filter(item => item.lookup_key === lookupKey)) {
|
|
182
|
-
await stripe.prices.update(price.id, { active: false })
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return await stripe.prices.create({
|
|
186
|
-
product: product.sku, currency, unit_amount: unitAmount, lookup_key: lookupKey,
|
|
187
|
-
transfer_lookup_key: true, nickname: plan.sku,
|
|
188
|
-
...(recurring != null ? { recurring } : { billing_scheme: 'per_unit' }),
|
|
189
|
-
...(behavior != null ? { tax_behavior: behavior } : {}),
|
|
190
|
-
...(options.length > 0 ? {
|
|
191
|
-
currency_options: Object.fromEntries(options.map(option => [option.currency, {
|
|
192
|
-
unit_amount: option.unitAmount, ...(behavior != null ? { tax_behavior: behavior } : {}),
|
|
193
|
-
}])),
|
|
194
|
-
} : {}),
|
|
195
|
-
metadata: { sku: plan.sku, ...(product.services && { services: product.services.join(',') }) },
|
|
196
|
-
})
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const syncStripeProducts = async (stripe: Stripe): Promise<void> => {
|
|
200
79
|
const fpRes = access.fingerprints()
|
|
201
80
|
const behavior = (await access.payment().pricingPolicy()).tax.behavior ?? null
|
|
202
|
-
const
|
|
203
|
-
const fxRates: StripeFxRateCache = new Map()
|
|
81
|
+
const fxRates: FxQuoteCache = new Map()
|
|
204
82
|
const rights = await access.payment().consumerRightsPolicy()
|
|
205
83
|
const regionCurrencies = new Set(Object.values(rights?.currencies ?? {})
|
|
206
84
|
.filter((code): code is string => typeof code === 'string').map(code => code.toLowerCase()))
|
|
207
|
-
for (const { product, plans } of await catalogueOf(ctx).
|
|
85
|
+
for (const { product, plans } of await catalogueOf(ctx).plansSoldThrough(paygate.id)) {
|
|
208
86
|
const resolvedPlans: ResolvedPlan[] = []
|
|
209
87
|
for (const plan of plans) {
|
|
210
88
|
const sourceUnitAmount = Math.round(plan.price * 100)
|
|
@@ -213,7 +91,7 @@ export const makeProductSyncHelper = (ctx: ApiContext): ProductSyncHelper => {
|
|
|
213
91
|
resolvedPlans.push({ plan, unitAmount: sourceUnitAmount, currency: sourceCurrency, sourceUnitAmount, sourceCurrency, options: [] })
|
|
214
92
|
continue
|
|
215
93
|
}
|
|
216
|
-
const settled = await fxOf(ctx).settlementAmount(
|
|
94
|
+
const settled = await fxOf(ctx).settlementAmount(paygate, sourceUnitAmount, sourceCurrency, fxRates)
|
|
217
95
|
const { unitAmount, options } = optionsOf(plan, settled.currency, sourceUnitAmount, sourceCurrency, regionCurrencies)
|
|
218
96
|
resolvedPlans.push({
|
|
219
97
|
plan, unitAmount: unitAmount ?? settled.amountMinor, currency: settled.currency, sourceUnitAmount, sourceCurrency,
|
|
@@ -224,27 +102,30 @@ export const makeProductSyncHelper = (ctx: ApiContext): ProductSyncHelper => {
|
|
|
224
102
|
const stored = await fpRes.bySku(product.sku)
|
|
225
103
|
// A row from before prices were persisted syncs once more, so `planPrices` can read it.
|
|
226
104
|
if (stored != null && stored.hash === hash && stored.prices != null) continue
|
|
227
|
-
const
|
|
105
|
+
const synced = await catalogue.syncProduct(product, resolvedPlans.map(resolved => ({
|
|
106
|
+
plan: resolved.plan, lookupKey: planHelper.planLookupKey(product, resolved.plan), unitAmountMinor: resolved.unitAmount,
|
|
107
|
+
currency: resolved.currency, options: resolved.options,
|
|
108
|
+
})), { behavior })
|
|
228
109
|
const prices: SyncedPrice[] = []
|
|
229
110
|
const syncedAt = new Date()
|
|
230
111
|
for (const resolved of resolvedPlans) {
|
|
231
|
-
const price =
|
|
112
|
+
const price = synced.prices.find(entry => entry.planSku === resolved.plan.sku)
|
|
232
113
|
if (price != null) {
|
|
233
114
|
prices.push({
|
|
234
|
-
planSku: resolved.plan.sku, priceId: price.
|
|
115
|
+
planSku: resolved.plan.sku, priceId: price.priceId, lookupKey: planHelper.planLookupKey(product, resolved.plan),
|
|
235
116
|
currency: resolved.currency, unitAmount: resolved.unitAmount, options: resolved.options,
|
|
236
|
-
...(behavior != null ? { taxBehavior: behavior } : price.
|
|
117
|
+
...(behavior != null ? { taxBehavior: behavior } : price.taxBehavior != null ? { taxBehavior: price.taxBehavior } : {}),
|
|
237
118
|
...(resolved.plan.recurring != null ? { interval: resolved.plan.recurring.interval } : {}),
|
|
238
119
|
sourceUnitAmount: resolved.sourceUnitAmount, sourceCurrency: resolved.sourceCurrency, syncedAt,
|
|
239
120
|
})
|
|
240
121
|
}
|
|
241
122
|
}
|
|
242
123
|
if (stored != null) {
|
|
243
|
-
await fpRes.update({ ...stored, hash, productId:
|
|
124
|
+
await fpRes.update({ ...stored, hash, productId: synced.productId, prices, updatedAt: new Date() })
|
|
244
125
|
} else {
|
|
245
|
-
await fpRes.create({ sku: product.sku, hash, productId:
|
|
126
|
+
await fpRes.create({ sku: product.sku, hash, productId: synced.productId, prices, updatedAt: new Date() })
|
|
246
127
|
}
|
|
247
|
-
log.info('Product synced to
|
|
128
|
+
log.info('Product synced to the paygate', { sku: product.sku, plans: plans.length, paygate: paygate.id }, { event: 'payment.product.synced' })
|
|
248
129
|
}
|
|
249
130
|
}
|
|
250
131
|
|
|
@@ -269,11 +150,12 @@ export const makeProductSyncHelper = (ctx: ApiContext): ProductSyncHelper => {
|
|
|
269
150
|
}
|
|
270
151
|
|
|
271
152
|
const syncPaymentProducts = async (): Promise<void> => {
|
|
272
|
-
const
|
|
273
|
-
|
|
153
|
+
for (const paygate of paygatesOf(ctx).all()) {
|
|
154
|
+
await syncProducts(paygate)
|
|
155
|
+
}
|
|
274
156
|
}
|
|
275
157
|
|
|
276
|
-
return {
|
|
158
|
+
return { syncProducts, syncedPlanPrices, syncPaymentProducts }
|
|
277
159
|
}
|
|
278
160
|
|
|
279
161
|
/** The catalogue sync of a context — one per context. */
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
1
|
import type { InitializedService, LazyService } from '@owlmeans/context'
|
|
3
2
|
import type { ResourceRecord } from '@owlmeans/resource'
|
|
4
3
|
import type { MongoResource } from '@owlmeans/mongo-resource'
|
|
@@ -42,7 +41,7 @@ export interface PaymentPlan extends ProductPlan {
|
|
|
42
41
|
quantityPolicy?: QuantityCheckoutPolicy
|
|
43
42
|
/**
|
|
44
43
|
* Exact prices in further currencies, major units by lowercase ISO 4217 code (`{ usd: 20 }`).
|
|
45
|
-
* Synced as the reusable
|
|
44
|
+
* Synced as further currencies of the reusable price, never converted.
|
|
46
45
|
*/
|
|
47
46
|
currencyPrices?: Record<string, number>
|
|
48
47
|
}
|
|
@@ -87,7 +86,7 @@ export interface PaymentPlanDef {
|
|
|
87
86
|
defaultQuantity?: number
|
|
88
87
|
/**
|
|
89
88
|
* Exact prices in further currencies, major units by ISO 4217 code (`{ usd: 20 }`): a recurring
|
|
90
|
-
* or quantity plan's synced
|
|
89
|
+
* or quantity plan's synced reusable price carries each as a further currency at exactly this
|
|
91
90
|
* amount, while its default currency (the settlement currency) is converted as before.
|
|
92
91
|
*/
|
|
93
92
|
currencyPrices?: Record<string, number>
|
|
@@ -98,56 +97,11 @@ export interface PaymentPlanDef {
|
|
|
98
97
|
withdrawal?: { components: PlanWithdrawalComponent[] }
|
|
99
98
|
}
|
|
100
99
|
|
|
101
|
-
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
export interface StripePricingDef {
|
|
107
|
-
/** Enable promotion-code entry on amount checkouts; absent/false keeps it disabled. */
|
|
108
|
-
amountPromotionCodes?: boolean
|
|
109
|
-
/** Overrides `STRIPE_FX_QUOTES_API_VERSION`, when Stripe moves or renames the preview. */
|
|
110
|
-
fxApiVersion?: string
|
|
111
|
-
/**
|
|
112
|
-
* Stripe settlement currency used when a catalogue price is declared in another currency.
|
|
113
|
-
* Recurring prices are converted during product sync; amount checkout is converted per session.
|
|
114
|
-
*/
|
|
115
|
-
settlementCurrency?: string
|
|
116
|
-
/** Explicit Stripe payment methods for subscription Checkout; absent keeps Stripe's dynamic selection. */
|
|
117
|
-
subscriptionPaymentMethodTypes?: string[]
|
|
118
|
-
/** Payment methods for a specific subscription charge currency; overrides the default list. */
|
|
119
|
-
subscriptionPaymentMethodTypesByCurrency?: Record<string, string[]>
|
|
120
|
-
/**
|
|
121
|
-
* Let a matching `unspecified` price take the declared `tax.behavior` even when the Stripe
|
|
122
|
-
* account's own tax-settings default resolves to the opposite one — which changes what an
|
|
123
|
-
* existing subscriber is charged at their next renewal. Absent/`false`: such a price is left
|
|
124
|
-
* `unspecified` and a `log.error` explains why.
|
|
125
|
-
*/
|
|
126
|
-
migrateUnspecifiedPrices?: boolean
|
|
127
|
-
/**
|
|
128
|
-
* The Stripe customer's e-mail is the application's: every checkout must pass
|
|
129
|
-
* `CreateLinkParams.email` (else `PaygateError('customer-email')`), which is written to the
|
|
130
|
-
* customer before the session — Checkout shows a customer's valid e-mail read-only, so no other
|
|
131
|
-
* address can be typed — and the portal configuration never offers to edit it.
|
|
132
|
-
*/
|
|
133
|
-
lockCustomerEmail?: boolean
|
|
134
|
-
/**
|
|
135
|
-
* The billing country a checkout knows — the locked profile's, else `CreateLinkParams.country` —
|
|
136
|
-
* is pinned on the Stripe customer (replacing a saved address of another country before any lock)
|
|
137
|
-
* and Checkout never collects another (`customer_update.address: 'never'`, collection `'auto'`):
|
|
138
|
-
* tax is calculated on it whatever the card form's "Country or region" says, the first completed
|
|
139
|
-
* purchase locks it, and the portal never offers to edit the address. Where Stripe Tax needs more
|
|
140
|
-
* than a country (US: a postal code; CA, IN: a postal code or province) and the customer carries
|
|
141
|
-
* none, Checkout still collects the address — Stripe refuses a session otherwise.
|
|
142
|
-
*/
|
|
143
|
-
lockCustomerCountry?: boolean
|
|
144
|
-
}
|
|
145
|
-
export interface StripePricingPluginConfig extends PluginConfig, StripePricingDef {}
|
|
146
|
-
|
|
147
|
-
/** `declarePaymentPricing`'s argument: the browser-safe `PricingPolicy` plus Stripe-only settings. */
|
|
148
|
-
export interface PricingDef extends PricingPolicy {
|
|
149
|
-
stripe?: StripePricingDef
|
|
150
|
-
}
|
|
100
|
+
/**
|
|
101
|
+
* `declarePaymentPricing`'s argument: the browser-safe `PricingPolicy`. A provider's own pricing
|
|
102
|
+
* settings are declared through its own package.
|
|
103
|
+
*/
|
|
104
|
+
export interface PricingDef extends PricingPolicy {}
|
|
151
105
|
|
|
152
106
|
/**
|
|
153
107
|
* The business the consumer contracts with — backend only, never advertised. `name` is what the
|
|
@@ -182,16 +136,6 @@ export interface ConsumerMailPluginConfig extends PluginConfig, ConsumerMailDef
|
|
|
182
136
|
/** `declareConsumerRights`'s argument: the advertised policy plus the backend-only trader and mail options. */
|
|
183
137
|
export interface ConsumerRightsDef extends ConsumerRightsDeclaration { trader?: TraderDef, mail?: ConsumerMailDef }
|
|
184
138
|
|
|
185
|
-
/** What the managed customer portal configuration shows. */
|
|
186
|
-
export interface PortalBrandingDef {
|
|
187
|
-
headline?: string
|
|
188
|
-
privacyPolicyUrl?: string
|
|
189
|
-
termsOfServiceUrl?: string
|
|
190
|
-
/** Where the portal's "return" link leads when a session names none. Absolute URL. */
|
|
191
|
-
returnUrl: string
|
|
192
|
-
}
|
|
193
|
-
export interface PortalBrandingConfig extends PluginConfig, PortalBrandingDef {}
|
|
194
|
-
|
|
195
139
|
// ---------------------------------------------------------------------------------------------
|
|
196
140
|
// Gateway
|
|
197
141
|
// ---------------------------------------------------------------------------------------------
|
|
@@ -204,7 +148,7 @@ export interface CreateLinkParams {
|
|
|
204
148
|
service: string
|
|
205
149
|
planSku?: string
|
|
206
150
|
amountMinor?: number
|
|
207
|
-
/**
|
|
151
|
+
/** The checkout and future paid-invoice language. The caller supplies a locale the paygate supports. */
|
|
208
152
|
locale?: string
|
|
209
153
|
/**
|
|
210
154
|
* Trusted application copy shown beside the Checkout confirmation button — never caller-provided
|
|
@@ -223,9 +167,12 @@ export interface CreateLinkParams {
|
|
|
223
167
|
ipCountry?: string
|
|
224
168
|
/**
|
|
225
169
|
* The buyer's e-mail as the application verified it (the signed-in person's account). Read only
|
|
226
|
-
* under `
|
|
170
|
+
* under the paygate's customer e-mail lock (`customers.locks().email`), where it is required and
|
|
171
|
+
* pinned on the paygate customer.
|
|
227
172
|
*/
|
|
228
173
|
email?: string
|
|
174
|
+
/** The paygate to check out through. Absent: the first registered paygate the plan is for sale through. */
|
|
175
|
+
paygate?: string
|
|
229
176
|
}
|
|
230
177
|
|
|
231
178
|
/** What a submit-text function is told about the session it labels. */
|
|
@@ -267,7 +214,7 @@ export interface CheckoutAttempt {
|
|
|
267
214
|
/** Amount mode: the pre-tax charge in `currency`. */
|
|
268
215
|
chargeMinor?: number
|
|
269
216
|
currency: string
|
|
270
|
-
/** The session lifetime the gateway will set; absent:
|
|
217
|
+
/** The session lifetime the gateway will set; absent: the paygate's own default. */
|
|
271
218
|
sessionTtlSeconds?: number
|
|
272
219
|
expiresAt?: Date
|
|
273
220
|
at: Date
|
|
@@ -290,7 +237,7 @@ export type CheckoutOutcome = 'paid' | 'expired' | 'failed'
|
|
|
290
237
|
|
|
291
238
|
/**
|
|
292
239
|
* How a checkout ended: `paid` (completed and paid), `expired`, or `failed` — an asynchronous
|
|
293
|
-
* payment failure, or a session that never became usable (a later plugin vetoed,
|
|
240
|
+
* payment failure, or a session that never became usable (a later plugin vetoed, the paygate refused,
|
|
294
241
|
* a `created` threw; then `sessionId` may be absent and `reservationId` names the hold to release).
|
|
295
242
|
*/
|
|
296
243
|
export interface CheckoutSettled {
|
|
@@ -320,7 +267,7 @@ export interface CheckoutPlugin {
|
|
|
320
267
|
created?: (ctx: ApiContext, created: CheckoutCreated) => Promise<void>
|
|
321
268
|
/** From the webhook (paid, expired, failed) or from a checkout that never became usable. */
|
|
322
269
|
settled?: (ctx: ApiContext, settled: CheckoutSettled) => Promise<void>
|
|
323
|
-
/** The longest a session may stay open; the gateway uses the smallest, clamped
|
|
270
|
+
/** The longest a session may stay open; the gateway uses the smallest, clamped into the paygate's `features.sessionTtl`. */
|
|
324
271
|
sessionTtlSeconds?: number
|
|
325
272
|
}
|
|
326
273
|
|
|
@@ -329,6 +276,11 @@ export interface PortalLinkOptions {
|
|
|
329
276
|
/** The target plan of `PortalFlow.Change`. */
|
|
330
277
|
planSku?: string
|
|
331
278
|
returnUrl: string
|
|
279
|
+
/**
|
|
280
|
+
* The paygate whose portal opens. Absent: the paygate of the entity's entitling subscription, else
|
|
281
|
+
* the first registered paygate holding a customer of the entity.
|
|
282
|
+
*/
|
|
283
|
+
paygate?: string
|
|
332
284
|
}
|
|
333
285
|
|
|
334
286
|
export interface PriceEstimateParams {
|
|
@@ -339,6 +291,8 @@ export interface PriceEstimateParams {
|
|
|
339
291
|
planSku?: string
|
|
340
292
|
/** ISO 3166-1 alpha-2. Absent: taken from the entity's paygate customer address. */
|
|
341
293
|
country?: string
|
|
294
|
+
/** The paygate to estimate through. Absent: the first registered paygate the plan is for sale through. */
|
|
295
|
+
paygate?: string
|
|
342
296
|
}
|
|
343
297
|
|
|
344
298
|
export interface GrantInternalPlanOptions {
|
|
@@ -350,21 +304,21 @@ export interface GrantInternalPlanOptions {
|
|
|
350
304
|
|
|
351
305
|
export interface SubscriptionRef {
|
|
352
306
|
entityId?: string
|
|
353
|
-
/** The paygate subscription id
|
|
307
|
+
/** The paygate subscription id. */
|
|
354
308
|
subscriptionId?: string
|
|
309
|
+
/** The paygate of `subscriptionId`. Absent: its row's paygate, else the first registered one with subscriptions. */
|
|
310
|
+
paygate?: string
|
|
355
311
|
}
|
|
356
312
|
|
|
357
313
|
export interface GatewayService extends InitializedService {
|
|
358
|
-
/** `false` when registered with `manage: false`: every
|
|
314
|
+
/** `false` when registered with `manage: false`: every paygate-calling method throws. */
|
|
359
315
|
managed: boolean
|
|
360
|
-
/** Whether the boot-time
|
|
316
|
+
/** Whether the boot-time paygate bootstrap runs: the `bootstrap` option, else `managed`. */
|
|
361
317
|
bootstrap: boolean
|
|
362
318
|
/** The alias whose host and base form the webhook URL: the `webhookService` option, else `cfg.service`. */
|
|
363
319
|
readonly webhookService: string
|
|
364
|
-
/** The key of this deployment's
|
|
320
|
+
/** The key of this deployment's paygate rows: the `owner` option, else `cfg.service`. */
|
|
365
321
|
readonly owner: string
|
|
366
|
-
/** The Stripe client every paygate call of this gateway uses: the `stripe` option, else the configured secret. */
|
|
367
|
-
stripe: StripeFactory
|
|
368
322
|
createLink: (ctx: ApiContext, params: CreateLinkParams) => Promise<string>
|
|
369
323
|
portalLink: (ctx: ApiContext, entityId: string, opts: PortalLinkOptions) => Promise<string>
|
|
370
324
|
/** @throws PaygateError('unmanaged') when `managed` is `false`. */
|
|
@@ -396,7 +350,7 @@ export interface GatewayService extends InitializedService {
|
|
|
396
350
|
* `createLink` enforces. Mongo and plugins only; works unmanaged.
|
|
397
351
|
*/
|
|
398
352
|
amountPolicy: (ctx: ApiContext, entityId: string, productSku: string, planSku?: string) => Promise<AmountPolicyView>
|
|
399
|
-
/** The synced prices of a product's plans, per currency — stored rows, no
|
|
353
|
+
/** The synced prices of a product's plans, per currency — stored rows, no paygate call. */
|
|
400
354
|
planPrices: (ctx: ApiContext, productSku: string) => Promise<PlanPriceView[]>
|
|
401
355
|
}
|
|
402
356
|
|
|
@@ -404,16 +358,17 @@ export interface PaymentGatewayOptions {
|
|
|
404
358
|
dbAlias?: string
|
|
405
359
|
serviceAlias?: string
|
|
406
360
|
/**
|
|
407
|
-
* `false` for a process that must read entitlements but never talk to
|
|
361
|
+
* `false` for a process that must read entitlements but never talk to a paygate: no paygate
|
|
408
362
|
* bootstrap at init, and `createLink` / `portalLink` / `resync*` / the webhook route throw
|
|
409
363
|
* `PaygateError('unmanaged')`.
|
|
410
364
|
*/
|
|
411
365
|
manage?: boolean
|
|
412
366
|
/**
|
|
413
|
-
* Whether this process brings
|
|
414
|
-
* portal configuration, webhook endpoint).
|
|
415
|
-
* still serves checkout, the portal, estimates
|
|
416
|
-
* bootstrap (`resync`, an application's
|
|
367
|
+
* Whether this process brings every registered paygate to the declared state at boot
|
|
368
|
+
* (`paygateBootstrapOf(ctx).bootstrap()`: products, portal configuration, webhook endpoint).
|
|
369
|
+
* Default: `manage`. A managed process with `false` still serves checkout, the portal, estimates
|
|
370
|
+
* and the resyncs, and still runs a FORCED bootstrap (`resync`, an application's
|
|
371
|
+
* `paygateBootstrapOf(ctx).bootstrap({ force: true })`).
|
|
417
372
|
* `true` on an unmanaged gateway is refused at construction.
|
|
418
373
|
*/
|
|
419
374
|
bootstrap?: boolean
|
|
@@ -425,20 +380,15 @@ export interface PaymentGatewayOptions {
|
|
|
425
380
|
*/
|
|
426
381
|
webhookService?: string
|
|
427
382
|
/**
|
|
428
|
-
* The key this deployment's
|
|
383
|
+
* The key this deployment's paygate rows are stored under: the `payment-webhook` rows' `service`,
|
|
429
384
|
* the signing-secret lookup, the endpoint's `owlmeans:<owner>` description and `service`
|
|
430
385
|
* metadata, the `portal:<owner>` fingerprint and the portal configuration's `service` metadata.
|
|
431
386
|
* Default: `cfg.service`. Give every process sharing the database the same value, so they read
|
|
432
387
|
* and replace one another's rows.
|
|
433
388
|
*/
|
|
434
389
|
owner?: string
|
|
435
|
-
/** The Stripe client for this gateway's paygate calls. Default: the configured secret (`stripeClient`). */
|
|
436
|
-
stripe?: StripeFactory
|
|
437
390
|
}
|
|
438
391
|
|
|
439
|
-
/** A Stripe client for one context — the default reads the configured secret. */
|
|
440
|
-
export interface StripeFactory { (ctx: ApiContext): Promise<Stripe> }
|
|
441
|
-
|
|
442
392
|
/** @deprecated use `PaymentGatewayOptions` */
|
|
443
393
|
export type PaymentResourceOptions = PaymentGatewayOptions
|
|
444
394
|
|
|
@@ -467,9 +417,9 @@ export interface AmountTopUpCompletion extends TopUpBase {
|
|
|
467
417
|
sourceChargeAmountMinor: number
|
|
468
418
|
/** The amount policy's catalogue currency. */
|
|
469
419
|
amountCurrency: string
|
|
470
|
-
/** Pre-tax subtotal actually charged by
|
|
420
|
+
/** Pre-tax subtotal actually charged by the paygate, in `currency`. */
|
|
471
421
|
chargeAmountMinor: number
|
|
472
|
-
/**
|
|
422
|
+
/** The charge currency of the paygate. */
|
|
473
423
|
currency: string
|
|
474
424
|
}
|
|
475
425
|
export type TopUpCompletion = QuantityTopUpCompletion | AmountTopUpCompletion
|
|
@@ -960,7 +910,7 @@ export interface SyncedPriceOption {
|
|
|
960
910
|
unitAmount: number
|
|
961
911
|
}
|
|
962
912
|
|
|
963
|
-
/** A reusable price as the last sync left it — what `planPrices` and checkout read without
|
|
913
|
+
/** A reusable price as the last sync left it — what `planPrices` and checkout read without the paygate. */
|
|
964
914
|
export interface SyncedPrice {
|
|
965
915
|
planSku: string
|
|
966
916
|
priceId: string
|
|
@@ -968,7 +918,7 @@ export interface SyncedPrice {
|
|
|
968
918
|
/** The default currency and its amount. */
|
|
969
919
|
currency: string
|
|
970
920
|
unitAmount: number
|
|
971
|
-
/**
|
|
921
|
+
/** The further currencies besides the default currency. */
|
|
972
922
|
options: SyncedPriceOption[]
|
|
973
923
|
taxBehavior?: string
|
|
974
924
|
interval?: 'month' | 'year'
|
|
@@ -1146,7 +1096,7 @@ export interface BillingProfileResource extends MongoResource<BillingProfileReco
|
|
|
1146
1096
|
* subscription's first invoice (`subscription`). Renewals are not purchases.
|
|
1147
1097
|
*/
|
|
1148
1098
|
export interface PurchaseRecord extends ResourceRecord {
|
|
1149
|
-
/**
|
|
1099
|
+
/** `<paygate>:<checkout session id>` (one-time) or `<paygate>:<subscription id>`. */
|
|
1150
1100
|
purchaseId: string
|
|
1151
1101
|
/** `CR-YYMMDD-XXXXXX` — what a consumer quotes. */
|
|
1152
1102
|
contractRef: string
|
|
@@ -1184,8 +1134,8 @@ export interface PurchaseRecord extends ResourceRecord {
|
|
|
1184
1134
|
units?: number
|
|
1185
1135
|
taxBehavior?: string
|
|
1186
1136
|
/**
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1137
|
+
* The terms acceptance the checkout itself collected — absent when the session did not collect it
|
|
1138
|
+
* (the policy's `checkoutTerms` off, or a paygate that created the session without the checkbox).
|
|
1189
1139
|
*/
|
|
1190
1140
|
termsAccepted?: boolean
|
|
1191
1141
|
textVersion?: string
|
|
@@ -1409,6 +1359,8 @@ export interface ConsumerMailData {
|
|
|
1409
1359
|
export interface ConsumerMailRenderer { (kind: ConsumerMailKind, data: ConsumerMailData, rendered: MailMessage): MailMessage | null | undefined | Promise<MailMessage | null | undefined> }
|
|
1410
1360
|
|
|
1411
1361
|
export interface LockOptions {
|
|
1362
|
+
/** The paygate the lock came from (its customer, its checkout). Default: the first registered paygate. */
|
|
1363
|
+
paygate?: string
|
|
1412
1364
|
customerId?: string
|
|
1413
1365
|
sessionId?: string
|
|
1414
1366
|
ipCountry?: string
|
|
@@ -1501,7 +1453,7 @@ export interface ConsumerRightsService extends LazyService {
|
|
|
1501
1453
|
}
|
|
1502
1454
|
|
|
1503
1455
|
/**
|
|
1504
|
-
* `appendConsumerRights` options. `manage
|
|
1456
|
+
* `appendConsumerRights` options. `manage` and `usage` apply to the service whenever the
|
|
1505
1457
|
* call comes — before or after the gateway registered it; the resources' `dbAlias` / `serviceAlias`
|
|
1506
1458
|
* are those of the first registration.
|
|
1507
1459
|
*/
|
|
@@ -1514,8 +1466,6 @@ export interface ConsumerRightsOptions {
|
|
|
1514
1466
|
/** The application's usage meter; without one a withdrawal is left to an operator (`review`). */
|
|
1515
1467
|
usage?: UsageMeter
|
|
1516
1468
|
alias?: string
|
|
1517
|
-
/** The paygate client; default: the configured Stripe secret. */
|
|
1518
|
-
stripe?: StripeFactory
|
|
1519
1469
|
dbAlias?: string
|
|
1520
1470
|
serviceAlias?: string
|
|
1521
1471
|
}
|
|
@@ -1586,7 +1536,7 @@ export type EntitlementGateOptions = CapabilityGateOptions
|
|
|
1586
1536
|
|
|
1587
1537
|
export interface LimitGateOptions extends EntityResolverOption {}
|
|
1588
1538
|
|
|
1589
|
-
export interface
|
|
1539
|
+
export interface PaygateBootstrapOptions {
|
|
1590
1540
|
/** Re-verify what the stored fingerprints say is in place. */
|
|
1591
1541
|
force?: boolean
|
|
1592
1542
|
}
|
package/src/utils/types.ts
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
|
-
|
|
3
1
|
/** A resource whose Mongo collection is reached directly — a conditional write's target. */
|
|
4
2
|
export interface CollectionHolder { collection: unknown }
|
|
5
3
|
|
|
6
4
|
/** The package's pure primitives: paygate payload readers, store guards and audit text. */
|
|
7
5
|
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
6
|
isDuplicateKey: (error: unknown) => boolean
|
|
14
|
-
/** A paygate answer meaning the object does not exist (deleted, or never did). */
|
|
15
|
-
isMissingObject: (error: unknown) => boolean
|
|
16
7
|
/** A paygate object reference, expanded or not, as its id. */
|
|
17
8
|
idOf: (value: string | { id?: string } | null | undefined) => string | undefined
|
|
18
9
|
/** Epoch seconds → `Date`. */
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
1
|
import { MONGO_DUPLICATE_KEY, mongoRefHelper } from '@owlmeans/mongo-resource'
|
|
3
|
-
import {
|
|
2
|
+
import type { PaymentService } from '@owlmeans/payment'
|
|
4
3
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
5
4
|
import { CONSUMER_RIGHTS_SERVICE } from './consts.js'
|
|
6
5
|
import { paymentAccessOf } from './access.js'
|
|
@@ -11,23 +10,9 @@ import type {
|
|
|
11
10
|
import type { CollectionHolder, PaymentUtils } from './utils/types.js'
|
|
12
11
|
|
|
13
12
|
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
13
|
const isDuplicateKey = (error: unknown): boolean =>
|
|
24
14
|
(error as { code?: unknown } | null)?.code === MONGO_DUPLICATE_KEY
|
|
25
15
|
|
|
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
16
|
const idOf = (value: string | { id?: string } | null | undefined): string | undefined =>
|
|
32
17
|
value == null ? undefined : typeof value === 'string' ? value : value.id
|
|
33
18
|
|
|
@@ -62,7 +47,7 @@ export const createPaymentUtils = (): PaymentUtils => {
|
|
|
62
47
|
(error instanceof Error ? error.message : String(error)).slice(0, 1000)
|
|
63
48
|
|
|
64
49
|
return {
|
|
65
|
-
|
|
50
|
+
isDuplicateKey, idOf, dateOf, compact, conditionalSet, conditionalDelete,
|
|
66
51
|
idCriteria, errorText,
|
|
67
52
|
}
|
|
68
53
|
}
|
|
@@ -97,6 +82,3 @@ export const consumerConsents = (ctx: ApiContext): ConsumerConsentResource => pa
|
|
|
97
82
|
export const consumerRightsOf = (
|
|
98
83
|
ctx: ApiContext, alias: string = CONSUMER_RIGHTS_SERVICE,
|
|
99
84
|
): ConsumerRightsService | null => paymentAccessOf(ctx).consumerRightsOf(alias)
|
|
100
|
-
|
|
101
|
-
/** @deprecated compat:factory-refactor — use `paymentAccessOf(ctx).stripeClient()` */
|
|
102
|
-
export const stripeClient = async (ctx: ApiContext): Promise<Stripe> => await paymentAccessOf(ctx).stripeClient()
|