@revenexx/cover 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/components/cart/recommend/CartCrossSell.vue +2 -1
- package/app/components/listing/ListingFilterBar.vue +1 -0
- package/app/components/listing/ListingFilterDrawer.vue +1 -0
- package/app/components/listing/ListingFilterRail.vue +1 -0
- package/app/components/search/filter/SearchFacets.vue +10 -3
- package/app/components/search/filter/SearchFacetsBar.vue +7 -3
- package/app/components/search/filter/SearchFacetsDrawer.vue +7 -2
- package/app/components/ui/forms/SearchInput.vue +4 -1
- package/app/composables/useFacetMeta.ts +6 -6
- package/app/composables/useProductListing.ts +18 -2
- package/app/composables/useSearchListing.ts +16 -0
- package/app/shared/locale.ts +114 -0
- package/nuxt.config.ts +22 -0
- package/package.json +1 -1
- package/server/api/account/quotes/[id]/order.post.ts +14 -3
- package/server/api/cart/calculate.post.ts +4 -0
- package/server/api/categories/[slug]/subcategories.get.ts +34 -31
- package/server/api/orders/index.post.ts +78 -31
- package/server/api/payment/psp-config.get.ts +11 -21
- package/server/api/quotes/request.post.ts +2 -2
- package/server/api/typesense/search.post.ts +61 -317
- package/server/api/typesense/suggest.post.ts +14 -9
- package/server/{api/typesense/drop.get.ts → dev/typesense/drop.ts} +2 -1
- package/server/{api/typesense/health.get.ts → dev/typesense/health.ts} +1 -0
- package/server/dev/typesense/seed.ts +59 -0
- package/server/dev/typesense/seedDocuments.ts +105 -0
- package/server/routes/punchout/start.get.ts +40 -3
- package/server/services/ApiBrandService.ts +28 -42
- package/server/services/ApiProductService.ts +10 -11
- package/server/utils/buyerIdentity.ts +11 -0
- package/server/utils/liveCatalog.ts +132 -49
- package/server/utils/liveListingSearch.ts +201 -0
- package/server/utils/livePrices.ts +24 -9
- package/server/utils/orderLinePrices.ts +72 -0
- package/server/utils/paymentFee.ts +59 -0
- package/server/utils/pspConfig.ts +42 -0
- package/server/utils/revenexxSdk.ts +25 -2
- package/server/utils/searchClient.ts +305 -0
- package/server/utils/searchMultiSearchFallback.ts +216 -0
- package/server/api/typesense/seed.get.ts +0 -111
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { PriceRange, SearchFacet } from "../../app/interfaces/search-facets";
|
|
2
|
+
import type { LiveSubcategoryIndex } from "./liveCatalog";
|
|
3
|
+
import {
|
|
4
|
+
liveCategorySubtreeCodes,
|
|
5
|
+
liveSubcategoryIndex,
|
|
6
|
+
liveTopCategoryIndex,
|
|
7
|
+
rollUpSubcategoryCounts,
|
|
8
|
+
} from "./liveCatalog";
|
|
9
|
+
import type { Locale } from "./locale";
|
|
10
|
+
import type { SearchClientResult, SearchFilters } from "./searchClient";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The bridge between the storefront's listing vocabulary and the live index.
|
|
14
|
+
*
|
|
15
|
+
* The listing UI was built against the demo schema: it filters and facets on
|
|
16
|
+
* `category` / `subcategory` (labels), `categorySlug` / `subcategorySlug`
|
|
17
|
+
* (codes), `manufacturer`, `available` and `attrs.*`. The live index knows
|
|
18
|
+
* `categories` (leaf codes), `manufacturer`, `kind` and `attrs.*`. This module
|
|
19
|
+
* translates the request one way and the facets the other, and nothing else:
|
|
20
|
+
* which fields a query matches and which facets exist is the search service's
|
|
21
|
+
* business (searchClient.ts).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Price filters are in euros in the URL and in cents in the index. */
|
|
25
|
+
const PRICE_FIELD = "price";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A label-valued facet value back to its code. An unmatched value is passed
|
|
29
|
+
* through as a code, so a hand-edited URL yields an honest zero-hit filter
|
|
30
|
+
* rather than silently dropping the constraint.
|
|
31
|
+
*/
|
|
32
|
+
function codesForLabels(values: readonly string[], labels: ReadonlyMap<string, string>): string[] {
|
|
33
|
+
return values.map((value) => {
|
|
34
|
+
if (labels.has(value)) {
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
return [...labels.entries()].find(([, label]) => label === value)?.[0] ?? value;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LiveListingRequest {
|
|
42
|
+
/** Structured filters for the query endpoint. */
|
|
43
|
+
readonly filters: SearchFilters;
|
|
44
|
+
/** The category the listing is pinned to ("" on the search page). */
|
|
45
|
+
readonly pinnedCategoryCode: string;
|
|
46
|
+
/** Whether the visitor asked for a price range. */
|
|
47
|
+
readonly priceRequested: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The listing's filters as the query endpoint takes them.
|
|
52
|
+
*
|
|
53
|
+
* Category and subcategory are collected SEPARATELY and the narrower wins: a
|
|
54
|
+
* subcategory replaces its parent's clause rather than being OR'd next to it.
|
|
55
|
+
* Products carry their LEAF category only, so a branch expands to every code
|
|
56
|
+
* beneath it however deep the tree runs.
|
|
57
|
+
*/
|
|
58
|
+
export async function buildLiveListingFilters(
|
|
59
|
+
filters: Readonly<Record<string, readonly string[] | undefined>> | undefined,
|
|
60
|
+
priceRange: PriceRange | undefined,
|
|
61
|
+
locale: Locale,
|
|
62
|
+
): Promise<LiveListingRequest> {
|
|
63
|
+
const out: Record<string, SearchFilters[string]> = {};
|
|
64
|
+
const categoryCodes: string[] = [];
|
|
65
|
+
const categoryLabels: string[] = [];
|
|
66
|
+
const subcategoryValues: string[] = [];
|
|
67
|
+
|
|
68
|
+
for (const [field, values] of Object.entries(filters ?? {})) {
|
|
69
|
+
if (!values?.length) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (field === "categorySlug" || field === "categories") {
|
|
73
|
+
categoryCodes.push(...values);
|
|
74
|
+
}
|
|
75
|
+
else if (field === "category") {
|
|
76
|
+
categoryLabels.push(...values);
|
|
77
|
+
}
|
|
78
|
+
else if (field === "subcategorySlug" || field === "subcategory") {
|
|
79
|
+
subcategoryValues.push(...values);
|
|
80
|
+
}
|
|
81
|
+
else if (field === "manufacturer" || field === "kind" || field.startsWith("attrs.")) {
|
|
82
|
+
out[field] = [...values];
|
|
83
|
+
}
|
|
84
|
+
else if (field === "available") {
|
|
85
|
+
// Sent only when it narrows; the service reports it as ignored on
|
|
86
|
+
// an index without the field, and the facet is not offered there.
|
|
87
|
+
const wanted = new Set(values);
|
|
88
|
+
if (wanted.size === 1) {
|
|
89
|
+
out.available = wanted.has("true");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// `enabled` is pinned by the service and anything else is not a field
|
|
93
|
+
// the live index has.
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Only a page's own category pins the listing (category pages send it as a
|
|
97
|
+
// fixed filter); a category picked in the search page's facet narrows the
|
|
98
|
+
// results but keeps the search page's facets.
|
|
99
|
+
const pinnedCategoryCode = categoryCodes[0] ?? "";
|
|
100
|
+
|
|
101
|
+
// The search page's `category` facet is label-valued (see liveListingFacets).
|
|
102
|
+
if (categoryLabels.length) {
|
|
103
|
+
const { labels } = await liveTopCategoryIndex(locale);
|
|
104
|
+
categoryCodes.push(...codesForLabels(categoryLabels, labels));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Resolved after the loop: `filters` merges the visitor's selection over
|
|
108
|
+
// the page's fixed filters, so `subcategory` can come before `categorySlug`.
|
|
109
|
+
const subcategoryCodes = subcategoryValues.length && pinnedCategoryCode
|
|
110
|
+
? codesForLabels(subcategoryValues, (await liveSubcategoryIndex(pinnedCategoryCode, locale)).labels)
|
|
111
|
+
: subcategoryValues;
|
|
112
|
+
|
|
113
|
+
const branchCodes = subcategoryCodes.length ? subcategoryCodes : categoryCodes;
|
|
114
|
+
if (branchCodes.length) {
|
|
115
|
+
out.categories = await liveCategorySubtreeCodes(branchCodes);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const priceRequested = priceRange?.min !== undefined || priceRange?.max !== undefined;
|
|
119
|
+
if (priceRequested) {
|
|
120
|
+
out[PRICE_FIELD] = {
|
|
121
|
+
...(priceRange?.min !== undefined ? { min: Math.round(priceRange.min * 100) } : {}),
|
|
122
|
+
...(priceRange?.max !== undefined ? { max: Math.round(priceRange.max * 100) } : {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return { filters: out, pinnedCategoryCode, priceRequested };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The service's facets as the listing UI asks for them.
|
|
131
|
+
*
|
|
132
|
+
* Every facet passes through under its own name. Two are derived on top,
|
|
133
|
+
* because the UI's names never meet the index's otherwise:
|
|
134
|
+
* - `category` (search page): `categories` leaf counts rolled up to the
|
|
135
|
+
* top-level categories, label-valued like the demo schema's field.
|
|
136
|
+
* - `subcategory` (category page): the same roll-up onto the pinned
|
|
137
|
+
* category's direct children.
|
|
138
|
+
* There is no derived `available`: it shows up only when the index facets
|
|
139
|
+
* such a field itself.
|
|
140
|
+
*/
|
|
141
|
+
export function mapListingFacets(
|
|
142
|
+
facets: SearchClientResult["facets"],
|
|
143
|
+
axes: { readonly top?: LiveSubcategoryIndex; readonly sub?: LiveSubcategoryIndex },
|
|
144
|
+
): SearchFacet[] {
|
|
145
|
+
const out: SearchFacet[] = facets.map(facet => ({
|
|
146
|
+
fieldName: facet.field,
|
|
147
|
+
counts: facet.counts.map(c => ({ value: c.value, count: c.count })),
|
|
148
|
+
}));
|
|
149
|
+
const leafCounts = facets.find(f => f.field === "categories")?.counts ?? [];
|
|
150
|
+
|
|
151
|
+
const derive = (fieldName: string, index: LiveSubcategoryIndex | undefined) => {
|
|
152
|
+
if (!index || out.some(f => f.fieldName === fieldName)) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const byBranch = rollUpSubcategoryCounts(leafCounts, index.branchOf);
|
|
156
|
+
if (byBranch.size) {
|
|
157
|
+
out.push({
|
|
158
|
+
fieldName,
|
|
159
|
+
counts: [...byBranch]
|
|
160
|
+
.map(([code, count]) => ({ value: index.labels.get(code) ?? code, count }))
|
|
161
|
+
.sort((a, b) => b.count - a.count),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
derive("category", axes.top);
|
|
167
|
+
derive("subcategory", axes.sub);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** mapListingFacets with the category axes loaded for this request. */
|
|
172
|
+
export async function liveListingFacets(
|
|
173
|
+
result: SearchClientResult,
|
|
174
|
+
pinnedCategoryCode: string,
|
|
175
|
+
locale: Locale,
|
|
176
|
+
): Promise<SearchFacet[]> {
|
|
177
|
+
const hasCategories = result.facets.some(f => f.field === "categories");
|
|
178
|
+
if (!hasCategories) {
|
|
179
|
+
return mapListingFacets(result.facets, {});
|
|
180
|
+
}
|
|
181
|
+
return mapListingFacets(result.facets, pinnedCategoryCode
|
|
182
|
+
? { sub: await liveSubcategoryIndex(pinnedCategoryCode, locale) }
|
|
183
|
+
: { top: await liveTopCategoryIndex(locale) });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Whether the price filter does anything on this index. Yes when the service
|
|
188
|
+
* facets a price field, or applied a price range it was sent; no when it
|
|
189
|
+
* reported the range as ignored, and no on the fallback, whose collection
|
|
190
|
+
* carries no price. Without a signal it stays hidden rather than offering a
|
|
191
|
+
* filter that does nothing.
|
|
192
|
+
*/
|
|
193
|
+
export function liveListingPriceFilter(result: SearchClientResult, priceRequested: boolean): boolean {
|
|
194
|
+
if (result.facets.some(f => f.field === PRICE_FIELD)) {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
if (result.source !== "query" || !priceRequested) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return !result.ignoredFilters.includes(PRICE_FIELD);
|
|
201
|
+
}
|
|
@@ -2,6 +2,8 @@ import type { H3Event } from "h3";
|
|
|
2
2
|
import type { Models } from "@revenexx/sdk";
|
|
3
3
|
|
|
4
4
|
import type { Offer, OfferRequestItem } from "../interfaces/offer";
|
|
5
|
+
import { principalContactId } from "./buyerIdentity";
|
|
6
|
+
import { sessionOrderRefs } from "./liveOrders";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Live prices from the prices app — THE live price call (the Offer domain's
|
|
@@ -21,15 +23,23 @@ interface ResolvedPrice {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
26
|
+
* Who a prices.resolve call prices for, as body fields (RAD-170).
|
|
27
|
+
*
|
|
28
|
+
* A signed-in buyer's call carries them as `X-Revenexx-Principal` (see
|
|
29
|
+
* useRevenexxSdk), and prices 1.0 then takes the contact AND its organization
|
|
30
|
+
* from the gateway — a body id that differs from those is refused as
|
|
31
|
+
* `buyer_mismatch`. So where there is a principal the body names nobody and
|
|
32
|
+
* the principal decides. Only a request acting for no contact states a scope
|
|
33
|
+
* in the body, and only a verified one (RAD-155): a forged organization id
|
|
34
|
+
* would show another company's contract prices.
|
|
25
35
|
*
|
|
26
36
|
* A guest carries none, and whether they may see prices at all is the prices
|
|
27
|
-
* app's answer (`anonymous_resolve_allowed`)
|
|
28
|
-
* see resolveLivePriceMap.
|
|
37
|
+
* app's answer (`anonymous_resolve_allowed`) — see resolveLivePriceMap.
|
|
29
38
|
*/
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
export function priceBuyerContext(event: H3Event): { contactId?: string; organizationId?: string } {
|
|
40
|
+
if (principalContactId(event)) {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
33
43
|
const { contact_id, organization_id } = sessionOrderRefs(event);
|
|
34
44
|
return {
|
|
35
45
|
...(contact_id ? { contactId: contact_id } : {}),
|
|
@@ -37,6 +47,12 @@ function priceContext(event: H3Event): { contactId?: string; organizationId?: st
|
|
|
37
47
|
};
|
|
38
48
|
}
|
|
39
49
|
|
|
50
|
+
/** Whether a price call acts for a known buyer — by principal or stated scope. */
|
|
51
|
+
function pricesForBuyer(event: H3Event): boolean {
|
|
52
|
+
const context = priceBuyerContext(event);
|
|
53
|
+
return Boolean(principalContactId(event) || context.contactId || context.organizationId);
|
|
54
|
+
}
|
|
55
|
+
|
|
40
56
|
/** Resolve tier ladder → the storefront tier map (quantity → TOTAL cents). */
|
|
41
57
|
function toPriceMap(resolved: ResolvedPrice): { [key: string]: number }[] {
|
|
42
58
|
if (resolved.on_request || resolved.unit_price === null) {
|
|
@@ -69,8 +85,7 @@ export async function resolveLivePriceMap(
|
|
|
69
85
|
// a price, which is the same "price on request" the UI already renders for an
|
|
70
86
|
// article the merchant quotes individually. Asking anyway would turn a
|
|
71
87
|
// deliberate policy into an error page.
|
|
72
|
-
|
|
73
|
-
if (!context.contactId && !context.organizationId) {
|
|
88
|
+
if (!pricesForBuyer(event)) {
|
|
74
89
|
const { prices: priceTerms } = await getShopSettings(event);
|
|
75
90
|
if (!priceTerms.anonymousAllowed) {
|
|
76
91
|
for (const item of items) {
|
|
@@ -83,7 +98,7 @@ export async function resolveLivePriceMap(
|
|
|
83
98
|
const currency = resolveSelectedCurrency(event);
|
|
84
99
|
const { prices } = await useRevenexxSdk().prices.pricesResolve({
|
|
85
100
|
items: items.map(i => ({ product_id: i.id, sku: i.sku, quantity: 1 })) as Models.PriceResolveItem[],
|
|
86
|
-
...
|
|
101
|
+
...priceBuyerContext(event),
|
|
87
102
|
...(currency ? { currency } : {}),
|
|
88
103
|
}) as unknown as { prices: ResolvedPrice[] };
|
|
89
104
|
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** The part of a prices.resolve answer an order line is priced from. */
|
|
2
|
+
export interface PriceResolveAnswer {
|
|
3
|
+
prices?: Array<{
|
|
4
|
+
product_id?: string | null;
|
|
5
|
+
sku?: string | null;
|
|
6
|
+
on_request?: boolean;
|
|
7
|
+
unit_price?: number | null;
|
|
8
|
+
tax_rate?: number | null;
|
|
9
|
+
}>;
|
|
10
|
+
tax?: { resolved?: boolean; reason?: string; message?: string };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface OrderLinePrice {
|
|
14
|
+
unitPrice: number;
|
|
15
|
+
taxRate: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The price and tax rate every order line is placed at — taken from the
|
|
20
|
+
* prices app's answer and nowhere else (RAD-175).
|
|
21
|
+
*
|
|
22
|
+
* The orders app takes the prices its caller supplies, on the assumption that
|
|
23
|
+
* only a server sets them. So a line the prices app did not price is not
|
|
24
|
+
* placed at the price the browser sent, or the one the cart was last shown
|
|
25
|
+
* with (which is the browser's again wherever prices did not answer): the
|
|
26
|
+
* order is refused instead. Equally a rate nobody could establish is refused
|
|
27
|
+
* rather than read as 0 % — `tax.resolved: false` means unknown, not untaxed.
|
|
28
|
+
*
|
|
29
|
+
* The answer holds one entry per requested item, in request order, so lines
|
|
30
|
+
* are matched by index: the same article can stand on two positions.
|
|
31
|
+
*/
|
|
32
|
+
export function orderLinePrices(answer: PriceResolveAnswer, lineCount: number): OrderLinePrice[] {
|
|
33
|
+
if (answer.tax?.resolved === false) {
|
|
34
|
+
throw createError({
|
|
35
|
+
status: 422,
|
|
36
|
+
message: `Prices could not be taxed${answer.tax.reason ? `: ${answer.tax.reason}` : ""}`,
|
|
37
|
+
data: { type: "tax_unresolved", reason: answer.tax.reason ?? null },
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const prices = answer.prices ?? [];
|
|
41
|
+
const unpriced: string[] = [];
|
|
42
|
+
const untaxed: string[] = [];
|
|
43
|
+
const lines: OrderLinePrice[] = [];
|
|
44
|
+
for (let index = 0; index < lineCount; index++) {
|
|
45
|
+
const p = prices[index];
|
|
46
|
+
const label = String(p?.sku ?? p?.product_id ?? `#${index + 1}`);
|
|
47
|
+
if (!p || p.on_request || p.unit_price == null || !Number.isFinite(Number(p.unit_price))) {
|
|
48
|
+
unpriced.push(label);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (p.tax_rate == null || !Number.isFinite(Number(p.tax_rate))) {
|
|
52
|
+
untaxed.push(label);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
lines.push({ unitPrice: Number(p.unit_price), taxRate: Number(p.tax_rate) });
|
|
56
|
+
}
|
|
57
|
+
if (unpriced.length) {
|
|
58
|
+
throw createError({
|
|
59
|
+
status: 422,
|
|
60
|
+
message: `Positions without a price cannot be ordered: ${unpriced.join(", ")}`,
|
|
61
|
+
data: { type: "price_on_request", positions: unpriced },
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (untaxed.length) {
|
|
65
|
+
throw createError({
|
|
66
|
+
status: 422,
|
|
67
|
+
message: `Positions without a tax rate cannot be ordered: ${untaxed.join(", ")}`,
|
|
68
|
+
data: { type: "tax_unresolved", positions: untaxed },
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return lines;
|
|
72
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a payment surcharge is placed at (RX-781).
|
|
3
|
+
*
|
|
4
|
+
* The payments app owns the surcharge: it computes the amount from the method's
|
|
5
|
+
* configuration and resolves the rate it is taxed at from the buyer's market
|
|
6
|
+
* (`markets.tax_classes`, via the method's tax class — RAD-200). The storefront
|
|
7
|
+
* resolves both server-side and hands them to the orders app, which taxes the
|
|
8
|
+
* surcharge and folds it into `grand_total`. The BFF computes no tax and sends
|
|
9
|
+
* no total, exactly as it already does for the shipping charge.
|
|
10
|
+
*
|
|
11
|
+
* Never from the browser. A fee the shopper could name is a fee the shopper
|
|
12
|
+
* could set.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** An eligible method as the payments app answers it. */
|
|
16
|
+
export interface EligibleMethodFee {
|
|
17
|
+
code: string;
|
|
18
|
+
fee: number;
|
|
19
|
+
/** Null means the rate could not be established — not that there is none. */
|
|
20
|
+
fee_tax_rate?: number | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PlacedPaymentFee {
|
|
24
|
+
fee: number;
|
|
25
|
+
taxRate: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The surcharge to place the order with, or null when there is none to place.
|
|
30
|
+
*
|
|
31
|
+
* Null covers the two cases that are not a problem: the chosen method is not in
|
|
32
|
+
* the answer at all (the mock payment path, or a method the shop serves without
|
|
33
|
+
* asking payments), and a method that charges nothing. Neither sends a `fee` —
|
|
34
|
+
* a zero would read as "resolved and free", which is a different claim.
|
|
35
|
+
*
|
|
36
|
+
* A surcharge whose rate the payments app could not establish throws 422. An
|
|
37
|
+
* unresolved rate is unknown, not zero, and the same rule already governs the
|
|
38
|
+
* shipping charge and the line prices (`checkout` AC-8): a surcharge placed at
|
|
39
|
+
* 0 % because nobody could say is an invoice the merchant owes the difference
|
|
40
|
+
* on, found months later.
|
|
41
|
+
*/
|
|
42
|
+
export function paymentFee(methods: EligibleMethodFee[], methodCode: string): PlacedPaymentFee | null {
|
|
43
|
+
const chosen = methods.find(m => m.code === methodCode);
|
|
44
|
+
if (!chosen) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
const fee = Number(chosen.fee ?? 0);
|
|
48
|
+
if (!Number.isFinite(fee) || fee <= 0) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
if (chosen.fee_tax_rate == null) {
|
|
52
|
+
throw createError({
|
|
53
|
+
status: 422,
|
|
54
|
+
message: `The surcharge of payment method '${methodCode}' could not be taxed`,
|
|
55
|
+
data: { type: "payment_fee_tax_unresolved", method: methodCode },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return { fee, taxRate: Number(chosen.fee_tax_rate) };
|
|
59
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** A row of `GET /v1/payments/providers`, as far as the tokenizer config reads it. */
|
|
2
|
+
export interface PspProviderRow {
|
|
3
|
+
provider: string;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
test_mode?: boolean;
|
|
6
|
+
options?: Record<string, unknown> | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type PspPublicConfig = Record<string, { publishableKey: string; testMode: boolean }>;
|
|
10
|
+
|
|
11
|
+
// Stripe's secret and restricted keys. Pasted into the public slot by mistake,
|
|
12
|
+
// either would be handed to every browser that opens the checkout.
|
|
13
|
+
const SECRET_KEY = /^(sk|rk)_/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The publishable keys of the enabled PSPs, for the checkout's client-side
|
|
17
|
+
* tokenizers (RAD-171) — and nothing else from the provider row.
|
|
18
|
+
*
|
|
19
|
+
* The payments app never reads `credentials` back, so the key cannot come from
|
|
20
|
+
* there: it is the merchant's to keep in the provider's `options` as
|
|
21
|
+
* `publishable_key`, which is free, non-secret configuration that the app does
|
|
22
|
+
* return. A value shaped like a secret key is dropped rather than published.
|
|
23
|
+
*/
|
|
24
|
+
export function pspPublicConfig(rows: PspProviderRow[]): { providers: PspPublicConfig; refused: string[] } {
|
|
25
|
+
const providers: PspPublicConfig = {};
|
|
26
|
+
const refused: string[] = [];
|
|
27
|
+
for (const row of rows) {
|
|
28
|
+
if (!row.enabled) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const key = row.options?.publishable_key;
|
|
32
|
+
if (typeof key !== "string" || key.trim() === "") {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (SECRET_KEY.test(key.trim())) {
|
|
36
|
+
refused.push(row.provider);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
providers[row.provider] = { publishableKey: key.trim(), testMode: row.test_mode !== false };
|
|
40
|
+
}
|
|
41
|
+
return { providers, refused };
|
|
42
|
+
}
|
|
@@ -59,6 +59,12 @@ export interface RevenexxSdkOptions {
|
|
|
59
59
|
export interface RevenexxSdkCallOptions {
|
|
60
60
|
readonly query?: Record<string, string | number | boolean | undefined>;
|
|
61
61
|
readonly body?: unknown;
|
|
62
|
+
/**
|
|
63
|
+
* Extra request headers for this one call, over the client's own (tenant,
|
|
64
|
+
* auth, market, channel, principal). The search client sends the locale
|
|
65
|
+
* this way, and a market or channel the caller names explicitly.
|
|
66
|
+
*/
|
|
67
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
export interface RevenexxSdk {
|
|
@@ -364,9 +370,11 @@ export function useRevenexxSdk(options: RevenexxSdkOptions = {}): RevenexxSdk {
|
|
|
364
370
|
}
|
|
365
371
|
}
|
|
366
372
|
|
|
373
|
+
const headers = { ...(options.headers ?? {}) };
|
|
374
|
+
|
|
367
375
|
if (method === "GET") {
|
|
368
376
|
// client.call appends flattened params to the URL for GET requests
|
|
369
|
-
return await client.call("get", url,
|
|
377
|
+
return await client.call("get", url, headers, query) as T;
|
|
370
378
|
}
|
|
371
379
|
|
|
372
380
|
for (const [key, value] of Object.entries(query)) {
|
|
@@ -375,7 +383,7 @@ export function useRevenexxSdk(options: RevenexxSdkOptions = {}): RevenexxSdk {
|
|
|
375
383
|
return await client.call(
|
|
376
384
|
method.toLowerCase(),
|
|
377
385
|
url,
|
|
378
|
-
{ "content-type": "application/json" },
|
|
386
|
+
{ ...headers, "content-type": "application/json" },
|
|
379
387
|
(options.body ?? {}) as never,
|
|
380
388
|
) as T;
|
|
381
389
|
}
|
|
@@ -409,3 +417,18 @@ export function useRevenexxSdk(options: RevenexxSdkOptions = {}): RevenexxSdk {
|
|
|
409
417
|
}
|
|
410
418
|
return instance;
|
|
411
419
|
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* A client that acts as the tenant, for a call that is the merchant's work and
|
|
423
|
+
* not the buyer's — even inside a buyer's request (RAD-174).
|
|
424
|
+
*
|
|
425
|
+
* Since every call made for a buyer carries their principal, the apps hold it
|
|
426
|
+
* to the buyer's own routes: the orders app answers `buyer_not_permitted` to a
|
|
427
|
+
* payment-status update made on a buyer's behalf, because a buyer does not say
|
|
428
|
+
* their own order is paid. Recording what the payments app answered, or
|
|
429
|
+
* undoing an order whose payment failed, is the storefront acting as the
|
|
430
|
+
* merchant's system, and is made as one.
|
|
431
|
+
*/
|
|
432
|
+
export function useTenantSdk(): RevenexxSdk {
|
|
433
|
+
return useRevenexxSdk({ principal: "" });
|
|
434
|
+
}
|