@aglyn/plugins-commerce 1.33.2 → 1.34.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 (107) hide show
  1. package/package.json +13 -13
  2. package/src/lib/components/cart.js +3 -1
  3. package/src/lib/components/cart.js.map +1 -1
  4. package/src/lib/components/console/catalog-organization-card.component.js +82 -69
  5. package/src/lib/components/console/catalog-organization-card.component.js.map +1 -1
  6. package/src/lib/components/console/discounts-card.component.js +46 -25
  7. package/src/lib/components/console/discounts-card.component.js.map +1 -1
  8. package/src/lib/components/console/gift-cards-card.component.js +81 -55
  9. package/src/lib/components/console/gift-cards-card.component.js.map +1 -1
  10. package/src/lib/components/console/host-coupons-card.component.js +36 -25
  11. package/src/lib/components/console/host-coupons-card.component.js.map +1 -1
  12. package/src/lib/components/console/locations-card.component.js +53 -32
  13. package/src/lib/components/console/locations-card.component.js.map +1 -1
  14. package/src/lib/components/console/member-posts-card.component.js +35 -32
  15. package/src/lib/components/console/member-posts-card.component.js.map +1 -1
  16. package/src/lib/components/console/new-store-tax-question.component.d.ts +17 -0
  17. package/src/lib/components/console/new-store-tax-question.component.js +197 -0
  18. package/src/lib/components/console/new-store-tax-question.component.js.map +1 -0
  19. package/src/lib/components/console/payments-settings-card.component.js +132 -5
  20. package/src/lib/components/console/payments-settings-card.component.js.map +1 -1
  21. package/src/lib/components/console/pos/pos-api.d.ts +6 -0
  22. package/src/lib/components/console/pos/pos-api.js.map +1 -1
  23. package/src/lib/components/console/pos/pos-tax-notice.component.d.ts +21 -0
  24. package/src/lib/components/console/pos/pos-tax-notice.component.js +59 -0
  25. package/src/lib/components/console/pos/pos-tax-notice.component.js.map +1 -0
  26. package/src/lib/components/console/pos-page.component.js +65 -17
  27. package/src/lib/components/console/pos-page.component.js.map +1 -1
  28. package/src/lib/components/console/registers-card.component.js +21 -13
  29. package/src/lib/components/console/registers-card.component.js.map +1 -1
  30. package/src/lib/components/console/reviews-moderation-card.component.js +108 -68
  31. package/src/lib/components/console/reviews-moderation-card.component.js.map +1 -1
  32. package/src/lib/components/console/site-member-purchases.component.js +5 -3
  33. package/src/lib/components/console/site-member-purchases.component.js.map +1 -1
  34. package/src/lib/components/console/subscription-detail-dialog.component.d.ts +2 -0
  35. package/src/lib/components/console/subscription-detail-dialog.component.js +1 -1
  36. package/src/lib/components/console/subscription-detail-dialog.component.js.map +1 -1
  37. package/src/lib/components/console/subscriptions-card.component.js +13 -5
  38. package/src/lib/components/console/subscriptions-card.component.js.map +1 -1
  39. package/src/lib/components/console/suppliers-card.component.js +47 -40
  40. package/src/lib/components/console/suppliers-card.component.js.map +1 -1
  41. package/src/lib/components/product-choice-cards.js +2 -1
  42. package/src/lib/components/product-choice-cards.js.map +1 -1
  43. package/src/lib/components/product-detail.js +9 -4
  44. package/src/lib/components/product-detail.js.map +1 -1
  45. package/src/lib/components/product-grid.js +3 -1
  46. package/src/lib/components/product-grid.js.map +1 -1
  47. package/src/lib/components/related-products.js +2 -1
  48. package/src/lib/components/related-products.js.map +1 -1
  49. package/src/lib/components/store-image-sizing.d.ts +46 -0
  50. package/src/lib/components/store-image-sizing.js +53 -0
  51. package/src/lib/components/store-image-sizing.js.map +1 -0
  52. package/src/lib/components/wishlist.js +2 -1
  53. package/src/lib/components/wishlist.js.map +1 -1
  54. package/src/lib/declarations.console-server.js +10 -0
  55. package/src/lib/declarations.console-server.js.map +1 -1
  56. package/src/lib/model/commerce-tax-step.d.ts +123 -0
  57. package/src/lib/model/commerce-tax-step.js +185 -0
  58. package/src/lib/model/commerce-tax-step.js.map +1 -0
  59. package/src/lib/model/index.d.ts +1 -0
  60. package/src/lib/model/index.js +1 -0
  61. package/src/lib/model/index.js.map +1 -1
  62. package/src/lib/model/payments-setup-status.d.ts +40 -0
  63. package/src/lib/model/payments-setup-status.js +63 -0
  64. package/src/lib/model/payments-setup-status.js.map +1 -1
  65. package/src/lib/model/subscription-view.d.ts +19 -0
  66. package/src/lib/model/subscription-view.js +16 -0
  67. package/src/lib/model/subscription-view.js.map +1 -1
  68. package/src/lib/model/tap-to-pay-launch.d.ts +103 -0
  69. package/src/lib/model/tap-to-pay-launch.js +148 -0
  70. package/src/lib/model/tap-to-pay-launch.js.map +1 -0
  71. package/src/lib/plugin.js +12 -0
  72. package/src/lib/plugin.js.map +1 -1
  73. package/src/lib/server/billing-webhook.js +140 -116
  74. package/src/lib/server/billing-webhook.js.map +1 -1
  75. package/src/lib/server/checkout.js +15 -4
  76. package/src/lib/server/checkout.js.map +1 -1
  77. package/src/lib/server/connect-return.d.ts +83 -0
  78. package/src/lib/server/connect-return.js +105 -0
  79. package/src/lib/server/connect-return.js.map +1 -0
  80. package/src/lib/server/connect.d.ts +18 -0
  81. package/src/lib/server/connect.js +60 -6
  82. package/src/lib/server/connect.js.map +1 -1
  83. package/src/lib/server/new-store-tax.d.ts +10 -0
  84. package/src/lib/server/new-store-tax.js +43 -0
  85. package/src/lib/server/new-store-tax.js.map +1 -0
  86. package/src/lib/server/pos-payment.js +3 -2
  87. package/src/lib/server/pos-payment.js.map +1 -1
  88. package/src/lib/server/pos-tax-step.d.ts +43 -0
  89. package/src/lib/server/pos-tax-step.js +55 -0
  90. package/src/lib/server/pos-tax-step.js.map +1 -0
  91. package/src/lib/server/pos-terminal-connection-token.d.ts +10 -1
  92. package/src/lib/server/pos-terminal-connection-token.js +18 -7
  93. package/src/lib/server/pos-terminal-connection-token.js.map +1 -1
  94. package/src/lib/server/pos-terminal.d.ts +9 -0
  95. package/src/lib/server/pos-terminal.js +17 -1
  96. package/src/lib/server/pos-terminal.js.map +1 -1
  97. package/src/lib/server/site-page-resolver.d.ts +8 -0
  98. package/src/lib/server/site-page-resolver.js +21 -3
  99. package/src/lib/server/site-page-resolver.js.map +1 -1
  100. package/src/lib/server/tap-to-pay-launch-admin.d.ts +18 -0
  101. package/src/lib/server/tap-to-pay-launch-admin.js +204 -0
  102. package/src/lib/server/tap-to-pay-launch-admin.js.map +1 -0
  103. package/src/lib/server/tap-to-pay-launch.d.ts +114 -0
  104. package/src/lib/server/tap-to-pay-launch.js +408 -0
  105. package/src/lib/server/tap-to-pay-launch.js.map +1 -0
  106. package/src/lib/server.js +15 -0
  107. package/src/lib/server.js.map +1 -1
@@ -1049,7 +1049,17 @@ async function findOrderForDispute(paymentIntentId) {
1049
1049
  * `metadata[feeCents]` (items only) while Stripe had charged percent-of-total,
1050
1050
  * so the same subscription's first and second cycles reported fees computed on
1051
1051
  * different bases.
1052
- */ async function chargeSubscriptionFeeOnItemsOnly(invoice, hostId, invoiceId, stripeAccount) {
1052
+ */ /**
1053
+ * A subscription record's currency fields (AGL-4257): the order's, plus the
1054
+ * difference to the store currency the subscriptions card sums for its
1055
+ * monthly recurring figure.
1056
+ */ function subscriptionCurrencyFields(fields, totals) {
1057
+ const delta = CommerceModel.subscriptionBaseTotalsDelta(totals, fields.baseTotals);
1058
+ return _extends({}, fields, delta ? {
1059
+ baseTotalsDelta: delta
1060
+ } : {});
1061
+ }
1062
+ async function chargeSubscriptionFeeOnItemsOnly(invoice, hostId, invoiceId, stripeAccount) {
1053
1063
  var _ref, _fee_amount, _fee_amount_refunded;
1054
1064
  var _fees_body_data, _fees_body;
1055
1065
  const chargedCents = Math.max(0, Math.round(Number((_ref = invoice == null ? void 0 : invoice.application_fee_amount) != null ? _ref : 0)));
@@ -2107,7 +2117,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2107
2117
  // ambiguous — $50 a month and $50 a year are the same number.
2108
2118
  lineItems: subLineItems,
2109
2119
  totals: subTotals
2110
- }, ((_liftedForSnapshot_subscription = liftedForSnapshot.subscription) == null ? void 0 : _liftedForSnapshot_subscription.interval) ? {
2120
+ }, subscriptionCurrencyFields(CommerceModel.orderCurrencyFields(object, subTotals), subTotals), ((_liftedForSnapshot_subscription = liftedForSnapshot.subscription) == null ? void 0 : _liftedForSnapshot_subscription.interval) ? {
2111
2121
  interval: liftedForSnapshot.subscription.interval
2112
2122
  } : {}, {
2113
2123
  checkoutSessionId: String(object.id)
@@ -2150,7 +2160,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2150
2160
  });
2151
2161
  if (!recorded) return;
2152
2162
  const subscriptionCents = Number((_subTotals_totalCents = subTotals.totalCents) != null ? _subTotals_totalCents : 0);
2153
- const subscriptionPrice = `$${(subscriptionCents / 100).toFixed(2)}${((_liftedForSnapshot_subscription = liftedForSnapshot.subscription) == null ? void 0 : _liftedForSnapshot_subscription.interval) ? `/${liftedForSnapshot.subscription.interval}` : ''}`;
2163
+ const subscriptionPrice = `${CommerceModel.saleMoneyText(subscriptionCents, object == null ? void 0 : object.currency)}${((_liftedForSnapshot_subscription = liftedForSnapshot.subscription) == null ? void 0 : _liftedForSnapshot_subscription.interval) ? `/${liftedForSnapshot.subscription.interval}` : ''}`;
2154
2164
  const subscriberEmail = object == null ? void 0 : (_object_customer_details = object.customer_details) == null ? void 0 : _object_customer_details.email;
2155
2165
  void notifyHostManagers(String(hostId), {
2156
2166
  type: 'content.order',
@@ -2192,7 +2202,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2192
2202
  interaction: {
2193
2203
  source: 'order',
2194
2204
  refId: String(object.subscription),
2195
- summary: `Started a subscription ($${(subscriptionCents / 100).toFixed(2)})`
2205
+ summary: `Started a subscription (${CommerceModel.saleMoneyText(subscriptionCents, object == null ? void 0 : object.currency)})`
2196
2206
  }
2197
2207
  }));
