@aglyn/plugins-commerce 1.15.0 → 1.15.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.
Files changed (87) hide show
  1. package/package.json +13 -13
  2. package/src/lib/components/cart.js +8 -3
  3. package/src/lib/components/cart.js.map +1 -1
  4. package/src/lib/components/console/payments-settings-card.component.js +1 -1
  5. package/src/lib/components/console/payments-settings-card.component.js.map +1 -1
  6. package/src/lib/components/console/subscription-detail-dialog.component.js +2 -2
  7. package/src/lib/components/console/subscription-detail-dialog.component.js.map +1 -1
  8. package/src/lib/components/console/subscriptions-card.component.js +4 -2
  9. package/src/lib/components/console/subscriptions-card.component.js.map +1 -1
  10. package/src/lib/components/product-detail.js +7 -3
  11. package/src/lib/components/product-detail.js.map +1 -1
  12. package/src/lib/components/quote-page-body.js +7 -3
  13. package/src/lib/components/quote-page-body.js.map +1 -1
  14. package/src/lib/components/related-products.js +6 -3
  15. package/src/lib/components/related-products.js.map +1 -1
  16. package/src/lib/components/storefront-payment-element.d.ts +7 -1
  17. package/src/lib/components/storefront-payment-element.js +11 -7
  18. package/src/lib/components/storefront-payment-element.js.map +1 -1
  19. package/src/lib/model/commerce-payment-methods.d.ts +26 -39
  20. package/src/lib/model/commerce-payment-methods.js +11 -118
  21. package/src/lib/model/commerce-payment-methods.js.map +1 -1
  22. package/src/lib/server/billing-webhook.d.ts +23 -0
  23. package/src/lib/server/billing-webhook.js +162 -30
  24. package/src/lib/server/billing-webhook.js.map +1 -1
  25. package/src/lib/server/cancel-order.js +4 -3
  26. package/src/lib/server/cancel-order.js.map +1 -1
  27. package/src/lib/server/cart-checkout.js +61 -14
  28. package/src/lib/server/cart-checkout.js.map +1 -1
  29. package/src/lib/server/catalog.js +58 -22
  30. package/src/lib/server/catalog.js.map +1 -1
  31. package/src/lib/server/checkout-session-expiry.d.ts +1 -1
  32. package/src/lib/server/checkout-session-expiry.js +19 -5
  33. package/src/lib/server/checkout-session-expiry.js.map +1 -1
  34. package/src/lib/server/checkout-status.js +4 -3
  35. package/src/lib/server/checkout-status.js.map +1 -1
  36. package/src/lib/server/checkout.js +42 -12
  37. package/src/lib/server/checkout.js.map +1 -1
  38. package/src/lib/server/connect.js +42 -11
  39. package/src/lib/server/connect.js.map +1 -1
  40. package/src/lib/server/draft-order.js +37 -12
  41. package/src/lib/server/draft-order.js.map +1 -1
  42. package/src/lib/server/manual-tax-rate.d.ts +11 -3
  43. package/src/lib/server/manual-tax-rate.js +10 -6
  44. package/src/lib/server/manual-tax-rate.js.map +1 -1
  45. package/src/lib/server/native-checkout.d.ts +10 -3
  46. package/src/lib/server/native-checkout.js +8 -3
  47. package/src/lib/server/native-checkout.js.map +1 -1
  48. package/src/lib/server/payment-method-domains.d.ts +50 -9
  49. package/src/lib/server/payment-method-domains.js +150 -51
  50. package/src/lib/server/payment-method-domains.js.map +1 -1
  51. package/src/lib/server/payment-methods.js +3 -2
  52. package/src/lib/server/payment-methods.js.map +1 -1
  53. package/src/lib/server/pos-order.js +5 -1
  54. package/src/lib/server/pos-order.js.map +1 -1
  55. package/src/lib/server/pos-stripe.d.ts +7 -7
  56. package/src/lib/server/pos-stripe.js +15 -13
  57. package/src/lib/server/pos-stripe.js.map +1 -1
  58. package/src/lib/server/pos-terminal.js +11 -3
  59. package/src/lib/server/pos-terminal.js.map +1 -1
  60. package/src/lib/server/quote-pay.d.ts +23 -1
  61. package/src/lib/server/quote-pay.js +112 -32
  62. package/src/lib/server/quote-pay.js.map +1 -1
  63. package/src/lib/server/quote.d.ts +2 -1
  64. package/src/lib/server/quote.js +12 -11
  65. package/src/lib/server/quote.js.map +1 -1
  66. package/src/lib/server/refund.js +13 -6
  67. package/src/lib/server/refund.js.map +1 -1
  68. package/src/lib/server/related.js +10 -3
  69. package/src/lib/server/related.js.map +1 -1
  70. package/src/lib/server/reserve.js +19 -6
  71. package/src/lib/server/reserve.js.map +1 -1
  72. package/src/lib/server/revenue-source.d.ts +6 -0
  73. package/src/lib/server/revenue-source.js +25 -9
  74. package/src/lib/server/revenue-source.js.map +1 -1
  75. package/src/lib/server/seller-stripe-account.d.ts +45 -0
  76. package/src/lib/server/seller-stripe-account.js +99 -0
  77. package/src/lib/server/seller-stripe-account.js.map +1 -0
  78. package/src/lib/server/storefront-tax-record.js +6 -1
  79. package/src/lib/server/storefront-tax-record.js.map +1 -1
  80. package/src/lib/server/storefront-tax.d.ts +33 -2
  81. package/src/lib/server/storefront-tax.js +39 -3
  82. package/src/lib/server/storefront-tax.js.map +1 -1
  83. package/src/lib/server/stripe-refund.d.ts +15 -0
  84. package/src/lib/server/stripe-refund.js +54 -26
  85. package/src/lib/server/stripe-refund.js.map +1 -1
  86. package/src/lib/server/subscription-portal.js +8 -3
  87. package/src/lib/server/subscription-portal.js.map +1 -1
