@owlmeans/server-payment 0.1.21 → 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +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/plugins/fx/types.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
export interface StripeFxRate {
|
|
4
|
-
fromCurrency: string
|
|
5
|
-
toCurrency: string
|
|
6
|
-
/** Stripe's fee-inclusive conversion rate (`base_rate × (1 − fx_fee_rate)`). */
|
|
7
|
-
exchangeRate: number
|
|
8
|
-
/** The rate before any fee (Stripe's `base_rate`; derived from `exchangeRate` and `fxFeeRate` when absent). */
|
|
9
|
-
baseRate: number
|
|
10
|
-
/** Market/reference rate used to translate catalogue value into settlement value. */
|
|
11
|
-
referenceRate: number
|
|
12
|
-
fxFeeRate?: number
|
|
13
|
-
}
|
|
1
|
+
import type { Paygate } from '../../paygate/types.js'
|
|
2
|
+
import type { FxQuoteCache } from '../types.local.js'
|
|
14
3
|
|
|
15
4
|
export interface SettlementAmount {
|
|
16
5
|
amountMinor: number
|
|
@@ -20,21 +9,19 @@ export interface SettlementAmount {
|
|
|
20
9
|
referenceRate: number
|
|
21
10
|
}
|
|
22
11
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/** Currency conversion through Stripe's FX Quotes, under a context's pricing settings. */
|
|
12
|
+
/** Currency conversion through a paygate's FX quotes, under its settlement currency. */
|
|
26
13
|
export interface FxHelper {
|
|
27
|
-
/** One unlocked Stripe FX quote. `toCurrency` units per one `fromCurrency` unit. */
|
|
28
|
-
stripeFxRate: (stripe: Stripe, fromCurrency: string, toCurrency: string, apiVersion: string) => Promise<StripeFxRate | null>
|
|
29
14
|
/**
|
|
30
|
-
* A catalogue amount in the currency the buyer is charged in: unchanged — no
|
|
31
|
-
* currencies are equal; otherwise converted at
|
|
15
|
+
* A catalogue amount in the currency the buyer is charged in: unchanged — no quote — when the
|
|
16
|
+
* currencies are equal; otherwise converted at the paygate's reference rate, rounded up.
|
|
17
|
+
*
|
|
18
|
+
* @throws PaygateError('fx-rate:<from>:<to>') when the paygate quotes no rate (or has no `fx`)
|
|
32
19
|
*/
|
|
33
20
|
chargeAmount: (
|
|
34
|
-
|
|
21
|
+
paygate: Paygate, sourceAmountMinor: number, sourceCurrency: string, chargeCurrency: string, cache?: FxQuoteCache,
|
|
35
22
|
) => Promise<SettlementAmount>
|
|
36
|
-
/** Translate a catalogue amount into the
|
|
23
|
+
/** Translate a catalogue amount into the paygate's settlement currency (unchanged when it declares none). */
|
|
37
24
|
settlementAmount: (
|
|
38
|
-
|
|
25
|
+
paygate: Paygate, sourceAmountMinor: number, sourceCurrency: string, cache?: FxQuoteCache,
|
|
39
26
|
) => Promise<SettlementAmount>
|
|
40
27
|
}
|
package/src/plugins/fx.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
1
|
import { PaygateError } from '@owlmeans/payment'
|
|
3
2
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
4
3
|
import { memoHelper } from '@owlmeans/context'
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import type { SettlementAmount,
|
|
8
|
-
import { paymentAccessOf } from '../access.js'
|
|
4
|
+
import type { FxQuote, Paygate } from '../paygate/types.js'
|
|
5
|
+
import type { FxQuoteCache } from './types.local.js'
|
|
6
|
+
import type { SettlementAmount, FxHelper } from './fx/types.js'
|
|
9
7
|
|
|
10
8
|
/** Convert whole source minor units at the reference rate, rounding up so value is never lost. */
|
|
11
9
|
const convertMinor = (amountMinor: number, rate: number): number => {
|
|
@@ -17,50 +15,28 @@ const convertMinor = (amountMinor: number, rate: number): number => {
|
|
|
17
15
|
return converted
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
export const makeFxHelper = (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (from === to) return { fromCurrency: from, toCurrency: to, exchangeRate: 1, baseRate: 1, referenceRate: 1 }
|
|
29
|
-
const response = await stripe.rawRequest(
|
|
30
|
-
'POST', '/v1/fx_quotes',
|
|
31
|
-
{ to_currency: to, 'from_currencies[]': from, lock_duration: 'none' },
|
|
32
|
-
{ apiVersion },
|
|
33
|
-
) as Stripe.Response<FxQuoteResponse>
|
|
34
|
-
const rate = response.rates?.[from]
|
|
35
|
-
if (rate?.exchange_rate == null) return null
|
|
36
|
-
return {
|
|
37
|
-
fromCurrency: from,
|
|
38
|
-
toCurrency: to,
|
|
39
|
-
exchangeRate: rate.exchange_rate,
|
|
40
|
-
baseRate: rate.rate_details?.base_rate ?? rate.exchange_rate / (1 - (rate.rate_details?.fx_fee_rate ?? 0)),
|
|
41
|
-
referenceRate: rate.rate_details?.reference_rate ?? rate.rate_details?.base_rate ?? rate.exchange_rate,
|
|
42
|
-
...(rate.rate_details?.fx_fee_rate != null ? { fxFeeRate: rate.rate_details.fx_fee_rate } : {}),
|
|
18
|
+
export const makeFxHelper = (_ctx: ApiContext): FxHelper => {
|
|
19
|
+
/** One quote per (from, to) within a run that shares `cache`; a paygate without `fx`, or without a rate, refuses. */
|
|
20
|
+
const quoteOf = async (paygate: Paygate, from: string, to: string, cache?: FxQuoteCache): Promise<FxQuote> => {
|
|
21
|
+
const key = `${from}:${to}`
|
|
22
|
+
let pending = cache?.get(key)
|
|
23
|
+
if (pending == null) {
|
|
24
|
+
pending = paygate.fx != null ? paygate.fx.quote(from, to) : Promise.resolve(null)
|
|
25
|
+
cache?.set(key, pending)
|
|
43
26
|
}
|
|
27
|
+
const quote = await pending
|
|
28
|
+
if (quote == null) throw new PaygateError(`fx-rate:${from}:${to}`)
|
|
29
|
+
|
|
30
|
+
return quote
|
|
44
31
|
}
|
|
45
32
|
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
cache?: StripeFxRateCache,
|
|
33
|
+
const converted = async (
|
|
34
|
+
paygate: Paygate, sourceAmountMinor: number, source: string, currency: string, cache?: FxQuoteCache,
|
|
49
35
|
): Promise<SettlementAmount> => {
|
|
50
|
-
const source = sourceCurrency.toLowerCase()
|
|
51
|
-
const currency = chargeCurrency.toLowerCase()
|
|
52
36
|
if (currency === source) {
|
|
53
37
|
return { amountMinor: sourceAmountMinor, currency, sourceAmountMinor, sourceCurrency: source, referenceRate: 1 }
|
|
54
38
|
}
|
|
55
|
-
const
|
|
56
|
-
const key = `${source}:${currency}:${apiVersion}`
|
|
57
|
-
let pending = cache?.get(key)
|
|
58
|
-
if (pending == null) {
|
|
59
|
-
pending = stripeFxRate(stripe, source, currency, apiVersion)
|
|
60
|
-
cache?.set(key, pending)
|
|
61
|
-
}
|
|
62
|
-
const quote = await pending
|
|
63
|
-
if (quote == null) throw new PaygateError(`fx-rate:${source}:${currency}`)
|
|
39
|
+
const quote = await quoteOf(paygate, source, currency, cache)
|
|
64
40
|
|
|
65
41
|
return {
|
|
66
42
|
amountMinor: convertMinor(sourceAmountMinor, quote.referenceRate), currency,
|
|
@@ -68,33 +44,21 @@ export const makeFxHelper = (ctx: ApiContext): FxHelper => {
|
|
|
68
44
|
}
|
|
69
45
|
}
|
|
70
46
|
|
|
47
|
+
const chargeAmount = async (
|
|
48
|
+
paygate: Paygate, sourceAmountMinor: number, sourceCurrency: string, chargeCurrency: string, cache?: FxQuoteCache,
|
|
49
|
+
): Promise<SettlementAmount> =>
|
|
50
|
+
await converted(paygate, sourceAmountMinor, sourceCurrency.toLowerCase(), chargeCurrency.toLowerCase(), cache)
|
|
51
|
+
|
|
71
52
|
const settlementAmount = async (
|
|
72
|
-
|
|
53
|
+
paygate: Paygate, sourceAmountMinor: number, sourceCurrency: string, cache?: FxQuoteCache,
|
|
73
54
|
): Promise<SettlementAmount> => {
|
|
74
55
|
const source = sourceCurrency.toLowerCase()
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
amountMinor: sourceAmountMinor, currency, sourceAmountMinor, sourceCurrency: source, referenceRate: 1,
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const apiVersion = pricing?.fxApiVersion ?? STRIPE_FX_QUOTES_API_VERSION
|
|
83
|
-
const key = `${source}:${currency}:${apiVersion}`
|
|
84
|
-
let pending = cache?.get(key)
|
|
85
|
-
if (pending == null) {
|
|
86
|
-
pending = stripeFxRate(stripe, source, currency, apiVersion)
|
|
87
|
-
cache?.set(key, pending)
|
|
88
|
-
}
|
|
89
|
-
const quote = await pending
|
|
90
|
-
if (quote == null) throw new PaygateError(`fx-rate:${source}:${currency}`)
|
|
91
|
-
return {
|
|
92
|
-
amountMinor: convertMinor(sourceAmountMinor, quote.referenceRate), currency,
|
|
93
|
-
sourceAmountMinor, sourceCurrency: source, referenceRate: quote.referenceRate,
|
|
94
|
-
}
|
|
56
|
+
const currency = (await paygate.settlementCurrency())?.toLowerCase() ?? source
|
|
57
|
+
|
|
58
|
+
return await converted(paygate, sourceAmountMinor, source, currency, cache)
|
|
95
59
|
}
|
|
96
60
|
|
|
97
|
-
return {
|
|
61
|
+
return { chargeAmount, settlementAmount }
|
|
98
62
|
}
|
|
99
63
|
|
|
100
64
|
/** The FX conversion of a context — one per context. */
|
|
@@ -1,45 +1,15 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
1
|
import type { PortalLinkOptions } from '../../types.js'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
force?: boolean
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/** The Stripe customer portal of a deployment: its configuration and its sessions. */
|
|
3
|
+
/** The customer portal of a context: which paygate, customer and subscription a portal session opens on. */
|
|
9
4
|
export interface PortalHelper {
|
|
10
|
-
/** The fingerprint sku of an owner's portal configuration: `portal:<owner>`. */
|
|
11
|
-
portalFingerprintSku: (owner: string) => string
|
|
12
|
-
/**
|
|
13
|
-
* Keep this deployment's own Stripe customer-portal configuration: customer, invoice and payment
|
|
14
|
-
* method self-service, cancellation at period end, and switching between the active recurring
|
|
15
|
-
* prices of every product sold through Stripe (both subscription features off when there are none).
|
|
16
|
-
* The customer may edit their tax id always, their e-mail unless `stripe.lockCustomerEmail`, their
|
|
17
|
-
* address unless the consumer-rights `countryLock` or `stripe.lockCustomerCountry`.
|
|
18
|
-
*
|
|
19
|
-
* A deployment's identity is its webhook URL (`webhookUrlOf`, on the gateway's `webhookService`) —
|
|
20
|
-
* also when that URL is undeliverable, as on a local run; its fingerprint row and label carry the
|
|
21
|
-
* gateway's `owner` (`gatewayOwnerOf`). Several deployments of one application may share a Stripe
|
|
22
|
-
* account, so each owns a configuration of its own, tagged
|
|
23
|
-
* `{ owlmeans: 'payment', service: <owner>, deployment: <webhook URL> }`.
|
|
24
|
-
*
|
|
25
|
-
* - An unchanged declaration (catalogue, branding, deployment key) makes no paygate call.
|
|
26
|
-
* - The configuration the `portal:<owner>` fingerprint row names is updated in place — unless its
|
|
27
|
-
* metadata tags it for another deployment (a moved webhook URL included), which is never
|
|
28
|
-
* overwritten; this deployment then proceeds as though it held no row.
|
|
29
|
-
* - Without a usable row, an active configuration tagged with exactly this owner and deployment
|
|
30
|
-
* key is adopted. Nothing else is: not an untagged one, not one carrying only the owner label,
|
|
31
|
-
* not one tagged for another deployment.
|
|
32
|
-
* - Otherwise a new configuration is created. Stripe cannot delete a portal configuration, so one
|
|
33
|
-
* this deployment can no longer identify stays in the account, and a lost row creates a new one
|
|
34
|
-
* unless its tagged configuration is found.
|
|
35
|
-
*/
|
|
36
|
-
ensurePortalConfiguration: (stripe: Stripe, opts?: EnsurePortalOptions) => Promise<string | null>
|
|
37
5
|
/**
|
|
38
|
-
* A
|
|
39
|
-
*
|
|
40
|
-
*
|
|
6
|
+
* A customer-portal session for one entity, through `opts.paygate`, else the paygate of the
|
|
7
|
+
* entity's entitling subscription, else the first registered paygate holding a customer of the
|
|
8
|
+
* entity. `Manage` opens the portal home and `PaymentMethod` the payment method form; `Cancel`,
|
|
9
|
+
* `Update` and `Change` act on the entity's entitling subscription of that paygate (`Change`
|
|
10
|
+
* confirms a switch of its one item to `planSku`'s price).
|
|
41
11
|
*
|
|
42
|
-
* @throws PortalUnavailable('customer' | 'subscription' | 'plan' | 'item')
|
|
12
|
+
* @throws PortalUnavailable('customer' | 'paygate' | 'subscription' | 'plan' | 'item') | UnknownPlan
|
|
43
13
|
*/
|
|
44
|
-
createPortalLink: (
|
|
14
|
+
createPortalLink: (entityId: string, opts: PortalLinkOptions) => Promise<string>
|
|
45
15
|
}
|
package/src/plugins/portal.ts
CHANGED
|
@@ -1,239 +1,78 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type Stripe from 'stripe'
|
|
3
|
-
import { ENTITLING_STATUSES, PortalFlow, PortalUnavailable, ProductError, UnknownPlan } from '@owlmeans/payment'
|
|
1
|
+
import { ENTITLING_STATUSES, INTERNAL_PAYGATE, PortalFlow, PortalUnavailable, UnknownPlan } from '@owlmeans/payment'
|
|
4
2
|
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
5
3
|
import { memoHelper } from '@owlmeans/context'
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
} from '
|
|
9
|
-
import type { PaymentProduct, PaymentSubscriptionRecord, PortalLinkOptions } from '../types.js'
|
|
10
|
-
import { log } from '../log.js'
|
|
11
|
-
import type { Claim, ConfigurationParams } from './types.local.js'
|
|
12
|
-
import type { EnsurePortalOptions, PortalHelper } from './portal/types.js'
|
|
4
|
+
import type { PaygateCustomerRecord, PaymentSubscriptionRecord, PortalLinkOptions } from '../types.js'
|
|
5
|
+
import type { Paygate, PortalSessionInput } from '../paygate/types.js'
|
|
6
|
+
import type { PortalHelper } from './portal/types.js'
|
|
13
7
|
import { paymentAccessOf } from '../access.js'
|
|
14
|
-
import { paymentUtils } from '../utils.js'
|
|
15
8
|
import { catalogueOf } from '../catalogue.js'
|
|
16
9
|
import { planHelper } from '../plan.js'
|
|
10
|
+
import { paygatesOf } from '../paygate/registry.js'
|
|
17
11
|
import { webhookOf } from './webhook-manager.js'
|
|
18
|
-
|
|
19
|
-
/** The fingerprint sku of an owner's portal configuration: `portal:<owner>`. */
|
|
20
|
-
const portalFingerprintSku = (owner: string): string => `${FINGERPRINT_PORTAL}:${owner}`
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Whose a configuration is by its metadata: `ours` carries this owner AND this deployment's key;
|
|
24
|
-
* `foreign` is tagged for another deployment or another owner; `unclaimed` names no deployment.
|
|
25
|
-
*/
|
|
26
|
-
const claimOf = (metadata: Stripe.Metadata | null | undefined, owner: string, deployment: string): Claim => {
|
|
27
|
-
const owned = metadata?.[STRIPE_OWNER_KEY] === STRIPE_OWNER_VALUE
|
|
28
|
-
const tagged = metadata?.[STRIPE_DEPLOYMENT_KEY]
|
|
29
|
-
if (owned && metadata?.service === owner && tagged === deployment) {
|
|
30
|
-
return 'ours'
|
|
31
|
-
}
|
|
32
|
-
if ((owned && metadata?.service !== owner) || (tagged != null && tagged !== '')) {
|
|
33
|
-
return 'foreign'
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return 'unclaimed'
|
|
37
|
-
}
|
|
12
|
+
import { ENTITLING_SORT } from './consts.local.js'
|
|
38
13
|
|
|
39
14
|
export const makePortalHelper = (ctx: ApiContext): PortalHelper => {
|
|
40
15
|
const access = paymentAccessOf(ctx)
|
|
41
16
|
|
|
42
|
-
/** The
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
hashable: recurring.map(plan => ({
|
|
58
|
-
sku: plan.sku, price: plan.price, currency: plan.currency ?? 'usd', interval: plan.recurring?.interval,
|
|
59
|
-
rank: planHelper.planRank(plan), behavior,
|
|
60
|
-
// A changed currency option replaces the Price — the portal's price list must follow it.
|
|
61
|
-
...(plan.currencyPrices != null ? { currencyPrices: plan.currencyPrices } : {}),
|
|
62
|
-
})).sort((a, b) => a.sku.localeCompare(b.sku)),
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return result.sort((a, b) => a.product.sku.localeCompare(b.product.sku))
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const activeRecurringPrices = async (stripe: Stripe, productSku: string): Promise<Stripe.Price[]> =>
|
|
70
|
-
(await stripe.prices.list({ product: productSku, active: true, limit: 100 })).data
|
|
71
|
-
.filter(price => price.recurring != null)
|
|
72
|
-
|
|
73
|
-
const listActiveConfigurations = async (stripe: Stripe): Promise<Stripe.BillingPortal.Configuration[]> => {
|
|
74
|
-
const configurations: Stripe.BillingPortal.Configuration[] = []
|
|
75
|
-
let startingAfter: string | undefined
|
|
76
|
-
for (;;) {
|
|
77
|
-
const page = await stripe.billingPortal.configurations.list({
|
|
78
|
-
active: true, limit: 100, ...(startingAfter != null ? { starting_after: startingAfter } : {}),
|
|
79
|
-
})
|
|
80
|
-
configurations.push(...page.data)
|
|
81
|
-
if (!page.has_more || page.data.length === 0) {
|
|
82
|
-
return configurations
|
|
83
|
-
}
|
|
84
|
-
startingAfter = page.data[page.data.length - 1].id
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const ensurePortalConfiguration = async (stripe: Stripe, opts: EnsurePortalOptions = {}): Promise<string | null> => {
|
|
89
|
-
const owner = webhookOf(ctx).gatewayOwnerOf()
|
|
90
|
-
const deployment = webhookOf(ctx).webhookUrlOf()
|
|
91
|
-
const sku = portalFingerprintSku(owner)
|
|
92
|
-
const branding = await access.portalBrandingConfig()
|
|
93
|
-
const catalog = await recurringCatalog()
|
|
94
|
-
const rights = await access.payment().consumerRightsPolicy()
|
|
95
|
-
// A locked billing country is never edited in the portal: tax follows the saved address.
|
|
96
|
-
const stripeConfig = await access.stripePricingConfig()
|
|
97
|
-
const countryLock = rights?.mechanisms.countryLock === true
|
|
98
|
-
// A pinned country and a locked customer e-mail are the application's: never edited in the portal.
|
|
99
|
-
const countryPin = stripeConfig?.lockCustomerCountry === true
|
|
100
|
-
const emailLock = stripeConfig?.lockCustomerEmail === true
|
|
101
|
-
const regionCurrencies = Object.values(rights?.currencies ?? {}).filter(code => code != null).sort()
|
|
102
|
-
const hash = createHash('sha256').update(JSON.stringify({
|
|
103
|
-
service: owner,
|
|
104
|
-
deployment,
|
|
105
|
-
...(countryLock ? { countryLock } : {}),
|
|
106
|
-
...(countryPin ? { countryPin } : {}),
|
|
107
|
-
...(emailLock ? { emailLock } : {}),
|
|
108
|
-
...(regionCurrencies.length > 0 ? { regionCurrencies } : {}),
|
|
109
|
-
branding: branding != null ? {
|
|
110
|
-
headline: branding.headline ?? null, privacyPolicyUrl: branding.privacyPolicyUrl ?? null,
|
|
111
|
-
termsOfServiceUrl: branding.termsOfServiceUrl ?? null, returnUrl: branding.returnUrl ?? null,
|
|
112
|
-
} : null,
|
|
113
|
-
products: catalog.map(entry => ({ product: entry.product.sku, plans: entry.hashable })),
|
|
114
|
-
})).digest('hex')
|
|
115
|
-
|
|
116
|
-
const stored = await access.fingerprints().bySku(sku)
|
|
117
|
-
if (stored != null && stored.hash === hash && stored.externalId != null && opts.force !== true) {
|
|
118
|
-
return stored.externalId
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const products: Array<{ product: string, prices: string[] }> = []
|
|
122
|
-
for (const entry of catalog) {
|
|
123
|
-
const prices = (await activeRecurringPrices(stripe, entry.product.sku)).map(price => price.id)
|
|
124
|
-
if (prices.length > 0) {
|
|
125
|
-
products.push({ product: entry.product.sku, prices })
|
|
126
|
-
}
|
|
17
|
+
/** The entity's highest-ranked entitling subscription of a paygate (any paygate but the internal one), or `null`. */
|
|
18
|
+
const entitlingSubscription = async (entityId: string, paygate?: string): Promise<PaymentSubscriptionRecord | null> =>
|
|
19
|
+
await access.subscriptions().load(
|
|
20
|
+
{ entityId, paygate: paygate ?? { $ne: INTERNAL_PAYGATE }, status: [...ENTITLING_STATUSES] },
|
|
21
|
+
{ sort: [...ENTITLING_SORT] as never },
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
/** The paygate a portal opens on and the entity's live customer there. */
|
|
25
|
+
const customerOf = async (
|
|
26
|
+
entityId: string, requested?: string,
|
|
27
|
+
): Promise<{ paygate: Paygate | null, customer: PaygateCustomerRecord | null }> => {
|
|
28
|
+
const paygates = paygatesOf(ctx)
|
|
29
|
+
const live = async (paygate: Paygate): Promise<PaygateCustomerRecord | null> => {
|
|
30
|
+
const customer = await access.paygateCustomers().byEntity(entityId, paygate.id)
|
|
31
|
+
return customer != null && customer.deletedAt == null ? customer : null
|
|
127
32
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const allowedUpdates: Stripe.BillingPortal.ConfigurationCreateParams.Features.CustomerUpdate.AllowedUpdate[] = [
|
|
132
|
-
...(emailLock ? [] : ['email' as const]), ...(countryLock || countryPin ? [] : ['address' as const]), 'tax_id',
|
|
133
|
-
]
|
|
134
|
-
const params: ConfigurationParams = {
|
|
135
|
-
features: {
|
|
136
|
-
customer_update: { enabled: true, allowed_updates: allowedUpdates },
|
|
137
|
-
invoice_history: { enabled: true },
|
|
138
|
-
payment_method_update: { enabled: true },
|
|
139
|
-
subscription_cancel: cancelable
|
|
140
|
-
? { enabled: true, mode: 'at_period_end', proration_behavior: 'none' }
|
|
141
|
-
: { enabled: false },
|
|
142
|
-
subscription_update: switchable
|
|
143
|
-
? {
|
|
144
|
-
enabled: true, default_allowed_updates: ['price'], products,
|
|
145
|
-
proration_behavior: 'create_prorations',
|
|
146
|
-
}
|
|
147
|
-
: { enabled: false },
|
|
148
|
-
},
|
|
149
|
-
...(branding != null ? {
|
|
150
|
-
business_profile: {
|
|
151
|
-
...(branding.headline != null ? { headline: branding.headline } : {}),
|
|
152
|
-
...(branding.privacyPolicyUrl != null ? { privacy_policy_url: branding.privacyPolicyUrl } : {}),
|
|
153
|
-
...(branding.termsOfServiceUrl != null ? { terms_of_service_url: branding.termsOfServiceUrl } : {}),
|
|
154
|
-
},
|
|
155
|
-
default_return_url: branding.returnUrl,
|
|
156
|
-
} : {}),
|
|
157
|
-
metadata,
|
|
33
|
+
if (requested != null) {
|
|
34
|
+
const paygate = paygates.find(requested)
|
|
35
|
+
return { paygate, customer: paygate != null ? await live(paygate) : null }
|
|
158
36
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
await stripe.billingPortal.configurations.update(current.id, params)
|
|
166
|
-
configurationId = current.id
|
|
167
|
-
}
|
|
168
|
-
} catch (error) {
|
|
169
|
-
if (!paymentUtils.isMissingObject(error)) {
|
|
170
|
-
throw error
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (configurationId == null) {
|
|
175
|
-
const existing = (await listActiveConfigurations(stripe))
|
|
176
|
-
.find(configuration => claimOf(configuration.metadata, owner, deployment) === 'ours')
|
|
177
|
-
if (existing != null) {
|
|
178
|
-
await stripe.billingPortal.configurations.update(existing.id, params)
|
|
179
|
-
configurationId = existing.id
|
|
180
|
-
} else {
|
|
181
|
-
configurationId = (await stripe.billingPortal.configurations.create(params)).id
|
|
37
|
+
const subscribed = (await entitlingSubscription(entityId))?.paygate
|
|
38
|
+
const preferred = subscribed != null ? paygates.find(subscribed) : null
|
|
39
|
+
for (const paygate of preferred != null ? [preferred, ...paygates.all().filter(item => item !== preferred)] : paygates.all()) {
|
|
40
|
+
const customer = await live(paygate)
|
|
41
|
+
if (customer != null) {
|
|
42
|
+
return { paygate, customer }
|
|
182
43
|
}
|
|
183
44
|
}
|
|
184
45
|
|
|
185
|
-
|
|
186
|
-
if (stored != null) {
|
|
187
|
-
await access.fingerprints().update({ ...stored, hash, externalId: configurationId, updatedAt: now })
|
|
188
|
-
} else {
|
|
189
|
-
await access.fingerprints().create({ sku, hash, externalId: configurationId, updatedAt: now })
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return configurationId
|
|
46
|
+
return { paygate: preferred, customer: null }
|
|
193
47
|
}
|
|
194
48
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
): Promise<PaymentSubscriptionRecord | null> => await access.subscriptions().load(
|
|
199
|
-
{ entityId, paygate: STRIPE_PAYGATE_ALIAS, status: [...ENTITLING_STATUSES] },
|
|
200
|
-
{ sort: [{ field: 'rank', order: 'desc' }, { field: 'createdAt', order: 'desc' }] },
|
|
201
|
-
)
|
|
202
|
-
|
|
203
|
-
const createPortalLink = async (stripe: Stripe, entityId: string, opts: PortalLinkOptions): Promise<string> => {
|
|
204
|
-
const customer = await access.paygateCustomers().byEntity(entityId, STRIPE_PAYGATE_ALIAS)
|
|
205
|
-
if (customer == null || customer.deletedAt != null) {
|
|
49
|
+
const createPortalLink = async (entityId: string, opts: PortalLinkOptions): Promise<string> => {
|
|
50
|
+
const { paygate, customer } = await customerOf(entityId, opts.paygate)
|
|
51
|
+
if (paygate == null || customer == null) {
|
|
206
52
|
throw new PortalUnavailable('customer')
|
|
207
53
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (configuration == null) {
|
|
212
|
-
await ensurePortalConfiguration(stripe).catch(error => {
|
|
213
|
-
log.error('Portal configuration unavailable', error)
|
|
214
|
-
})
|
|
215
|
-
configuration = (await access.fingerprints().bySku(sku))?.externalId ?? null
|
|
54
|
+
const portal = paygate.portal
|
|
55
|
+
if (portal == null) {
|
|
56
|
+
throw new PortalUnavailable('paygate')
|
|
216
57
|
}
|
|
217
58
|
|
|
218
|
-
|
|
59
|
+
const input: PortalSessionInput = {
|
|
60
|
+
customerId: customer.externalId, flow: opts.flow, returnUrl: opts.returnUrl,
|
|
61
|
+
owner: webhookOf(ctx).gatewayOwnerOf(), deployment: () => webhookOf(ctx).deploymentOf(paygate.id),
|
|
62
|
+
}
|
|
219
63
|
switch (opts.flow) {
|
|
220
64
|
case PortalFlow.Manage:
|
|
221
|
-
break
|
|
222
65
|
case PortalFlow.PaymentMethod:
|
|
223
|
-
flowData = { type: 'payment_method_update' }
|
|
224
66
|
break
|
|
225
67
|
case PortalFlow.Cancel:
|
|
226
68
|
case PortalFlow.Update:
|
|
227
69
|
case PortalFlow.Change: {
|
|
228
|
-
const subscription = await
|
|
70
|
+
const subscription = await entitlingSubscription(entityId, paygate.id)
|
|
229
71
|
if (subscription == null) {
|
|
230
72
|
throw new PortalUnavailable('subscription')
|
|
231
73
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
} else if (opts.flow === PortalFlow.Update) {
|
|
235
|
-
flowData = { type: 'subscription_update', subscription_update: { subscription: subscription.externalId } }
|
|
236
|
-
} else {
|
|
74
|
+
input.subscriptionId = subscription.externalId
|
|
75
|
+
if (opts.flow === PortalFlow.Change) {
|
|
237
76
|
if (opts.planSku == null || opts.planSku === '') {
|
|
238
77
|
throw new PortalUnavailable('plan')
|
|
239
78
|
}
|
|
@@ -245,39 +84,19 @@ export const makePortalHelper = (ctx: ApiContext): PortalHelper => {
|
|
|
245
84
|
if (subscription.itemId == null) {
|
|
246
85
|
throw new PortalUnavailable('item')
|
|
247
86
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (price == null) {
|
|
251
|
-
throw new ProductError(`price:${lookupKey}`)
|
|
252
|
-
}
|
|
253
|
-
flowData = {
|
|
254
|
-
type: 'subscription_update_confirm',
|
|
255
|
-
subscription_update_confirm: {
|
|
256
|
-
subscription: subscription.externalId,
|
|
257
|
-
items: [{ id: subscription.itemId, price: price.id, quantity: 1 }],
|
|
258
|
-
},
|
|
259
|
-
}
|
|
87
|
+
input.itemId = subscription.itemId
|
|
88
|
+
input.lookupKey = planHelper.planLookupKey(product, plan)
|
|
260
89
|
}
|
|
261
90
|
break
|
|
262
91
|
}
|
|
263
92
|
default:
|
|
264
93
|
throw new PortalUnavailable(`flow:${String(opts.flow)}`)
|
|
265
94
|
}
|
|
266
|
-
if (flowData != null) {
|
|
267
|
-
flowData.after_completion = { type: 'redirect', redirect: { return_url: opts.returnUrl } }
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const session = await stripe.billingPortal.sessions.create({
|
|
271
|
-
customer: customer.externalId,
|
|
272
|
-
return_url: opts.returnUrl,
|
|
273
|
-
...(configuration != null ? { configuration } : {}),
|
|
274
|
-
...(flowData != null ? { flow_data: flowData } : {}),
|
|
275
|
-
})
|
|
276
95
|
|
|
277
|
-
return
|
|
96
|
+
return await portal.createSession(input)
|
|
278
97
|
}
|
|
279
98
|
|
|
280
|
-
return {
|
|
99
|
+
return { createPortalLink }
|
|
281
100
|
}
|
|
282
101
|
|
|
283
102
|
/** The customer portal of a context — one per context. */
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
1
|
import type { PaymentFulfillmentRecord, PaymentSubscriptionRecord } from '../../types.js'
|
|
2
|
+
import type { Paygate, PaygateCharge } from '../../paygate/types.js'
|
|
3
3
|
|
|
4
4
|
/** What a refund or a dispute is about: a one-time fulfillment, or one invoice of a subscription. */
|
|
5
5
|
export type PaymentTarget =
|
|
6
|
-
| { kind: 'fulfillment', record: PaymentFulfillmentRecord, charge:
|
|
6
|
+
| { kind: 'fulfillment', record: PaymentFulfillmentRecord, charge: PaygateCharge | null, paymentIntentId?: string }
|
|
7
7
|
| {
|
|
8
|
-
kind: 'subscription', record: PaymentSubscriptionRecord, charge:
|
|
8
|
+
kind: 'subscription', record: PaymentSubscriptionRecord, charge: PaygateCharge | null,
|
|
9
9
|
paymentIntentId?: string, invoiceId: string,
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -14,18 +14,18 @@ export interface PaymentReference {
|
|
|
14
14
|
chargeId?: string
|
|
15
15
|
invoiceId?: string
|
|
16
16
|
/** The charge, when the event already carries it. */
|
|
17
|
-
charge?:
|
|
17
|
+
charge?: PaygateCharge | null
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/** What a refund or a dispute of a context is about. */
|
|
21
21
|
export interface PaymentTargetHelper {
|
|
22
|
-
/** A charge by id, or `null` when the paygate has none. */
|
|
23
|
-
retrieveCharge: (
|
|
22
|
+
/** A charge by id, or `null` when the paygate has none (or reads no payments). */
|
|
23
|
+
retrieveCharge: (paygate: Paygate, chargeId: string) => Promise<PaygateCharge | null>
|
|
24
24
|
/**
|
|
25
|
-
* Resolve a refund or a dispute to the record it is about: by payment
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
25
|
+
* Resolve a refund or a dispute to the record it is about: by payment to a fulfillment; by charge
|
|
26
|
+
* to a fulfillment (stored charge, else the charge's payment — whose charge id is then remembered),
|
|
27
|
+
* else the charge's invoice; by invoice to the subscription whose latest invoice it is, else the
|
|
28
|
+
* invoice's subscription. `null` when nothing here paid for it.
|
|
29
29
|
*/
|
|
30
|
-
resolvePaymentTarget: (
|
|
30
|
+
resolvePaymentTarget: (paygate: Paygate, ref: PaymentReference) => Promise<PaymentTarget | null>
|
|
31
31
|
}
|