@springmicro/cart 0.7.23 → 0.7.25
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/dist/index.js +7156 -6873
- package/dist/index.umd.cjs +68 -60
- package/package.json +5 -5
- package/src/checkout/ReviewCartAndCalculateTaxes.tsx +75 -69
- package/src/checkout/components/CartList.tsx +11 -28
- package/src/checkout/components/CartProductCard.tsx +3 -1
- package/src/checkout/index.tsx +3 -17
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@springmicro/cart",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.25",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"@nanostores/persistent": "^0.10.1",
|
|
25
25
|
"@nanostores/query": "^0.3.3",
|
|
26
26
|
"@nanostores/react": "^0.7.2",
|
|
27
|
-
"@springmicro/utils": "^0.7.
|
|
27
|
+
"@springmicro/utils": "^0.7.25",
|
|
28
28
|
"dotenv": "^16.4.5",
|
|
29
29
|
"nanostores": "^0.10.3",
|
|
30
|
-
"react": "
|
|
30
|
+
"react": "18.3.1",
|
|
31
31
|
"react-credit-cards-2": "^1.0.2",
|
|
32
|
-
"react-dom": "
|
|
32
|
+
"react-dom": "18.3.1",
|
|
33
33
|
"unstorage": "^1.10.2",
|
|
34
34
|
"vite-plugin-dts": "^3.9.0"
|
|
35
35
|
},
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"vite-plugin-css-injected-by-js": "^3.5.1",
|
|
50
50
|
"yup": "^1.4.0"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "e67eb316de4309e612e59ca86e3ad73e4aab5e7a"
|
|
53
53
|
}
|
|
@@ -41,7 +41,7 @@ import CloseIcon from "@mui/icons-material/Close";
|
|
|
41
41
|
|
|
42
42
|
async function createOrder(
|
|
43
43
|
cart,
|
|
44
|
-
|
|
44
|
+
billing_information,
|
|
45
45
|
apiBaseUrl,
|
|
46
46
|
affiliateCode,
|
|
47
47
|
dev,
|
|
@@ -69,18 +69,22 @@ async function createOrder(
|
|
|
69
69
|
"Content-Type": "application/json",
|
|
70
70
|
},
|
|
71
71
|
body: JSON.stringify({
|
|
72
|
-
customer_create,
|
|
73
72
|
cart_data: {
|
|
74
73
|
items: cart.items.map((li) => ({
|
|
75
74
|
...li,
|
|
76
75
|
name: undefined,
|
|
77
76
|
image: undefined,
|
|
78
77
|
})),
|
|
78
|
+
billing_information,
|
|
79
79
|
},
|
|
80
80
|
affiliate_code: affiliateCode,
|
|
81
81
|
}),
|
|
82
82
|
credentials: "include",
|
|
83
83
|
});
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
throw "Something when wrong when creating your order";
|
|
86
|
+
}
|
|
87
|
+
|
|
84
88
|
const order = await res.json();
|
|
85
89
|
|
|
86
90
|
if (!order) throw "Missing order";
|
|
@@ -93,49 +97,61 @@ async function createOrder(
|
|
|
93
97
|
return order;
|
|
94
98
|
}
|
|
95
99
|
|
|
96
|
-
async function getTax(apiBaseUrl, taxProvider, items, v, products, dev) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
100
|
+
// async function getTax(apiBaseUrl, taxProvider, items, v, products, dev) {
|
|
101
|
+
// dev && console.log("Getting tax", items);
|
|
102
|
+
// dev &&
|
|
103
|
+
// console.log(
|
|
104
|
+
// "Getting product data",
|
|
105
|
+
// products,
|
|
106
|
+
// products[items[0].product_id],
|
|
107
|
+
// );
|
|
108
|
+
// const taxable_items = items.filter(
|
|
109
|
+
// (i) => i.unit_amount > 0 && products[i.product_id].type != "DONATION",
|
|
110
|
+
// );
|
|
111
|
+
// dev && console.log("Taxable items", taxable_items);
|
|
112
|
+
// if (taxable_items.length === 0) return { tax_amount: 0 };
|
|
113
|
+
|
|
114
|
+
// const res = await fetch(
|
|
115
|
+
// `${apiBaseUrl}/api/ecommerce/tax?tax_provider=${taxProvider ?? "dummy"}`,
|
|
116
|
+
// {
|
|
117
|
+
// method: "POST",
|
|
118
|
+
// body: JSON.stringify({
|
|
119
|
+
// cart_data: taxable_items.map((i) => ({
|
|
120
|
+
// amount: i.unit_amount,
|
|
121
|
+
// reference: i.reference,
|
|
122
|
+
// })),
|
|
123
|
+
// address: {
|
|
124
|
+
// country: v.country,
|
|
125
|
+
// city: v.locality,
|
|
126
|
+
// line1: v.street_address,
|
|
127
|
+
// postal_code: v.postal_code,
|
|
128
|
+
// state: v.region,
|
|
129
|
+
// },
|
|
130
|
+
// }),
|
|
131
|
+
// headers: {
|
|
132
|
+
// "Content-Type": "application/json",
|
|
133
|
+
// },
|
|
134
|
+
// },
|
|
135
|
+
// );
|
|
136
|
+
// return await res.json();
|
|
137
|
+
// }
|
|
138
|
+
|
|
139
|
+
// getTax(
|
|
140
|
+
// apiBaseUrl,
|
|
141
|
+
// taxProvider,
|
|
142
|
+
// cart.items.map((i) => ({
|
|
143
|
+
// unit_amount: prices[i.price_id].unit_amount,
|
|
144
|
+
// reference: i.name,
|
|
145
|
+
// product_id: i.product_id,
|
|
146
|
+
// })),
|
|
147
|
+
// values,
|
|
148
|
+
// products,
|
|
149
|
+
// dev,
|
|
150
|
+
// ),
|
|
134
151
|
|
|
135
152
|
export default function ReviewAndCalculateTaxes({
|
|
136
153
|
subtotal,
|
|
137
154
|
discount,
|
|
138
|
-
taxState,
|
|
139
155
|
shipping,
|
|
140
156
|
statusState,
|
|
141
157
|
cart,
|
|
@@ -163,7 +179,6 @@ export default function ReviewAndCalculateTaxes({
|
|
|
163
179
|
const stacked = useMediaQuery(theme.breakpoints.down("lg"));
|
|
164
180
|
|
|
165
181
|
const [status, setStatus] = statusState;
|
|
166
|
-
const [tax, setTax] = taxState;
|
|
167
182
|
const [order, setOrder] = orderState;
|
|
168
183
|
|
|
169
184
|
const [precheckErrors, setPrecheckErrors] = useState([]);
|
|
@@ -174,32 +189,23 @@ export default function ReviewAndCalculateTaxes({
|
|
|
174
189
|
// status === 0 when this is clicked.
|
|
175
190
|
// Creates an order and calculates tax.
|
|
176
191
|
dev && console.log("Cart items", cart.items);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
product_id: i.product_id,
|
|
195
|
-
})),
|
|
196
|
-
values,
|
|
197
|
-
products,
|
|
198
|
-
dev,
|
|
199
|
-
),
|
|
200
|
-
])
|
|
201
|
-
.then(([order, taxData]) => {
|
|
202
|
-
setTax(taxData);
|
|
192
|
+
|
|
193
|
+
createOrder(
|
|
194
|
+
cart,
|
|
195
|
+
{
|
|
196
|
+
email: values.email,
|
|
197
|
+
name: values.name,
|
|
198
|
+
country: values.country,
|
|
199
|
+
locality: values.locality,
|
|
200
|
+
street_address: values.street_address,
|
|
201
|
+
postal_code: values.postal_code,
|
|
202
|
+
state: values.region,
|
|
203
|
+
},
|
|
204
|
+
apiBaseUrl,
|
|
205
|
+
affiliateCode,
|
|
206
|
+
dev,
|
|
207
|
+
)
|
|
208
|
+
.then((order) => {
|
|
203
209
|
setOrder(order);
|
|
204
210
|
setStatus(1);
|
|
205
211
|
setFormDisabled(false);
|
|
@@ -423,7 +429,7 @@ export default function ReviewAndCalculateTaxes({
|
|
|
423
429
|
statusState: [status, setStatus],
|
|
424
430
|
cart,
|
|
425
431
|
subtotal,
|
|
426
|
-
|
|
432
|
+
orderState,
|
|
427
433
|
shipping,
|
|
428
434
|
discount,
|
|
429
435
|
prices,
|
|
@@ -10,7 +10,7 @@ export function CartList({
|
|
|
10
10
|
statusState,
|
|
11
11
|
cart,
|
|
12
12
|
subtotal,
|
|
13
|
-
|
|
13
|
+
orderState,
|
|
14
14
|
shipping,
|
|
15
15
|
discount,
|
|
16
16
|
prices,
|
|
@@ -23,6 +23,9 @@ export function CartList({
|
|
|
23
23
|
}) {
|
|
24
24
|
const [status, setStatus] = statusState;
|
|
25
25
|
const [formError, setFormError] = formErrorState;
|
|
26
|
+
const [order] = orderState;
|
|
27
|
+
const { taxes = "Pending", total } = order ?? {};
|
|
28
|
+
|
|
26
29
|
return (
|
|
27
30
|
<form
|
|
28
31
|
style={{ flexGrow: 1 }}
|
|
@@ -65,29 +68,9 @@ export function CartList({
|
|
|
65
68
|
<Typography sx={{ fontSize: "20px", p: 1 }}>
|
|
66
69
|
Subtotal: {formatPrice(subtotal)}
|
|
67
70
|
</Typography>
|
|
68
|
-
{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
title={`Tax could not be calculated. Error: ${tax.error}`}
|
|
72
|
-
>
|
|
73
|
-
<Typography
|
|
74
|
-
sx={{
|
|
75
|
-
fontSize: "20px",
|
|
76
|
-
bgcolor: "#ff000040",
|
|
77
|
-
borderRadius: 2,
|
|
78
|
-
p: 1,
|
|
79
|
-
textDecoration: "underline dotted",
|
|
80
|
-
cursor: "help",
|
|
81
|
-
}}
|
|
82
|
-
>
|
|
83
|
-
Taxes: {formatPrice(tax.tax_amount)}
|
|
84
|
-
</Typography>
|
|
85
|
-
</Tooltip>
|
|
86
|
-
) : (
|
|
87
|
-
<Typography sx={{ fontSize: "20px", p: 1 }}>
|
|
88
|
-
Taxes: {formatPrice(tax.tax_amount)}
|
|
89
|
-
</Typography>
|
|
90
|
-
)}
|
|
71
|
+
<Typography sx={{ fontSize: "20px", p: 1 }}>
|
|
72
|
+
Taxes: {formatPrice(taxes)}
|
|
73
|
+
</Typography>
|
|
91
74
|
</Box>
|
|
92
75
|
{(discount != 0 || shipping != 0) && (
|
|
93
76
|
<Box
|
|
@@ -108,12 +91,12 @@ export function CartList({
|
|
|
108
91
|
<Box>
|
|
109
92
|
<Typography style={{ fontSize: "26px" }}>
|
|
110
93
|
Total:{" "}
|
|
111
|
-
{typeof subtotal === "number"
|
|
94
|
+
{total || typeof subtotal === "number"
|
|
112
95
|
? formatPrice(
|
|
113
96
|
subtotal +
|
|
114
|
-
(
|
|
97
|
+
(typeof taxes === "string" ? 0 : taxes) +
|
|
115
98
|
shipping -
|
|
116
|
-
discount
|
|
99
|
+
discount,
|
|
117
100
|
)
|
|
118
101
|
: subtotal}
|
|
119
102
|
</Typography>
|
|
@@ -156,7 +139,7 @@ export function CartList({
|
|
|
156
139
|
<Box className="checkout-list">
|
|
157
140
|
{cart.items.map((p, i) => (
|
|
158
141
|
<CartProductCard
|
|
159
|
-
product={products[p.product_id] ??
|
|
142
|
+
product={{ ...p, ...(products[p.product_id] ?? {}) }}
|
|
160
143
|
i={i}
|
|
161
144
|
price={prices[p.price_id]}
|
|
162
145
|
disableProductLink={disableProductLink}
|
|
@@ -38,7 +38,9 @@ export function CartProductCard({
|
|
|
38
38
|
!disableMissingImage && <div className="missing-image" />
|
|
39
39
|
)}
|
|
40
40
|
<div className="two-row">
|
|
41
|
-
<Typography>
|
|
41
|
+
<Typography>
|
|
42
|
+
{product.name.replace("%s", product.hidden_id)}
|
|
43
|
+
</Typography>
|
|
42
44
|
<Typography>
|
|
43
45
|
{price
|
|
44
46
|
? `$${(price.unit_amount / 100).toFixed(2)}${
|
package/src/checkout/index.tsx
CHANGED
|
@@ -47,18 +47,17 @@ export default function Checkout({
|
|
|
47
47
|
}) {
|
|
48
48
|
const store = useStore(cartStore);
|
|
49
49
|
const localCart = useMemo(() => JSON.parse(store), [store]);
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const [order, setOrder] = useState(_order); // starts undefined on a typical checkout. If an order is provided, it's likely an invoice.
|
|
51
|
+
console.log("CART", invoiceId, order, localCart);
|
|
52
|
+
const cart = invoiceId ? { items: order.basket } : localCart; // todo fix calculation
|
|
52
53
|
|
|
53
54
|
const [products, setProducts] = useState({});
|
|
54
55
|
const [prices, setPrices] = useState({});
|
|
55
56
|
const [subtotal, setSubtotal] = useState("Loading prices...");
|
|
56
|
-
const taxState = useState<any>({ tax_amount: "TBD" });
|
|
57
57
|
const shipping = 0;
|
|
58
58
|
const discount = 0;
|
|
59
59
|
|
|
60
60
|
const [status, setStatus] = useState(0);
|
|
61
|
-
const [order, setOrder] = useState(_order); // starts undefined on a typical checkout. If an order is provided, it's likely an invoice.
|
|
62
61
|
const [successData, setSuccessData] = useState(null);
|
|
63
62
|
|
|
64
63
|
useEffect(() => {
|
|
@@ -166,18 +165,6 @@ export default function Checkout({
|
|
|
166
165
|
);
|
|
167
166
|
}, [cart, prices]);
|
|
168
167
|
|
|
169
|
-
useEffect(() => {
|
|
170
|
-
const taxable_items = cart.items.filter(
|
|
171
|
-
(i) =>
|
|
172
|
-
prices[i.price_id]?.unit_amount > 0 &&
|
|
173
|
-
products[i.product_id]?.type !== "DONATION",
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
if (taxable_items.length === 0) {
|
|
177
|
-
taxState[1]({ tax_amount: 0 });
|
|
178
|
-
}
|
|
179
|
-
}, [cart, prices, products]);
|
|
180
|
-
|
|
181
168
|
if (status === 0 && cart.items.length === 0)
|
|
182
169
|
return (
|
|
183
170
|
<div
|
|
@@ -221,7 +208,6 @@ export default function Checkout({
|
|
|
221
208
|
discount={discount}
|
|
222
209
|
shipping={shipping}
|
|
223
210
|
taxProvider={taxProvider}
|
|
224
|
-
taxState={taxState}
|
|
225
211
|
statusState={[status, setStatus]}
|
|
226
212
|
cart={cart}
|
|
227
213
|
prices={prices}
|