@@ -30,9 +30,14 @@ import { type SiteReturnHost } from '@aglyn/aglyn/app-utils/site-return-url';
30
30
  * registered for the user running the charge." Storefront charges are
31
31
  * DESTINATION charges created by the platform with no `Stripe-Account` header
32
32
  * and no `on_behalf_of`, so the platform runs them and the platform's key
33
- * registers the domain, with no `Stripe-Account` header. A direct-charge flow
34
- * would register on the connected account instead; there is none today, and
35
- * `registerOnPlatform` is named for that reason.
33
+ * registers the domain, with no `Stripe-Account` header.
34
+ *
35
+ * A DIRECT-charge seller runs its own charges: the session is created on the
36
+ * seller's connected account, so Apple Pay and Google Pay show only on a
37
+ * domain registered ON THAT ACCOUNT (`Stripe-Account` header). Every entry
38
+ * point below takes the seller's account for that case, and the platform's
39
+ * registration is left as it is, because the seller's hardware purchases and
40
+ * any sale from before the switch still run on the platform.
36
41
  *
37
42
  * ## When
38
43
  *
@@ -54,8 +59,10 @@ import { type SiteReturnHost } from '@aglyn/aglyn/app-utils/site-return-url';
54
59
  * `paymentMethodDomains/{mode}~{domain}` records what Stripe last said, so a
55
60
  * checkout costs one document read rather than a Stripe round trip. The mode is
56
61
  * in the id because a test key and a live key register in different worlds and
57
- * a domain registered in one is not registered in the other. Server-only: the
58
- * rules deny every client.
62
+ * a domain registered in one is not registered in the other. A seller's
63
+ * registration is cached under its own id, `{mode}~{account}~{domain}`,
64
+ * because it is a different Stripe object from the platform's for the same
65
+ * name. Server-only: the rules deny every client.
59
66
  */
60
67
  export declare const PAYMENT_METHOD_DOMAINS_COLLECTION = "paymentMethodDomains";
61
68
  /** How long a recorded registration is trusted before Stripe is asked again. */
