@akinon/pz-flow-payment 1.107.0 → 1.108.0-rc.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @akinon/pz-flow-payment
2
2
 
3
+ ## 1.108.0-rc.1
4
+
5
+ ### Minor Changes
6
+
7
+ - 21d88c1: ZERO-3640: Remove redundant API calls
8
+
9
+ ## 1.108.0-rc.0
10
+
11
+ ### Minor Changes
12
+
13
+ - ce2e9e20: ZERO-3640: Add error handling and WalletCompletePage request to FlowPayment
14
+
3
15
  ## 1.107.0
4
16
 
5
17
  ## 1.106.0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akinon/pz-flow-payment",
3
3
  "license": "MIT",
4
- "version": "1.107.0",
4
+ "version": "1.108.0-rc.1",
5
5
  "main": "src/index.tsx",
6
6
  "dependencies": {
7
7
  "@checkout.com/checkout-web-components": "0.7.0-beta"
@@ -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,7 @@ 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);
63
82
 
64
83
  // Memoize the amount to track actual changes
65
84
  const currentAmount = useMemo(() => {
@@ -89,6 +108,48 @@ export default function FlowPayment({
89
108
  }
90
109
  };
91
110
 
111
+ const handlePaymentSuccess = async (paymentData: any) => {
112
+ setErrors(null);
113
+
114
+ try {
115
+ const paymentCompleteResponse = await dispatch(
116
+ checkoutApi.endpoints.setWalletCompletePage.initiate(true)
117
+ ).unwrap();
118
+
119
+ if (paymentCompleteResponse.errors) {
120
+ setErrors(paymentCompleteResponse.errors);
121
+ return;
122
+ }
123
+
124
+ if (
125
+ paymentCompleteResponse.context_list.find(
126
+ (c) => c.page_name === 'ThankYouPage'
127
+ )
128
+ ) {
129
+ const redirectUrl = paymentCompleteResponse.context_list.find(
130
+ (c) => c.page_name === 'ThankYouPage'
131
+ )?.page_context.context_data.redirect_url;
132
+
133
+ const redirectUrlWithLocale = `${
134
+ window.location.origin
135
+ }${getUrlPathWithLocale(
136
+ redirectUrl,
137
+ getCookie('pz-locale') || 'en'
138
+ )}`;
139
+
140
+ window.location.href = redirectUrlWithLocale;
141
+ }
142
+ } catch (error) {
143
+ console.error('Error processing payment completion:', error);
144
+ setErrors(error);
145
+ }
146
+ };
147
+
148
+ const handlePaymentError = async (error: any) => {
149
+ console.error('Payment error occurred:', error);
150
+ setErrors(error);
151
+ };
152
+
92
153
  const initFlowPaymentWebComponents = async ({
93
154
  publicKey
94
155
  }: {
@@ -158,7 +219,14 @@ export default function FlowPayment({
158
219
  : undefined
159
220
  });
160
221
 
161
- const flowComponent = checkout.create('flow');
222
+ const flowComponent = checkout.create('flow', {
223
+ onPaymentCompleted: async (_self, paymentResponse) => {
224
+ handlePaymentSuccess(paymentResponse);
225
+ },
226
+ onError: async (_self, error) => {
227
+ handlePaymentError(error);
228
+ }
229
+ });
162
230
  flowComponentRef.current = flowComponent;
163
231
 
164
232
  if (container) {
@@ -210,5 +278,14 @@ export default function FlowPayment({
210
278
  };
211
279
  }, []);
212
280
 
213
- return <div id="flow-container" className="flow-payment-container"></div>;
281
+ return (
282
+ <div className="flow-payment-container">
283
+ <div id="flow-container"></div>
284
+ {errors && (
285
+ <div className="text-error-100 text-xs mt-5">
286
+ {formatErrorMessage(errors)}
287
+ </div>
288
+ )}
289
+ </div>
290
+ );
214
291
  }