@akinon/pz-flow-payment 1.102.0-rc.79 → 1.102.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 CHANGED
@@ -1,16 +1,6 @@
1
1
  # @akinon/pz-flow-payment
2
2
 
3
- ## 1.102.0-rc.79
4
-
5
- ## 1.102.0-rc.78
6
-
7
- ## 1.102.0-rc.77
8
-
9
- ### Minor Changes
10
-
11
- - 1b9e9be: BRDG-14604: Refactor FlowPayment component to utilize RTK Query mutations for payment options and wallet selection, enhancing session management and initialization logic
12
- - d8be48fb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
13
- - 8b1d24eb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
3
+ ## 1.102.0
14
4
 
15
5
  ## 1.101.0
16
6
 
@@ -39,29 +29,6 @@
39
29
  ### Minor Changes
40
30
 
41
31
  - 69e4cc5: BRDG-14604: Refactor FlowPayment component to optimize flow initialization and cleanup logic
42
- - d8be48fb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
43
- - 8b1d24eb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
44
-
45
- ## 1.96.0-rc.60
46
-
47
- ## 1.96.0-rc.59
48
-
49
- ## 1.96.0-rc.58
50
-
51
- ## 1.96.0-rc.57
52
-
53
- ## 1.96.0-rc.56
54
-
55
- ### Minor Changes
56
-
57
- - 69e4cc5: BRDG-14604: Refactor FlowPayment component to optimize flow initialization and cleanup logic
58
-
59
- ## 1.96.0-rc.55
60
-
61
- ### Minor Changes
62
-
63
- - d8be48fb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
64
- - 8b1d24eb: ZERO-3422: Update fetch method to use dynamic request method in wallet complete redirection middleware
65
32
 
66
33
  ## 1.95.0
67
34
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akinon/pz-flow-payment",
3
3
  "license": "MIT",
4
- "version": "1.102.0-rc.79",
4
+ "version": "1.102.0",
5
5
  "main": "src/index.tsx",