@@ -74,6 +81,11 @@ export interface PaymentMethodDomainStatus {
74
81
  export interface PaymentMethodDomainRecord {
75
82
  domain: string;
76
83
  mode: StripeMode;
84
+ /**
85
+ * The connected account the domain is registered on, for a direct-charge
86
+ * seller; absent or null for the platform's own registration.
87
+ */
88
+ stripeAccount?: string | null;
77
89
  stripeId: string | null;
78
90
  enabled: boolean;
79
91
  status: PaymentMethodDomainStatus;
@@ -109,8 +121,11 @@ export declare function stripeModeOfKey(key: string | undefined): StripeMode | n
109
121
  * must not try to register it.
110
122
  */
111
123
  export declare function registrableDomain(input: unknown): string | null;
112
- /** The cache document id for a domain in a mode. */
113
- export declare function paymentMethodDomainDocId(mode: StripeMode, domain: string): string;
124
+ /**
125
+ * The cache document id for a domain in a mode, on the platform or on one
126
+ * seller's connected account.
127
+ */
128
+ export declare function paymentMethodDomainDocId(mode: StripeMode, domain: string, stripeAccount?: string | null): string;
114
129
  /**
115
130
  * The domains a site's custom domain needs: the domain and its `www.` twin,
116
131
  * since the platform answers on both (`siteOwnHosts`).
@@ -123,6 +138,12 @@ export declare function customDomainNames(cname: unknown): string[];
123
138
  * so two checkouts racing on a fresh subdomain open one registration.
124
139
  */
125
140
  export declare function registerOnPlatform(domain: string, signal?: AbortSignal): Promise<StripeDomainObject>;
141
+ /**
142
+ * Register (or re-enable) one domain on a direct-charge seller's connected
143
+ * account: the same steps as {@link registerOnPlatform}, each sent with the
144
+ * seller's `Stripe-Account`.
145
+ */
146
+ export declare function registerOnAccount(domain: string, stripeAccount: string, signal?: AbortSignal): Promise<StripeDomainObject>;
126
147
  export interface EnsureDomainResult {
127
148
  domain: string;
128
149
  outcome: 'registered' | 'cached' | 'skipped' | 'failed';
@@ -140,6 +161,8 @@ export declare function ensurePaymentMethodDomain(rawDomain: string, options?: {
140
161
  hostId?: string | null;
141
162
  signal?: AbortSignal;
142
163
  force?: boolean;
164
+ /** Register on this direct-charge seller's account, not the platform. */
165
+ stripeAccount?: string | null;
143
166
  }): Promise<EnsureDomainResult>;
144
167
  /**
145
168
  * Disable a domain the platform no longer serves for a site. Stripe has no
@@ -147,7 +170,9 @@ export declare function ensurePaymentMethodDomain(rawDomain: string, options?: {
147
170
  * `ensurePaymentMethodDomain` re-enables the same registration if the name is
148
171
  * connected again.
149
172
  */
150
- export declare function disablePaymentMethodDomain(rawDomain: string): Promise<{
173
+ export declare function disablePaymentMethodDomain(rawDomain: string, options?: {
174
+ stripeAccount?: string | null;
175
+ }): Promise<{
151
176
  domain: string;
152
177
  outcome: 'disabled' | 'absent' | 'skipped' | 'failed';
153
178
  error?: string;
@@ -171,6 +196,11 @@ export declare function ensureCheckoutDomain(input: {
171
196
  site: SiteReturnHost | null | undefined;
172
197
  hostId: string;
173
198
  budgetMs?: number;
199
+ /**
200
+ * The seller's connected account when this checkout is a DIRECT charge:
201
+ * the session runs there, so the domain must be registered there.
202
+ */
203
+ stripeAccount?: string;
174
204
  }): Promise<EnsureDomainResult | null>;
175
205
  /** One page of the daily reconcile. */
176
206
  export declare const RECONCILE_PAGE_SIZE = 200;
@@ -188,6 +218,8 @@ export declare function reconcileCustomDomains(gate: {
188
218
  isLocked(hostId: string): Promise<boolean>;
189
219
  }, options?: {
190
220
  maxPages?: number;
221
+ /** The site's direct-charge seller account, or null (tests inject it). */
222
+ sellerAccount?: (hostId: string) => Promise<string | null>;
191
223
  }): Promise<{
192
224
  hosts: number;
193
225
  registered: number;
@@ -198,12 +230,21 @@ export declare function reconcileCustomDomains(gate: {
198
230
  export declare function onHostDomainAttached(payload: {
199
231
  hostId: string;
200
232
  domain: string;
233
+ }, options?: {
234
+ sellerAccount?: (hostId: string) => Promise<string | null>;
201
235
  }): Promise<void>;
202
236
  /** Core's `host.domain.released`: disable what no other site still serves. */
203
237
  export declare function onHostDomainReleased(payload: {
204
238
  hostId: string;
205
239
  domain: string;
240
+ }, options?: {
241
+ sellerAccount?: (hostId: string) => Promise<string | null>;
206
242
  }): Promise<void>;
207
243
  /** The registrations a settings card shows for one site. */
208
- export declare function readSiteDomainRecords(site: SiteReturnHost | null | undefined): Promise<PaymentMethodDomainRecord[]>;
244
+ export declare function readSiteDomainRecords(site: SiteReturnHost | null | undefined,
245
+ /**
246
+ * The site's direct-charge seller account: its registrations are the ones
247
+ * the site's checkout uses, so they are the ones to show.
248
+ */
249
+ stripeAccount?: string | null): Promise<PaymentMethodDomainRecord[]>;
209
250
  export {};
@@ -17,6 +17,7 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
17
17
  */ import { siteOwnHosts } from "@aglyn/aglyn/app-utils/site-return-url";
18
18
  import { TENANT_APEX } from "@aglyn/aglyn/app-utils/tenant-apex";
19
19
  import { firebaseAdmin } from "@aglyn/tenant-data-admin";
20
+ import { directSellerAccountForHost } from "./seller-stripe-account.js";
20
21
  /**
21
22
  * Stripe payment method domains for storefronts (AGL-3629).
22
23
  *
@@ -32,9 +33,14 @@ import { firebaseAdmin } from "@aglyn/tenant-data-admin";
32
33
  * registered for the user running the charge." Storefront charges are
33
34
  * DESTINATION charges created by the platform with no `Stripe-Account` header
34
35
  * and no `on_behalf_of`, so the platform runs them and the platform's key
35
- * registers the domain, with no `Stripe-Account` header. A direct-charge flow
36
- * would register on the connected account instead; there is none today, and
37
- * `registerOnPlatform` is named for that reason.
36
+ * registers the domain, with no `Stripe-Account` header.
37
+ *
38
+ * A DIRECT-charge seller runs its own charges: the session is created on the
39
+ * seller's connected account, so Apple Pay and Google Pay show only on a
40
+ * domain registered ON THAT ACCOUNT (`Stripe-Account` header). Every entry
41
+ * point below takes the seller's account for that case, and the platform's
42
+ * registration is left as it is, because the seller's hardware purchases and
43
+ * any sale from before the switch still run on the platform.
38
44
  *
39
45
  * ## When
40
46
  *
@@ -56,8 +62,10 @@ import { firebaseAdmin } from "@aglyn/tenant-data-admin";
56
62
  * `paymentMethodDomains/{mode}~{domain}` records what Stripe last said, so a
57
63
  * checkout costs one document read rather than a Stripe round trip. The mode is
58
64
  * in the id because a test key and a live key register in different worlds and
59
- * a domain registered in one is not registered in the other. Server-only: the
60
- * rules deny every client.
65
+ * a domain registered in one is not registered in the other. A seller's
66
+ * registration is cached under its own id, `{mode}~{account}~{domain}`,
67
+ * because it is a different Stripe object from the platform's for the same
68
+ * name. Server-only: the rules deny every client.
61
69
  */ export const PAYMENT_METHOD_DOMAINS_COLLECTION = 'paymentMethodDomains';
62
70
  /** How long a recorded registration is trusted before Stripe is asked again. */ export const PAYMENT_METHOD_DOMAIN_RECHECK_MS = 7 * 24 * 60 * 60 * 1000;
63
71
  /** The most a checkout waits on Stripe before selling without the domain. */ export const CHECKOUT_DOMAIN_BUDGET_MS = 2500;
@@ -79,8 +87,16 @@ import { firebaseAdmin } from "@aglyn/tenant-data-admin";
79
87
  if (!/^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(value)) return null;
80
88
  return value;
81
89
  }
82
- /** The cache document id for a domain in a mode. */ export function paymentMethodDomainDocId(mode, domain) {
83
- return `${mode}~${domain}`;
90
+ /**
91
+ * The cache document id for a domain in a mode, on the platform or on one
92
+ * seller's connected account.
93
+ */ export function paymentMethodDomainDocId(mode, domain, stripeAccount) {
94
+ const account = accountOrNull(stripeAccount);
95
+ return account ? `${mode}~${account}~${domain}` : `${mode}~${domain}`;
96
+ }
97
+ /** A usable connected-account id, or null for "the platform". */ function accountOrNull(value) {
98
+ const id = typeof value === 'string' ? value.trim() : '';
99
+ return /^acct_[A-Za-z0-9]+$/.test(id) ? id : null;
84
100
  }
85
101
  /**
86
102
  * The domains a site's custom domain needs: the domain and its `www.` twin,
@@ -115,6 +131,8 @@ async function stripeRequest(method, path, params, options = {}) {
115
131
  Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`
116
132
  }, method === 'POST' && {
117
133
  'Content-Type': 'application/x-www-form-urlencoded'
134
+ }, options.stripeAccount && {
135
+ 'Stripe-Account': options.stripeAccount
118
136
  }, options.idempotencyKey && {
119
137
  'Idempotency-Key': options.idempotencyKey
120
138
  })
@@ -131,14 +149,15 @@ async function stripeRequest(method, path, params, options = {}) {
131
149
  }
132
150
  return payload;
133
151
  }
134
- async function findOnStripe(domain, signal) {
152
+ async function findOnStripe(domain, signal, stripeAccount) {
135
153
  var _ref;
136
154
  var _list_data;
137
155
  const list = await stripeRequest('GET', 'payment_method_domains', {
138
156
  domain_name: domain,
139
157
  limit: '1'
140
158
  }, {
141
- signal
159
+ signal,
160
+ stripeAccount
142
161
  });
143
162
  return (_ref = (_list_data = list.data) == null ? void 0 : _list_data.find((entry)=>entry.domain_name === domain)) != null ? _ref : null;
144
163
  }
@@ -148,13 +167,31 @@ async function findOnStripe(domain, signal) {
148
167
  * is enabled rather than duplicated, and the create carries an idempotency key
149
168
  * so two checkouts racing on a fresh subdomain open one registration.
150
169
  */ export async function registerOnPlatform(domain, signal) {
151
- const existing = await findOnStripe(domain, signal);
170
+ return registerDomain(domain, {
171
+ signal
172
+ });
173
+ }
174
+ /**
175
+ * Register (or re-enable) one domain on a direct-charge seller's connected
176
+ * account: the same steps as {@link registerOnPlatform}, each sent with the
177
+ * seller's `Stripe-Account`.
178
+ */ export async function registerOnAccount(domain, stripeAccount, signal) {
179
+ return registerDomain(domain, {
180
+ signal,
181
+ stripeAccount
182
+ });
183
+ }
184
+ async function registerDomain(domain, options) {
185
+ const { signal } = options;
186
+ const stripeAccount = accountOrNull(options.stripeAccount);
187
+ const existing = await findOnStripe(domain, signal, stripeAccount);
152
188
  if (existing) {
153
189
  if (existing.enabled) return existing;
154
190
  return stripeRequest('POST', `payment_method_domains/${encodeURIComponent(existing.id)}`, {
155
191
  enabled: 'true'
156
192
  }, {
157
- signal
193
+ signal,
194
+ stripeAccount
158
195
  });
159
196
  }
160
197
  try {
@@ -162,24 +199,31 @@ async function findOnStripe(domain, signal) {
162
199
  domain_name: domain,
163
200
  enabled: 'true'
164
201
  }, {
165
- idempotencyKey: `aglyn-pmd-register:${domain}`,
166
- signal
202
+ // Stripe scopes the key per account; the account is in it anyway so
203
+ // a log line names which registration it was.
204
+ idempotencyKey: stripeAccount ? `aglyn-pmd-register:${stripeAccount}:${domain}` : `aglyn-pmd-register:${domain}`,
205
+ signal,
206
+ stripeAccount
167
207
  });
168
208
  } catch (error) {
169
209
  // Lost a race to a create that landed between the lookup and this call:
170
210
  // the registration exists, which is what was wanted.
171
- const raced = await findOnStripe(domain, signal).catch(()=>null);
211
+ const raced = await findOnStripe(domain, signal, stripeAccount).catch(()=>null);
172
212
  if (raced) return raced;
173
213
  throw error;
174
214
  }
175
215
  }
176
- function docRef(mode, domain) {
177
- return firebaseAdmin.app().firestore().collection(PAYMENT_METHOD_DOMAINS_COLLECTION).doc(paymentMethodDomainDocId(mode, domain));
216
+ function docRef(mode, domain, stripeAccount) {
217
+ return firebaseAdmin.app().firestore().collection(PAYMENT_METHOD_DOMAINS_COLLECTION).doc(paymentMethodDomainDocId(mode, domain, stripeAccount));
178
218
  }
179
- async function record(mode, domain, patch) {
180
- await docRef(mode, domain).set(_extends({
219
+ async function record(mode, domain, patch, stripeAccount) {
220
+ const account = accountOrNull(stripeAccount);
221
+ await docRef(mode, domain, account).set(_extends({
181
222
  domain,
182
- mode,
223
+ mode
224
+ }, account ? {
225
+ stripeAccount: account
226
+ } : {}, {
183
227
  checkedAtMs: Date.now()
184
228
  }, patch), {
185
229
  merge: true
@@ -200,11 +244,12 @@ async function record(mode, domain, patch) {
200
244
  outcome: 'skipped'
201
245
  };
202
246
  }
247
+ const account = accountOrNull(options.stripeAccount);
203
248
  try {
204
249
  var _options_hostId;
205
250
  if (!options.force) {
206
251
  var _data_checkedAtMs;
207
- const cached = await docRef(mode, domain).get();
252
+ const cached = await docRef(mode, domain, account).get();
208
253
  const data = cached.data == null ? void 0 : cached.data.call(cached);
209
254
  if (cached.exists && (data == null ? void 0 : data.enabled) === true && data.stripeId && Date.now() - Number((_data_checkedAtMs = data.checkedAtMs) != null ? _data_checkedAtMs : 0) < PAYMENT_METHOD_DOMAIN_RECHECK_MS) {
210
255
  return {
@@ -214,18 +259,21 @@ async function record(mode, domain, patch) {
214
259
  };
215
260
  }
216
261
  }
217
- const object = await registerOnPlatform(domain, options.signal);
218
- const next = {
262
+ const object = account ? await registerOnAccount(domain, account, options.signal) : await registerOnPlatform(domain, options.signal);
263
+ const next = _extends({
219
264
  domain,
220
- mode,
265
+ mode
266
+ }, account ? {
267
+ stripeAccount: account
268
+ } : {}, {
221
269
  stripeId: object.id,
222
270
  enabled: object.enabled !== false,
223
271
  status: statusOf(object),
224
272
  hostId: (_options_hostId = options.hostId) != null ? _options_hostId : null,
225
273
  checkedAtMs: Date.now(),
226
274
  lastError: null
227
- };
228
- await record(mode, domain, next);
275
+ });
276
+ await record(mode, domain, next, account);
229
277
  return {
230
278
  domain,
231
279
  outcome: 'registered',
@@ -238,7 +286,7 @@ async function record(mode, domain, patch) {
238
286
  enabled: false,
239
287
  hostId: (_options_hostId1 = options.hostId) != null ? _options_hostId1 : null,
240
288
  lastError: message.slice(0, 500)
241
- }).catch(()=>undefined);
289
+ }, account).catch(()=>undefined);
242
290
  return {
243
291
  domain,
244
292
  outcome: 'failed',
@@ -251,21 +299,22 @@ async function record(mode, domain, patch) {
251
299
  * delete for a payment method domain; disabling stops every method on it, and
252
300
  * `ensurePaymentMethodDomain` re-enables the same registration if the name is
253
301
  * connected again.
254
- */ export async function disablePaymentMethodDomain(rawDomain) {
302
+ */ export async function disablePaymentMethodDomain(rawDomain, options = {}) {
255
303
  const domain = registrableDomain(rawDomain);
256
304
  const mode = stripeModeOfKey(process.env.STRIPE_SECRET_KEY);
257
305
  if (!domain || !mode) return {
258
306
  domain: String(rawDomain != null ? rawDomain : ''),
259
307
  outcome: 'skipped'
260
308
  };
309
+ const account = accountOrNull(options.stripeAccount);
261
310
  try {
262
- const existing = await findOnStripe(domain);
311
+ const existing = await findOnStripe(domain, undefined, account);
263
312
  if (!existing) {
264
313
  await record(mode, domain, {
265
314
  enabled: false,
266
315
  stripeId: null,
267
316
  lastError: null
268
- });
317
+ }, account);
269
318
  return {
270
319
  domain,
271
320
  outcome: 'absent'
@@ -273,13 +322,15 @@ async function record(mode, domain, patch) {
273
322
  }
274
323
  const object = existing.enabled ? await stripeRequest('POST', `payment_method_domains/${encodeURIComponent(existing.id)}`, {
275
324
  enabled: 'false'
325
+ }, {
326
+ stripeAccount: account
276
327
  }) : existing;
277
328
  await record(mode, domain, {
278
329
  stripeId: object.id,
279
330
  enabled: false,
280
331
  status: statusOf(object),
281
332
  lastError: null
282
- });
333
+ }, account);
283
334
  return {
284
335
  domain,
285
336
  outcome: 'disabled'
@@ -334,10 +385,12 @@ async function record(mode, domain, patch) {
334
385
  const budget = (_input_budgetMs = input.budgetMs) != null ? _input_budgetMs : CHECKOUT_DOMAIN_BUDGET_MS;
335
386
  const timer = setTimeout(()=>controller.abort(), budget);
336
387
  try {
337
- return await ensurePaymentMethodDomain(hostname, {
388
+ return await ensurePaymentMethodDomain(hostname, _extends({
338
389
  hostId: input.hostId,
339
390
  signal: controller.signal
340
- });
391
+ }, input.stripeAccount ? {
392
+ stripeAccount: input.stripeAccount
393
+ } : {}));
341
394
  } finally{
342
395
  clearTimeout(timer);
343
396
  }
@@ -353,7 +406,8 @@ async function record(mode, domain, patch) {
353
406
  * Paged by `cname` with a cursor so it reads every site once and stops; the
354
407
  * `cname > ''` range uses Firestore's automatic single-field index.
355
408
  */ export async function reconcileCustomDomains(gate, options = {}) {
356
- var _options_maxPages;
409
+ var _options_sellerAccount, _options_maxPages;
410
+ const sellerAccount = (_options_sellerAccount = options.sellerAccount) != null ? _options_sellerAccount : directSellerAccountForHost;
357
411
  const result = {
358
412
  hosts: 0,
359
413
  registered: 0,
@@ -379,12 +433,24 @@ async function record(mode, domain, patch) {
379
433
  result.skippedLocked += 1;
380
434
  continue;
381
435
  }
436
+ // A direct-charge seller runs its own charges, so its domains are
437
+ // registered on its account as well as the platform's.
438
+ const account = await sellerAccount(hostDoc.id).catch(()=>null);
382
439
  for (const domain of customDomainNames(hostDoc.get('cname'))){
383
- const outcome = await ensurePaymentMethodDomain(domain, {
384
- hostId: hostDoc.id
385
- });
386
- if (outcome.outcome === 'registered') result.registered += 1;
387
- if (outcome.outcome === 'failed') result.failed += 1;
440
+ for (const stripeAccount of account ? [
441
+ null,
442
+ account
443
+ ] : [
444
+ null
445
+ ]){
446
+ const outcome = await ensurePaymentMethodDomain(domain, _extends({
447
+ hostId: hostDoc.id
448
+ }, stripeAccount ? {
449
+ stripeAccount
450
+ } : {}));
451
+ if (outcome.outcome === 'registered') result.registered += 1;
452
+ if (outcome.outcome === 'failed') result.failed += 1;
453
+ }
388
454
  }
389
455
  }
390
456
  cursor = String((_ref = (_snapshot_docs_ = snapshot.docs[snapshot.docs.length - 1]) == null ? void 0 : _snapshot_docs_.get('cname')) != null ? _ref : '');
@@ -392,22 +458,48 @@ async function record(mode, domain, patch) {
392
458
  }
393
459
  return result;
394
460
  }
395
- /** Core's `host.domain.attached`: register the custom domain and its twin. */ export async function onHostDomainAttached(payload) {
461
+ /** Core's `host.domain.attached`: register the custom domain and its twin. */ export async function onHostDomainAttached(payload, options = {}) {
462
+ var _options_sellerAccount;
463
+ // The platform's registration as before, and the seller's own as well when
464
+ // the site sells by direct charges, since those sessions run on its account.
465
+ const account = await ((_options_sellerAccount = options.sellerAccount) != null ? _options_sellerAccount : directSellerAccountForHost)(payload.hostId).catch(()=>null);
396
466
  for (const domain of customDomainNames(payload.domain)){
397
- const outcome = await ensurePaymentMethodDomain(domain, {
398
- hostId: payload.hostId,
399
- force: true
400
- });
401
- if (outcome.outcome === 'failed') {
402
- console.warn(`[commerce] payment method domain ${domain} not registered: ${outcome.error}`);
467
+ for (const stripeAccount of account ? [
468
+ null,
469
+ account
470
+ ] : [
471
+ null
472
+ ]){
473
+ const outcome = await ensurePaymentMethodDomain(domain, _extends({
474
+ hostId: payload.hostId,
475
+ force: true
476
+ }, stripeAccount ? {
477
+ stripeAccount
478
+ } : {}));
479
+ if (outcome.outcome === 'failed') {
480
+ console.warn(`[commerce] payment method domain ${domain}${stripeAccount ? ` on ${stripeAccount}` : ''} not registered: ${outcome.error}`);
481
+ }
403
482
  }
404
483
  }
405
484
  }
406
- /** Core's `host.domain.released`: disable what no other site still serves. */ export async function onHostDomainReleased(payload) {
485
+ /** Core's `host.domain.released`: disable what no other site still serves. */ export async function onHostDomainReleased(payload, options = {}) {
486
+ var _options_sellerAccount;
407
487
  const names = payload.domain.endsWith(`.${TENANT_APEX}`) ? [
408
488
  registrableDomain(payload.domain)
409
489
  ].filter((name)=>Boolean(name)) : customDomainNames(payload.domain);
490
+ const account = await ((_options_sellerAccount = options.sellerAccount) != null ? _options_sellerAccount : directSellerAccountForHost)(payload.hostId).catch(()=>null);
410
491
  for (const domain of names){
492
+ // The PLATFORM's registration is shared by every site that answers on
493
+ // the name, so it waits until no other site does. The seller's own is
494
+ // that seller's alone, and goes with this site's release.
495
+ if (account) {
496
+ const onSeller = await disablePaymentMethodDomain(domain, {
497
+ stripeAccount: account
498
+ });
499
+ if (onSeller.outcome === 'failed') {
500
+ console.warn(`[commerce] payment method domain ${domain} on ${account} not disabled: ${onSeller.error}`);
501
+ }
502
+ }
411
503
  if (await domainStillClaimed(domain, payload.hostId)) continue;
412
504
  const outcome = await disablePaymentMethodDomain(domain);
413
505
  if (outcome.outcome === 'failed') {
@@ -415,16 +507,23 @@ async function record(mode, domain, patch) {
415
507
  }
416
508
  }
417
509
  }
418
- /** The registrations a settings card shows for one site. */ export async function readSiteDomainRecords(site) {
510
+ /** The registrations a settings card shows for one site. */ export async function readSiteDomainRecords(site, /**
511
+ * The site's direct-charge seller account: its registrations are the ones
512
+ * the site's checkout uses, so they are the ones to show.
513
+ */ stripeAccount) {
419
514
  const mode = stripeModeOfKey(process.env.STRIPE_SECRET_KEY);
420
515
  if (!mode) return [];
516
+ const account = accountOrNull(stripeAccount);
421
517
  const names = siteOwnHosts(site).filter((name)=>registrableDomain(name));
422
- const snapshots = await Promise.all(names.map((name)=>docRef(mode, name).get()));
518
+ const snapshots = await Promise.all(names.map((name)=>docRef(mode, name, account).get()));
423
519
  return snapshots.map((snapshot, index)=>{
424
520
  const data = snapshot.exists ? snapshot.data == null ? void 0 : snapshot.data.call(snapshot) : null;
425
- return data != null ? data : {
521
+ return data != null ? data : _extends({
426
522
  domain: names[index],
427
- mode,
523
+ mode
524
+ }, account ? {
525
+ stripeAccount: account
526
+ } : {}, {
428
527
  stripeId: null,
429
528
  enabled: false,
430
529
  status: {
@@ -437,7 +536,7 @@ async function record(mode, domain, patch) {
437
536
  hostId: null,
438
537
  checkedAtMs: 0,
439
538
  lastError: null
440
- };
539
+ });
441
540
  });
442
541
  }
443
542