@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/stripe.ts
DELETED
|
@@ -1,571 +0,0 @@
|
|
|
1
|
-
import type Stripe from 'stripe'
|
|
2
|
-
import { BillingCountryLocked, CheckoutPricingMode, CONSUMER_RIGHTS_COPY_VERSION, ConsumerRightsError, PaygateError, ProductError, ProductType, TaxBehavior, type PricingPolicy, consumerCopyHelper, consumerRegionHelper, consumerRightsPolicyHelper } from '@owlmeans/payment'
|
|
3
|
-
import type { Context as ApiContext } from '@owlmeans/server-api'
|
|
4
|
-
import { memoHelper } from '@owlmeans/context'
|
|
5
|
-
import { STRIPE_PAYGATE_ALIAS } from '../consts.js'
|
|
6
|
-
import type { Admitted } from './types.js'
|
|
7
|
-
import type {
|
|
8
|
-
CheckoutAttempt, CheckoutPlugin, CheckoutTextContext, CreateLinkParams, PaymentPlan, PaymentProduct,
|
|
9
|
-
} from '../types.js'
|
|
10
|
-
import { log } from '../log.js'
|
|
11
|
-
import { CUSTOM_TEXT_MAX, EMAIL } from './consts.local.js'
|
|
12
|
-
import type { BuyerContext, CheckoutOptionsFlags, StripeErrorShape } from './types.local.js'
|
|
13
|
-
import { fxOf } from './fx.js'
|
|
14
|
-
import { paymentAccessOf } from '../access.js'
|
|
15
|
-
import { paymentUtils } from '../utils.js'
|
|
16
|
-
import { planHelper } from '../plan.js'
|
|
17
|
-
import { checkoutPluginsOf } from './checkout-plugins.js'
|
|
18
|
-
import { checkoutPolicyHelper } from './checkout-policy.js'
|
|
19
|
-
import type { StripeCheckoutHelper } from './stripe/types.js'
|
|
20
|
-
import { stripeSessionHelper } from './session.js'
|
|
21
|
-
import { consumerFormatHelper } from '../consumer/format.js'
|
|
22
|
-
import { consumerRecordsOf } from '../consumer/records.js'
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A Checkout Session's tax and currency options, entirely driven by the declared `PricingPolicy` —
|
|
26
|
-
* an undeclared one (`DEFAULT_PRICING_POLICY`) reproduces exactly what every session hard-coded
|
|
27
|
-
* before this policy existed: automatic tax and tax-id collection on, no Adaptive Pricing.
|
|
28
|
-
*
|
|
29
|
-
* `customer_update.address` lets automatic tax use the billing address Checkout just collected
|
|
30
|
-
* rather than only a previously saved one; `customer_update.name` lets tax-id collection save the
|
|
31
|
-
* business name it collects. Each is included only for the concern that needs it. Under a country
|
|
32
|
-
* lock the saved address is kept (`address: 'never'`, collection `'auto'`), so tax follows the
|
|
33
|
-
* locked country and Checkout cannot move it.
|
|
34
|
-
*/
|
|
35
|
-
const checkoutOptions = (
|
|
36
|
-
policy: PricingPolicy, promotions: boolean, flags: CheckoutOptionsFlags = {},
|
|
37
|
-
): Partial<Stripe.Checkout.SessionCreateParams> => {
|
|
38
|
-
const customerUpdate: Stripe.Checkout.SessionCreateParams.CustomerUpdate = {}
|
|
39
|
-
if (policy.tax.automatic) customerUpdate.address = flags.locked === true ? 'never' : 'auto'
|
|
40
|
-
if (policy.tax.collectTaxId) customerUpdate.name = 'auto'
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
...(policy.tax.automatic
|
|
44
|
-
? { automatic_tax: { enabled: true }, billing_address_collection: flags.locked === true ? 'auto' as const : 'required' as const }
|
|
45
|
-
: {}),
|
|
46
|
-
...(policy.tax.collectTaxId ? { tax_id_collection: { enabled: true } } : {}),
|
|
47
|
-
...(Object.keys(customerUpdate).length > 0 ? { customer_update: customerUpdate } : {}),
|
|
48
|
-
adaptive_pricing: { enabled: policy.currency.adaptive === true && flags.adaptive !== false },
|
|
49
|
-
allow_promotion_codes: promotions,
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const sharedSession = (
|
|
54
|
-
customer: Stripe.Customer, params: CreateLinkParams, product: PaymentProduct,
|
|
55
|
-
plan: PaymentPlan, metadata: Record<string, string>,
|
|
56
|
-
): Pick<Stripe.Checkout.SessionCreateParams, 'customer' | 'success_url' | 'cancel_url' | 'metadata' | 'locale'> => ({
|
|
57
|
-
customer: customer.id,
|
|
58
|
-
success_url: params.successUrl,
|
|
59
|
-
cancel_url: params.cancelUrl ?? params.successUrl,
|
|
60
|
-
...(params.locale != null ? { locale: params.locale as Stripe.Checkout.SessionCreateParams.Locale } : {}),
|
|
61
|
-
metadata: {
|
|
62
|
-
pricingMode: plan.pricingMode ?? CheckoutPricingMode.Quantity,
|
|
63
|
-
currency: (plan.currency ?? 'usd').toLowerCase(),
|
|
64
|
-
entityId: params.entityId,
|
|
65
|
-
...(params.profileId && { profileId: params.profileId }),
|
|
66
|
-
service: params.service,
|
|
67
|
-
productSku: product.sku,
|
|
68
|
-
planSku: plan.sku,
|
|
69
|
-
...metadata,
|
|
70
|
-
},
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
/** A saved address holding only `country` — stale lines and a postal code of another country cleared. */
|
|
74
|
-
const countryOnlyAddress = (country: string): Stripe.AddressParam => ({
|
|
75
|
-
country, line1: '', line2: '', city: '', state: '', postal_code: '',
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* The consumer-rights metadata every session (and its subscription) carries. `termsCollected`
|
|
80
|
-
* says whether Checkout asked for the terms checkbox — `'false'` also after the fallback of a
|
|
81
|
-
* Dashboard without a terms URL; `countryPinned` that tax was calculated on `country`, held on the
|
|
82
|
-
* customer by `stripe.lockCustomerCountry`, so that country is the one the purchase locks.
|
|
83
|
-
*/
|
|
84
|
-
const consumerMetadata = (buyer: BuyerContext, params: CreateLinkParams, startRequestId?: string): Record<string, string> =>
|
|
85
|
-
buyer.policy == null ? {} : paymentUtils.compact({
|
|
86
|
-
region: buyer.region ?? undefined,
|
|
87
|
-
country: buyer.country,
|
|
88
|
-
language: buyer.language,
|
|
89
|
-
termsVersion: buyer.policy.textVersion,
|
|
90
|
-
copyVersion: CONSUMER_RIGHTS_COPY_VERSION,
|
|
91
|
-
termsCollected: String(buyer.policy.mechanisms.checkoutTerms === true),
|
|
92
|
-
ipCountry: params.ipCountry?.toUpperCase(),
|
|
93
|
-
startRequestId,
|
|
94
|
-
countryPinned: buyer.countryPinned ? 'true' : undefined,
|
|
95
|
-
}) as Record<string, string>
|
|
96
|
-
|
|
97
|
-
const limited = (message: string, field: string): string => {
|
|
98
|
-
if (message.length > CUSTOM_TEXT_MAX) {
|
|
99
|
-
throw new ConsumerRightsError(`copy:length:${field}`)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return message
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** The terms checkbox and its text, when the policy asks for them on every checkout. */
|
|
106
|
-
const termsOptions = (buyer: BuyerContext): Partial<Stripe.Checkout.SessionCreateParams> & { terms?: string } => {
|
|
107
|
-
const policy = buyer.policy
|
|
108
|
-
if (policy?.mechanisms.checkoutTerms !== true) {
|
|
109
|
-
return {}
|
|
110
|
-
}
|
|
111
|
-
const links = consumerRightsPolicyHelper.linksOf(policy, buyer.language)
|
|
112
|
-
const withInformation = buyer.inScope && links.withdrawalInformation != null
|
|
113
|
-
const message = limited(consumerCopyHelper.consumerText(buyer.language, `checkout.terms-acceptance.${withInformation ? 'in-scope' : 'other'}`, {
|
|
114
|
-
billingTerms: links.billingTerms, ...(withInformation ? { withdrawalInformation: links.withdrawalInformation as string } : {}),
|
|
115
|
-
}), 'terms')
|
|
116
|
-
|
|
117
|
-
return { consent_collection: { terms_of_service: 'required' }, terms: message }
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const textContextOf = (buyer: BuyerContext, currency: string, extra: Partial<CheckoutTextContext> = {}): CheckoutTextContext => paymentUtils.compact({
|
|
121
|
-
language: buyer.language, currency, region: buyer.region, country: buyer.country, ...extra,
|
|
122
|
-
}) as CheckoutTextContext
|
|
123
|
-
|
|
124
|
-
const submitTextOf = (params: CreateLinkParams, context: CheckoutTextContext): string | undefined => {
|
|
125
|
-
if (params.submitText == null) {
|
|
126
|
-
return undefined
|
|
127
|
-
}
|
|
128
|
-
const message = typeof params.submitText === 'function' ? params.submitText(context) : params.submitText
|
|
129
|
-
|
|
130
|
-
return message.trim() === '' ? undefined : message
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** The same session without the terms checkbox and its text — and saying so in its metadata. */
|
|
134
|
-
const withoutTerms = (params: Stripe.Checkout.SessionCreateParams): Stripe.Checkout.SessionCreateParams => {
|
|
135
|
-
const { consent_collection: consent, custom_text: customText, ...rest } = params
|
|
136
|
-
const { terms_of_service: _terms, ...otherConsent } = consent ?? {}
|
|
137
|
-
const { terms_of_service_acceptance: _acceptance, ...otherText } = customText ?? {}
|
|
138
|
-
const marked = (metadata: Stripe.MetadataParam | undefined): Stripe.MetadataParam | undefined =>
|
|
139
|
-
metadata != null ? { ...metadata, termsCollected: 'false' } : metadata
|
|
140
|
-
|
|
141
|
-
return {
|
|
142
|
-
...rest,
|
|
143
|
-
...(Object.keys(otherConsent).length > 0 ? { consent_collection: otherConsent } : {}),
|
|
144
|
-
...(Object.keys(otherText).length > 0 ? { custom_text: otherText } : {}),
|
|
145
|
-
metadata: marked(rest.metadata) ?? { termsCollected: 'false' },
|
|
146
|
-
...(rest.subscription_data != null
|
|
147
|
-
? { subscription_data: { ...rest.subscription_data, metadata: marked(rest.subscription_data.metadata) ?? { termsCollected: 'false' } } }
|
|
148
|
-
: {}),
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** One console warning per context — a process builds one. */
|
|
153
|
-
const warnedTermsFallback = new WeakSet<object>()
|
|
154
|
-
|
|
155
|
-
export const makeStripeCheckoutHelper = (ctx: ApiContext): StripeCheckoutHelper => {
|
|
156
|
-
const access = paymentAccessOf(ctx)
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* The e-mail a checkout pins on its Stripe customer: `params.email` under
|
|
160
|
-
* `stripe.lockCustomerEmail`, `undefined` without the lock.
|
|
161
|
-
*
|
|
162
|
-
* @throws PaygateError('customer-email') — the lock is declared and no usable e-mail was passed
|
|
163
|
-
*/
|
|
164
|
-
const lockedEmailOf = async (params: CreateLinkParams): Promise<string | undefined> => {
|
|
165
|
-
if ((await access.stripePricingConfig())?.lockCustomerEmail !== true) {
|
|
166
|
-
return undefined
|
|
167
|
-
}
|
|
168
|
-
const email = params.email?.trim()
|
|
169
|
-
if (email == null || !EMAIL.test(email)) {
|
|
170
|
-
throw new PaygateError('customer-email')
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return email
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The entity's Stripe customer, created on first use. Under `stripe.lockCustomerEmail` it carries
|
|
178
|
-
* the buyer's e-mail — written over any other one — because Checkout shows a customer's valid
|
|
179
|
-
* e-mail read-only (and only asks for one, then saves it, while the customer has none).
|
|
180
|
-
*/
|
|
181
|
-
const ensureStripeCustomer = async (stripe: Stripe, params: CreateLinkParams): Promise<Stripe.Customer> => {
|
|
182
|
-
const email = await lockedEmailOf(params)
|
|
183
|
-
const resource = access.paygateCustomers()
|
|
184
|
-
const existing = await resource.byEntity(params.entityId, STRIPE_PAYGATE_ALIAS)
|
|
185
|
-
if (existing != null && existing.deletedAt == null) {
|
|
186
|
-
const retrieved = await stripe.customers.retrieve(existing.externalId)
|
|
187
|
-
if (!(retrieved as Stripe.DeletedCustomer).deleted) {
|
|
188
|
-
const customer = retrieved as Stripe.Customer
|
|
189
|
-
const update: Stripe.CustomerUpdateParams = {
|
|
190
|
-
...(params.locale != null && customer.preferred_locales?.[0] !== params.locale ? { preferred_locales: [params.locale] } : {}),
|
|
191
|
-
...(email != null && customer.email?.toLowerCase() !== email.toLowerCase() ? { email } : {}),
|
|
192
|
-
}
|
|
193
|
-
return Object.keys(update).length > 0 ? await stripe.customers.update(customer.id, update) : customer
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const created = await stripe.customers.create({
|
|
197
|
-
metadata: {
|
|
198
|
-
entityId: params.entityId, ...(params.profileId && { profileId: params.profileId }),
|
|
199
|
-
service: params.service,
|
|
200
|
-
},
|
|
201
|
-
...(params.locale != null ? { preferred_locales: [params.locale] } : {}),
|
|
202
|
-
...(email != null ? { email } : {}),
|
|
203
|
-
})
|
|
204
|
-
if (existing != null) {
|
|
205
|
-
const { deletedAt: _deleted, ...kept } = existing
|
|
206
|
-
await resource.update({ ...kept, externalId: created.id })
|
|
207
|
-
} else {
|
|
208
|
-
await resource.create({
|
|
209
|
-
paygate: STRIPE_PAYGATE_ALIAS, externalId: created.id, entityId: params.entityId,
|
|
210
|
-
...(params.profileId != null ? { profileId: params.profileId } : {}),
|
|
211
|
-
})
|
|
212
|
-
}
|
|
213
|
-
return created
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const findPrice = async (stripe: Stripe, productSku: string, lookupKey: string): Promise<Stripe.Price> => {
|
|
217
|
-
const prices = await stripe.prices.list({ product: productSku, active: true, lookup_keys: [lookupKey] })
|
|
218
|
-
if (prices.data.length === 0) throw new ProductError(`price:${lookupKey}`)
|
|
219
|
-
return prices.data[0]
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* The buyer as the consumer-rights policy sees it — a locked profile overrides the declared
|
|
224
|
-
* country (a different one is `BillingCountryLocked`), an organization that already paid before
|
|
225
|
-
* its country was locked is locked lazily from its paygate customer's address, and a locked
|
|
226
|
-
* customer address that no longer carries the locked country refuses (the operator relocks).
|
|
227
|
-
*
|
|
228
|
-
* Under `stripe.lockCustomerCountry` the known country (the locked one, else the declared one) is
|
|
229
|
-
* written to the customer — over a saved address of another country only before any lock — and
|
|
230
|
-
* Checkout keeps it wherever Stripe Tax can calculate on it.
|
|
231
|
-
*/
|
|
232
|
-
const buyerOf = async (
|
|
233
|
-
stripe: Stripe, params: CreateLinkParams, catalogueCurrency: string,
|
|
234
|
-
): Promise<{ buyer: BuyerContext, customer: Stripe.Customer }> => {
|
|
235
|
-
const policy = await access.payment().consumerRightsPolicy()
|
|
236
|
-
const rights = policy != null ? access.consumerRightsOf() : null
|
|
237
|
-
let profile = rights != null ? await rights.profile(params.entityId) : null
|
|
238
|
-
const declared = params.country != null && params.country.trim() !== '' ? params.country.trim().toUpperCase() : undefined
|
|
239
|
-
if (profile?.locked === true && declared != null && declared !== profile.country) {
|
|
240
|
-
throw new BillingCountryLocked({ country: profile.country as string, requested: declared })
|
|
241
|
-
}
|
|
242
|
-
let customer = await ensureStripeCustomer(stripe, params)
|
|
243
|
-
const customerCountry = customer.address?.country?.toUpperCase() ?? undefined
|
|
244
|
-
if (rights != null && policy?.mechanisms.countryLock === true && profile == null && customerCountry != null
|
|
245
|
-
&& await consumerRecordsOf(ctx).hasPaid(params.entityId)
|
|
246
|
-
&& !await consumerRecordsOf(ctx).wasUnlocked(params.entityId)) {
|
|
247
|
-
// An organization that paid before countries were locked: its saved address is its country.
|
|
248
|
-
// One an operator unlocked is locked again by its next completed purchase instead.
|
|
249
|
-
profile = await rights.lock(params.entityId, customerCountry, 'customer', { customerId: customer.id })
|
|
250
|
-
}
|
|
251
|
-
if (profile?.locked === true && declared != null && declared !== profile.country) {
|
|
252
|
-
throw new BillingCountryLocked({ country: profile.country as string, requested: declared })
|
|
253
|
-
}
|
|
254
|
-
if (profile?.locked === true && customerCountry != null && customerCountry !== profile.country) {
|
|
255
|
-
throw new BillingCountryLocked({ country: profile.country as string, requested: customerCountry })
|
|
256
|
-
}
|
|
257
|
-
const pinning = (await access.stripePricingConfig())?.lockCustomerCountry === true
|
|
258
|
-
const country = profile?.country ?? declared
|
|
259
|
-
if (pinning && country != null && customerCountry !== country) {
|
|
260
|
-
// Pinned: the known country replaces whatever an unlocked customer saved (a locked customer's
|
|
261
|
-
// other country was refused above), so Checkout has nothing else to offer.
|
|
262
|
-
customer = await stripe.customers.update(customer.id, { address: countryOnlyAddress(country) })
|
|
263
|
-
} else if (policy != null && profile == null && declared != null && customerCountry == null) {
|
|
264
|
-
// Preselect the declared country on Checkout's address form; never over a saved address.
|
|
265
|
-
customer = await stripe.customers.update(customer.id, { address: { country: declared } })
|
|
266
|
-
}
|
|
267
|
-
const savedCountry = customer.address?.country?.toUpperCase() ?? undefined
|
|
268
|
-
const kept = savedCountry != null && savedCountry === country && stripeSessionHelper.isTaxLocatable(customer.address)
|
|
269
|
-
const countryPinned = pinning && kept
|
|
270
|
-
const region = policy != null ? consumerRegionHelper.regionOf(country, policy) : null
|
|
271
|
-
const settlement = (await access.stripePricingConfig())?.settlementCurrency?.toLowerCase()
|
|
272
|
-
const regional = policy?.currencies != null && Object.keys(policy.currencies).length > 0
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
customer,
|
|
276
|
-
buyer: {
|
|
277
|
-
policy,
|
|
278
|
-
profile,
|
|
279
|
-
...(country != null ? { country } : {}),
|
|
280
|
-
region,
|
|
281
|
-
inScope: policy != null ? consumerRegionHelper.inScope(region, country, policy) : false,
|
|
282
|
-
language: params.consumerLanguage ?? profile?.language ?? consumerRegionHelper.billingLanguageOf(country, policy),
|
|
283
|
-
chargeCurrency: regional
|
|
284
|
-
? (profile?.currency ?? consumerRegionHelper.chargeCurrencyOf(region, policy, settlement ?? catalogueCurrency)).toLowerCase()
|
|
285
|
-
: null,
|
|
286
|
-
addressLocked: kept && (profile?.locked === true || countryPinned),
|
|
287
|
-
countryPinned,
|
|
288
|
-
},
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/** Whether the synced reusable price of a plan can be charged in `currency` (its default or an option). */
|
|
293
|
-
const priceCarries = async (
|
|
294
|
-
product: PaymentProduct, plan: PaymentPlan, price: Stripe.Price, currency: string,
|
|
295
|
-
): Promise<{ carries: boolean, unitAmount?: number }> => {
|
|
296
|
-
if (price.currency === currency) {
|
|
297
|
-
return { carries: true, ...(price.unit_amount != null ? { unitAmount: price.unit_amount } : {}) }
|
|
298
|
-
}
|
|
299
|
-
const listed = (price as unknown as { currency_options?: Record<string, { unit_amount?: number | null }> }).currency_options?.[currency]
|
|
300
|
-
if (listed != null) {
|
|
301
|
-
return { carries: true, ...(listed.unit_amount != null ? { unitAmount: listed.unit_amount } : {}) }
|
|
302
|
-
}
|
|
303
|
-
const synced = (await access.fingerprints().bySku(product.sku))?.prices?.find(entry => entry.planSku === plan.sku && entry.priceId === price.id)
|
|
304
|
-
const option = synced?.options.find(entry => entry.currency === currency)
|
|
305
|
-
|
|
306
|
-
return option != null ? { carries: true, unitAmount: option.unitAmount } : { carries: false }
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* A Dashboard without a terms-of-service URL must never stop a payment: the session is created
|
|
311
|
-
* once more without the checkbox, an operator is told (once), and the degraded checkout is audited
|
|
312
|
-
* as a `checkout-terms-fallback` event of the organization (`externalId` = the session).
|
|
313
|
-
*/
|
|
314
|
-
const createWithoutTerms = async (
|
|
315
|
-
stripe: Stripe, attempt: CheckoutAttempt, params: Stripe.Checkout.SessionCreateParams, refusal: unknown,
|
|
316
|
-
): Promise<Stripe.Checkout.Session> => {
|
|
317
|
-
const stripeMessage = paymentUtils.errorText(refusal)
|
|
318
|
-
if (!warnedTermsFallback.has(ctx)) {
|
|
319
|
-
warnedTermsFallback.add(ctx)
|
|
320
|
-
log.warn('Stripe refused the terms-of-service checkbox: this account has no terms of service URL. '
|
|
321
|
-
+ 'Checkouts continue WITHOUT the checkbox (metadata termsCollected=false). Operator: set the Billing Terms URL '
|
|
322
|
-
+ 'in the Stripe Dashboard → Settings → Public details (test and live mode alike).')
|
|
323
|
-
}
|
|
324
|
-
const typed = refusal as StripeErrorShape
|
|
325
|
-
const detail = JSON.stringify(paymentUtils.compact({
|
|
326
|
-
message: stripeMessage, code: typed.code ?? typed.raw?.code, param: typed.param ?? typed.raw?.param,
|
|
327
|
-
mode: attempt.mode, productSku: attempt.productSku, planSku: attempt.planSku,
|
|
328
|
-
}))
|
|
329
|
-
try {
|
|
330
|
-
const session = await stripe.checkout.sessions.create(withoutTerms(params))
|
|
331
|
-
await consumerRecordsOf(ctx).recordEvent({
|
|
332
|
-
recordId: attempt.entityId, recordKind: 'checkout', entityId: attempt.entityId, action: 'checkout-terms-fallback',
|
|
333
|
-
ok: false, externalId: session.id, detail,
|
|
334
|
-
})
|
|
335
|
-
|
|
336
|
-
return session
|
|
337
|
-
} catch (error) {
|
|
338
|
-
await consumerRecordsOf(ctx).recordEvent({
|
|
339
|
-
recordId: attempt.entityId, recordKind: 'checkout', entityId: attempt.entityId, action: 'checkout-terms-fallback',
|
|
340
|
-
ok: false, detail, error: paymentUtils.errorText(error),
|
|
341
|
-
})
|
|
342
|
-
throw error
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Create the session, then tell the plugins it exists. A plugin's `created` that throws expires
|
|
348
|
-
* the fresh session and releases every admission before the error propagates. A session Stripe
|
|
349
|
-
* refuses for the terms checkbox alone (no terms URL in the Dashboard) is created once more
|
|
350
|
-
* without it — under the same admissions.
|
|
351
|
-
*/
|
|
352
|
-
const createSession = async (
|
|
353
|
-
stripe: Stripe, plugins: readonly CheckoutPlugin[], attempt: CheckoutAttempt,
|
|
354
|
-
params: Stripe.Checkout.SessionCreateParams,
|
|
355
|
-
): Promise<string> => {
|
|
356
|
-
const admitted: Admitted[] = await checkoutPluginsOf(ctx).admitCheckout(plugins, attempt)
|
|
357
|
-
const holders = admitted.filter(entry => entry.plugin.admit != null)
|
|
358
|
-
let session: Stripe.Checkout.Session
|
|
359
|
-
try {
|
|
360
|
-
try {
|
|
361
|
-
session = await stripe.checkout.sessions.create(params)
|
|
362
|
-
} catch (error) {
|
|
363
|
-
if (params.consent_collection?.terms_of_service !== 'required' || !stripeSessionHelper.isMissingTermsUrl(error)) {
|
|
364
|
-
throw error
|
|
365
|
-
}
|
|
366
|
-
session = await createWithoutTerms(stripe, attempt, params, error)
|
|
367
|
-
}
|
|
368
|
-
} catch (error) {
|
|
369
|
-
await checkoutPluginsOf(ctx).releaseAdmitted(holders, attempt)
|
|
370
|
-
throw error
|
|
371
|
-
}
|
|
372
|
-
if (session.url == null) {
|
|
373
|
-
await checkoutPluginsOf(ctx).releaseAdmitted(holders, attempt, session.id)
|
|
374
|
-
throw new PaygateError('session')
|
|
375
|
-
}
|
|
376
|
-
for (const { plugin, reservationId } of admitted) {
|
|
377
|
-
if (plugin.created == null) {
|
|
378
|
-
continue
|
|
379
|
-
}
|
|
380
|
-
try {
|
|
381
|
-
await plugin.created(ctx, { ...attempt, sessionId: session.id, url: session.url, ...(reservationId != null ? { reservationId } : {}) })
|
|
382
|
-
} catch (error) {
|
|
383
|
-
try {
|
|
384
|
-
await stripe.checkout.sessions.expire(session.id)
|
|
385
|
-
} catch (expireError) {
|
|
386
|
-
log.error('Could not expire a checkout a plugin refused', { sessionId: session.id, error: expireError })
|
|
387
|
-
}
|
|
388
|
-
await checkoutPluginsOf(ctx).releaseAdmitted(holders, attempt, session.id)
|
|
389
|
-
throw error
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return session.url
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const createCheckoutLink = async (
|
|
397
|
-
stripe: Stripe, params: CreateLinkParams, plugins: readonly CheckoutPlugin[] = [],
|
|
398
|
-
): Promise<string> => {
|
|
399
|
-
const product = await access.payment().product(params.productSku) as PaymentProduct
|
|
400
|
-
const plans = (await access.payment().allPlans(product.sku) as PaymentPlan[])
|
|
401
|
-
.filter(plan => planHelper.isSoldThrough(product, plan, STRIPE_PAYGATE_ALIAS))
|
|
402
|
-
if (params.planSku != null && !plans.some(plan => plan.sku === params.planSku)) {
|
|
403
|
-
throw new ProductError(`plan:${params.planSku}`)
|
|
404
|
-
}
|
|
405
|
-
const consumable = product.type === ProductType.Consumable
|
|
406
|
-
const plan = consumable
|
|
407
|
-
? plans.find(item => item.sku === params.planSku) ?? plans[0]
|
|
408
|
-
: plans.find(item => item.sku === params.planSku) ?? plans.find(item => item.recurring != null) ?? plans[0]
|
|
409
|
-
if (plan == null) throw new ProductError('plan')
|
|
410
|
-
const catalogueCurrency = (plan.amountPolicy?.currency ?? plan.currency ?? 'usd').toLowerCase()
|
|
411
|
-
const { buyer, customer } = await buyerOf(stripe, params, catalogueCurrency)
|
|
412
|
-
const pricing = await access.payment().pricingPolicy()
|
|
413
|
-
const stripePricing = await access.stripePricingConfig()
|
|
414
|
-
const settlement = stripePricing?.settlementCurrency?.toLowerCase()
|
|
415
|
-
const ttl = checkoutPolicyHelper.sessionTtlOf(plugins)
|
|
416
|
-
const now = new Date()
|
|
417
|
-
const expiresAt = ttl != null ? new Date((Math.floor(now.getTime() / 1000) + ttl) * 1000) : undefined
|
|
418
|
-
const expiry = expiresAt != null ? { expires_at: Math.floor(expiresAt.getTime() / 1000) } : {}
|
|
419
|
-
const { terms, ...termsParams } = termsOptions(buyer)
|
|
420
|
-
const base = {
|
|
421
|
-
entityId: params.entityId, productSku: product.sku, planSku: plan.sku, at: now,
|
|
422
|
-
...(ttl != null ? { sessionTtlSeconds: ttl } : {}), ...(expiresAt != null ? { expiresAt } : {}),
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
if (consumable && plan.pricingMode === CheckoutPricingMode.Amount) {
|
|
426
|
-
if (params.amountMinor == null) throw new ProductError('amount')
|
|
427
|
-
const { chargeMinor: sourceChargeMinor, currency: amountCurrency } = stripeSessionHelper.amountCheckoutLineItem(
|
|
428
|
-
product, plan, params.amountMinor, pricing.tax.behavior,
|
|
429
|
-
)
|
|
430
|
-
const view = await checkoutPluginsOf(ctx).narrowAmountFor(plugins, {
|
|
431
|
-
entityId: params.entityId, productSku: product.sku, planSku: plan.sku,
|
|
432
|
-
base: plan.amountPolicy as NonNullable<PaymentPlan['amountPolicy']>, at: now,
|
|
433
|
-
})
|
|
434
|
-
checkoutPolicyHelper.assertAmountAllowed(view, params.amountMinor)
|
|
435
|
-
const charged = buyer.chargeCurrency != null
|
|
436
|
-
? await fxOf(ctx).chargeAmount(stripe, sourceChargeMinor, amountCurrency, buyer.chargeCurrency)
|
|
437
|
-
: await fxOf(ctx).settlementAmount(stripe, sourceChargeMinor, amountCurrency)
|
|
438
|
-
const adaptive = buyer.chargeCurrency == null || charged.currency === (settlement ?? amountCurrency)
|
|
439
|
-
const context = textContextOf(buyer, charged.currency, { unitAmountMinor: charged.amountMinor })
|
|
440
|
-
const submit = submitTextOf(params, context)
|
|
441
|
-
?? (buyer.policy != null && buyer.inScope && buyer.country != null
|
|
442
|
-
? consumerCopyHelper.consumerText(buyer.language, 'checkout.top-up', {
|
|
443
|
-
product: product.title, country: consumerFormatHelper.countryName(buyer.country, buyer.language),
|
|
444
|
-
})
|
|
445
|
-
: undefined)
|
|
446
|
-
const customText = paymentUtils.compact({
|
|
447
|
-
...(submit != null ? { submit: { message: limited(submit, 'submit') } } : {}),
|
|
448
|
-
...(terms != null ? { terms_of_service_acceptance: { message: terms } } : {}),
|
|
449
|
-
})
|
|
450
|
-
const lineItem: Stripe.Checkout.SessionCreateParams.LineItem = {
|
|
451
|
-
price_data: {
|
|
452
|
-
product: product.sku, currency: charged.currency, unit_amount: charged.amountMinor,
|
|
453
|
-
tax_behavior: pricing.tax.behavior,
|
|
454
|
-
},
|
|
455
|
-
quantity: 1,
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return await createSession(stripe, plugins, {
|
|
459
|
-
...base, mode: 'amount', amountMinor: params.amountMinor, amountCurrency, chargeMinor: charged.amountMinor,
|
|
460
|
-
currency: charged.currency,
|
|
461
|
-
}, {
|
|
462
|
-
mode: 'payment', line_items: [lineItem], invoice_creation: { enabled: true },
|
|
463
|
-
...checkoutOptions(pricing, stripePricing?.amountPromotionCodes === true, { locked: buyer.addressLocked, adaptive }),
|
|
464
|
-
...termsParams,
|
|
465
|
-
...(Object.keys(customText).length > 0 ? { custom_text: customText } : {}),
|
|
466
|
-
...expiry,
|
|
467
|
-
...sharedSession(customer, params, product, plan, {
|
|
468
|
-
pricingMode: CheckoutPricingMode.Amount,
|
|
469
|
-
currency: charged.currency,
|
|
470
|
-
amountCurrency,
|
|
471
|
-
amountMinor: String(params.amountMinor),
|
|
472
|
-
sourceChargeAmountMinor: String(sourceChargeMinor),
|
|
473
|
-
chargeAmountMinor: String(charged.amountMinor),
|
|
474
|
-
...consumerMetadata(buyer, params),
|
|
475
|
-
}),
|
|
476
|
-
})
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
const price = await findPrice(stripe, product.sku, planHelper.planLookupKey(product, plan))
|
|
480
|
-
const forced = buyer.chargeCurrency != null
|
|
481
|
-
? await priceCarries(product, plan, price, buyer.chargeCurrency) : { carries: false }
|
|
482
|
-
if (buyer.chargeCurrency != null && !forced.carries) {
|
|
483
|
-
log.warn('Plan has no price in the charge currency; Stripe picks the currency', { plan: plan.sku, currency: buyer.chargeCurrency })
|
|
484
|
-
}
|
|
485
|
-
const currency = forced.carries ? buyer.chargeCurrency as string : price.currency
|
|
486
|
-
const adaptive = buyer.chargeCurrency == null || currency === (settlement ?? price.currency)
|
|
487
|
-
// Forced only when the synced price carries it: a session currency the price lacks is refused.
|
|
488
|
-
const currencyParam = forced.carries ? { currency } : {}
|
|
489
|
-
|
|
490
|
-
if (consumable) {
|
|
491
|
-
const quantityPolicy = plan.quantityPolicy ?? {
|
|
492
|
-
minimum: plan.minQuantity ?? 1,
|
|
493
|
-
maximum: plan.maxQuantity ?? Math.max(100_000, plan.minQuantity ?? 1),
|
|
494
|
-
default: plan.defaultQuantity ?? plan.minQuantity ?? 1,
|
|
495
|
-
}
|
|
496
|
-
const submit = submitTextOf(params, textContextOf(buyer, currency, paymentUtils.compact({ unitAmountMinor: forced.unitAmount ?? price.unit_amount ?? undefined })))
|
|
497
|
-
const customText = paymentUtils.compact({
|
|
498
|
-
...(submit != null ? { submit: { message: limited(submit, 'submit') } } : {}),
|
|
499
|
-
...(terms != null ? { terms_of_service_acceptance: { message: terms } } : {}),
|
|
500
|
-
})
|
|
501
|
-
|
|
502
|
-
return await createSession(stripe, plugins, { ...base, mode: 'quantity', currency }, {
|
|
503
|
-
mode: 'payment',
|
|
504
|
-
line_items: [stripeSessionHelper.quantityCheckoutLineItem(price, quantityPolicy)],
|
|
505
|
-
invoice_creation: { enabled: true },
|
|
506
|
-
...currencyParam,
|
|
507
|
-
...checkoutOptions(pricing, true, { locked: buyer.addressLocked, adaptive }),
|
|
508
|
-
...termsParams,
|
|
509
|
-
...(Object.keys(customText).length > 0 ? { custom_text: customText } : {}),
|
|
510
|
-
...expiry,
|
|
511
|
-
...sharedSession(customer, params, product, plan, {
|
|
512
|
-
pricingMode: CheckoutPricingMode.Quantity,
|
|
513
|
-
...(forced.carries ? { currency } : {}),
|
|
514
|
-
...consumerMetadata(buyer, params),
|
|
515
|
-
}),
|
|
516
|
-
})
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
// A subscription starts services at once: in scope (or unknown and protected) it needs the
|
|
520
|
-
// consumer's express start request, fresh and bound to this organization and plan.
|
|
521
|
-
const start = buyer.policy != null ? await access.consumerRightsOf()?.assertStartRequest(params.entityId, plan.sku, params.startRequestId) ?? null : null
|
|
522
|
-
const subscriptionPaymentMethodTypes = (stripePricing?.subscriptionPaymentMethodTypesByCurrency?.[currency]
|
|
523
|
-
?? stripePricing?.subscriptionPaymentMethodTypes) as string[] | undefined
|
|
524
|
-
const unitAmount = forced.unitAmount ?? price.unit_amount ?? undefined
|
|
525
|
-
const interval = plan.recurring?.interval
|
|
526
|
-
const context = textContextOf(buyer, currency, paymentUtils.compact({ unitAmountMinor: unitAmount, interval }))
|
|
527
|
-
const renewal = buyer.policy != null && unitAmount != null && interval != null
|
|
528
|
-
? consumerCopyHelper.consumerText(buyer.language, `checkout.renewal.${interval}`, {
|
|
529
|
-
price: consumerCopyHelper.consumerText(buyer.language, `checkout.price.${pricing.tax.behavior === TaxBehavior.Inclusive ? 'inclusive' : 'exclusive'}`, {
|
|
530
|
-
amount: consumerFormatHelper.formatMoney(unitAmount, currency, buyer.language),
|
|
531
|
-
}),
|
|
532
|
-
})
|
|
533
|
-
: undefined
|
|
534
|
-
const submit = submitTextOf(params, context) ?? renewal
|
|
535
|
-
const links = buyer.policy != null ? consumerRightsPolicyHelper.linksOf(buyer.policy, buyer.language) : null
|
|
536
|
-
const afterSubmit = buyer.policy?.mechanisms.cancellation === true && links?.cancellation != null
|
|
537
|
-
? consumerCopyHelper.consumerText(buyer.language, 'checkout.renewal.after-submit', { cancelUrl: links.cancellation })
|
|
538
|
-
: undefined
|
|
539
|
-
const customText = paymentUtils.compact({
|
|
540
|
-
...(submit != null ? { submit: { message: limited(submit, 'submit') } } : {}),
|
|
541
|
-
...(afterSubmit != null ? { after_submit: { message: limited(afterSubmit, 'after-submit') } } : {}),
|
|
542
|
-
...(terms != null ? { terms_of_service_acceptance: { message: terms } } : {}),
|
|
543
|
-
})
|
|
544
|
-
const startRequestId = start?.id ?? undefined
|
|
545
|
-
const metadata = consumerMetadata(buyer, params, startRequestId)
|
|
546
|
-
|
|
547
|
-
return await createSession(stripe, plugins, { ...base, mode: 'subscription', currency }, {
|
|
548
|
-
mode: 'subscription', line_items: [{ price: price.id, quantity: 1 }],
|
|
549
|
-
...currencyParam,
|
|
550
|
-
...(subscriptionPaymentMethodTypes != null ? { payment_method_types: subscriptionPaymentMethodTypes } : {}),
|
|
551
|
-
...(Object.keys(customText).length > 0 ? { custom_text: customText } : {}),
|
|
552
|
-
subscription_data: {
|
|
553
|
-
metadata: {
|
|
554
|
-
pricingMode: CheckoutPricingMode.Quantity, entityId: params.entityId,
|
|
555
|
-
service: params.service, productSku: product.sku, planSku: plan.sku,
|
|
556
|
-
...(params.profileId != null ? { profileId: params.profileId } : {}),
|
|
557
|
-
...metadata,
|
|
558
|
-
},
|
|
559
|
-
},
|
|
560
|
-
...checkoutOptions(pricing, true, { locked: buyer.addressLocked, adaptive }),
|
|
561
|
-
...termsParams,
|
|
562
|
-
...expiry,
|
|
563
|
-
...sharedSession(customer, params, product, plan, metadata),
|
|
564
|
-
})
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
return { createCheckoutLink }
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
/** The Stripe Checkout of a context — one per context. */
|
|
571
|
-
export const stripeCheckoutOf = memoHelper.oncePer(makeStripeCheckoutHelper)
|