@akinon/pz-flow-payment 2.0.11-beta.0 → 2.0.11-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 +5 -1
- package/package.json +1 -1
- package/src/views/index.tsx +62 -40
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/views/index.tsx
CHANGED
|
@@ -76,7 +76,10 @@ export default function FlowPayment({
|
|
|
76
76
|
const dispatch = useAppDispatch();
|
|
77
77
|
const flowComponentRef = useRef<any>(null);
|
|
78
78
|
const lastAmountRef = useRef<string | null>(null);
|
|
79
|
+
const lastSessionRef = useRef<unknown>(null);
|
|
80
|
+
const prevUnpaidRef = useRef(parseFloat(preOrder.unpaid_amount || '0') || 0);
|
|
79
81
|
const isInitializingRef = useRef<boolean>(false);
|
|
82
|
+
const walletInitInFlightRef = useRef<boolean>(false);
|
|
80
83
|
const [errors, setErrors] = useState<any>(null);
|
|
81
84
|
const [walletSelectionReady, setWalletSelectionReady] =
|
|
82
85
|
useState<boolean>(false);
|
|
@@ -88,45 +91,51 @@ export default function FlowPayment({
|
|
|
88
91
|
}, [preOrder.total_amount]);
|
|
89
92
|
|
|
90
93
|
const initWalletSelection = async () => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
})
|
|
95
|
-
).unwrap();
|
|
96
|
-
|
|
97
|
-
const walletPaymentPageContext =
|
|
98
|
-
walletSelectionPageResponse.context_list.find(
|
|
99
|
-
(c) => c.page_name === 'WalletPaymentPage'
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
if (!walletPaymentPageContext) {
|
|
103
|
-
setErrors(
|
|
104
|
-
'Payment method not available. Please try a different payment option.'
|
|
105
|
-
);
|
|
106
|
-
setWalletSelectionReady(false);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
94
|
+
if (walletInitInFlightRef.current) return;
|
|
95
|
+
walletInitInFlightRef.current = true;
|
|
96
|
+
setWalletSelectionReady(false);
|
|
109
97
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
).unwrap();
|
|
117
|
-
|
|
118
|
-
if (walletPaymentPageResponse.errors) {
|
|
119
|
-
setErrors(walletPaymentPageResponse.errors);
|
|
120
|
-
setWalletSelectionReady(false);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
98
|
+
try {
|
|
99
|
+
const walletSelectionPageResponse = await dispatch(
|
|
100
|
+
checkoutApi.endpoints.setWalletSelectionPage.initiate({
|
|
101
|
+
payment_option: preOrder.payment_option?.pk
|
|
102
|
+
})
|
|
103
|
+
).unwrap();
|
|
123
104
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
105
|
+
const walletPaymentPageContext =
|
|
106
|
+
walletSelectionPageResponse.context_list.find(
|
|
107
|
+
(c) => c.page_name === 'WalletPaymentPage'
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
if (!walletPaymentPageContext) {
|
|
111
|
+
setErrors(
|
|
112
|
+
'Payment method not available. Please try a different payment option.'
|
|
113
|
+
);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Initialize wallet payment page to get payment session data
|
|
118
|
+
const walletPaymentPageResponse = await dispatch(
|
|
119
|
+
checkoutApi.endpoints.setWalletPaymentPage.initiate({
|
|
120
|
+
payment_method: 'checkout_flow',
|
|
121
|
+
wallet_method: preOrder.wallet_method
|
|
122
|
+
})
|
|
123
|
+
).unwrap();
|
|
124
|
+
|
|
125
|
+
if (walletPaymentPageResponse.errors) {
|
|
126
|
+
setErrors(walletPaymentPageResponse.errors);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Store the order number from the wallet payment page response
|
|
131
|
+
if (walletPaymentPageResponse.pre_order?.number) {
|
|
132
|
+
setOrderNumber(walletPaymentPageResponse.pre_order.number);
|
|
133
|
+
}
|
|
128
134
|
|
|
129
|
-
|
|
135
|
+
setWalletSelectionReady(true);
|
|
136
|
+
} finally {
|
|
137
|
+
walletInitInFlightRef.current = false;
|
|
138
|
+
}
|
|
130
139
|
};
|
|
131
140
|
|
|
132
141
|
// Reset payment state and get a fresh orderNumber + payment session for retry
|
|
@@ -193,9 +202,13 @@ export default function FlowPayment({
|
|
|
193
202
|
return;
|
|
194
203
|
}
|
|
195
204
|
|
|
196
|
-
//
|
|
197
|
-
if (
|
|
198
|
-
|
|
205
|
+
// Skip if neither amount nor payment session has changed
|
|
206
|
+
if (
|
|
207
|
+
lastAmountRef.current === currentAmount &&
|
|
208
|
+
lastSessionRef.current === preOrder.context_extras &&
|
|
209
|
+
flowComponentRef.current
|
|
210
|
+
) {
|
|
211
|
+
return;
|
|
199
212
|
}
|
|
200
213
|
|
|
201
214
|
isInitializingRef.current = true;
|
|
@@ -228,8 +241,9 @@ export default function FlowPayment({
|
|
|
228
241
|
return;
|
|
229
242
|
}
|
|
230
243
|
|
|
231
|
-
// Update
|
|
244
|
+
// Update tracking refs after getting fresh data
|
|
232
245
|
lastAmountRef.current = currentAmount;
|
|
246
|
+
lastSessionRef.current = paymentSession;
|
|
233
247
|
|
|
234
248
|
const checkout = await loadCheckoutWebComponents({
|
|
235
249
|
publicKey,
|
|
@@ -267,6 +281,14 @@ export default function FlowPayment({
|
|
|
267
281
|
initWalletSelection();
|
|
268
282
|
}, []);
|
|
269
283
|
|
|
284
|
+
// Re-init wallet selection when unpaid amount actually changes
|
|
285
|
+
useEffect(() => {
|
|
286
|
+
const current = parseFloat(preOrder.unpaid_amount || '0') || 0;
|
|
287
|
+
if (prevUnpaidRef.current === current) return;
|
|
288
|
+
prevUnpaidRef.current = current;
|
|
289
|
+
initWalletSelection();
|
|
290
|
+
}, [preOrder.unpaid_amount]);
|
|
291
|
+
|
|
270
292
|
useEffect(() => {
|
|
271
293
|
if (
|
|
272
294
|
!preOrder.wallet_method ||
|