@akinon/next 2.0.0-beta.12 → 2.0.0-beta.13
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/CHANGELOG.md +282 -29
- package/api/auth.ts +99 -77
- package/api/cache.ts +41 -5
- package/api/client.ts +3 -3
- package/api/form.ts +85 -0
- package/api/image-proxy.ts +75 -0
- package/api/product-categories.ts +53 -0
- package/api/similar-product-list.ts +63 -0
- package/api/similar-products.ts +111 -0
- package/api/virtual-try-on.ts +382 -0
- package/bin/pz-generate-routes.js +105 -0
- package/bin/pz-prebuild.js +1 -1
- package/bin/pz-predev.js +1 -0
- package/components/accordion.tsx +21 -6
- package/components/button.tsx +1 -1
- package/components/file-input.tsx +65 -3
- package/components/input.tsx +2 -2
- package/components/modal.tsx +32 -16
- package/components/plugin-module.tsx +61 -3
- package/components/select.tsx +2 -2
- package/components/selected-payment-option-view.tsx +21 -0
- package/data/client/checkout.ts +130 -74
- package/data/server/category.ts +11 -9
- package/data/server/flatpage.ts +4 -1
- package/data/server/form.ts +4 -1
- package/data/server/landingpage.ts +4 -1
- package/data/server/list.ts +5 -4
- package/data/server/menu.ts +4 -1
- package/data/server/product.ts +97 -52
- package/data/server/seo.ts +4 -1
- package/data/server/special-page.ts +5 -4
- package/data/server/widget.ts +4 -1
- package/data/urls.ts +3 -2
- package/hocs/client/with-segment-defaults.tsx +2 -2
- package/hocs/server/with-segment-defaults.tsx +65 -20
- package/hooks/index.ts +1 -0
- package/hooks/use-loyalty-availability.ts +21 -0
- package/hooks/use-payment-options.ts +2 -1
- package/hooks/use-pz-params.ts +37 -0
- package/instrumentation/index.ts +0 -1
- package/instrumentation/node.ts +2 -20
- package/jest.config.js +7 -1
- package/lib/cache-handler.mjs +527 -15
- package/lib/cache.ts +260 -31
- package/localization/provider.tsx +2 -5
- package/middlewares/checkout-provider.ts +1 -1
- package/middlewares/complete-gpay.ts +33 -26
- package/middlewares/complete-masterpass.ts +34 -26
- package/middlewares/complete-wallet.ts +183 -0
- package/middlewares/default.ts +346 -235
- package/middlewares/index.ts +8 -2
- package/middlewares/locale.ts +0 -1
- package/middlewares/masterpass-rest-callback.ts +220 -0
- package/middlewares/pretty-url.ts +21 -8
- package/middlewares/redirection-payment.ts +33 -26
- package/middlewares/saved-card-redirection.ts +34 -26
- package/middlewares/three-d-redirection.ts +33 -26
- package/middlewares/url-redirection.ts +9 -15
- package/middlewares/wallet-complete-redirection.ts +207 -0
- package/package.json +20 -11
- package/plugins.d.ts +19 -4
- package/plugins.js +9 -1
- package/redux/actions.ts +47 -0
- package/redux/middlewares/checkout.ts +20 -8
- package/redux/middlewares/index.ts +12 -10
- package/redux/middlewares/pre-order/address.ts +1 -1
- package/redux/middlewares/pre-order/attribute-based-shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/data-source-shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/delivery-option.ts +1 -1
- package/redux/middlewares/pre-order/index.ts +3 -1
- package/redux/middlewares/pre-order/installment-option.ts +2 -1
- package/redux/middlewares/pre-order/payment-option-reset.ts +37 -0
- package/redux/middlewares/pre-order/payment-option.ts +1 -1
- package/redux/middlewares/pre-order/pre-order-validation.ts +4 -3
- package/redux/middlewares/pre-order/redirection.ts +2 -2
- package/redux/middlewares/pre-order/set-pre-order.ts +2 -2
- package/redux/middlewares/pre-order/shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/shipping-step.ts +1 -1
- package/redux/reducers/checkout.ts +9 -1
- package/redux/reducers/index.ts +5 -1
- package/sentry/index.ts +54 -17
- package/types/commerce/checkout.ts +11 -1
- package/types/index.ts +96 -6
- package/types/next-auth.d.ts +2 -2
- package/utils/app-fetch.ts +2 -2
- package/utils/generate-commerce-search-params.ts +3 -2
- package/utils/get-checkout-path.ts +3 -0
- package/utils/index.ts +38 -11
- package/utils/override-middleware.ts +1 -0
- package/utils/pz-segments.ts +92 -0
- package/utils/redirect-ignore.ts +35 -0
- package/utils/redirect.ts +9 -3
- package/with-pz-config.js +10 -4
package/lib/cache.ts
CHANGED
|
@@ -1,8 +1,67 @@
|
|
|
1
1
|
import { createPool, Pool } from 'generic-pool';
|
|
2
2
|
import { RedisClientType } from 'redis';
|
|
3
3
|
import Settings from 'settings';
|
|
4
|
-
import { CacheOptions } from '../types';
|
|
4
|
+
import { CacheOptions, SearchParams } from '../types';
|
|
5
5
|
import logger from '../utils/log';
|
|
6
|
+
const CACHE_VERSION = 'v2';
|
|
7
|
+
|
|
8
|
+
const compressData = async (data: string): Promise<Uint8Array> => {
|
|
9
|
+
const stream = new CompressionStream('gzip');
|
|
10
|
+
const writer = stream.writable.getWriter();
|
|
11
|
+
const reader = stream.readable.getReader();
|
|
12
|
+
|
|
13
|
+
writer.write(new TextEncoder().encode(data));
|
|
14
|
+
writer.close();
|
|
15
|
+
|
|
16
|
+
const chunks: Uint8Array[] = [];
|
|
17
|
+
let done = false;
|
|
18
|
+
|
|
19
|
+
while (!done) {
|
|
20
|
+
const { value, done: readerDone } = await reader.read();
|
|
21
|
+
done = readerDone;
|
|
22
|
+
if (value) chunks.push(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
26
|
+
const result = new Uint8Array(totalLength);
|
|
27
|
+
let offset = 0;
|
|
28
|
+
|
|
29
|
+
for (const chunk of chunks) {
|
|
30
|
+
result.set(chunk, offset);
|
|
31
|
+
offset += chunk.length;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const decompressData = async (compressed: Uint8Array): Promise<string> => {
|
|
38
|
+
const stream = new DecompressionStream('gzip');
|
|
39
|
+
const writer = stream.writable.getWriter();
|
|
40
|
+
const reader = stream.readable.getReader();
|
|
41
|
+
|
|
42
|
+
writer.write(compressed as any);
|
|
43
|
+
writer.close();
|
|
44
|
+
|
|
45
|
+
const chunks: Uint8Array[] = [];
|
|
46
|
+
let done = false;
|
|
47
|
+
|
|
48
|
+
while (!done) {
|
|
49
|
+
const { value, done: readerDone } = await reader.read();
|
|
50
|
+
done = readerDone;
|
|
51
|
+
if (value) chunks.push(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
55
|
+
const result = new Uint8Array(totalLength);
|
|
56
|
+
let offset = 0;
|
|
57
|
+
|
|
58
|
+
for (const chunk of chunks) {
|
|
59
|
+
result.set(chunk, offset);
|
|
60
|
+
offset += chunk.length;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new TextDecoder().decode(result);
|
|
64
|
+
};
|
|
6
65
|
|
|
7
66
|
const hashCacheKey = (object?: Record<string, string>) => {
|
|
8
67
|
if (!object) {
|
|
@@ -19,30 +78,32 @@ const hashCacheKey = (object?: Record<string, string>) => {
|
|
|
19
78
|
return `_${encodeURIComponent(cacheKey)}`;
|
|
20
79
|
};
|
|
21
80
|
export const CacheKey = {
|
|
22
|
-
List: (searchParams:
|
|
81
|
+
List: (searchParams: SearchParams, headers?: Record<string, string>) =>
|
|
23
82
|
`list_${encodeURIComponent(JSON.stringify(searchParams))}${hashCacheKey(
|
|
24
83
|
headers
|
|
25
84
|
)}`,
|
|
26
85
|
Category: (
|
|
27
86
|
pk: number,
|
|
28
|
-
searchParams?:
|
|
87
|
+
searchParams?: SearchParams,
|
|
29
88
|
headers?: Record<string, string>
|
|
30
89
|
) =>
|
|
31
90
|
`category_${pk}_${encodeURIComponent(
|
|
32
91
|
JSON.stringify(searchParams)
|
|
33
92
|
)}${hashCacheKey(headers)}`,
|
|
93
|
+
Basket: (namespace?: string) => `basket${namespace ? `_${namespace}` : ''}`,
|
|
94
|
+
AllBaskets: () => 'all_baskets',
|
|
34
95
|
CategorySlug: (slug: string) => `category_${slug}`,
|
|
35
96
|
SpecialPage: (
|
|
36
97
|
pk: number,
|
|
37
|
-
searchParams:
|
|
98
|
+
searchParams: SearchParams,
|
|
38
99
|
headers?: Record<string, string>
|
|
39
100
|
) =>
|
|
40
101
|
`special_page_${pk}_${encodeURIComponent(
|
|
41
102
|
JSON.stringify(searchParams)
|
|
42
103
|
)}${hashCacheKey(headers)}`,
|
|
43
|
-
Product: (pk: number, searchParams:
|
|
104
|
+
Product: (pk: number, searchParams: SearchParams) =>
|
|
44
105
|
`product_${pk}_${encodeURIComponent(JSON.stringify(searchParams))}`,
|
|
45
|
-
GroupProduct: (pk: number, searchParams:
|
|
106
|
+
GroupProduct: (pk: number, searchParams: SearchParams) =>
|
|
46
107
|
`group_product_${pk}_${encodeURIComponent(JSON.stringify(searchParams))}`,
|
|
47
108
|
FlatPage: (pk: number) => `flat_page_${pk}`,
|
|
48
109
|
LandingPage: (pk: number) => `landing_page_${pk}`,
|
|
@@ -58,8 +119,32 @@ export const CacheKey = {
|
|
|
58
119
|
export class Cache {
|
|
59
120
|
static PROXY_URL = `${process.env.NEXT_PUBLIC_URL}/api/cache`;
|
|
60
121
|
|
|
122
|
+
private static serializeValue(value: any): string {
|
|
123
|
+
return typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private static validateKey(key: string): boolean {
|
|
127
|
+
return !(!key || key.trim() === '');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private static validateKeyValuePairs(keyValuePairs: Record<string, any>): {
|
|
131
|
+
isValid: boolean;
|
|
132
|
+
invalidKeys: string[];
|
|
133
|
+
} {
|
|
134
|
+
if (!keyValuePairs || Object.keys(keyValuePairs).length === 0) {
|
|
135
|
+
return { isValid: false, invalidKeys: [] };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const invalidKeys = Object.keys(keyValuePairs).filter(
|
|
139
|
+
(key) => !this.validateKey(key)
|
|
140
|
+
);
|
|
141
|
+
return { isValid: invalidKeys.length === 0, invalidKeys };
|
|
142
|
+
}
|
|
143
|
+
|
|
61
144
|
static formatKey(key: string, locale: string) {
|
|
62
|
-
return encodeURIComponent(
|
|
145
|
+
return encodeURIComponent(
|
|
146
|
+
`${CACHE_VERSION}_${Settings.commerceUrl}_${locale}_${key}`
|
|
147
|
+
);
|
|
63
148
|
}
|
|
64
149
|
|
|
65
150
|
static clientPool: Pool<RedisClientType> = createPool(
|
|
@@ -96,9 +181,9 @@ export class Cache {
|
|
|
96
181
|
return await Cache.clientPool.acquire();
|
|
97
182
|
}
|
|
98
183
|
|
|
99
|
-
static async get(key: string) {
|
|
100
|
-
let value;
|
|
101
|
-
let client;
|
|
184
|
+
static async get(key: string): Promise<any> {
|
|
185
|
+
let value: any;
|
|
186
|
+
let client: RedisClientType | undefined;
|
|
102
187
|
|
|
103
188
|
try {
|
|
104
189
|
client = await Cache.getClient();
|
|
@@ -108,9 +193,7 @@ export class Cache {
|
|
|
108
193
|
} else {
|
|
109
194
|
value = null;
|
|
110
195
|
}
|
|
111
|
-
logger.debug('Redis get success', { key, value });
|
|
112
196
|
} catch (error) {
|
|
113
|
-
logger.error('Redis get error', { key, error });
|
|
114
197
|
value = null;
|
|
115
198
|
} finally {
|
|
116
199
|
if (client) {
|
|
@@ -121,14 +204,13 @@ export class Cache {
|
|
|
121
204
|
return value;
|
|
122
205
|
}
|
|
123
206
|
|
|
124
|
-
static async set(key: string, value: any, expire?: number) {
|
|
207
|
+
static async set(key: string, value: any, expire?: number): Promise<boolean> {
|
|
125
208
|
let success = false;
|
|
126
|
-
let client;
|
|
209
|
+
let client: RedisClientType | undefined;
|
|
127
210
|
|
|
128
211
|
try {
|
|
129
212
|
client = await Cache.getClient();
|
|
130
|
-
const serializedValue =
|
|
131
|
-
typeof value === 'object' ? JSON.stringify(value) : value;
|
|
213
|
+
const serializedValue = Cache.serializeValue(value);
|
|
132
214
|
|
|
133
215
|
if (expire) {
|
|
134
216
|
await client.set(key, serializedValue, { EX: expire });
|
|
@@ -137,9 +219,7 @@ export class Cache {
|
|
|
137
219
|
}
|
|
138
220
|
|
|
139
221
|
success = true;
|
|
140
|
-
logger.debug('Redis set success', { key, value });
|
|
141
222
|
} catch (error) {
|
|
142
|
-
logger.error('Redis set error', { key, error });
|
|
143
223
|
success = false;
|
|
144
224
|
} finally {
|
|
145
225
|
if (client) {
|
|
@@ -168,7 +248,8 @@ export class Cache {
|
|
|
168
248
|
|
|
169
249
|
const defaultOptions: CacheOptions = {
|
|
170
250
|
cache: true,
|
|
171
|
-
expire: Settings.redis.defaultExpirationTime
|
|
251
|
+
expire: Settings.redis.defaultExpirationTime,
|
|
252
|
+
compressed: process.env.CACHE_COMPRESSION_ENABLED !== 'false'
|
|
172
253
|
};
|
|
173
254
|
|
|
174
255
|
const _options = Object.assign(defaultOptions, options);
|
|
@@ -178,21 +259,22 @@ export class Cache {
|
|
|
178
259
|
_options.expire = 120;
|
|
179
260
|
}
|
|
180
261
|
|
|
181
|
-
logger.debug('Cache wrap', { key, formattedKey, _options });
|
|
182
|
-
|
|
183
262
|
if (_options.cache) {
|
|
184
|
-
let cachedValue;
|
|
263
|
+
let cachedValue: any;
|
|
185
264
|
|
|
186
265
|
if (_options.useProxy) {
|
|
187
266
|
const body = new URLSearchParams();
|
|
188
267
|
|
|
189
268
|
body.append('key', formattedKey);
|
|
269
|
+
if (_options.compressed) {
|
|
270
|
+
body.append('compressed', 'true');
|
|
271
|
+
}
|
|
190
272
|
|
|
191
273
|
cachedValue = await Cache.proxyRequest('POST', body);
|
|
192
|
-
logger.debug('Cache proxy request success', { key });
|
|
193
|
-
logger.trace('Cache proxy request', { key, cachedValue });
|
|
194
274
|
} else {
|
|
195
|
-
cachedValue =
|
|
275
|
+
cachedValue = _options.compressed
|
|
276
|
+
? await Cache.getCompressed(formattedKey)
|
|
277
|
+
: await Cache.get(formattedKey);
|
|
196
278
|
}
|
|
197
279
|
|
|
198
280
|
if (cachedValue) {
|
|
@@ -200,8 +282,6 @@ export class Cache {
|
|
|
200
282
|
}
|
|
201
283
|
}
|
|
202
284
|
|
|
203
|
-
logger.debug('Redis cache miss. Setting new value...', { key });
|
|
204
|
-
|
|
205
285
|
const data = await handler();
|
|
206
286
|
|
|
207
287
|
if (data && _options.cache) {
|
|
@@ -215,14 +295,19 @@ export class Cache {
|
|
|
215
295
|
'expire',
|
|
216
296
|
String(_options?.expire ?? Settings.redis.defaultExpirationTime)
|
|
217
297
|
);
|
|
298
|
+
if (_options.compressed) {
|
|
299
|
+
body.append('compressed', 'true');
|
|
300
|
+
}
|
|
218
301
|
await Cache.proxyRequest('PUT', body);
|
|
219
|
-
|
|
220
|
-
logger.debug('Cache proxy request', { key, body: body.toString() });
|
|
221
302
|
} catch (error) {
|
|
222
303
|
logger.error('Cache proxy error', error);
|
|
223
304
|
}
|
|
224
305
|
} else {
|
|
225
|
-
|
|
306
|
+
if (_options.compressed) {
|
|
307
|
+
await Cache.setCompressed(formattedKey, data, _options?.expire);
|
|
308
|
+
} else {
|
|
309
|
+
await Cache.set(formattedKey, JSON.stringify(data), _options?.expire);
|
|
310
|
+
}
|
|
226
311
|
}
|
|
227
312
|
}
|
|
228
313
|
|
|
@@ -234,7 +319,7 @@ export class Cache {
|
|
|
234
319
|
await fetch(Cache.PROXY_URL, {
|
|
235
320
|
method,
|
|
236
321
|
headers: {
|
|
237
|
-
authorization: process.env.CACHE_SECRET
|
|
322
|
+
authorization: process.env.CACHE_SECRET || ''
|
|
238
323
|
},
|
|
239
324
|
body
|
|
240
325
|
})
|
|
@@ -242,4 +327,148 @@ export class Cache {
|
|
|
242
327
|
|
|
243
328
|
return response;
|
|
244
329
|
}
|
|
330
|
+
|
|
331
|
+
static async mset(
|
|
332
|
+
keyValuePairs: Record<string, any>,
|
|
333
|
+
expire?: number
|
|
334
|
+
): Promise<boolean> {
|
|
335
|
+
const validation = Cache.validateKeyValuePairs(keyValuePairs);
|
|
336
|
+
if (!validation.isValid) {
|
|
337
|
+
if (validation.invalidKeys.length > 0) {
|
|
338
|
+
logger.error('Invalid keys in mset', {
|
|
339
|
+
invalidKeys: validation.invalidKeys
|
|
340
|
+
});
|
|
341
|
+
} else {
|
|
342
|
+
logger.warn('mset called with empty keyValuePairs');
|
|
343
|
+
}
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
let success = false;
|
|
348
|
+
let client: RedisClientType | undefined;
|
|
349
|
+
|
|
350
|
+
try {
|
|
351
|
+
client = await Cache.getClient();
|
|
352
|
+
const pipeline = client.multi();
|
|
353
|
+
|
|
354
|
+
Object.entries(keyValuePairs).forEach(([key, value]) => {
|
|
355
|
+
const serializedValue = Cache.serializeValue(value);
|
|
356
|
+
if (expire) {
|
|
357
|
+
pipeline.set(key, serializedValue, { EX: expire });
|
|
358
|
+
} else {
|
|
359
|
+
pipeline.set(key, serializedValue);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const results = await pipeline.exec();
|
|
364
|
+
|
|
365
|
+
const failures =
|
|
366
|
+
results?.filter((result) => result instanceof Error) || [];
|
|
367
|
+
|
|
368
|
+
if (failures.length > 0) {
|
|
369
|
+
success = false;
|
|
370
|
+
} else {
|
|
371
|
+
success = true;
|
|
372
|
+
}
|
|
373
|
+
} catch (error) {
|
|
374
|
+
success = false;
|
|
375
|
+
} finally {
|
|
376
|
+
if (client) {
|
|
377
|
+
await Cache.clientPool.release(client);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return success;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
static async setCompressed(
|
|
385
|
+
key: string,
|
|
386
|
+
value: any,
|
|
387
|
+
expire?: number
|
|
388
|
+
): Promise<boolean> {
|
|
389
|
+
if (!Cache.validateKey(key)) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
let success = false;
|
|
394
|
+
let client: RedisClientType | undefined;
|
|
395
|
+
|
|
396
|
+
try {
|
|
397
|
+
client = await Cache.getClient();
|
|
398
|
+
const serializedValue = Cache.serializeValue(value);
|
|
399
|
+
|
|
400
|
+
try {
|
|
401
|
+
const compressed = await compressData(serializedValue);
|
|
402
|
+
const compressedBase64 = Buffer.from(compressed).toString('base64');
|
|
403
|
+
|
|
404
|
+
if (expire) {
|
|
405
|
+
await client.set(key, compressedBase64, { EX: expire });
|
|
406
|
+
} else {
|
|
407
|
+
await client.set(key, compressedBase64);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
success = true;
|
|
411
|
+
} catch (compressionError) {
|
|
412
|
+
if (expire) {
|
|
413
|
+
await client.set(key, serializedValue, { EX: expire });
|
|
414
|
+
} else {
|
|
415
|
+
await client.set(key, serializedValue);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
success = true;
|
|
419
|
+
}
|
|
420
|
+
} catch (error) {
|
|
421
|
+
success = false;
|
|
422
|
+
} finally {
|
|
423
|
+
if (client) {
|
|
424
|
+
await Cache.clientPool.release(client);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return success;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static async getCompressed(key: string): Promise<unknown> {
|
|
432
|
+
if (!Cache.validateKey(key)) {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let value: unknown;
|
|
437
|
+
let client: RedisClientType | undefined;
|
|
438
|
+
|
|
439
|
+
try {
|
|
440
|
+
client = await Cache.getClient();
|
|
441
|
+
const compressed = await client.get(key);
|
|
442
|
+
|
|
443
|
+
if (compressed) {
|
|
444
|
+
const compressedBuffer = Buffer.from(compressed, 'base64');
|
|
445
|
+
|
|
446
|
+
try {
|
|
447
|
+
const decompressedString = await decompressData(
|
|
448
|
+
new Uint8Array(compressedBuffer)
|
|
449
|
+
);
|
|
450
|
+
value = JSON.parse(decompressedString);
|
|
451
|
+
return value;
|
|
452
|
+
} catch (decompressionError) {
|
|
453
|
+
try {
|
|
454
|
+
const rawString = compressed;
|
|
455
|
+
const parsedData = JSON.parse(rawString);
|
|
456
|
+
return parsedData;
|
|
457
|
+
} catch (jsonError) {
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
} else {
|
|
462
|
+
value = null;
|
|
463
|
+
}
|
|
464
|
+
} catch (error) {
|
|
465
|
+
value = null;
|
|
466
|
+
} finally {
|
|
467
|
+
if (client) {
|
|
468
|
+
await Cache.clientPool.release(client);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return value;
|
|
473
|
+
}
|
|
245
474
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import React, { createContext } from 'react';
|
|
4
4
|
import { getTranslateFn } from '../utils';
|
|
5
|
-
import {
|
|
5
|
+
import { usePzParams } from '../hooks/use-pz-params';
|
|
6
6
|
import { Currency, Locale } from '../types';
|
|
7
7
|
import settings from 'settings';
|
|
8
8
|
|
|
@@ -34,10 +34,7 @@ export default function LocalizationProvider({
|
|
|
34
34
|
localeUrlStrategy
|
|
35
35
|
} = settings.localization;
|
|
36
36
|
|
|
37
|
-
const { locale, currency } =
|
|
38
|
-
locale: string;
|
|
39
|
-
currency: string;
|
|
40
|
-
};
|
|
37
|
+
const { locale, currency } = usePzParams();
|
|
41
38
|
|
|
42
39
|
return (
|
|
43
40
|
<LocalizationContext.Provider
|
|
@@ -64,7 +64,7 @@ const withCheckoutProvider =
|
|
|
64
64
|
const location = request.headers.get('location');
|
|
65
65
|
const redirectUrl = new URL(
|
|
66
66
|
request.headers.get('location'),
|
|
67
|
-
location.startsWith('http') ? '' :
|
|
67
|
+
location.startsWith('http') ? '' : url.origin
|
|
68
68
|
);
|
|
69
69
|
|
|
70
70
|
redirectUrl.pathname = getUrlPathWithLocale(
|
|
@@ -4,6 +4,7 @@ import { Buffer } from 'buffer';
|
|
|
4
4
|
import logger from '../utils/log';
|
|
5
5
|
import { getUrlPathWithLocale } from '../utils/localization';
|
|
6
6
|
import { PzNextRequest } from '.';
|
|
7
|
+
import { getCheckoutPath } from '../utils';
|
|
7
8
|
|
|
8
9
|
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
10
|
if (stream) {
|
|
@@ -34,18 +35,22 @@ const withCompleteGpay =
|
|
|
34
35
|
const url = req.nextUrl.clone();
|
|
35
36
|
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
36
37
|
const sessionId = req.cookies.get('osessionid');
|
|
38
|
+
const currentLocale = req.middlewareParams?.rewrites?.locale;
|
|
37
39
|
|
|
38
40
|
if (url.search.indexOf('GPayCompletePage') === -1) {
|
|
39
41
|
return middleware(req, event);
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
const
|
|
44
|
+
const isPostCheckout = req.cookies.get('pz-post-checkout-flow')?.value === 'true';
|
|
45
|
+
const requestUrl = `${Settings.commerceUrl}${getCheckoutPath(isPostCheckout)}${url.search}`;
|
|
43
46
|
const requestHeaders = {
|
|
44
47
|
'X-Requested-With': 'XMLHttpRequest',
|
|
45
48
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
46
49
|
Cookie: req.headers.get('cookie') ?? '',
|
|
47
50
|
'x-currency': req.cookies.get('pz-currency')?.value ?? '',
|
|
48
|
-
'x-forwarded-for': ip
|
|
51
|
+
'x-forwarded-for': ip,
|
|
52
|
+
'Accept-Language':
|
|
53
|
+
currentLocale ?? req.cookies.get('pz-locale')?.value ?? ''
|
|
49
54
|
};
|
|
50
55
|
|
|
51
56
|
try {
|
|
@@ -59,17 +64,9 @@ const withCompleteGpay =
|
|
|
59
64
|
ip
|
|
60
65
|
}
|
|
61
66
|
);
|
|
62
|
-
|
|
63
|
-
return NextResponse.redirect(
|
|
64
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
65
|
-
'/orders/checkout/',
|
|
66
|
-
req.cookies.get('pz-locale')?.value
|
|
67
|
-
)}`,
|
|
68
|
-
303
|
|
69
|
-
);
|
|
70
67
|
}
|
|
71
68
|
|
|
72
|
-
const
|
|
69
|
+
const fetchResponse = await fetch(requestUrl, {
|
|
73
70
|
method: 'POST',
|
|
74
71
|
headers: requestHeaders,
|
|
75
72
|
body
|
|
@@ -77,14 +74,14 @@ const withCompleteGpay =
|
|
|
77
74
|
|
|
78
75
|
logger.info('Complete GPay payment request', {
|
|
79
76
|
requestUrl,
|
|
80
|
-
status:
|
|
77
|
+
status: fetchResponse.status,
|
|
81
78
|
requestHeaders,
|
|
82
79
|
ip
|
|
83
80
|
});
|
|
84
81
|
|
|
85
|
-
const
|
|
82
|
+
const responseData = await fetchResponse.json();
|
|
86
83
|
|
|
87
|
-
const { context_list: contextList, errors } =
|
|
84
|
+
const { context_list: contextList, errors } = responseData;
|
|
88
85
|
const redirectionContext = contextList?.find(
|
|
89
86
|
(context) => context.page_context?.redirect_url
|
|
90
87
|
);
|
|
@@ -98,18 +95,27 @@ const withCompleteGpay =
|
|
|
98
95
|
ip
|
|
99
96
|
});
|
|
100
97
|
|
|
101
|
-
|
|
98
|
+
const errorResponse = NextResponse.redirect(
|
|
102
99
|
`${url.origin}${getUrlPathWithLocale(
|
|
103
100
|
'/orders/checkout/',
|
|
104
101
|
req.cookies.get('pz-locale')?.value
|
|
105
102
|
)}`,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
103
|
+
303
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
// Forward set-cookie headers from the upstream response
|
|
107
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
108
|
+
setCookies.forEach((cookie) => {
|
|
109
|
+
errorResponse.headers.append('Set-Cookie', cookie);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Add error cookie
|
|
113
|
+
errorResponse.headers.append(
|
|
114
|
+
'Set-Cookie',
|
|
115
|
+
`pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
112
116
|
);
|
|
117
|
+
|
|
118
|
+
return errorResponse;
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
logger.info('Order success page context list', {
|
|
@@ -124,7 +130,7 @@ const withCompleteGpay =
|
|
|
124
130
|
{
|
|
125
131
|
middleware: 'complete-gpay',
|
|
126
132
|
requestHeaders,
|
|
127
|
-
response: JSON.stringify(
|
|
133
|
+
response: JSON.stringify(responseData),
|
|
128
134
|
ip
|
|
129
135
|
}
|
|
130
136
|
);
|
|
@@ -152,10 +158,11 @@ const withCompleteGpay =
|
|
|
152
158
|
// So we use 303 status code to change the method to GET
|
|
153
159
|
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
154
160
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
161
|
+
// Forward all set-cookie headers from the upstream response
|
|
162
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
163
|
+
setCookies.forEach((cookie) => {
|
|
164
|
+
nextResponse.headers.append('Set-Cookie', cookie);
|
|
165
|
+
});
|
|
159
166
|
|
|
160
167
|
return nextResponse;
|
|
161
168
|
} catch (error) {
|