@akinon/pz-flow-payment 1.95.0 → 1.96.0-rc.56
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 +13 -0
- package/package.json +1 -1
- package/src/views/index.tsx +88 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @akinon/pz-flow-payment
|
|
2
2
|
|
|
3
|
+
## 1.96.0-rc.56
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 69e4cc5: BRDG-14604: Refactor FlowPayment component to optimize flow initialization and cleanup logic
|
|
8
|
+
|
|
9
|
+
## 1.96.0-rc.55
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- d8be48fb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
|
|
14
|
+
- 8b1d24eb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
|
|
15
|
+
|
|
3
16
|
## 1.95.0
|
|
4
17
|
|
|
5
18
|
## 1.94.0
|
package/package.json
CHANGED
package/src/views/index.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
PaymentSessionResponse
|
|
9
9
|
} from '@checkout.com/checkout-web-components';
|
|
10
10
|
import { RootState } from '@theme/redux/store';
|
|
11
|
-
import { useEffect } from 'react';
|
|
11
|
+
import { useEffect, useRef, useMemo } from 'react';
|
|
12
12
|
|
|
13
13
|
export default function FlowPayment({
|
|
14
14
|
options
|
|
@@ -56,6 +56,14 @@ export default function FlowPayment({
|
|
|
56
56
|
(state: RootState) => state.checkout
|
|
57
57
|
);
|
|
58
58
|
const dispatch = useAppDispatch();
|
|
59
|
+
const flowComponentRef = useRef<any>(null);
|
|
60
|
+
const lastAmountRef = useRef<string | null>(null);
|
|
61
|
+
const isInitializingRef = useRef<boolean>(false);
|
|
62
|
+
|
|
63
|
+
// Memoize the amount to track actual changes
|
|
64
|
+
const currentAmount = useMemo(() => {
|
|
65
|
+
return preOrder.total_amount;
|
|
66
|
+
}, [preOrder.total_amount]);
|
|
59
67
|
|
|
60
68
|
const initWalletSelection = async () => {
|
|
61
69
|
const walletSelectionPageResponse = await dispatch(
|
|
@@ -81,21 +89,73 @@ export default function FlowPayment({
|
|
|
81
89
|
}: {
|
|
82
90
|
publicKey: string;
|
|
83
91
|
}) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
translations: options?.translations,
|
|
89
|
-
paymentSession: preOrder.context_extras as PaymentSessionResponse,
|
|
90
|
-
appearance: options?.appearance,
|
|
91
|
-
componentOptions: options?.componentOptions
|
|
92
|
-
? { flow: options.componentOptions }
|
|
93
|
-
: undefined
|
|
94
|
-
});
|
|
92
|
+
// Prevent multiple simultaneous initializations
|
|
93
|
+
if (isInitializingRef.current) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
95
96
|
|
|
96
|
-
|
|
97
|
+
// Check if amount has actually changed
|
|
98
|
+
if (lastAmountRef.current === currentAmount && flowComponentRef.current) {
|
|
99
|
+
return; // Don't recreate component if amount hasn't changed
|
|
100
|
+
}
|
|
97
101
|
|
|
98
|
-
|
|
102
|
+
isInitializingRef.current = true;
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
// Destroy existing flow component if it exists
|
|
106
|
+
if (flowComponentRef.current) {
|
|
107
|
+
try {
|
|
108
|
+
flowComponentRef.current.destroy();
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.warn('Error destroying flow component:', error);
|
|
111
|
+
}
|
|
112
|
+
flowComponentRef.current = null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Clear any existing flow component before creating a new one
|
|
116
|
+
const container = document.getElementById('flow-container');
|
|
117
|
+
if (container) {
|
|
118
|
+
container.innerHTML = '';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Only get fresh payment data when amount has changed from the last recorded amount
|
|
122
|
+
let paymentSession = preOrder.context_extras;
|
|
123
|
+
|
|
124
|
+
if (lastAmountRef.current !== currentAmount || !paymentSession) {
|
|
125
|
+
const walletPaymentResponse = await dispatch(
|
|
126
|
+
checkoutApi.endpoints.setWalletPaymentPage.initiate({})
|
|
127
|
+
).unwrap();
|
|
128
|
+
paymentSession =
|
|
129
|
+
walletPaymentResponse?.pre_order?.context_extras ||
|
|
130
|
+
preOrder.context_extras;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Update the last amount reference after getting fresh data
|
|
134
|
+
lastAmountRef.current = currentAmount;
|
|
135
|
+
|
|
136
|
+
const checkout = await loadCheckoutWebComponents({
|
|
137
|
+
publicKey,
|
|
138
|
+
environment: options?.environment || 'production',
|
|
139
|
+
locale: options?.locale || 'en',
|
|
140
|
+
translations: options?.translations,
|
|
141
|
+
paymentSession: paymentSession as PaymentSessionResponse,
|
|
142
|
+
appearance: options?.appearance,
|
|
143
|
+
componentOptions: options?.componentOptions
|
|
144
|
+
? { flow: options.componentOptions }
|
|
145
|
+
: undefined
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const flowComponent = checkout.create('flow');
|
|
149
|
+
flowComponentRef.current = flowComponent;
|
|
150
|
+
|
|
151
|
+
if (container) {
|
|
152
|
+
flowComponent.mount(container);
|
|
153
|
+
}
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error('Error initializing flow payment components:', error);
|
|
156
|
+
} finally {
|
|
157
|
+
isInitializingRef.current = false;
|
|
158
|
+
}
|
|
99
159
|
};
|
|
100
160
|
|
|
101
161
|
useEffect(() => {
|
|
@@ -118,8 +178,22 @@ export default function FlowPayment({
|
|
|
118
178
|
}, [
|
|
119
179
|
preOrder.wallet_method,
|
|
120
180
|
preOrder.token,
|
|
181
|
+
currentAmount, // Use memoized amount instead of preOrder.total_amount
|
|
121
182
|
walletPaymentData?.data?.public_key
|
|
122
183
|
]);
|
|
123
184
|
|
|
185
|
+
// Cleanup function when component unmounts
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
return () => {
|
|
188
|
+
if (flowComponentRef.current) {
|
|
189
|
+
try {
|
|
190
|
+
flowComponentRef.current.destroy();
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.warn('Error destroying flow component on unmount:', error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}, []);
|
|
197
|
+
|
|
124
198
|
return <div id="flow-container" className="flow-payment-container"></div>;
|
|
125
199
|
}
|