2198
2208
  }
@@ -2247,7 +2257,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2247
2257
  // instead. Self-selection is on the same discriminator every other
2248
2258
  // section of this file uses.
2249
2259
  if ((subscriptionMeta == null ? void 0 : subscriptionMeta.type) === 'commerce-subscription' && invoiceHostId && subscriptionId && invoiceId) {
2250
- var _soldSnapshot_get, _ref17, _ref18, _ref19, _ref20, _ref21;
2260
+ var _soldSnapshot_get, _ref17, _ref18, _ref19, _ref20, _ref21, _ref22;
2251
2261
  var _this, _this1, _object_status_transitions, _object_customer_shipping;
2252
2262
  const firestore = firebaseAdmin.app().firestore();
2253
2263
  const hostRef = firestore.collection('hosts').doc(invoiceHostId);
@@ -2315,7 +2325,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2315
2325
  productType: soldLine.productType
2316
2326
  } : {});
2317
2327
  } else {
2318
- var _ref22, _soldSnapshot_get1, _ref23, _ref24;
2328
+ var _ref23, _soldSnapshot_get1, _ref24, _ref25;
2319
2329
  // AGL-1763: no product id, no read. `doc('__missing__')` was a
2320
2330
  // deliberate miss that Firestore does not permit — a document id
2321
2331
  // matching `__.*__` is RESERVED, so the backend rejects the path with
@@ -2328,18 +2338,18 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2328
2338
  // exactly the population this branch now serves: a subscription
2329
2339
  // Aglyn has no record of, whose metadata nothing of ours wrote.
2330
2340
  const productSnapshot = productId ? await hostRef.collection('products').doc(productId).get() : null;
2331
- const lifted = CommerceModel.liftLegacyProduct((_ref22 = productSnapshot == null ? void 0 : productSnapshot.data()) != null ? _ref22 : {
2341
+ const lifted = CommerceModel.liftLegacyProduct((_ref23 = productSnapshot == null ? void 0 : productSnapshot.data()) != null ? _ref23 : {
2332
2342
  name: 'Subscription'
2333
2343
  });
2334
2344
  const variantId = String((_soldSnapshot_get1 = soldSnapshot.get('variantId')) != null ? _soldSnapshot_get1 : '');
2335
2345
  const variant = variantId ? lifted.variants.find((item)=>item.id === variantId) : lifted.variants[0];
2336
- const variantOptions = Object.values((_ref23 = variant == null ? void 0 : variant.options) != null ? _ref23 : {});
2346
+ const variantOptions = Object.values((_ref24 = variant == null ? void 0 : variant.options) != null ? _ref24 : {});
2337
2347
  snapshot = _extends({
2338
2348
  productId
2339
2349
  }, variantId ? {
2340
2350
  variantId
2341
2351
  } : {}, {
2342
- name: String((_ref24 = productSnapshot == null ? void 0 : productSnapshot.get('name')) != null ? _ref24 : 'Subscription')
2352
+ name: String((_ref25 = productSnapshot == null ? void 0 : productSnapshot.get('name')) != null ? _ref25 : 'Subscription')
2343
2353
  }, variantOptions.length ? {
2344
2354
  variantLabel: variantOptions.join(' / ')
2345
2355
  } : {}, (variant == null ? void 0 : variant.sku) ? {
@@ -2368,8 +2378,16 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2368
2378
  const invoiceTotals = _extends({}, computedInvoiceTotals, {
2369
2379
  feeCents: feeCentsInForce
2370
2380
  });
2371
- const paidCents = Math.max(0, Math.round(Number((_ref20 = object == null ? void 0 : object.amount_paid) != null ? _ref20 : 0)));
2372
- const billingReason = String((_ref21 = object == null ? void 0 : object.billing_reason) != null ? _ref21 : '');
2381
+ // The cycle's currency, and its store-currency figures at the rate
2382
+ // locked when the subscription was sold (AGL-4257), from the
2383
+ // subscription's own metadata. A store-currency subscription carries
2384
+ // no rate and records its currency alone, as before.
2385
+ const invoiceCurrencyFields = CommerceModel.orderCurrencyFields({
2386
+ currency: (_ref20 = object == null ? void 0 : object.currency) != null ? _ref20 : 'usd',
2387
+ metadata: subscriptionMeta
2388
+ }, invoiceTotals);
2389
+ const paidCents = Math.max(0, Math.round(Number((_ref21 = object == null ? void 0 : object.amount_paid) != null ? _ref21 : 0)));
2390
+ const billingReason = String((_ref22 = object == null ? void 0 : object.billing_reason) != null ? _ref22 : '');
2373
2391
  // The subscription's FIRST invoice is also a paid invoice, and
2374
2392
  // `checkout.session.completed` has already counted that money into the
2375
2393
  // contact's lifetime value and already told the managers about it
@@ -2434,7 +2452,12 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2434
2452
  }, (object == null ? void 0 : object.number) ? {
2435
2453
  number: String(object.number)
2436
2454
  } : {}, {
2437
- currency: String((_ref = object == null ? void 0 : object.currency) != null ? _ref : 'usd'),
2455
+ currency: String((_ref = object == null ? void 0 : object.currency) != null ? _ref : 'usd')
2456
+ }, invoiceCurrencyFields.baseCurrency ? {
2457
+ baseCurrency: invoiceCurrencyFields.baseCurrency,
2458
+ fxRate: invoiceCurrencyFields.fxRate,
2459
+ baseTotals: invoiceCurrencyFields.baseTotals
2460
+ } : {}, {
2438
2461
  paidCents,
2439
2462
  /** Stripe's own total, which a credit balance can exceed. */ invoiceTotalCents: Math.max(0, Math.round(Number((_ref1 = object == null ? void 0 : object.total) != null ? _ref1 : paidCents))),
2440
2463
  lineItems: invoiceLineItems,
@@ -2509,7 +2532,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2509
2532
  status: 'active',
2510
2533
  lineItems: invoiceLineItems,
2511
2534
  totals: invoiceTotals
2512
- }, interval ? {
2535
+ }, subscriptionCurrencyFields(invoiceCurrencyFields, invoiceTotals), interval ? {
2513
2536
  interval
2514
2537
  } : {}, {
2515
2538
  // NOT `Date.now()`. This subscription began before Aglyn knew
@@ -2579,7 +2602,8 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2579
2602
  status: 'paid',
2580
2603
  channel: 'subscription',
2581
2604
  lineItems: invoiceLineItems,
2582
- totals: invoiceTotals,
2605
+ totals: invoiceTotals
2606
+ }, invoiceCurrencyFields, {
2583
2607
  // WHICH TAX THIS CYCLE CARRIED (AGL-2451), from the invoice's own
2584
2608
  // `automatic_tax.enabled` and never from its tax lines. This is
2585
2609
  // the door the distinction matters most at: a MANUAL-mode
@@ -2646,9 +2670,9 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2646
2670
  // `renewalOrgUnreadable` and not `!renewalOrg` (AGL-2258): a null org
2647
2671
  // we successfully read still stops, which is AGL-2071's decision.
2648
2672
  if (!renewalEntitled && !renewalOrgUnreadable) {
2649
- var _ref25, _ref26;
2673
+ var _ref26, _ref27;
2650
2674
  await stopLapsedStorefrontSubscription(invoiceHostId, subscriptionId, subscriptionRef, {
2651
- subscriberEmail: (_ref25 = (_ref26 = object == null ? void 0 : object.customer_email) != null ? _ref26 : soldSnapshot.get('customerEmail')) != null ? _ref25 : null,
2675
+ subscriberEmail: (_ref26 = (_ref27 = object == null ? void 0 : object.customer_email) != null ? _ref27 : soldSnapshot.get('customerEmail')) != null ? _ref26 : null,
2652
2676
  productName: snapshot.name || 'a subscription product',
2653
2677
  org: renewalOrg != null ? renewalOrg : null
2654
2678
  }, // A direct-charge subscription lives on the seller's account.
@@ -2711,8 +2735,8 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2711
2735
  const stockVariantId = (_snapshot_variantId = snapshot.variantId) != null ? _snapshot_variantId : (_stocked_variants_ = stocked.variants[0]) == null ? void 0 : _stocked_variants_.id;
2712
2736
  const tracked = stocked.variants.some((variant)=>variant.id === stockVariantId && variant.inventory != null);
2713
2737
  if (stockVariantId && tracked) {
2714
- var _ref27;
2715
- const cycleQuantity = (_ref27 = cycleLine == null ? void 0 : cycleLine.quantity) != null ? _ref27 : 1;
2738
+ var _ref28;
2739
+ const cycleQuantity = (_ref28 = cycleLine == null ? void 0 : cycleLine.quantity) != null ? _ref28 : 1;
2716
2740
  // Atomic since AGL-2320, like every other decrement: a renewal
2717
2741
  // batch bills many subscribers at once, so two cycles of the same
2718
2742
  // physical box are exactly the concurrent pair that lost a
@@ -2737,8 +2761,8 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2737
2761
  }
2738
2762
  }
2739
2763
  if (!isOpeningInvoice && paidCents > 0) {
2740
- var _ref28, _ref29;
2741
- const renewalEmail = (_ref28 = (_ref29 = object == null ? void 0 : object.customer_email) != null ? _ref29 : soldSnapshot.get('customerEmail')) != null ? _ref28 : null;
2764
+ var _ref29, _ref30;
2765
+ const renewalEmail = (_ref29 = (_ref30 = object == null ? void 0 : object.customer_email) != null ? _ref30 : soldSnapshot.get('customerEmail')) != null ? _ref29 : null;
2742
2766
  // A physical cycle lands in Orders (AGL-1750); every cycle, digital
2743
2767
  // and service ones too, is listed under the subscription in
2744
2768
  // Products → Subscriptions (AGL-4191), which this links to.
@@ -2779,7 +2803,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2779
2803
  var _object_metadata18;
2780
2804
  const { hostId, reservationId } = (_object_metadata18 = object.metadata) != null ? _object_metadata18 : {};
2781
2805
  if (hostId && reservationId) {
2782
- var _ref30, _ref31;
2806
+ var _ref31, _ref32;
2783
2807
  var _object_metadata19;
2784
2808
  const firestore = firebaseAdmin.app().firestore();
2785
2809
  const reservationRef = firestore.collection('hosts').doc(String(hostId)).collection('reservations').doc(String(reservationId));
@@ -2792,7 +2816,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2792
2816
  // `amount_total` is stay-plus-tax and Stripe's own tax fields read
2793
2817
  // zero; the session's metadata is the only witness, exactly as it is
2794
2818
  // for a buy-now sale.
2795
- const taxCents = Math.max(0, Math.round(Number((_ref30 = object == null ? void 0 : (_object_metadata19 = object.metadata) == null ? void 0 : _object_metadata19.taxCents) != null ? _ref30 : 0)));
2819
+ const taxCents = Math.max(0, Math.round(Number((_ref31 = object == null ? void 0 : (_object_metadata19 = object.metadata) == null ? void 0 : _object_metadata19.taxCents) != null ? _ref31 : 0)));
2796
2820
  // What the guest actually handed over, MINUS that tax. This is the
2797
2821
  // DEPOSIT when the resource has one (`reserve.ts` charges
2798
2822
  // `depositCents || totalCents`), never the stay's `totalCents` — see
@@ -2806,7 +2830,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2806
2830
  // are and under-state what the merchant collects at the register. The
2807
2831
  // same figure feeds the guest's lifetime value, which tax is no part
2808
2832
  // of either (AGL-1755).
2809
- const paidCents = Math.max(0, Math.round(Number((_ref31 = object == null ? void 0 : object.amount_total) != null ? _ref31 : 0)) - taxCents);
2833
+ const paidCents = Math.max(0, Math.round(Number((_ref32 = object == null ? void 0 : object.amount_total) != null ? _ref32 : 0)) - taxCents);
2810
2834
  // Redelivery guard (AGL-1755), the AGL-1748 shape. The `pending` ->
2811
2835
  // `confirmed` transition was always the guard, but it was a
2812
2836
  // read-then-write with every side effect hanging off it, so two
@@ -2877,7 +2901,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2877
2901
  return true;
2878
2902
  });
2879
2903
  if (confirmedNow) {
2880
- var _reservation_nights, _reservation_totalCents, _ref32, _ref33, _ref34, _ref35, _ref36;
2904
+ var _reservation_nights, _reservation_totalCents, _ref33, _ref34, _ref35, _ref36, _ref37;
2881
2905
  var _object_customer_details3, _object_customer_details4, _object_customer_details5;
2882
2906
  const reservation = reserved != null ? reserved : {};
2883
2907
  // A day key, so its UTC date IS the check-in date.
@@ -2903,10 +2927,10 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2903
2927
  // property, so the guest is told it too (AGL-3432).
2904
2928
  const balanceCents = Math.max(0, Math.round(Number((_reservation_totalCents = reservation['totalCents']) != null ? _reservation_totalCents : 0)) - paidCents);
2905
2929
  const balance = `$${(balanceCents / 100).toFixed(2)}`;
2906
- const reservedBy = (_ref32 = (_ref33 = object == null ? void 0 : (_object_customer_details3 = object.customer_details) == null ? void 0 : _object_customer_details3.email) != null ? _ref33 : reservation['guestEmail']) != null ? _ref32 : 'A guest';
2930
+ const reservedBy = (_ref33 = (_ref34 = object == null ? void 0 : (_object_customer_details3 = object.customer_details) == null ? void 0 : _object_customer_details3.email) != null ? _ref34 : reservation['guestEmail']) != null ? _ref33 : 'A guest';
2907
2931
  // Which room or unit was reserved (AGL-3432): on a property with
2908
2932
  // several, "a stay" does not say which one to get ready.
2909
- const resourceName = reservation['resourceId'] ? String((_ref34 = await firestore.collection('hosts').doc(String(hostId)).collection('resources').doc(String(reservation['resourceId'])).get().then((snapshot)=>snapshot.get('name')).catch(()=>'')) != null ? _ref34 : '').trim() : '';
2933
+ const resourceName = reservation['resourceId'] ? String((_ref35 = await firestore.collection('hosts').doc(String(hostId)).collection('resources').doc(String(reservation['resourceId'])).get().then((snapshot)=>snapshot.get('name')).catch(()=>'')) != null ? _ref35 : '').trim() : '';
2910
2934
  void notifyHostManagers(String(hostId), {
2911
2935
  type: 'content.booking',
2912
2936
  title: 'New reservation on {site}',
@@ -2944,16 +2968,16 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2944
2968
  // exactly once. Taking `totalCents` here is what WOULD double-count:
2945
2969
  // it would claim money the guest has not paid yet and will pay again
2946
2970
  // through the register.
2947
- const guestContactEmail = (_ref35 = (_ref36 = object == null ? void 0 : (_object_customer_details4 = object.customer_details) == null ? void 0 : _object_customer_details4.email) != null ? _ref36 : reservation['guestEmail']) != null ? _ref35 : null;
2971
+ const guestContactEmail = (_ref36 = (_ref37 = object == null ? void 0 : (_object_customer_details4 = object.customer_details) == null ? void 0 : _object_customer_details4.email) != null ? _ref37 : reservation['guestEmail']) != null ? _ref36 : null;
2948
2972
  if (guestContactEmail) {
2949
- var _ref37, _ref38;
2973
+ var _ref38, _ref39;
2950
2974
  var _object_customer_details6;
2951
2975
  await recordCapturedContact(_extends({
2952
2976
  orgId: '',
2953
2977
  hostId: String(hostId),
2954
2978
  identity: {
2955
2979
  email: guestContactEmail,
2956
- name: (_ref37 = (_ref38 = object == null ? void 0 : (_object_customer_details6 = object.customer_details) == null ? void 0 : _object_customer_details6.name) != null ? _ref38 : reservation['guestName']) != null ? _ref37 : null
2980
+ name: (_ref38 = (_ref39 = object == null ? void 0 : (_object_customer_details6 = object.customer_details) == null ? void 0 : _object_customer_details6.name) != null ? _ref39 : reservation['guestName']) != null ? _ref38 : null
2957
2981
  },
2958
2982
  surface: 'relationship',
2959
2983
  // A paid stay is a sale (AGL-2612), whatever the source says
@@ -2971,7 +2995,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2971
2995
  }
2972
2996
  const guestEmail = object == null ? void 0 : (_object_customer_details5 = object.customer_details) == null ? void 0 : _object_customer_details5.email;
2973
2997
  if (guestEmail) {
2974
- var _reservation_nights1, _ref39;
2998
+ var _reservation_nights1, _ref40;
2975
2999
  // The balance as a whole line, empty when the stay is paid in
2976
3000
  // full so the built-in copy leaves it out.
2977
3001
  const balanceNote = balanceCents > 0 ? `Still to pay: ${balance}, at the property. It has not been charged.` : '';
@@ -2986,7 +3010,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
2986
3010
  });
2987
3011
  const emailOutcome = await sendEmail(_extends({
2988
3012
  to: guestEmail,
2989
- subject: (_ref39 = designed == null ? void 0 : designed.subject) != null ? _ref39 : 'Reservation confirmed',
3013
+ subject: (_ref40 = designed == null ? void 0 : designed.subject) != null ? _ref40 : 'Reservation confirmed',
2990
3014
  text: (designed == null ? void 0 : designed.text) || fallbackText
2991
3015
  }, (designed == null ? void 0 : designed.html) ? {
2992
3016
  html: designed.html
@@ -3013,7 +3037,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3013
3037
  var _object_metadata20;
3014
3038
  const { hostId, cartId, feeCents, couponCode } = (_object_metadata20 = object.metadata) != null ? _object_metadata20 : {};
3015
3039
  if (hostId && cartId) {
3016
- var _cartSnapshot_data, _ref40, _object_shipping_details_address_postal_code, _object_shipping_details_address_country, _ref41, _ref42, _ref43, _ref44, _ref45;
3040
+ var _cartSnapshot_data, _ref41, _object_shipping_details_address_postal_code, _object_shipping_details_address_country, _ref42, _ref43, _ref44, _ref45, _ref46;
3017
3041
  var _object_shipping_details, _object_customer_details7, _object_metadata21, _object_customer_details8, _object_customer_details9, _object_customer_details10, _object_metadata22, _this2, _object_metadata23;
3018
3042
  const firestore = firebaseAdmin.app().firestore();
3019
3043
  const hostRef = firestore.collection('hosts').doc(String(hostId));
@@ -3096,7 +3120,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3096
3120
  unitAmountCents: (_ref = chargedUnits == null ? void 0 : chargedUnits[lineIndex]) != null ? _ref : Math.round(Number((_ref1 = variant == null ? void 0 : variant.priceUsd) != null ? _ref1 : 0) * 100) + extraCents
3097
3121
  });
3098
3122
  }).filter(Boolean);
3099
- const shipping = (_ref40 = object == null ? void 0 : object.shipping_details) != null ? _ref40 : object == null ? void 0 : object.customer_details;
3123
+ const shipping = (_ref41 = object == null ? void 0 : object.shipping_details) != null ? _ref41 : object == null ? void 0 : object.customer_details;
3100
3124
  // The number the merchant knows this order by, carried out of the
3101
3125
  // transaction for the notices below (AGL-3432). The document id is
3102
3126
  // the Stripe session id, which appears nowhere in the console.
@@ -3363,10 +3387,10 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3363
3387
  // matters, and the order carries the buyer identity anyway.
3364
3388
  const licenseKeysByProduct = {};
3365
3389
  for (const line of lineItems){
3366
- var _ref46;
3390
+ var _ref47;
3367
3391
  var _object_customer_details11;
3368
3392
  if (line.productType !== 'digital') continue;
3369
- const keys = await assignLicenseKeys(firestore, hostRef, String(hostId), line.productId, String(object.id), (_ref46 = object == null ? void 0 : (_object_customer_details11 = object.customer_details) == null ? void 0 : _object_customer_details11.email) != null ? _ref46 : null, line.quantity);
3393
+ const keys = await assignLicenseKeys(firestore, hostRef, String(hostId), line.productId, String(object.id), (_ref47 = object == null ? void 0 : (_object_customer_details11 = object.customer_details) == null ? void 0 : _object_customer_details11.email) != null ? _ref47 : null, line.quantity);
3370
3394
  if (keys.length) licenseKeysByProduct[line.productId] = keys;
3371
3395
  }
3372
3396
  if (Object.keys(licenseKeysByProduct).length) {
@@ -3387,9 +3411,9 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3387
3411
  // The store's receipt switch and the status link (AGL-3610).
3388
3412
  const receiptExtras = await onlineReceiptExtras(String(hostId), String(object.id));
3389
3413
  if (isEmailConfigured() && buyerEmailForReceipt && receiptExtras.enabled) {
3390
- var _ref47, _ref48, _ref49, _ref50, _ref51;
3414
+ var _ref48, _ref49, _ref50, _ref51, _ref52;
3391
3415
  const receiptSettings = await hostRef.collection('settings').doc('store').get().catch(()=>null);
3392
- const receiptFooter = String((_ref47 = receiptSettings == null ? void 0 : receiptSettings.get('receiptFooter')) != null ? _ref47 : '');
3416
+ const receiptFooter = String((_ref48 = receiptSettings == null ? void 0 : receiptSettings.get('receiptFooter')) != null ? _ref48 : '');
3393
3417
  const linesText = lineItems.map((line)=>`${line.quantity}× ${line.name}${line.variantLabel ? ` (${line.variantLabel})` : ''} — ${saleMoney(line.unitAmountCents * line.quantity)}`).concat(// Optional lines the buyer took (AGL-3635), as charged.
3394
3418
  cartExtras.map((extra)=>`${extra.label} — $${(extra.amountCents / 100).toFixed(2)}`)).join('\n');
3395
3419
  // The keys assigned above the gate (AGL-2149); the receipt only
@@ -3404,11 +3428,11 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3404
3428
  // Digital delivery links (AGL-302); reuse the canonical mint the
3405
3429
  // tenant download endpoint verifies so the secret can never drift.
3406
3430
  const downloadToken = mintDownloadToken(hostId, String(object.id));
3407
- const siteOrigin = String((_ref48 = object == null ? void 0 : object.success_url) != null ? _ref48 : '').replace(/\/\?.*$|\?.*$/, '');
3431
+ const siteOrigin = String((_ref49 = object == null ? void 0 : object.success_url) != null ? _ref49 : '').replace(/\/\?.*$|\?.*$/, '');
3408
3432
  const downloadLines = lineItems.filter((line)=>line.productType === 'digital').map((line)=>`Download ${line.name}: ${siteOrigin}/api/commerce/download` + `?hostId=${hostId}&orderId=${object.id}` + `&productId=${line.productId}&token=${downloadToken}`).join('\n');
3409
- const orderTotal = saleMoney(Number((_ref49 = object == null ? void 0 : object.amount_total) != null ? _ref49 : 0));
3433
+ const orderTotal = saleMoney(Number((_ref50 = object == null ? void 0 : object.amount_total) != null ? _ref50 : 0));
3410
3434
  // Where to collect it, or when it is coming (AGL-3624).
3411
- const fulfillmentText = (_ref50 = cartLocalFulfillment == null ? void 0 : cartLocalFulfillment.summary) != null ? _ref50 : '';
3435
+ const fulfillmentText = (_ref51 = cartLocalFulfillment == null ? void 0 : cartLocalFulfillment.summary) != null ? _ref51 : '';
3412
3436
  const orderSummary = [
3413
3437
  linesText,
3414
3438
  licenseText,
@@ -3429,7 +3453,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3429
3453
  }, receiptExtras.tokens));
3430
3454
  const emailOutcome2 = await sendEmail(_extends({
3431
3455
  to: buyerEmailForReceipt,
3432
- subject: (_ref51 = designed == null ? void 0 : designed.subject) != null ? _ref51 : `Receipt for your order`,
3456
+ subject: (_ref52 = designed == null ? void 0 : designed.subject) != null ? _ref52 : `Receipt for your order`,
3433
3457
  text: (designed == null ? void 0 : designed.text) || fallbackText
3434
3458
  }, (designed == null ? void 0 : designed.html) ? {
3435
3459
  html: designed.html
@@ -3510,8 +3534,8 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3510
3534
  // is byte-identical to what it was before this issue for exactly that
3511
3535
  // reason. The hold refuses the SECOND shopper; it never gates the
3512
3536
  // first one's goods.
3513
- await releaseStockHold(hostRef, String((_ref41 = (_object_metadata21 = object.metadata) == null ? void 0 : _object_metadata21.stockHoldKey) != null ? _ref41 : ''));
3514
- const cartOrderTotal = saleMoney(Number((_ref42 = object == null ? void 0 : object.amount_total) != null ? _ref42 : 0));
3537
+ await releaseStockHold(hostRef, String((_ref42 = (_object_metadata21 = object.metadata) == null ? void 0 : _object_metadata21.stockHoldKey) != null ? _ref42 : ''));
3538
+ const cartOrderTotal = saleMoney(Number((_ref43 = object == null ? void 0 : object.amount_total) != null ? _ref43 : 0));
3515
3539
  const cartUnits = lineItems.reduce((sum, line)=>sum + line.quantity, 0);
3516
3540
  void notifyHostManagers(String(hostId), {
3517
3541
  type: 'content.order',
@@ -3537,27 +3561,27 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3537
3561
  hostId: String(hostId),
3538
3562
  identity: {
3539
3563
  email: object == null ? void 0 : (_object_customer_details9 = object.customer_details) == null ? void 0 : _object_customer_details9.email,
3540
- name: (_ref43 = object == null ? void 0 : (_object_customer_details10 = object.customer_details) == null ? void 0 : _object_customer_details10.name) != null ? _ref43 : null
3564
+ name: (_ref44 = object == null ? void 0 : (_object_customer_details10 = object.customer_details) == null ? void 0 : _object_customer_details10.name) != null ? _ref44 : null
3541
3565
  },
3542
3566
  surface: 'relationship',
3543
3567
  lifecycleFloor: 'customer'
3544
3568
  }, marketingOptIn ? {
3545
3569
  marketingConsent: true
3546
3570
  } : {}, {
3547
- purchaseCents: Number((_ref44 = object == null ? void 0 : object.amount_total) != null ? _ref44 : 0),
3571
+ purchaseCents: Number((_ref45 = object == null ? void 0 : object.amount_total) != null ? _ref45 : 0),
3548
3572
  interaction: {
3549
3573
  source: 'order',
3550
3574
  refId: String(object.id),
3551
- summary: `Placed an order (${saleMoney(Number((_ref45 = object == null ? void 0 : object.amount_total) != null ? _ref45 : 0))})`
3575
+ summary: `Placed an order (${saleMoney(Number((_ref46 = object == null ? void 0 : object.amount_total) != null ? _ref46 : 0))})`
3552
3576
  }
3553
3577
  }));
3554
3578
  if (couponCode) {
3555
- var _ref52;
3579
+ var _ref53;
3556
3580
  var _object_metadata24;
3557
3581
  await settleRedemption({
3558
3582
  firestore,
3559
3583
  ref: hostRef.collection('coupons').doc(String(couponCode)),
3560
- holdKey: String((_ref52 = (_object_metadata24 = object.metadata) == null ? void 0 : _object_metadata24.couponHoldKey) != null ? _ref52 : ''),
3584
+ holdKey: String((_ref53 = (_object_metadata24 = object.metadata) == null ? void 0 : _object_metadata24.couponHoldKey) != null ? _ref53 : ''),
3561
3585
  orderRef,
3562
3586
  label: `coupon ${couponCode}`,
3563
3587
  detail: `Coupon ${couponCode} was applied to this order but no longer ` + 'exists, so the redemption is uncounted against its limit.'
@@ -3565,10 +3589,10 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3565
3589
  }
3566
3590
  // Gift card balance settlement (AGL-322, made a settlement by AGL-2449).
3567
3591
  if ((_object_metadata22 = object.metadata) == null ? void 0 : _object_metadata22.giftCardCode) {
3568
- var _ref53, _ref54;
3592
+ var _ref54, _ref55;
3569
3593
  var _object_metadata25, _object_metadata26;
3570
- const giftCardCents = Number((_ref53 = (_object_metadata25 = object.metadata) == null ? void 0 : _object_metadata25.giftCardCents) != null ? _ref53 : 0);
3571
- const holdKey = String((_ref54 = (_object_metadata26 = object.metadata) == null ? void 0 : _object_metadata26.giftCardHoldKey) != null ? _ref54 : '');
3594
+ const giftCardCents = Number((_ref54 = (_object_metadata25 = object.metadata) == null ? void 0 : _object_metadata25.giftCardCents) != null ? _ref54 : 0);
3595
+ const holdKey = String((_ref55 = (_object_metadata26 = object.metadata) == null ? void 0 : _object_metadata26.giftCardHoldKey) != null ? _ref55 : '');
3572
3596
  const cardRef = hostRef.collection('giftCards').doc(String(object.metadata.giftCardCode));
3573
3597
  const orphanNote = `$${(giftCardCents / 100).toFixed(2)} was applied from gift card ` + `${object.metadata.giftCardCode}, which no longer exists. The ` + 'balance was not deducted from any card.';
3574
3598
  if (!holdKey) {
@@ -3648,7 +3672,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3648
3672
  const lineProduct = productsById.get(line.productId);
3649
3673
  if (!(lineProduct == null ? void 0 : lineProduct.giftCard)) continue;
3650
3674
  for(let unit = 0; unit < line.quantity; unit += 1){
3651
- var _ref55, _ref56;
3675
+ var _ref56, _ref57;
3652
3676
  var _object_customer_details12, _object_customer_details13, _object_customer_details14;
3653
3677
  const code = `GC-${createHmac('sha256', String(object.id)).update(`${line.productId}:${unit}:${Date.now()}`).digest('hex').slice(0, 12).toUpperCase()}`;
3654
3678
  // THE EMAIL IS GATED ON THE WRITE (AGL-2161). This `.set()` used
@@ -3667,13 +3691,13 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3667
3691
  const issued = await hostRef.collection('giftCards').doc(code).set({
3668
3692
  initialCents: line.unitAmountCents,
3669
3693
  balanceCents: line.unitAmountCents,
3670
- recipientEmail: (_ref55 = object == null ? void 0 : (_object_customer_details12 = object.customer_details) == null ? void 0 : _object_customer_details12.email) != null ? _ref55 : null,
3694
+ recipientEmail: (_ref56 = object == null ? void 0 : (_object_customer_details12 = object.customer_details) == null ? void 0 : _object_customer_details12.email) != null ? _ref56 : null,
3671
3695
  orderId: String(object.id),
3672
3696
  // Which line bought it, so a refund that withdraws that line
3673
3697
  // voids this card and no other (AGL-3363).
3674
3698
  productId: line.productId,
3675
3699
  // What the console's Gift cards search asks (AGL-3321).
3676
- searchTokens: giftCardSearchTokens(code, (_ref56 = object == null ? void 0 : (_object_customer_details13 = object.customer_details) == null ? void 0 : _object_customer_details13.email) != null ? _ref56 : null),
3700
+ searchTokens: giftCardSearchTokens(code, (_ref57 = object == null ? void 0 : (_object_customer_details13 = object.customer_details) == null ? void 0 : _object_customer_details13.email) != null ? _ref57 : null),
3677
3701
  createdAtMs: Date.now()
3678
3702
  }).then(()=>true).catch((error)=>{
3679
3703
  console.error('Gift card issuance failed', hostId, code, error);
@@ -3701,7 +3725,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3701
3725
  }
3702
3726
  const giftTo = object == null ? void 0 : (_object_customer_details14 = object.customer_details) == null ? void 0 : _object_customer_details14.email;
3703
3727
  if (giftTo) {
3704
- var _ref57;
3728
+ var _ref58;
3705
3729
  const giftValue = `$${(line.unitAmountCents / 100).toFixed(2)}`;
3706
3730
  const fallbackText = `Gift card code: ${code}\n` + `Value: ${giftValue}\n\n` + 'Enter it at checkout to apply the balance.';
3707
3731
  // Site-owner-designed template when published (AGL-771). A
@@ -3714,7 +3738,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3714
3738
  });
3715
3739
  const emailOutcome3 = await sendEmail(_extends({
3716
3740
  to: giftTo,
3717
- subject: (_ref57 = designed == null ? void 0 : designed.subject) != null ? _ref57 : 'Your gift card',
3741
+ subject: (_ref58 = designed == null ? void 0 : designed.subject) != null ? _ref58 : 'Your gift card',
3718
3742
  text: (designed == null ? void 0 : designed.text) || fallbackText
3719
3743
  }, (designed == null ? void 0 : designed.html) ? {
3720
3744
  html: designed.html
@@ -3736,12 +3760,12 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3736
3760
  }
3737
3761
  // Discounts engine redemptions (AGL-305).
3738
3762
  if ((_object_metadata23 = object.metadata) == null ? void 0 : _object_metadata23.discountId) {
3739
- var _ref58;
3763
+ var _ref59;
3740
3764
  var _object_metadata27;
3741
3765
  await settleRedemption({
3742
3766
  firestore,
3743
3767
  ref: hostRef.collection('discounts').doc(String(object.metadata.discountId)),
3744
- holdKey: String((_ref58 = (_object_metadata27 = object.metadata) == null ? void 0 : _object_metadata27.discountHoldKey) != null ? _ref58 : ''),
3768
+ holdKey: String((_ref59 = (_object_metadata27 = object.metadata) == null ? void 0 : _object_metadata27.discountHoldKey) != null ? _ref59 : ''),
3745
3769
  orderRef,
3746
3770
  label: `discount ${object.metadata.discountId}`,
3747
3771
  detail: `Discount ${object.metadata.discountId} was applied to this ` + 'order but no longer exists, so the redemption is uncounted ' + 'against its limit.'
@@ -3758,7 +3782,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3758
3782
  var _object_metadata28;
3759
3783
  const { hostId, orderId, productId } = (_object_metadata28 = object.metadata) != null ? _object_metadata28 : {};
3760
3784
  if (hostId && orderId) {
3761
- var _ref59, _ref60, _ref61;
3785
+ var _ref60, _ref61, _ref62;
3762
3786
  var _object_total_details, _object_total_details1;
3763
3787
  const firestore = firebaseAdmin.app().firestore();
3764
3788
  const hostRef = firestore.collection('hosts').doc(String(hostId));
@@ -3797,7 +3821,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3797
3821
  //
3798
3822
  // The whole map is written rather than one field of it, so a
3799
3823
  // `merge`-set's nested-map merge and a plain replace agree.
3800
- const shippingCents = Math.max(0, Math.round(Number((_ref59 = object == null ? void 0 : (_object_total_details = object.total_details) == null ? void 0 : _object_total_details.amount_shipping) != null ? _ref59 : 0) || 0));
3824
+ const shippingCents = Math.max(0, Math.round(Number((_ref60 = object == null ? void 0 : (_object_total_details = object.total_details) == null ? void 0 : _object_total_details.amount_shipping) != null ? _ref60 : 0) || 0));
3801
3825
  // Tax joins shipping in the fold (AGL-1953). `draft-order.ts` now
3802
3826
  // declares tax — a Stripe Tax Rate for a `manual` store, whose figure
3803
3827
  // it already froze into `totals`, or `automatic_tax` for a `stripe`
@@ -3810,8 +3834,8 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
3810
3834
  // `amount_tax` 0 here (its own figure stays on the order, and its
3811
3835
  // AGL-1953 witness is metadata, not this field), and a draft composed
3812
3836
  // by a store with no tax reports 0 too.
3813
- const stripeTaxCents = Math.max(0, Math.round(Number((_ref60 = object == null ? void 0 : (_object_total_details1 = object.total_details) == null ? void 0 : _object_total_details1.amount_tax) != null ? _ref60 : 0) || 0));
3814
- const chargedTotalCents = Number((_ref61 = object == null ? void 0 : object.amount_total) != null ? _ref61 : NaN);
3837
+ const stripeTaxCents = Math.max(0, Math.round(Number((_ref61 = object == null ? void 0 : (_object_total_details1 = object.total_details) == null ? void 0 : _object_total_details1.amount_tax) != null ? _ref61 : 0) || 0));
3838
+ const chargedTotalCents = Number((_ref62 = object == null ? void 0 : object.amount_total) != null ? _ref62 : NaN);
3815
3839
  // `shipping_details` ONLY, unlike the cart branch's
3816
3840
  // `?? customer_details` fallback: that address is the BILLING one, and
3817
3841
  // a card sale rung up at a register completes here too — it must not
@@ -4040,7 +4064,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4040
4064
  });
4041
4065
  }
4042
4066
  if (flipped) {
4043
- var _ref62, _ref63, _ref64, _ref65, _ref66, _ref67, _ref68;
4067
+ var _ref63, _ref64, _ref65, _ref66, _ref67, _ref68, _ref69;
4044
4068
  var _object_metadata29, _object_customer_details15, _order_totals, _object_metadata30, _object_customer_details16;
4045
4069
  const order = paidOrder;
4046
4070
  // A legacy register QR sale is a register sale completing (AGL-3607).
@@ -4066,23 +4090,23 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4066
4090
  // the hold existed, which Stripe's at-least-once delivery would run
4067
4091
  // twice. The transition is what makes it once.
4068
4092
  if ((_object_metadata29 = object.metadata) == null ? void 0 : _object_metadata29.discountId) {
4069
- var _ref69;
4093
+ var _ref70;
4070
4094
  var _object_metadata31;
4071
4095
  await settleRedemption({
4072
4096
  firestore,
4073
4097
  ref: hostRef.collection('discounts').doc(String(object.metadata.discountId)),
4074
- holdKey: String((_ref69 = (_object_metadata31 = object.metadata) == null ? void 0 : _object_metadata31.discountHoldKey) != null ? _ref69 : ''),
4098
+ holdKey: String((_ref70 = (_object_metadata31 = object.metadata) == null ? void 0 : _object_metadata31.discountHoldKey) != null ? _ref70 : ''),
4075
4099
  orderRef,
4076
4100
  label: `discount ${object.metadata.discountId}`,
4077
4101
  detail: `Discount ${object.metadata.discountId} was applied to this ` + 'order but no longer exists, so the redemption is uncounted ' + 'against its limit.'
4078
4102
  });
4079
4103
  }
4080
4104
  const draftLabel = CommerceModel.formatOrderNumber(order, String(orderId));
4081
- const draftPayer = (_ref62 = (_ref63 = object == null ? void 0 : (_object_customer_details15 = object.customer_details) == null ? void 0 : _object_customer_details15.email) != null ? _ref63 : order.customerEmail) != null ? _ref62 : null;
4082
- const draftPaidCents = Number((_ref64 = object == null ? void 0 : object.amount_total) != null ? _ref64 : 0) || Number((_ref65 = (_order_totals = order.totals) == null ? void 0 : _order_totals.totalCents) != null ? _ref65 : 0);
4105
+ const draftPayer = (_ref63 = (_ref64 = object == null ? void 0 : (_object_customer_details15 = object.customer_details) == null ? void 0 : _object_customer_details15.email) != null ? _ref64 : order.customerEmail) != null ? _ref63 : null;
4106
+ const draftPaidCents = Number((_ref65 = object == null ? void 0 : object.amount_total) != null ? _ref65 : 0) || Number((_ref66 = (_order_totals = order.totals) == null ? void 0 : _order_totals.totalCents) != null ? _ref66 : 0);
4083
4107
  const isQuotePayment = CommerceModel.isQuote(order);
4084
4108
  // A draft paid through its invoice's link (AGL-4099).
4085
- const isInvoicePayment = !isQuotePayment && String((_ref66 = object == null ? void 0 : (_object_metadata30 = object.metadata) == null ? void 0 : _object_metadata30.payLinkVersion) != null ? _ref66 : '') !== '';
4109
+ const isInvoicePayment = !isQuotePayment && String((_ref67 = object == null ? void 0 : (_object_metadata30 = object.metadata) == null ? void 0 : _object_metadata30.payLinkVersion) != null ? _ref67 : '') !== '';
4086
4110
  void notifyHostManagers(String(hostId), {
4087
4111
  type: 'content.order',
4088
4112
  title: isQuotePayment ? `Quote paid — ${CommerceModel.formatQuoteNumber(order, String(orderId))}` : isInvoicePayment ? `Invoice paid — ${draftLabel}` : `Draft order paid — ${draftLabel}`,
@@ -4102,17 +4126,17 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4102
4126
  // figure the draft was priced at, and the two agree by construction,
4103
4127
  // but only one of them is the money that moved. Stored totals remain
4104
4128
  // the fallback for a session shape that reports no total.
4105
- const draftEmail = (_ref67 = (_ref68 = object == null ? void 0 : (_object_customer_details16 = object.customer_details) == null ? void 0 : _object_customer_details16.email) != null ? _ref68 : order.customerEmail) != null ? _ref67 : null;
4129
+ const draftEmail = (_ref68 = (_ref69 = object == null ? void 0 : (_object_customer_details16 = object.customer_details) == null ? void 0 : _object_customer_details16.email) != null ? _ref69 : order.customerEmail) != null ? _ref68 : null;
4106
4130
  if (draftEmail) {
4107
- var _ref70, _ref71, _ref72, _ref73;
4131
+ var _ref71, _ref72, _ref73, _ref74;
4108
4132
  var _order_totals1, _object_customer_details17;
4109
- const chargedCents = Number((_ref70 = object == null ? void 0 : object.amount_total) != null ? _ref70 : 0) || Number((_ref71 = (_order_totals1 = order.totals) == null ? void 0 : _order_totals1.totalCents) != null ? _ref71 : 0);
4133
+ const chargedCents = Number((_ref71 = object == null ? void 0 : object.amount_total) != null ? _ref71 : 0) || Number((_ref72 = (_order_totals1 = order.totals) == null ? void 0 : _order_totals1.totalCents) != null ? _ref72 : 0);
4110
4134
  await recordCapturedContact(_extends({
4111
4135
  orgId: '',
4112
4136
  hostId: String(hostId),
4113
4137
  identity: {
4114
4138
  email: draftEmail,
4115
- name: (_ref72 = (_ref73 = object == null ? void 0 : (_object_customer_details17 = object.customer_details) == null ? void 0 : _object_customer_details17.name) != null ? _ref73 : order.customerName) != null ? _ref72 : null
4139
+ name: (_ref73 = (_ref74 = object == null ? void 0 : (_object_customer_details17 = object.customer_details) == null ? void 0 : _object_customer_details17.name) != null ? _ref74 : order.customerName) != null ? _ref73 : null
4116
4140
  },
4117
4141
  surface: 'relationship',
4118
4142
  lifecycleFloor: 'customer'
@@ -4139,15 +4163,15 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4139
4163
  key: 'paid'
4140
4164
  });
4141
4165
  if (productId) {
4142
- var _productSnapshot_data, _ref74, _ref75;
4166
+ var _productSnapshot_data, _ref75, _ref76;
4143
4167
  var _object_metadata32, _lifted_variants_, _order_lineItems_, _order_lineItems;
4144
4168
  const productRef = hostRef.collection('products').doc(String(productId));
4145
4169
  const productSnapshot = await productRef.get();
4146
4170
  const lifted = CommerceModel.liftLegacyProduct((_productSnapshot_data = productSnapshot.data()) != null ? _productSnapshot_data : {
4147
4171
  name: 'Product'
4148
4172
  });
4149
- const soldVariantId = String((_ref74 = (_object_metadata32 = object.metadata) == null ? void 0 : _object_metadata32.variantId) != null ? _ref74 : '') || ((_lifted_variants_ = lifted.variants[0]) == null ? void 0 : _lifted_variants_.id);
4150
- const quantity = (_ref75 = (_order_lineItems = order.lineItems) == null ? void 0 : (_order_lineItems_ = _order_lineItems[0]) == null ? void 0 : _order_lineItems_.quantity) != null ? _ref75 : 1;
4173
+ const soldVariantId = String((_ref75 = (_object_metadata32 = object.metadata) == null ? void 0 : _object_metadata32.variantId) != null ? _ref75 : '') || ((_lifted_variants_ = lifted.variants[0]) == null ? void 0 : _lifted_variants_.id);
4174
+ const quantity = (_ref76 = (_order_lineItems = order.lineItems) == null ? void 0 : (_order_lineItems_ = _order_lineItems[0]) == null ? void 0 : _order_lineItems_.quantity) != null ? _ref76 : 1;
4151
4175
  if (soldVariantId) {
4152
4176
  // Atomic since AGL-2320.
4153
4177
  const moved = await decrementVariantStock({
@@ -4199,10 +4223,10 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4199
4223
  // would decrement it twice.
4200
4224
  const paidProducts = new Map();
4201
4225
  for (const line of (_order_lineItems1 = order.lineItems) != null ? _order_lineItems1 : []){
4202
- var _ref76, _ref77, _line_variantId1;
4226
+ var _ref77, _ref78, _line_variantId1;
4203
4227
  var _lineProduct_variants_;
4204
- const lineProductId = String((_ref76 = line == null ? void 0 : line.productId) != null ? _ref76 : '');
4205
- const soldQty = Math.round(Number((_ref77 = line == null ? void 0 : line.quantity) != null ? _ref77 : 0));
4228
+ const lineProductId = String((_ref77 = line == null ? void 0 : line.productId) != null ? _ref77 : '');
4229
+ const soldQty = Math.round(Number((_ref78 = line == null ? void 0 : line.quantity) != null ? _ref78 : 0));
4206
4230
  if (!lineProductId || !(soldQty > 0)) continue;
4207
4231
  // Firestore reserves `__…__` ids and `.doc()` throws
4208
4232
  // synchronously on one; a corrupt line must not fail the
@@ -4265,7 +4289,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4265
4289
  var _object_metadata33;
4266
4290
  const { hostId, productId, feeCents, couponCode } = (_object_metadata33 = object.metadata) != null ? _object_metadata33 : {};
4267
4291
  if (hostId && productId) {
4268
- var _ref78, _productForSnapshot_get2, _productForSnapshot_data1, _ref79, _ref80, _object_shipping_details_address_postal_code1, _object_shipping_details_address_country1, _ref81, _ref82, _ref83, _ref84, _ref85;
4292
+ var _ref79, _productForSnapshot_get2, _productForSnapshot_data1, _ref80, _ref81, _object_shipping_details_address_postal_code1, _object_shipping_details_address_country1, _ref82, _ref83, _ref84, _ref85, _ref86;
4269
4293
  var _object_metadata34, _object_metadata35, _buyNowLineItems_, _object_shipping_details1, _object_customer_details18, _object_customer_details19, _object_customer_details20, _object_metadata36;
4270
4294
  const firestore = firebaseAdmin.app().firestore();
4271
4295
  const hostRef = firestore.collection('hosts').doc(String(hostId));
@@ -4276,7 +4300,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4276
4300
  // is bounded to Stripe's at-least-once edge).
4277
4301
  const orderRef = hostRef.collection('orders').doc(String(object.id));
4278
4302
  const counterRef = hostRef.collection('counters').doc('orders');
4279
- const amountCents = Number((_ref78 = object == null ? void 0 : object.amount_total) != null ? _ref78 : 0);
4303
+ const amountCents = Number((_ref79 = object == null ? void 0 : object.amount_total) != null ? _ref79 : 0);
4280
4304
  const productForSnapshot = await hostRef.collection('products').doc(String(productId)).get();
4281
4305
  const snapshotName = String((_productForSnapshot_get2 = productForSnapshot.get('name')) != null ? _productForSnapshot_get2 : 'Product');
4282
4306
  // AGL-1711: the line item and totals used to be fabricated from
@@ -4289,7 +4313,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4289
4313
  name: snapshotName
4290
4314
  });
4291
4315
  const soldVariant = ((_object_metadata34 = object.metadata) == null ? void 0 : _object_metadata34.variantId) ? liftedForSnapshot.variants.find((item)=>item.id === String(object.metadata.variantId)) : liftedForSnapshot.variants[0];
4292
- const variantOptions = Object.values((_ref79 = soldVariant == null ? void 0 : soldVariant.options) != null ? _ref79 : {});
4316
+ const variantOptions = Object.values((_ref80 = soldVariant == null ? void 0 : soldVariant.options) != null ? _ref80 : {});
4293
4317
  // The add-ons bought with it (AGL-4100), named from the product; their
4294
4318
  // price is already in the session's `unitAmountCents`, so a price
4295
4319
  // edit since the sale moves no money on the record.
@@ -4308,7 +4332,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4308
4332
  } : {}, liftedForSnapshot.supplierId ? {
4309
4333
  supplierId: liftedForSnapshot.supplierId
4310
4334
  } : {}));
4311
- const soldQuantity = (_ref80 = (_buyNowLineItems_ = buyNowLineItems[0]) == null ? void 0 : _buyNowLineItems_.quantity) != null ? _ref80 : 1;
4335
+ const soldQuantity = (_ref81 = (_buyNowLineItems_ = buyNowLineItems[0]) == null ? void 0 : _buyNowLineItems_.quantity) != null ? _ref81 : 1;
4312
4336
  // The number the merchant knows this order by, for the notices below
4313
4337
  // (AGL-3432).
4314
4338
  let buyNowOrderNumber;
@@ -4431,7 +4455,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4431
4455
  // In-app order notification (wave v6): host managers see sales
4432
4456
  // in the bell, not just the owner's email: what came in, from whom
4433
4457
  // and where (AGL-3432).
4434
- const buyNowTotal = saleMoney(Number((_ref81 = object == null ? void 0 : object.amount_total) != null ? _ref81 : 0));
4458
+ const buyNowTotal = saleMoney(Number((_ref82 = object == null ? void 0 : object.amount_total) != null ? _ref82 : 0));
4435
4459
  void notifyHostManagers(String(hostId), {
4436
4460
  type: 'content.order',
4437
4461
  title: `New order on {site} — ${buyNowTotal}`,
@@ -4581,15 +4605,15 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4581
4605
  hostId: String(hostId),
4582
4606
  identity: {
4583
4607
  email: object == null ? void 0 : (_object_customer_details19 = object.customer_details) == null ? void 0 : _object_customer_details19.email,
4584
- name: (_ref82 = object == null ? void 0 : (_object_customer_details20 = object.customer_details) == null ? void 0 : _object_customer_details20.name) != null ? _ref82 : null
4608
+ name: (_ref83 = object == null ? void 0 : (_object_customer_details20 = object.customer_details) == null ? void 0 : _object_customer_details20.name) != null ? _ref83 : null
4585
4609
  },
4586
4610
  surface: 'relationship',
4587
4611
  lifecycleFloor: 'customer',
4588
- purchaseCents: Number((_ref83 = object == null ? void 0 : object.amount_total) != null ? _ref83 : 0),
4612
+ purchaseCents: Number((_ref84 = object == null ? void 0 : object.amount_total) != null ? _ref84 : 0),
4589
4613
  interaction: {
4590
4614
  source: 'order',
4591
4615
  refId: String(object.id),
4592
- summary: `Placed an order (${saleMoney(Number((_ref84 = object == null ? void 0 : object.amount_total) != null ? _ref84 : 0))})`
4616
+ summary: `Placed an order (${saleMoney(Number((_ref85 = object == null ? void 0 : object.amount_total) != null ? _ref85 : 0))})`
4593
4617
  }
4594
4618
  });
4595
4619
  const productRef = hostRef.collection('products').doc(String(productId));
@@ -4607,12 +4631,12 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4607
4631
  // serialize; the snapshot above is still read, for the product NAME on
4608
4632
  // the receipt below, and no longer decides any count.
4609
4633
  {
4610
- var _productSnapshot_data2, _ref86;
4634
+ var _productSnapshot_data2, _ref87;
4611
4635
  var _object_metadata37, _lifted_variants_1;
4612
4636
  const lifted = CommerceModel.liftLegacyProduct((_productSnapshot_data2 = productSnapshot.data()) != null ? _productSnapshot_data2 : {
4613
4637
  name: 'Product'
4614
4638
  });
4615
- const soldVariantId = String((_ref86 = (_object_metadata37 = object.metadata) == null ? void 0 : _object_metadata37.variantId) != null ? _ref86 : '') || ((_lifted_variants_1 = lifted.variants[0]) == null ? void 0 : _lifted_variants_1.id);
4639
+ const soldVariantId = String((_ref87 = (_object_metadata37 = object.metadata) == null ? void 0 : _object_metadata37.variantId) != null ? _ref87 : '') || ((_lifted_variants_1 = lifted.variants[0]) == null ? void 0 : _lifted_variants_1.id);
4616
4640
  if (soldVariantId) {
4617
4641
  // AGL-1711: `-1` regardless of how many units were bought, so a
4618
4642
  // 3-unit buy-now sale decremented stock by one and the difference
@@ -4645,14 +4669,14 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4645
4669
  // Settlement, the same way round and for the same reason as the cart
4646
4670
  // branch above (AGL-2356): the decrement lands first, then the
4647
4671
  // reservation is dropped.
4648
- await releaseStockHold(hostRef, String((_ref85 = (_object_metadata36 = object.metadata) == null ? void 0 : _object_metadata36.stockHoldKey) != null ? _ref85 : ''));
4672
+ await releaseStockHold(hostRef, String((_ref86 = (_object_metadata36 = object.metadata) == null ? void 0 : _object_metadata36.stockHoldKey) != null ? _ref86 : ''));
4649
4673
  if (couponCode) {
4650
- var _ref87;
4674
+ var _ref88;
4651
4675
  var _object_metadata38;
4652
4676
  await settleRedemption({
4653
4677
  firestore,
4654
4678
  ref: hostRef.collection('coupons').doc(String(couponCode)),
4655
- holdKey: String((_ref87 = (_object_metadata38 = object.metadata) == null ? void 0 : _object_metadata38.couponHoldKey) != null ? _ref87 : ''),
4679
+ holdKey: String((_ref88 = (_object_metadata38 = object.metadata) == null ? void 0 : _object_metadata38.couponHoldKey) != null ? _ref88 : ''),
4656
4680
  orderRef,
4657
4681
  label: `coupon ${couponCode}`,
4658
4682
  detail: `Coupon ${couponCode} was applied to this order but no longer ` + 'exists, so the redemption is uncounted against its limit.'
@@ -4667,19 +4691,19 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4667
4691
  // Receipt + seller notification (AGL-96): env-gated like every
4668
4692
  // other outbound email; failures never fail the webhook.
4669
4693
  if (isEmailConfigured()) {
4670
- var _productSnapshot_get, _ref88;
4694
+ var _productSnapshot_get, _ref89;
4671
4695
  var _object_customer_details21, _org, _this3;
4672
4696
  const productName = String((_productSnapshot_get = productSnapshot.get('name')) != null ? _productSnapshot_get : 'your purchase');
4673
- const amount = (Number((_ref88 = object == null ? void 0 : object.amount_total) != null ? _ref88 : 0) / 100).toFixed(2);
4697
+ const amount = (Number((_ref89 = object == null ? void 0 : object.amount_total) != null ? _ref89 : 0) / 100).toFixed(2);
4674
4698
  const buyerEmail = object == null ? void 0 : (_object_customer_details21 = object.customer_details) == null ? void 0 : _object_customer_details21.email;
4675
4699
  const orderTotal = `$${amount}`;
4676
4700
  // The store's receipt switch and the status link (AGL-3610).
4677
4701
  const receiptExtras = await onlineReceiptExtras(String(hostId), String(object.id));
4678
4702
  if (buyerEmail && receiptExtras.enabled) {
4679
- var _ref89, _ref90;
4703
+ var _ref90, _ref91;
4680
4704
  // The store's own Receipt footer, as the cart receipt carries it
4681
4705
  // (AGL-3432).
4682
- const receiptFooter = String((_ref89 = await hostRef.collection('settings').doc('store').get().then((snapshot)=>snapshot.get('receiptFooter')).catch(()=>'')) != null ? _ref89 : '');
4706
+ const receiptFooter = String((_ref90 = await hostRef.collection('settings').doc('store').get().then((snapshot)=>snapshot.get('receiptFooter')).catch(()=>'')) != null ? _ref90 : '');
4683
4707
  // The quantity with the name (AGL-3432), so a three-unit sale
4684
4708
  // does not read as one item at the whole total.
4685
4709
  const receiptLine = `${soldQuantity}× ${productName}` + // Where to collect it, or when it is coming (AGL-3624).
@@ -4694,7 +4718,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4694
4718
  }, receiptExtras.tokens));
4695
4719
  const emailOutcome5 = await sendEmail(_extends({
4696
4720
  to: String(buyerEmail),
4697
- subject: (_ref90 = designed == null ? void 0 : designed.subject) != null ? _ref90 : `Receipt: ${productName}`,
4721
+ subject: (_ref91 = designed == null ? void 0 : designed.subject) != null ? _ref91 : `Receipt: ${productName}`,
4698
4722
  text: (designed == null ? void 0 : designed.text) || fallbackText
4699
4723
  }, (designed == null ? void 0 : designed.html) ? {
4700
4724
  html: designed.html
@@ -4729,7 +4753,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4729
4753
  // SSO.
4730
4754
  const seller = (_this4 = await findUserByUidAcrossPools(sellerUid).catch(()=>null)) == null ? void 0 : _this4.record;
4731
4755
  if (seller == null ? void 0 : seller.email) {
4732
- var _hostSnapshot_get, _ref91;
4756
+ var _hostSnapshot_get, _ref92;
4733
4757
  const siteName = String((_hostSnapshot_get = hostSnapshot.get('displayName')) != null ? _hostSnapshot_get : hostId);
4734
4758
  const fallbackText = `You made a sale on ${siteName}!\n\n${productName} — $${amount}` + (buyerEmail ? `\nBuyer: ${buyerEmail}` : '') + `\nOrder reference: ${object.id}`;
4735
4759
  // Site-owner-designed template when published (AGL-771).
@@ -4743,7 +4767,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4743
4767
  });
4744
4768
  const emailOutcome6 = await sendEmail(_extends({
4745
4769
  to: seller.email,
4746
- subject: (_ref91 = designed == null ? void 0 : designed.subject) != null ? _ref91 : `New order: ${productName}`,
4770
+ subject: (_ref92 = designed == null ? void 0 : designed.subject) != null ? _ref92 : `New order: ${productName}`,
4747
4771
  text: (designed == null ? void 0 : designed.text) || fallbackText
4748
4772
  }, (designed == null ? void 0 : designed.html) ? {
4749
4773
  html: designed.html
@@ -4774,9 +4798,9 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4774
4798
  // the refund is written on the order's timeline. Nothing else is moved: a
4775
4799
  // Dashboard refund is staff's act, and they chose its transfer reversal.
4776
4800
  if (type === 'charge.refunded') {
4777
- var _ref92, _ref93;
4801
+ var _ref93, _ref94;
4778
4802
  var _object_payment_intent, _snapshot_ref_parent_parent;
4779
- const paymentIntentId = typeof (object == null ? void 0 : object.payment_intent) === 'string' ? object.payment_intent : String((_ref92 = object == null ? void 0 : (_object_payment_intent = object.payment_intent) == null ? void 0 : _object_payment_intent.id) != null ? _ref92 : '');
4803
+ const paymentIntentId = typeof (object == null ? void 0 : object.payment_intent) === 'string' ? object.payment_intent : String((_ref93 = object == null ? void 0 : (_object_payment_intent = object.payment_intent) == null ? void 0 : _object_payment_intent.id) != null ? _ref93 : '');
4780
4804
  const lookup = paymentIntentId ? await findOrderForDispute(paymentIntentId) : {
4781
4805
  kind: 'not-ours'
4782
4806
  };
@@ -4784,20 +4808,20 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4784
4808
  const snapshot = lookup.snapshot;
4785
4809
  // The seller's copy of a destination sale's refund is not a second one.
4786
4810
  if (!connectEventOrderIsDirect(event, snapshot)) return;
4787
- const hostId = String((_ref93 = (_snapshot_ref_parent_parent = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent.id) != null ? _ref93 : '');
4811
+ const hostId = String((_ref94 = (_snapshot_ref_parent_parent = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent.id) != null ? _ref94 : '');
4788
4812
  const external = await recordExternalRefund(snapshot.ref, object);
4789
4813
  if (external && hostId) {
4790
- var _ref94, _ref95, _ref96;
4814
+ var _ref95, _ref96, _ref97;
4791
4815
  var _object_refunds;
4792
4816
  // A Stripe Dashboard refund is a refund to every subscriber too
4793
4817
  // (AGL-3645): accounting books it and the merchant's webhooks hear it,
4794
4818
  // as they do a console refund. Keyed by the charge's refunded total,
4795
4819
  // which the record above only ever advances once per amount.
4796
- const latestRefund = ((_ref94 = object == null ? void 0 : (_object_refunds = object.refunds) == null ? void 0 : _object_refunds.data) != null ? _ref94 : [])[0];
4820
+ const latestRefund = ((_ref95 = object == null ? void 0 : (_object_refunds = object.refunds) == null ? void 0 : _object_refunds.data) != null ? _ref95 : [])[0];
4797
4821
  await raiseOrderEvent(ORDER_REFUNDED_EVENT, {
4798
4822
  hostId,
4799
4823
  orderId: snapshot.id,
4800
- key: `external-refund:${String((_ref95 = object == null ? void 0 : object.id) != null ? _ref95 : paymentIntentId)}:${Number((_ref96 = object == null ? void 0 : object.amount_refunded) != null ? _ref96 : 0)}`,
4824
+ key: `external-refund:${String((_ref96 = object == null ? void 0 : object.id) != null ? _ref96 : paymentIntentId)}:${Number((_ref97 = object == null ? void 0 : object.amount_refunded) != null ? _ref97 : 0)}`,
4801
4825
  extra: {
4802
4826
  refund: {
4803
4827
  id: (latestRefund == null ? void 0 : latestRefund.id) ? String(latestRefund.id) : null,
@@ -4847,7 +4871,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4847
4871
  // merchant; both report the site so the seller's fraud ledger can name it.
4848
4872
  const risk = paymentRiskEventFrom(type, object);
4849
4873
  if (risk && risk.signal.kind !== 'dispute') {
4850
- var _ref97, _snapshot_data1, _ref98, _ref99;
4874
+ var _ref98, _snapshot_data1, _ref99, _ref100;
4851
4875
  var _snapshot_ref_parent_parent1, _order_totals2;
4852
4876
  const lookup = risk.paymentIntentId ? await findOrderForDispute(risk.paymentIntentId) : {
4853
4877
  kind: 'not-ours'
@@ -4855,9 +4879,9 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4855
4879
  if (lookup.kind !== 'order') return;
4856
4880
  const snapshot = lookup.snapshot;
4857
4881
  if (!connectEventOrderIsDirect(event, snapshot)) return;
4858
- const hostId = String((_ref97 = (_snapshot_ref_parent_parent1 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent1.id) != null ? _ref97 : '');
4882
+ const hostId = String((_ref98 = (_snapshot_ref_parent_parent1 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent1.id) != null ? _ref98 : '');
4859
4883
  const order = CommerceModel.liftLegacyOrder((_snapshot_data1 = snapshot.data()) != null ? _snapshot_data1 : {});
4860
- const orderCents = Number((_ref98 = (_ref99 = (_order_totals2 = order.totals) == null ? void 0 : _order_totals2.totalCents) != null ? _ref99 : order.amountCents) != null ? _ref98 : 0);
4884
+ const orderCents = Number((_ref99 = (_ref100 = (_order_totals2 = order.totals) == null ? void 0 : _order_totals2.totalCents) != null ? _ref100 : order.amountCents) != null ? _ref99 : 0);
4861
4885
  await recordPaymentRiskOnRecord({
4862
4886
  ref: snapshot.ref,
4863
4887
  signal: risk.signal,
@@ -4920,7 +4944,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4920
4944
  // finds). AGL-1554 will add its own `closed` branch for the marketplace
4921
4945
  // side; the two coexist because each self-selects by finding its own record.
4922
4946
  if (type === 'charge.dispute.created' || type === 'charge.dispute.closed') {
4923
- var _dispute_payment_intent, _ref100;
4947
+ var _dispute_payment_intent, _ref101;
4924
4948
  var _snapshot_ref_parent_parent2;
4925
4949
  const dispute = object != null ? object : {};
4926
4950
  const paymentIntentId = String((_dispute_payment_intent = dispute.payment_intent) != null ? _dispute_payment_intent : '');
@@ -4951,9 +4975,9 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4951
4975
  const claimed = lookup.kind === 'order' || lookup.kind === 'unresolved';
4952
4976
  const snapshot = lookup.kind === 'order' ? lookup.snapshot : null;
4953
4977
  if (snapshot) {
4954
- var _ref101;
4978
+ var _ref102;
4955
4979
  var _snapshot_ref_parent_parent3;
4956
- const hostId = String((_ref101 = (_snapshot_ref_parent_parent3 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent3.id) != null ? _ref101 : '');
4980
+ const hostId = String((_ref102 = (_snapshot_ref_parent_parent3 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent3.id) != null ? _ref102 : '');
4957
4981
  // A DIRECT-charge order was paid on the seller's own account, so the
4958
4982
  // dispute is on that account too: Stripe took the money and the dispute
4959
4983
  // fee from the seller's balance, and there is no transfer for Aglyn to
@@ -4976,7 +5000,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4976
5000
  }, notifyRiskEvent);
4977
5001
  const { opened, record } = await recordDisputeOpened(snapshot, dispute);
4978
5002
  if (opened && hostId) {
4979
- var _snapshot_data3, _record_evidenceDueByMs, _ref102;
5003
+ var _snapshot_data3, _record_evidenceDueByMs, _ref103;
4980
5004
  // Time-critical — Stripe's evidence window is days, not weeks — so
4981
5005
  // this is awaited rather than fired off with `void`: the handler is
4982
5006
  // serverless and work left running past the response is work the
@@ -4995,16 +5019,16 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
4995
5019
  },
4996
5020
  amount: `$${(record.amountCents / 100).toFixed(2)}`,
4997
5021
  evidenceDueByMs: (_record_evidenceDueByMs = record.evidenceDueByMs) != null ? _record_evidenceDueByMs : null,
4998
- dedupeKey: `sale:${String((_ref102 = dispute == null ? void 0 : dispute.id) != null ? _ref102 : snapshot.id)}`,
5022
+ dedupeKey: `sale:${String((_ref103 = dispute == null ? void 0 : dispute.id) != null ? _ref103 : snapshot.id)}`,
4999
5023
  staffEvidence: record.reason ? `Reason: ${record.reason.replace(/_/g, ' ')}.` : null
5000
5024
  });
5001
5025
  }
5002
5026
  } else {
5003
- var _ref103, _snapshot_data4, _ref104;
5027
+ var _ref104, _snapshot_data4, _ref105;
5004
5028
  // Lost: the money went back, so the cards it bought are voided. Won
5005
5029
  // (or closed on a warning): the dispute's freeze lifts; a freeze a
5006
5030
  // fraud warning or review put on stays for the merchant (AGL-3363).
5007
- await applyOrderGiftCardRisk(snapshot, String((_ref103 = dispute == null ? void 0 : dispute.status) != null ? _ref103 : '') === 'lost' ? {
5031
+ await applyOrderGiftCardRisk(snapshot, String((_ref104 = dispute == null ? void 0 : dispute.status) != null ? _ref104 : '') === 'lost' ? {
5008
5032
  kind: 'void',
5009
5033
  reason: 'dispute-lost'
5010
5034
  } : {
@@ -5078,7 +5102,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
5078
5102
  //
5079
5103
  // Never for a DIRECT charge: the shopper's bank already took the money
5080
5104
  // from the seller's own balance, and there is no transfer to reverse.
5081
- if (String((_ref104 = dispute == null ? void 0 : dispute.status) != null ? _ref104 : '') === 'lost' && !disputeOnSellerAccount) {
5105
+ if (String((_ref105 = dispute == null ? void 0 : dispute.status) != null ? _ref105 : '') === 'lost' && !disputeOnSellerAccount) {
5082
5106
  const pulledBackCents = await reverseSellerShare(snapshot.ref, dispute);
5083
5107
  // The merchant-experience half of the AGL-1794 decision, and not an
5084
5108
  // optional extra: the reversal takes money out of the connected
@@ -5108,7 +5132,7 @@ export const commerceBillingWebhookHandler = async ({ type, object, event, reque
5108
5132
  return _extends({
5109
5133
  claimed
5110
5134
  }, snapshot ? {
5111
- hostId: String((_ref100 = (_snapshot_ref_parent_parent2 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent2.id) != null ? _ref100 : '')
5135
+ hostId: String((_ref101 = (_snapshot_ref_parent_parent2 = snapshot.ref.parent.parent) == null ? void 0 : _snapshot_ref_parent_parent2.id) != null ? _ref101 : '')
5112
5136
  } : {});
5113
5137
  }
5114
5138
  };