6
6
  "dependencies": {
7
7
  "@checkout.com/checkout-web-components": "0.7.0-beta"
@@ -1,10 +1,5 @@
1
1
  import { checkoutApi } from '@akinon/next/data/client/checkout';
2
2
  import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks';
3
- import {
4
- useSetPaymentOptionMutation,
5
- useSetWalletSelectionPageMutation,
6
- useSetWalletPaymentPageMutation
7
- } from '@akinon/next/data/client/checkout';
8
3
  import {
9
4
  ComponentOptions,
10
5
  CustomTranslations,
@@ -66,38 +61,35 @@ export default function FlowPayment({
66
61
  const isInitializingRef = useRef<boolean>(false);
67
62
  const walletPaymentInitialized = useRef<boolean>(false);
68
63
 
69
- // RTK Query mutations
70
- const [setPaymentOption] = useSetPaymentOptionMutation();
71
- const [setWalletSelectionPage] = useSetWalletSelectionPageMutation();
72
- const [setWalletPaymentPage] = useSetWalletPaymentPageMutation();
73
-
74
64
  // Memoize the amount to track actual changes
75
65
  const currentAmount = useMemo(() => {
76
66
  return preOrder.total_amount;
77
67
  }, [preOrder.total_amount]);
78
68
 
79
69
  const initWalletSelection = async () => {
80
- if (!walletPaymentInitialized.current) {
81
- walletPaymentInitialized.current = true;
70
+ const walletSelectionPageResponse = await dispatch(
71
+ checkoutApi.endpoints.setWalletSelectionPage.initiate({
72
+ payment_option: preOrder.payment_option?.pk
73
+ })
74
+ ).unwrap();
82
75
 
83
- const walletSelectionPageResponse = await dispatch(
84
- checkoutApi.endpoints.setWalletSelectionPage.initiate({
85
- payment_option: preOrder.payment_option?.pk
86
- })
87
- ).unwrap();
76
+ const walletPaymentPageContext =
77
+ walletSelectionPageResponse.context_list.find(
78
+ (c) => c.page_name === 'WalletPaymentPage'
79
+ );
88
80
 
89
- const walletPaymentPageContext =
90
- walletSelectionPageResponse.context_list.find(
91
- (c) => c.page_name === 'WalletPaymentPage'
92
- );
81
+ if (!walletPaymentPageContext) {
82
+ return;
83
+ }
93
84
 
94
- if (walletPaymentPageContext) {
95
- dispatch(checkoutApi.endpoints.setWalletPaymentPage.initiate({}));
96
- }
85
+ // FIX: Only call setWalletPaymentPage if not already initialized
86
+ if (!walletPaymentInitialized.current) {
87
+ walletPaymentInitialized.current = true;
88
+ dispatch(checkoutApi.endpoints.setWalletPaymentPage.initiate({}));
97
89
  }
98
90
  };
99
91
 
100
- const refreshPaymentSessionAndInitFlow = async ({
92
+ const initFlowPaymentWebComponents = async ({
101
93
  publicKey
102
94
  }: {
103
95
  publicKey: string;
@@ -107,70 +99,13 @@ export default function FlowPayment({
107
99
  return;
108
100
  }
109
101
 
110
- isInitializingRef.current = true;
111
-
112
- try {
113
- // Step 1: Re-set the current payment option to refresh the session with new amount
114
- if (preOrder.payment_option?.pk) {
115
- const paymentOptionResponse = await setPaymentOption(
116
- preOrder.payment_option.pk
117
- ).unwrap();
118
-
119
- // Check if WalletSelectionPage is available in the context list
120
- const walletSelectionPageContext =
121
- paymentOptionResponse.context_list?.find(
122
- (c) => c.page_name === 'WalletSelectionPage'
123
- );
124
-
125
- if (!walletSelectionPageContext) {
126
- console.warn(
127
- 'WalletSelectionPage not found in payment option response context list'
128
- );
129
- return;
130
- }
131
- }
132
-
133
- // Step 2: Set wallet selection page
134
- const walletSelectionResponse = await setWalletSelectionPage({
135
- payment_option: preOrder.payment_option?.pk
136
- }).unwrap();
137
-
138
- const walletPaymentPageContext =
139
- walletSelectionResponse.context_list?.find(
140
- (c) => c.page_name === 'WalletPaymentPage'
141
- );
142
-
143
- if (!walletPaymentPageContext) {
144
- return;
145
- }
146
-
147
- // Step 3: Set wallet payment page to get updated payment session
148
- const walletPaymentResponse = await setWalletPaymentPage({}).unwrap();
149
-
150
- // Step 4: Initialize flow component with fresh payment session
151
- await initFlowPaymentWebComponents({
152
- publicKey,
153
- paymentSession:
154
- walletPaymentResponse?.pre_order?.context_extras ||
155
- preOrder.context_extras
156
- });
157
- } catch (error) {
158
- console.error(
159
- 'Error refreshing payment session and initializing flow:',
160
- error
161
- );
162
- } finally {
163
- isInitializingRef.current = false;
102
+ // Check if amount has actually changed
103
+ if (lastAmountRef.current === currentAmount && flowComponentRef.current) {
104
+ return; // Don't recreate component if amount hasn't changed
164
105
  }
165
- };
166
106
 
167
- const initFlowPaymentWebComponents = async ({
168
- publicKey,
169
- paymentSession
170
- }: {
171
- publicKey: string;
172
- paymentSession?: any;
173
- }) => {
107
+ isInitializingRef.current = true;
108
+
174
109
  try {
175
110
  // Destroy existing flow component if it exists
176
111
  if (flowComponentRef.current) {
@@ -188,7 +123,27 @@ export default function FlowPayment({
188
123
  container.innerHTML = '';
189
124
  }
190
125
 
191
- // Update the last amount reference
126
+ // FIX: Use existing payment session instead of making another API call
127
+ let paymentSession = preOrder.context_extras;
128
+
129
+ // Only get fresh payment data if we don't have a valid session
130
+ if (!paymentSession && lastAmountRef.current !== currentAmount) {
131
+ // Check if another call is already in progress
132
+ if (!walletPaymentInitialized.current) {
133
+ walletPaymentInitialized.current = true;
134
+ const walletPaymentResponse = await dispatch(
135
+ checkoutApi.endpoints.setWalletPaymentPage.initiate({})
136
+ ).unwrap();
137
+ paymentSession =
138
+ walletPaymentResponse?.pre_order?.context_extras ||
139
+ preOrder.context_extras;
140
+ } else {
141
+ // Use existing context_extras if another call was already made
142
+ paymentSession = preOrder.context_extras;
143
+ }
144
+ }
145
+
146
+ // Update the last amount reference after getting fresh data
192
147
  lastAmountRef.current = currentAmount;
193
148
 
194
149
  const checkout = await loadCheckoutWebComponents({
@@ -196,8 +151,7 @@ export default function FlowPayment({
196
151
  environment: options?.environment || 'production',
197
152
  locale: options?.locale || 'en',
198
153
  translations: options?.translations,
199
- paymentSession:
200
- paymentSession || (preOrder.context_extras as PaymentSessionResponse),
154
+ paymentSession: paymentSession as PaymentSessionResponse,
201
155
  appearance: options?.appearance,
202
156
  componentOptions: options?.componentOptions
203
157
  ? { flow: options.componentOptions }
@@ -212,6 +166,8 @@ export default function FlowPayment({
212
166
  }
213
167
  } catch (error) {
214
168
  console.error('Error initializing flow payment components:', error);
169
+ } finally {
170
+ isInitializingRef.current = false;
215
171
  }
216
172
  };
217
173
 
@@ -219,7 +175,6 @@ export default function FlowPayment({
219
175
  initWalletSelection();
220
176
  }, []);
221
177
 
222
- // Initial flow component initialization
223
178
  useEffect(() => {
224
179
  if (
225
180
  !preOrder.wallet_method ||
@@ -230,37 +185,16 @@ export default function FlowPayment({
230
185
  return;
231
186
  }
232
187
 
233
- // Only initialize if component doesn't exist yet (initial mount)
234
- if (!flowComponentRef.current) {
235
- initFlowPaymentWebComponents({
236
- publicKey: walletPaymentData.data.public_key
237
- });
238
- }
188
+ initFlowPaymentWebComponents({
189
+ publicKey: walletPaymentData.data.public_key
190
+ });
239
191
  }, [
240
192
  preOrder.wallet_method,
241
193
  preOrder.token,
194
+ currentAmount, // Use memoized amount instead of preOrder.total_amount
242
195
  walletPaymentData?.data?.public_key
243
196
  ]);
244
197
 
245
- // Handle amount changes by refreshing payment session
246
- useEffect(() => {
247
- if (
248
- !preOrder.wallet_method ||
249
- !preOrder.token ||
250
- !walletPaymentData?.data?.public_key ||
251
- preOrder.wallet_method !== 'checkout_flow'
252
- ) {
253
- return;
254
- }
255
-
256
- // Only refresh if component exists and amount has changed
257
- if (flowComponentRef.current && lastAmountRef.current !== currentAmount) {
258
- refreshPaymentSessionAndInitFlow({
259
- publicKey: walletPaymentData.data.public_key
260
- });
261
- }
262
- }, [currentAmount]);
263
-
264
198
  // Cleanup function when component unmounts
265
199
  useEffect(() => {
266
200
  return () => {