@akinon/next 1.14.0 → 1.14.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.
- package/.eslintrc.js +40 -40
- package/.prettierrc +13 -13
- package/CHANGELOG.md +6 -0
- package/api/auth.ts +231 -231
- package/api/cache.ts +44 -44
- package/api/client.ts +174 -174
- package/api/logout.ts +42 -42
- package/bin/pz-check-dependencies.js +98 -98
- package/bin/pz-install-plugins.js +33 -33
- package/bin/pz-install-theme.js +58 -58
- package/bin/pz-postbuild.js +1 -1
- package/bin/pz-postdev.js +1 -1
- package/bin/pz-postinstall.js +6 -6
- package/bin/pz-poststart.js +1 -1
- package/bin/pz-prebuild.js +4 -4
- package/bin/pz-predev.js +4 -4
- package/bin/pz-prestart.js +1 -1
- package/bin/run-script.js +44 -44
- package/components/accordion.tsx +52 -52
- package/components/button.tsx +46 -46
- package/components/client-root.tsx +19 -19
- package/components/icon.tsx +18 -18
- package/components/image.tsx +133 -133
- package/components/index.ts +17 -17
- package/components/input.tsx +110 -110
- package/components/lazy-component.tsx +33 -33
- package/components/loader-spinner.tsx +23 -23
- package/components/mobile-app-toggler.tsx +26 -26
- package/components/oauth-login.tsx +24 -24
- package/components/price.tsx +55 -55
- package/components/pz-providers.tsx +24 -24
- package/components/pz-root.tsx +21 -21
- package/components/radio.tsx +18 -18
- package/components/react-portal.tsx +45 -45
- package/components/redirect-three-d/content/index.tsx +74 -74
- package/components/redirect-three-d/index.tsx +17 -17
- package/components/trans.tsx +39 -39
- package/data/client/account.ts +208 -208
- package/data/client/api.ts +85 -85
- package/data/client/basket.ts +82 -82
- package/data/client/misc.ts +101 -101
- package/data/client/product.ts +89 -89
- package/data/client/user.ts +99 -99
- package/data/client/wishlist.ts +118 -118
- package/data/server/category.ts +132 -132
- package/data/server/flatpage.ts +21 -21
- package/data/server/form.ts +22 -22
- package/data/server/index.ts +10 -10
- package/data/server/landingpage.ts +24 -24
- package/data/server/list.ts +67 -67
- package/data/server/menu.ts +35 -35
- package/data/server/product.ts +86 -86
- package/data/server/seo.ts +48 -48
- package/data/server/special-page.ts +47 -47
- package/data/server/widget.ts +27 -27
- package/data/urls.ts +221 -221
- package/hocs/client/index.ts +1 -1
- package/hocs/client/with-segment-defaults.tsx +25 -25
- package/hocs/server/index.ts +1 -1
- package/hocs/server/with-segment-defaults.tsx +85 -85
- package/hooks/index.ts +10 -10
- package/hooks/use-captcha.tsx +76 -76
- package/hooks/use-common-product-attributes.ts +36 -36
- package/hooks/use-debounce.ts +20 -20
- package/hooks/use-localization.ts +78 -78
- package/hooks/use-media-query.ts +36 -36
- package/hooks/use-mobile-iframe-handler.ts +23 -23
- package/hooks/use-on-click-outside.tsx +28 -28
- package/hooks/use-router.ts +45 -45
- package/hooks/use-translation.ts +14 -14
- package/lib/cache.ts +215 -215
- package/localization/index.ts +5 -5
- package/localization/provider.tsx +58 -58
- package/middlewares/currency.ts +100 -100
- package/middlewares/default.ts +256 -256
- package/middlewares/index.ts +29 -29
- package/middlewares/locale.ts +68 -68
- package/middlewares/oauth-login.ts +79 -79
- package/middlewares/pretty-url.ts +104 -104
- package/middlewares/redirection-payment.ts +160 -160
- package/middlewares/three-d-redirection.ts +159 -159
- package/middlewares/url-redirection.ts +65 -65
- package/package.json +2 -2
- package/redux/hooks.ts +7 -7
- package/redux/middlewares/index.ts +50 -50
- package/redux/reducers/checkout.ts +184 -184
- package/redux/reducers/config.ts +28 -28
- package/redux/reducers/header.ts +59 -59
- package/redux/reducers/root.ts +61 -61
- package/sentry/index.ts +27 -27
- package/tailwind/rtl.js +137 -137
- package/types/commerce/account.ts +64 -64
- package/types/commerce/address.ts +94 -94
- package/types/commerce/basket.ts +43 -43
- package/types/commerce/category.ts +114 -114
- package/types/commerce/checkout.ts +143 -143
- package/types/commerce/flatpage.ts +7 -7
- package/types/commerce/form.ts +66 -66
- package/types/commerce/index.ts +12 -12
- package/types/commerce/landingpage.ts +7 -7
- package/types/commerce/misc.ts +127 -127
- package/types/commerce/order.ts +119 -119
- package/types/commerce/product.ts +109 -109
- package/types/commerce/widget.ts +28 -28
- package/types/gtm.ts +16 -16
- package/types/index.ts +274 -274
- package/types/metadata.ts +7 -7
- package/types/next-auth.d.ts +24 -24
- package/utils/app-fetch.ts +69 -69
- package/utils/deep-merge.js +24 -24
- package/utils/image-loader.ts +31 -31
- package/utils/index.ts +150 -150
- package/utils/localization.ts +29 -29
- package/utils/log.ts +138 -138
- package/utils/menu-generator.ts +27 -27
- package/utils/mobile-3d-iframe.ts +77 -77
- package/utils/server-translation.ts +57 -57
- package/utils/server-variables.ts +9 -9
- package/with-pz-config.js +94 -94
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
|
-
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
3
|
-
import Settings from 'settings';
|
|
4
|
-
import logger from '../utils/log';
|
|
5
|
-
import { PzNextRequest } from '.';
|
|
6
|
-
import { getUrlPathWithLocale } from '../utils/localization';
|
|
7
|
-
|
|
8
|
-
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
|
-
if (stream) {
|
|
10
|
-
const chunks = [];
|
|
11
|
-
let result = '';
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
for await (const chunk of stream as any) {
|
|
15
|
-
chunks.push(Buffer.from(chunk));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
result = Buffer.concat(chunks).toString('utf-8');
|
|
19
|
-
} catch (error) {
|
|
20
|
-
logger.error('Error while reading body stream', {
|
|
21
|
-
middleware: 'redirection-payment',
|
|
22
|
-
error
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const withRedirectionPayment =
|
|
32
|
-
(middleware: NextMiddleware) =>
|
|
33
|
-
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
34
|
-
const url = req.nextUrl.clone();
|
|
35
|
-
const searchParams = new URLSearchParams(url.search);
|
|
36
|
-
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
37
|
-
|
|
38
|
-
if (searchParams.get('page') !== 'RedirectionPageCompletePage') {
|
|
39
|
-
return middleware(req, event);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const requestUrl = `${Settings.commerceUrl}/orders/checkout/${url.search}`;
|
|
43
|
-
const requestHeaders = {
|
|
44
|
-
'X-Requested-With': 'XMLHttpRequest',
|
|
45
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
46
|
-
Cookie: req.headers.get('cookie') ?? '',
|
|
47
|
-
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const body = await streamToString(req.body);
|
|
52
|
-
|
|
53
|
-
const request = await fetch(requestUrl, {
|
|
54
|
-
method: 'POST',
|
|
55
|
-
headers: requestHeaders,
|
|
56
|
-
body
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
logger.info('Complete redirection payment request', {
|
|
60
|
-
requestUrl,
|
|
61
|
-
status: request.status,
|
|
62
|
-
requestHeaders,
|
|
63
|
-
ip
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const response = await request.json();
|
|
67
|
-
|
|
68
|
-
const { context_list: contextList, errors } = response;
|
|
69
|
-
const redirectionContext = contextList?.find(
|
|
70
|
-
(context) => context.page_context?.redirect_url
|
|
71
|
-
);
|
|
72
|
-
const redirectUrl = redirectionContext?.page_context?.redirect_url;
|
|
73
|
-
|
|
74
|
-
if (errors && Object.keys(errors).length) {
|
|
75
|
-
logger.error('Error while completing redirection payment', {
|
|
76
|
-
middleware: 'redirection-payment',
|
|
77
|
-
errors,
|
|
78
|
-
requestHeaders,
|
|
79
|
-
ip
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return NextResponse.redirect(
|
|
83
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
84
|
-
'/orders/checkout/',
|
|
85
|
-
req.cookies.get('pz-locale')?.value
|
|
86
|
-
)}`,
|
|
87
|
-
{
|
|
88
|
-
status: 303,
|
|
89
|
-
headers: {
|
|
90
|
-
'Set-Cookie': `pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
logger.info('Order success page context list', {
|
|
97
|
-
middleware: 'redirection-payment',
|
|
98
|
-
contextList,
|
|
99
|
-
ip
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
if (!redirectUrl) {
|
|
103
|
-
logger.warn(
|
|
104
|
-
'No redirection url for order success page found in page_context',
|
|
105
|
-
{
|
|
106
|
-
middleware: 'redirection-payment',
|
|
107
|
-
requestHeaders,
|
|
108
|
-
response: JSON.stringify(response),
|
|
109
|
-
ip
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
114
|
-
'/orders/checkout/',
|
|
115
|
-
req.cookies.get('pz-locale')?.value
|
|
116
|
-
)}`;
|
|
117
|
-
|
|
118
|
-
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
122
|
-
redirectUrl,
|
|
123
|
-
req.cookies.get('pz-locale')?.value
|
|
124
|
-
)}`;
|
|
125
|
-
|
|
126
|
-
logger.info('Redirecting to order success page', {
|
|
127
|
-
middleware: 'redirection-payment',
|
|
128
|
-
redirectUrlWithLocale,
|
|
129
|
-
ip
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// Using POST method while redirecting causes an error,
|
|
133
|
-
// So we use 303 status code to change the method to GET
|
|
134
|
-
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
135
|
-
|
|
136
|
-
nextResponse.headers.set(
|
|
137
|
-
'Set-Cookie',
|
|
138
|
-
request.headers.get('set-cookie') ?? ''
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
return nextResponse;
|
|
142
|
-
} catch (error) {
|
|
143
|
-
logger.error('Error while completing redirection payment', {
|
|
144
|
-
middleware: 'redirection-payment',
|
|
145
|
-
error,
|
|
146
|
-
requestHeaders,
|
|
147
|
-
ip
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
return NextResponse.redirect(
|
|
151
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
152
|
-
'/orders/checkout/',
|
|
153
|
-
req.cookies.get('pz-locale')?.value
|
|
154
|
-
)}`,
|
|
155
|
-
303
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export default withRedirectionPayment;
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
3
|
+
import Settings from 'settings';
|
|
4
|
+
import logger from '../utils/log';
|
|
5
|
+
import { PzNextRequest } from '.';
|
|
6
|
+
import { getUrlPathWithLocale } from '../utils/localization';
|
|
7
|
+
|
|
8
|
+
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
|
+
if (stream) {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
let result = '';
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
for await (const chunk of stream as any) {
|
|
15
|
+
chunks.push(Buffer.from(chunk));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
result = Buffer.concat(chunks).toString('utf-8');
|
|
19
|
+
} catch (error) {
|
|
20
|
+
logger.error('Error while reading body stream', {
|
|
21
|
+
middleware: 'redirection-payment',
|
|
22
|
+
error
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const withRedirectionPayment =
|
|
32
|
+
(middleware: NextMiddleware) =>
|
|
33
|
+
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
34
|
+
const url = req.nextUrl.clone();
|
|
35
|
+
const searchParams = new URLSearchParams(url.search);
|
|
36
|
+
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
37
|
+
|
|
38
|
+
if (searchParams.get('page') !== 'RedirectionPageCompletePage') {
|
|
39
|
+
return middleware(req, event);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const requestUrl = `${Settings.commerceUrl}/orders/checkout/${url.search}`;
|
|
43
|
+
const requestHeaders = {
|
|
44
|
+
'X-Requested-With': 'XMLHttpRequest',
|
|
45
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
46
|
+
Cookie: req.headers.get('cookie') ?? '',
|
|
47
|
+
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const body = await streamToString(req.body);
|
|
52
|
+
|
|
53
|
+
const request = await fetch(requestUrl, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: requestHeaders,
|
|
56
|
+
body
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
logger.info('Complete redirection payment request', {
|
|
60
|
+
requestUrl,
|
|
61
|
+
status: request.status,
|
|
62
|
+
requestHeaders,
|
|
63
|
+
ip
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const response = await request.json();
|
|
67
|
+
|
|
68
|
+
const { context_list: contextList, errors } = response;
|
|
69
|
+
const redirectionContext = contextList?.find(
|
|
70
|
+
(context) => context.page_context?.redirect_url
|
|
71
|
+
);
|
|
72
|
+
const redirectUrl = redirectionContext?.page_context?.redirect_url;
|
|
73
|
+
|
|
74
|
+
if (errors && Object.keys(errors).length) {
|
|
75
|
+
logger.error('Error while completing redirection payment', {
|
|
76
|
+
middleware: 'redirection-payment',
|
|
77
|
+
errors,
|
|
78
|
+
requestHeaders,
|
|
79
|
+
ip
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return NextResponse.redirect(
|
|
83
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
84
|
+
'/orders/checkout/',
|
|
85
|
+
req.cookies.get('pz-locale')?.value
|
|
86
|
+
)}`,
|
|
87
|
+
{
|
|
88
|
+
status: 303,
|
|
89
|
+
headers: {
|
|
90
|
+
'Set-Cookie': `pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
logger.info('Order success page context list', {
|
|
97
|
+
middleware: 'redirection-payment',
|
|
98
|
+
contextList,
|
|
99
|
+
ip
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (!redirectUrl) {
|
|
103
|
+
logger.warn(
|
|
104
|
+
'No redirection url for order success page found in page_context',
|
|
105
|
+
{
|
|
106
|
+
middleware: 'redirection-payment',
|
|
107
|
+
requestHeaders,
|
|
108
|
+
response: JSON.stringify(response),
|
|
109
|
+
ip
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
114
|
+
'/orders/checkout/',
|
|
115
|
+
req.cookies.get('pz-locale')?.value
|
|
116
|
+
)}`;
|
|
117
|
+
|
|
118
|
+
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
122
|
+
redirectUrl,
|
|
123
|
+
req.cookies.get('pz-locale')?.value
|
|
124
|
+
)}`;
|
|
125
|
+
|
|
126
|
+
logger.info('Redirecting to order success page', {
|
|
127
|
+
middleware: 'redirection-payment',
|
|
128
|
+
redirectUrlWithLocale,
|
|
129
|
+
ip
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Using POST method while redirecting causes an error,
|
|
133
|
+
// So we use 303 status code to change the method to GET
|
|
134
|
+
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
135
|
+
|
|
136
|
+
nextResponse.headers.set(
|
|
137
|
+
'Set-Cookie',
|
|
138
|
+
request.headers.get('set-cookie') ?? ''
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return nextResponse;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
logger.error('Error while completing redirection payment', {
|
|
144
|
+
middleware: 'redirection-payment',
|
|
145
|
+
error,
|
|
146
|
+
requestHeaders,
|
|
147
|
+
ip
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return NextResponse.redirect(
|
|
151
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
152
|
+
'/orders/checkout/',
|
|
153
|
+
req.cookies.get('pz-locale')?.value
|
|
154
|
+
)}`,
|
|
155
|
+
303
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export default withRedirectionPayment;
|
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
2
|
-
import Settings from 'settings';
|
|
3
|
-
import { Buffer } from 'buffer';
|
|
4
|
-
import logger from '../utils/log';
|
|
5
|
-
import { getUrlPathWithLocale } from '../utils/localization';
|
|
6
|
-
import { PzNextRequest } from '.';
|
|
7
|
-
|
|
8
|
-
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
|
-
if (stream) {
|
|
10
|
-
const chunks = [];
|
|
11
|
-
let result = '';
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
for await (const chunk of stream as any) {
|
|
15
|
-
chunks.push(Buffer.from(chunk));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
result = Buffer.concat(chunks).toString('utf-8');
|
|
19
|
-
} catch (error) {
|
|
20
|
-
logger.error('Error while reading body stream', {
|
|
21
|
-
middleware: 'three-d-redirection',
|
|
22
|
-
error
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const withThreeDRedirection =
|
|
32
|
-
(middleware: NextMiddleware) =>
|
|
33
|
-
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
34
|
-
const url = req.nextUrl.clone();
|
|
35
|
-
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
36
|
-
|
|
37
|
-
if (url.search.indexOf('CreditCardThreeDSecurePage') === -1) {
|
|
38
|
-
return middleware(req, event);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const requestUrl = `${Settings.commerceUrl}/orders/checkout/${url.search}`;
|
|
42
|
-
const requestHeaders = {
|
|
43
|
-
'X-Requested-With': 'XMLHttpRequest',
|
|
44
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
45
|
-
Cookie: `osessionid=${req.cookies.get('osessionid')?.value ?? ''}`,
|
|
46
|
-
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
const body = await streamToString(req.body);
|
|
51
|
-
|
|
52
|
-
const request = await fetch(requestUrl, {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: requestHeaders,
|
|
55
|
-
body
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
logger.info('Complete 3D payment request', {
|
|
59
|
-
requestUrl,
|
|
60
|
-
status: request.status,
|
|
61
|
-
requestHeaders,
|
|
62
|
-
ip
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const response = await request.json();
|
|
66
|
-
|
|
67
|
-
const { context_list: contextList, errors } = response;
|
|
68
|
-
const redirectionContext = contextList?.find(
|
|
69
|
-
(context) => context.page_context?.redirect_url
|
|
70
|
-
);
|
|
71
|
-
const redirectUrl = redirectionContext?.page_context?.redirect_url;
|
|
72
|
-
|
|
73
|
-
if (errors && Object.keys(errors).length) {
|
|
74
|
-
logger.error('Error while completing 3D payment', {
|
|
75
|
-
middleware: 'three-d-redirection',
|
|
76
|
-
errors,
|
|
77
|
-
requestHeaders,
|
|
78
|
-
ip
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
return NextResponse.redirect(
|
|
82
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
83
|
-
'/orders/checkout/',
|
|
84
|
-
req.cookies.get('pz-locale')?.value
|
|
85
|
-
)}`,
|
|
86
|
-
{
|
|
87
|
-
status: 303,
|
|
88
|
-
headers: {
|
|
89
|
-
'Set-Cookie': `pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
logger.info('Order success page context list', {
|
|
96
|
-
middleware: 'three-d-redirection',
|
|
97
|
-
contextList,
|
|
98
|
-
ip
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
if (!redirectUrl) {
|
|
102
|
-
logger.warn(
|
|
103
|
-
'No redirection url for order success page found in page_context. Redirecting to checkout page.',
|
|
104
|
-
{
|
|
105
|
-
middleware: 'three-d-redirection',
|
|
106
|
-
requestHeaders,
|
|
107
|
-
response: JSON.stringify(response),
|
|
108
|
-
ip
|
|
109
|
-
}
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
113
|
-
'/orders/checkout/',
|
|
114
|
-
req.cookies.get('pz-locale')?.value
|
|
115
|
-
)}`;
|
|
116
|
-
|
|
117
|
-
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
121
|
-
redirectUrl,
|
|
122
|
-
req.cookies.get('pz-locale')?.value
|
|
123
|
-
)}`;
|
|
124
|
-
|
|
125
|
-
logger.info('Redirecting to order success page', {
|
|
126
|
-
middleware: 'three-d-redirection',
|
|
127
|
-
redirectUrlWithLocale,
|
|
128
|
-
ip
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// Using POST method while redirecting causes an error,
|
|
132
|
-
// So we use 303 status code to change the method to GET
|
|
133
|
-
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
134
|
-
|
|
135
|
-
nextResponse.headers.set(
|
|
136
|
-
'Set-Cookie',
|
|
137
|
-
request.headers.get('set-cookie') ?? ''
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
return nextResponse;
|
|
141
|
-
} catch (error) {
|
|
142
|
-
logger.error('Error while completing 3D payment', {
|
|
143
|
-
middleware: 'three-d-redirection',
|
|
144
|
-
error,
|
|
145
|
-
requestHeaders,
|
|
146
|
-
ip
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
return NextResponse.redirect(
|
|
150
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
151
|
-
'/orders/checkout/',
|
|
152
|
-
req.cookies.get('pz-locale')?.value
|
|
153
|
-
)}`,
|
|
154
|
-
303
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export default withThreeDRedirection;
|
|
1
|
+
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
2
|
+
import Settings from 'settings';
|
|
3
|
+
import { Buffer } from 'buffer';
|
|
4
|
+
import logger from '../utils/log';
|
|
5
|
+
import { getUrlPathWithLocale } from '../utils/localization';
|
|
6
|
+
import { PzNextRequest } from '.';
|
|
7
|
+
|
|
8
|
+
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
|
+
if (stream) {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
let result = '';
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
for await (const chunk of stream as any) {
|
|
15
|
+
chunks.push(Buffer.from(chunk));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
result = Buffer.concat(chunks).toString('utf-8');
|
|
19
|
+
} catch (error) {
|
|
20
|
+
logger.error('Error while reading body stream', {
|
|
21
|
+
middleware: 'three-d-redirection',
|
|
22
|
+
error
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const withThreeDRedirection =
|
|
32
|
+
(middleware: NextMiddleware) =>
|
|
33
|
+
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
34
|
+
const url = req.nextUrl.clone();
|
|
35
|
+
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
36
|
+
|
|
37
|
+
if (url.search.indexOf('CreditCardThreeDSecurePage') === -1) {
|
|
38
|
+
return middleware(req, event);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const requestUrl = `${Settings.commerceUrl}/orders/checkout/${url.search}`;
|
|
42
|
+
const requestHeaders = {
|
|
43
|
+
'X-Requested-With': 'XMLHttpRequest',
|
|
44
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
45
|
+
Cookie: `osessionid=${req.cookies.get('osessionid')?.value ?? ''}`,
|
|
46
|
+
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const body = await streamToString(req.body);
|
|
51
|
+
|
|
52
|
+
const request = await fetch(requestUrl, {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
headers: requestHeaders,
|
|
55
|
+
body
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
logger.info('Complete 3D payment request', {
|
|
59
|
+
requestUrl,
|
|
60
|
+
status: request.status,
|
|
61
|
+
requestHeaders,
|
|
62
|
+
ip
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const response = await request.json();
|
|
66
|
+
|
|
67
|
+
const { context_list: contextList, errors } = response;
|
|
68
|
+
const redirectionContext = contextList?.find(
|
|
69
|
+
(context) => context.page_context?.redirect_url
|
|
70
|
+
);
|
|
71
|
+
const redirectUrl = redirectionContext?.page_context?.redirect_url;
|
|
72
|
+
|
|
73
|
+
if (errors && Object.keys(errors).length) {
|
|
74
|
+
logger.error('Error while completing 3D payment', {
|
|
75
|
+
middleware: 'three-d-redirection',
|
|
76
|
+
errors,
|
|
77
|
+
requestHeaders,
|
|
78
|
+
ip
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return NextResponse.redirect(
|
|
82
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
83
|
+
'/orders/checkout/',
|
|
84
|
+
req.cookies.get('pz-locale')?.value
|
|
85
|
+
)}`,
|
|
86
|
+
{
|
|
87
|
+
status: 303,
|
|
88
|
+
headers: {
|
|
89
|
+
'Set-Cookie': `pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
logger.info('Order success page context list', {
|
|
96
|
+
middleware: 'three-d-redirection',
|
|
97
|
+
contextList,
|
|
98
|
+
ip
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (!redirectUrl) {
|
|
102
|
+
logger.warn(
|
|
103
|
+
'No redirection url for order success page found in page_context. Redirecting to checkout page.',
|
|
104
|
+
{
|
|
105
|
+
middleware: 'three-d-redirection',
|
|
106
|
+
requestHeaders,
|
|
107
|
+
response: JSON.stringify(response),
|
|
108
|
+
ip
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
113
|
+
'/orders/checkout/',
|
|
114
|
+
req.cookies.get('pz-locale')?.value
|
|
115
|
+
)}`;
|
|
116
|
+
|
|
117
|
+
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
121
|
+
redirectUrl,
|
|
122
|
+
req.cookies.get('pz-locale')?.value
|
|
123
|
+
)}`;
|
|
124
|
+
|
|
125
|
+
logger.info('Redirecting to order success page', {
|
|
126
|
+
middleware: 'three-d-redirection',
|
|
127
|
+
redirectUrlWithLocale,
|
|
128
|
+
ip
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Using POST method while redirecting causes an error,
|
|
132
|
+
// So we use 303 status code to change the method to GET
|
|
133
|
+
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
134
|
+
|
|
135
|
+
nextResponse.headers.set(
|
|
136
|
+
'Set-Cookie',
|
|
137
|
+
request.headers.get('set-cookie') ?? ''
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return nextResponse;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
logger.error('Error while completing 3D payment', {
|
|
143
|
+
middleware: 'three-d-redirection',
|
|
144
|
+
error,
|
|
145
|
+
requestHeaders,
|
|
146
|
+
ip
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return NextResponse.redirect(
|
|
150
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
151
|
+
'/orders/checkout/',
|
|
152
|
+
req.cookies.get('pz-locale')?.value
|
|
153
|
+
)}`,
|
|
154
|
+
303
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export default withThreeDRedirection;
|