@akinon/pz-flow-payment 1.107.0 → 1.108.0-rc.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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/views/index.tsx +99 -3
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/views/index.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { checkoutApi } from '@akinon/next/data/client/checkout';
|
|
2
2
|
import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks';
|
|
3
|
+
import { getCookie } from '@akinon/next/utils';
|
|
4
|
+
import { getUrlPathWithLocale } from '@akinon/next/utils/localization';
|
|
3
5
|
import {
|
|
4
6
|
ComponentOptions,
|
|
5
7
|
CustomTranslations,
|
|
@@ -8,7 +10,23 @@ import {
|
|
|
8
10
|
PaymentSessionResponse
|
|
9
11
|
} from '@checkout.com/checkout-web-components';
|
|
10
12
|
import { RootState } from '@theme/redux/store';
|
|
11
|
-
import { useEffect, useRef, useMemo } from 'react';
|
|
13
|
+
import { useEffect, useRef, useMemo, useState } from 'react';
|
|
14
|
+
|
|
15
|
+
const formatErrorMessage = (errors: any): string => {
|
|
16
|
+
if (typeof errors === 'string') {
|
|
17
|
+
return errors;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (Array.isArray(errors)) {
|
|
21
|
+
return errors.join(', ');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (typeof errors === 'object' && errors !== null) {
|
|
25
|
+
return Object.values(errors).join(', ');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return 'An error occurred during payment processing';
|
|
29
|
+
};
|
|
12
30
|
|
|
13
31
|
export default function FlowPayment({
|
|
14
32
|
options
|
|
@@ -60,6 +78,9 @@ export default function FlowPayment({
|
|
|
60
78
|
const lastAmountRef = useRef<string | null>(null);
|
|
61
79
|
const isInitializingRef = useRef<boolean>(false);
|
|
62
80
|
const walletPaymentInitialized = useRef<boolean>(false);
|
|
81
|
+
const [errors, setErrors] = useState<any>(null);
|
|
82
|
+
|
|
83
|
+
const environment = options?.environment || 'sandbox';
|
|
63
84
|
|
|
64
85
|
// Memoize the amount to track actual changes
|
|
65
86
|
const currentAmount = useMemo(() => {
|
|
@@ -89,6 +110,65 @@ export default function FlowPayment({
|
|
|
89
110
|
}
|
|
90
111
|
};
|
|
91
112
|
|
|
113
|
+
const handlePaymentSuccess = async (paymentData: any) => {
|
|
114
|
+
setErrors(null);
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const walletPaymentPageResponse = await dispatch(
|
|
118
|
+
checkoutApi.endpoints.setWalletPaymentPage.initiate({
|
|
119
|
+
payment_token: JSON.stringify(paymentData)
|
|
120
|
+
})
|
|
121
|
+
).unwrap();
|
|
122
|
+
|
|
123
|
+
if (walletPaymentPageResponse.errors) {
|
|
124
|
+
setErrors(walletPaymentPageResponse.errors);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
walletPaymentPageResponse.context_list.find(
|
|
130
|
+
(c) => c.page_name === 'WalletCompletePage'
|
|
131
|
+
)
|
|
132
|
+
) {
|
|
133
|
+
const paymentCompleteResponse = await dispatch(
|
|
134
|
+
checkoutApi.endpoints.setWalletCompletePage.initiate(true)
|
|
135
|
+
).unwrap();
|
|
136
|
+
|
|
137
|
+
if (paymentCompleteResponse.errors) {
|
|
138
|
+
setErrors(paymentCompleteResponse.errors);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
paymentCompleteResponse.context_list.find(
|
|
144
|
+
(c) => c.page_name === 'ThankYouPage'
|
|
145
|
+
)
|
|
146
|
+
) {
|
|
147
|
+
const redirectUrl = paymentCompleteResponse.context_list.find(
|
|
148
|
+
(c) => c.page_name === 'ThankYouPage'
|
|
149
|
+
)?.page_context.context_data.redirect_url;
|
|
150
|
+
|
|
151
|
+
const redirectUrlWithLocale = `${
|
|
152
|
+
window.location.origin
|
|
153
|
+
}${getUrlPathWithLocale(
|
|
154
|
+
redirectUrl,
|
|
155
|
+
getCookie('pz-locale') || 'en'
|
|
156
|
+
)}`;
|
|
157
|
+
|
|
158
|
+
window.location.href = redirectUrlWithLocale;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error('Error processing payment completion:', error);
|
|
163
|
+
setErrors(error);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const handlePaymentError = async (error: any) => {
|
|
168
|
+
console.error('Payment error occurred:', error);
|
|
169
|
+
setErrors(error);
|
|
170
|
+
};
|
|
171
|
+
|
|
92
172
|
const initFlowPaymentWebComponents = async ({
|
|
93
173
|
publicKey
|
|
94
174
|
}: {
|
|
@@ -158,7 +238,14 @@ export default function FlowPayment({
|
|
|
158
238
|
: undefined
|
|
159
239
|
});
|
|
160
240
|
|
|
161
|
-
const flowComponent = checkout.create('flow'
|
|
241
|
+
const flowComponent = checkout.create('flow', {
|
|
242
|
+
onPaymentCompleted: async (_self, paymentResponse) => {
|
|
243
|
+
handlePaymentSuccess(paymentResponse);
|
|
244
|
+
},
|
|
245
|
+
onError: async (_self, error) => {
|
|
246
|
+
handlePaymentError(error);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
162
249
|
flowComponentRef.current = flowComponent;
|
|
163
250
|
|
|
164
251
|
if (container) {
|
|
@@ -210,5 +297,14 @@ export default function FlowPayment({
|
|
|
210
297
|
};
|
|
211
298
|
}, []);
|
|
212
299
|
|
|
213
|
-
return
|
|
300
|
+
return (
|
|
301
|
+
<div className="flow-payment-container">
|
|
302
|
+
<div id="flow-container"></div>
|
|
303
|
+
{errors && (
|
|
304
|
+
<div className="text-error-100 text-xs mt-5">
|
|
305
|
+
{formatErrorMessage(errors)}
|
|
306
|
+
</div>
|
|
307
|
+
)}
|
|
308
|
+
</div>
|
|
309
|
+
);
|
|
214
310
|
}
|