@vectoriox/iox-ui 5.93.0 → 5.94.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/commerce-core/index.d.ts +553 -3
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs +988 -5
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs.map +1 -1
- package/package.json +1 -1
- package/src/commerce-core/cart.ts +305 -0
- package/src/commerce-core/catalog.ts +42 -0
- package/src/commerce-core/checkout.ts +202 -0
- package/src/commerce-core/decimal.ts +28 -0
- package/src/commerce-core/labels.ts +29 -0
- package/src/commerce-core/limits.ts +9 -0
- package/src/commerce-core/money-error.ts +1 -0
- package/src/commerce-core/order-snapshot.ts +151 -0
- package/src/commerce-core/orders.ts +172 -0
- package/src/commerce-core/payments.ts +196 -0
- package/src/commerce-core/public-api.ts +15 -4
- package/src/commerce-core/shipping.ts +168 -0
- package/src/commerce-core/text.ts +13 -0
- package/src/commerce-core/vat.ts +38 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { CurrencyCode } from './currency';
|
|
2
|
+
import { CommerceLabel } from './labels';
|
|
3
|
+
import { MAX_CART_AMOUNT } from './limits';
|
|
4
|
+
import { isMoney, money, Money, subtractMoney } from './money';
|
|
5
|
+
import { filled } from './text';
|
|
6
|
+
|
|
7
|
+
/** Shipping v1 (C-D9): methods per studio, one country. Zones and weights come later. */
|
|
8
|
+
export enum ShippingMethodKind {
|
|
9
|
+
Pickup = 'pickup',
|
|
10
|
+
Flat = 'flat',
|
|
11
|
+
/** A flat price that becomes free once the goods reach `freeOver`. */
|
|
12
|
+
FreeOver = 'freeOver',
|
|
13
|
+
/** No address, nothing shipped — offered only when nothing in the cart ships. */
|
|
14
|
+
Digital = 'digital',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** What an owner must fill for a method of a kind. */
|
|
18
|
+
export enum ShippingField {
|
|
19
|
+
Price = 'price',
|
|
20
|
+
FreeOver = 'freeOver',
|
|
21
|
+
PickupAddress = 'pickupAddress',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ShippingMethod {
|
|
25
|
+
id: string;
|
|
26
|
+
kind: ShippingMethodKind;
|
|
27
|
+
name: string;
|
|
28
|
+
enabled: boolean;
|
|
29
|
+
price?: Money | null;
|
|
30
|
+
freeOver?: Money | null;
|
|
31
|
+
pickupAddress?: string | null;
|
|
32
|
+
pickupHours?: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ShippingKindRule {
|
|
36
|
+
readonly needsAddress: boolean;
|
|
37
|
+
/** Offered for a cart where something ships (true) or for one where nothing does (false). */
|
|
38
|
+
readonly forPhysical: boolean;
|
|
39
|
+
readonly requires: readonly ShippingField[];
|
|
40
|
+
readonly price: (method: ShippingMethod, goods: Money) => Money;
|
|
41
|
+
readonly label: CommerceLabel;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const zero = (goods: Money): Money => money(0, goods.currency);
|
|
45
|
+
|
|
46
|
+
export const SHIPPING_KINDS: Readonly<Record<ShippingMethodKind, ShippingKindRule>> = Object.freeze({
|
|
47
|
+
[ShippingMethodKind.Pickup]: {
|
|
48
|
+
needsAddress: false,
|
|
49
|
+
forPhysical: true,
|
|
50
|
+
requires: [ShippingField.PickupAddress],
|
|
51
|
+
price: (_method: ShippingMethod, goods: Money) => zero(goods),
|
|
52
|
+
label: { en: 'Pickup', he: 'איסוף עצמי' },
|
|
53
|
+
},
|
|
54
|
+
[ShippingMethodKind.Flat]: {
|
|
55
|
+
needsAddress: true,
|
|
56
|
+
forPhysical: true,
|
|
57
|
+
requires: [ShippingField.Price],
|
|
58
|
+
price: (method: ShippingMethod) => method.price as Money,
|
|
59
|
+
label: { en: 'Flat-rate delivery', he: 'משלוח במחיר קבוע' },
|
|
60
|
+
},
|
|
61
|
+
[ShippingMethodKind.FreeOver]: {
|
|
62
|
+
needsAddress: true,
|
|
63
|
+
forPhysical: true,
|
|
64
|
+
requires: [ShippingField.Price, ShippingField.FreeOver],
|
|
65
|
+
price: (method: ShippingMethod, goods: Money) =>
|
|
66
|
+
goods.amount >= (method.freeOver as Money).amount ? zero(goods) : (method.price as Money),
|
|
67
|
+
label: { en: 'Delivery, free over an amount', he: 'משלוח, חינם מעל סכום' },
|
|
68
|
+
},
|
|
69
|
+
[ShippingMethodKind.Digital]: {
|
|
70
|
+
needsAddress: false,
|
|
71
|
+
forPhysical: false,
|
|
72
|
+
requires: [],
|
|
73
|
+
price: (_method: ShippingMethod, goods: Money) => zero(goods),
|
|
74
|
+
label: { en: 'Digital — nothing shipped', he: 'דיגיטלי — ללא משלוח' },
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const validAmount = (value: unknown, currency: CurrencyCode): boolean =>
|
|
79
|
+
isMoney(value) && value.currency === currency && value.amount >= 0 && value.amount <= MAX_CART_AMOUNT;
|
|
80
|
+
|
|
81
|
+
const SHIPPING_FIELD_CHECKS: Readonly<Record<ShippingField, (method: ShippingMethod, currency: CurrencyCode) => boolean>> = Object.freeze({
|
|
82
|
+
[ShippingField.Price]: (method: ShippingMethod, currency: CurrencyCode) => validAmount(method.price, currency),
|
|
83
|
+
[ShippingField.FreeOver]: (method: ShippingMethod, currency: CurrencyCode) => validAmount(method.freeOver, currency),
|
|
84
|
+
[ShippingField.PickupAddress]: (method: ShippingMethod) => filled(method.pickupAddress),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const isShippingMethodKind = (value: unknown): value is ShippingMethodKind =>
|
|
88
|
+
typeof value === 'string' && Object.prototype.hasOwnProperty.call(SHIPPING_KINDS, value);
|
|
89
|
+
|
|
90
|
+
export function shippingNeedsAddress(kind: ShippingMethodKind): boolean {
|
|
91
|
+
return isShippingMethodKind(kind) && SHIPPING_KINDS[kind].needsAddress;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The fields a method lacks (or holds in another currency) — empty = it can be offered. */
|
|
95
|
+
export function shippingMethodProblems(method: ShippingMethod, currency: CurrencyCode): ShippingField[] {
|
|
96
|
+
if (!method || !isShippingMethodKind(method.kind)) return [ShippingField.Price];
|
|
97
|
+
return SHIPPING_KINDS[method.kind].requires.filter(field => !SHIPPING_FIELD_CHECKS[field](method, currency));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Enabled, complete, with an id and a name: the ONE rule both the offer and the checkout gate read. */
|
|
101
|
+
export function shippingMethodReady(method: ShippingMethod, currency: CurrencyCode): boolean {
|
|
102
|
+
return !!method && method.enabled === true && filled(method.id) && filled(method.name)
|
|
103
|
+
&& shippingMethodProblems(method, currency).length === 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ShippingQuote {
|
|
107
|
+
methodId: string;
|
|
108
|
+
kind: ShippingMethodKind;
|
|
109
|
+
name: string;
|
|
110
|
+
price: Money;
|
|
111
|
+
/** A free-over method whose threshold the goods reached. */
|
|
112
|
+
free: boolean;
|
|
113
|
+
needsAddress: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** What a quote reads of a cart. */
|
|
117
|
+
export interface ShippingGoods {
|
|
118
|
+
currency: CurrencyCode;
|
|
119
|
+
/** The goods after discounts — what a free-over threshold is compared with. */
|
|
120
|
+
goods: Money;
|
|
121
|
+
requiresShipping: boolean;
|
|
122
|
+
lineCount: number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The part of a `PricedCart` the offer reads. */
|
|
126
|
+
export interface ShippingCart {
|
|
127
|
+
currency: CurrencyCode;
|
|
128
|
+
subtotal: Money;
|
|
129
|
+
discount: Money;
|
|
130
|
+
requiresShipping: boolean;
|
|
131
|
+
lines: readonly unknown[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function quoteShipping(methods: readonly ShippingMethod[], cart: ShippingGoods): ShippingQuote[] {
|
|
135
|
+
const list: readonly ShippingMethod[] = Array.isArray(methods) ? methods : [];
|
|
136
|
+
if (!cart.lineCount) return [];
|
|
137
|
+
const seen = new Set<string>();
|
|
138
|
+
return list
|
|
139
|
+
.filter(method => shippingMethodReady(method, cart.currency)
|
|
140
|
+
&& SHIPPING_KINDS[method.kind].forPhysical === cart.requiresShipping
|
|
141
|
+
&& !seen.has(method.id) && !!seen.add(method.id))
|
|
142
|
+
.map(method => {
|
|
143
|
+
const rule = SHIPPING_KINDS[method.kind];
|
|
144
|
+
const price = rule.price(method, cart.goods);
|
|
145
|
+
return {
|
|
146
|
+
methodId: method.id,
|
|
147
|
+
kind: method.kind,
|
|
148
|
+
name: method.name,
|
|
149
|
+
price: money(price.amount, price.currency),
|
|
150
|
+
free: method.kind === ShippingMethodKind.FreeOver && price.amount === 0,
|
|
151
|
+
needsAddress: rule.needsAddress,
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The methods a buyer may choose for this cart, each priced for it (free over: free when the goods after
|
|
158
|
+
* discounts are AT or above the threshold). A cart where nothing ships is offered Digital methods only; a cart
|
|
159
|
+
* where anything ships never is.
|
|
160
|
+
*/
|
|
161
|
+
export function availableShippingMethods(methods: readonly ShippingMethod[], pricedCart: ShippingCart): ShippingQuote[] {
|
|
162
|
+
return quoteShipping(methods, {
|
|
163
|
+
currency: pricedCart.currency,
|
|
164
|
+
goods: subtractMoney(pricedCart.subtotal, pricedCart.discount),
|
|
165
|
+
requiresShipping: pricedCart.requiresShipping,
|
|
166
|
+
lineCount: pricedCart.lines.length,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const filled = (value: unknown): value is string => typeof value === 'string' && value.trim() !== '';
|
|
2
|
+
|
|
3
|
+
export const looksLikeEmail = (value: unknown): boolean => filled(value) && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value.trim());
|
|
4
|
+
|
|
5
|
+
export const digitsOf = (value: unknown): string => (typeof value === 'string' ? value.replace(/\D/g, '') : '');
|
|
6
|
+
|
|
7
|
+
/** A phone a person can be called on: 7–15 digits once spaces, dashes, brackets and a leading + are dropped. */
|
|
8
|
+
export const looksLikePhone = (value: unknown): boolean =>
|
|
9
|
+
filled(value) && /^\+?[\d\s\-().]+$/.test(value.trim()) && digitsOf(value).length >= 7 && digitsOf(value).length <= 15;
|
|
10
|
+
|
|
11
|
+
/** An Israeli mobile number (Bit sends to one): 05X-XXXXXXX, or +972 5X-XXXXXXX. */
|
|
12
|
+
export const looksLikeIsraeliMobile = (value: unknown): boolean =>
|
|
13
|
+
filled(value) && /^\+?[\d\s\-().]+$/.test(value.trim()) && /^(?:9725|05)\d{8}$/.test(digitsOf(value));
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { currencyExponent } from './currency';
|
|
2
|
+
import { decimalToScaledInteger, DecimalRounding, mulDivHalfEven, parseDecimal } from './decimal';
|
|
3
|
+
import { Money } from './money';
|
|
4
|
+
import { MoneyError, MoneyErrorCode } from './money-error';
|
|
5
|
+
|
|
6
|
+
export const MAX_VAT_PERCENT = 100;
|
|
7
|
+
/** 17, 18, 7.7, 8.1 — four decimals is more than any rate in force; it also keeps the arithmetic below 2^53. */
|
|
8
|
+
export const VAT_PERCENT_MAX_DECIMALS = 4;
|
|
9
|
+
|
|
10
|
+
export function isVatPercent(value: unknown): value is number {
|
|
11
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > MAX_VAT_PERCENT) return false;
|
|
12
|
+
const decimal = parseDecimal(String(value));
|
|
13
|
+
return !!decimal && decimal.frac.length <= VAT_PERCENT_MAX_DECIMALS;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The VAT a VAT-INCLUSIVE amount already holds (C-D8): amount × v / (100 + v), rounded ONCE to a minor unit,
|
|
18
|
+
* half-to-even. Exact: v is read as the integer R = v·10^s (s ≤ 4 decimals), so the share is
|
|
19
|
+
* amount × R / (100·10^s + R) in whole numbers. 118.00 at 18 % → 18.00; 100.00 at 17 % → 14.53.
|
|
20
|
+
*/
|
|
21
|
+
export function includedVat(total: Money, vatPercent: number): Money {
|
|
22
|
+
currencyExponent(total.currency);
|
|
23
|
+
if (typeof total.amount !== 'number' || !Number.isSafeInteger(total.amount)) {
|
|
24
|
+
throw new MoneyError(MoneyErrorCode.NotInteger, `total must be a whole number of minor units, got ${String(total.amount)}`);
|
|
25
|
+
}
|
|
26
|
+
if (!isVatPercent(vatPercent)) {
|
|
27
|
+
throw new MoneyError(MoneyErrorCode.OutOfRange, `VAT must be 0–${MAX_VAT_PERCENT} with at most ${VAT_PERCENT_MAX_DECIMALS} decimals, got ${String(vatPercent)}`);
|
|
28
|
+
}
|
|
29
|
+
const decimal = parseDecimal(String(vatPercent))!;
|
|
30
|
+
const scale = decimal.frac.length;
|
|
31
|
+
const rate = decimalToScaledInteger(decimal, scale, DecimalRounding.Exact);
|
|
32
|
+
const magnitude = Math.abs(total.amount);
|
|
33
|
+
if (magnitude > Number.MAX_SAFE_INTEGER / 2) {
|
|
34
|
+
throw new MoneyError(MoneyErrorCode.Overflow, 'Amount is beyond the range VAT is computed for');
|
|
35
|
+
}
|
|
36
|
+
const share = mulDivHalfEven(magnitude, rate, 100 * 10 ** scale + rate);
|
|
37
|
+
return { amount: total.amount < 0 && share !== 0 ? -share : share, currency: total.currency };
|
|
38
|
+
}
|