@@ -113,7 +113,6 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
113113  }  =  Object . assign ( { } ,  PayButton . defaultProps ,  props ) ; 
114114
115115  const  [ paymentId ,  setPaymentId ]  =  useState < string  |  undefined > ( undefined ) ; 
116-   const  [ fetchingPaymentId ,  setFetchingPaymentId ]  =  useState < boolean  |  undefined > ( ) ; 
117116  const  [ addressType ,  setAddressType ]  =  useState < CryptoCurrency > ( 
118117    getCurrencyTypeFromAddress ( to ) , 
119118  ) ; 
@@ -136,16 +135,57 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
136135      } 
137136    } ,  300 ) ; 
138137  } ; 
138+ 
139+   const  getPaymentId  =  useCallback ( async  ( 
140+     currency : Currency , 
141+     amount : number , 
142+     convertedAmount : number  |  undefined , 
143+     to : string  |  undefined , 
144+   ) : Promise < string  |  undefined >  =>  { 
145+     if  ( disablePaymentId  ||  ! to )  return  paymentId 
146+     try  { 
147+       const  amountToUse  = 
148+         ( isFiat ( currency )  ||  randomSatoshis )  &&  convertedAmount 
149+         ? convertedAmount 
150+         : amount 
151+ 
152+       console . log ( 'Creating payment ID with amount:' ,  amountToUse ) 
153+       const  responsePaymentId  =  await  createPayment ( amountToUse ,  to ,  apiBaseUrl ) 
154+ 
155+       setPaymentId ( responsePaymentId ) 
156+       return  responsePaymentId 
157+     }  catch  ( err )  { 
158+       console . error ( 'Error creating payment ID:' ,  err ) 
159+       return  undefined 
160+     } 
161+   } ,  [ disablePaymentId ,  apiBaseUrl ,  isFiat ,  randomSatoshis ] ) 
162+ 
139163  const  handleButtonClick  =  useCallback ( async  ( ) : Promise < void >  =>  { 
140-     if  ( onOpen  !==  undefined )  { 
164+ 
165+     if  ( onOpen )  { 
141166      if  ( isFiat ( currency ) )  { 
142-         void  waitPrice ( ( )  =>  {   onOpen ( cryptoAmountRef . current ,  to ,  paymentId )   } ) 
167+         void  waitPrice ( ( )  =>  onOpen ( cryptoAmountRef . current ,  to ,  paymentId ) ) 
143168      }  else  { 
144169        onOpen ( amount ,  to ,  paymentId ) 
145170      } 
146171    } 
147-     setDialogOpen ( true ) ; 
148-   } ,  [ cryptoAmount ,  to ,  paymentId ,  price ] ) 
172+ 
173+     if  ( ! disablePaymentId  &&  ! paymentId )  { 
174+       await  getPaymentId ( currency ,  Number ( amount ) ,  convertedAmount ,  to ) 
175+     } 
176+ 
177+     setDialogOpen ( true ) 
178+   } ,  [ 
179+     onOpen , 
180+     isFiat , 
181+     currency , 
182+     amount , 
183+     to , 
184+     paymentId , 
185+     disablePaymentId , 
186+     getPaymentId , 
187+     convertedAmount , 
188+   ] ) 
149189
150190  const  handleCloseDialog  =  ( success ?: boolean ,  paymentId ?: string ) : void =>  { 
151191    if  ( onClose  !==  undefined )  onClose ( success ,  paymentId ) ; 
@@ -188,42 +228,42 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
188228      return 
189229    } 
190230    ( async  ( )  =>  { 
191-     if  ( txsSocket  ===  undefined )  { 
192-       const  expectedAmount  =  currencyObj  ? currencyObj ?. float  : undefined 
193-       await  setupChronikWebSocket ( { 
194-         address : to , 
195-         txsSocket, 
196-         apiBaseUrl, 
197-         wsBaseUrl, 
198-         setTxsSocket, 
199-         setNewTxs, 
200-         setDialogOpen, 
201-         checkSuccessInfo : { 
202-           currency, 
203-           price, 
204-           randomSatoshis : randomSatoshis  ??  false , 
205-           disablePaymentId, 
206-           expectedAmount, 
207-           expectedOpReturn : opReturn , 
208-           expectedPaymentId : paymentId , 
209-           currencyObj, 
210-         } 
211-       } ) 
212-     } 
213-     if  ( altpaymentSocket  ===  undefined  &&  useAltpayment )  { 
214-       await  setupAltpaymentSocket ( { 
215-         addressType, 
216-         altpaymentSocket, 
217-         wsBaseUrl, 
218-         setAltpaymentSocket, 
219-         setCoins, 
220-         setCoinPair, 
221-         setLoadingPair, 
222-         setAltpaymentShift, 
223-         setLoadingShift, 
224-         setAltpaymentError, 
225-       } ) 
226-     } 
231+        if  ( txsSocket  ===  undefined )  { 
232+          const  expectedAmount  =  currencyObj  ? currencyObj ?. float  : undefined 
233+          await  setupChronikWebSocket ( { 
234+            address : to , 
235+            txsSocket, 
236+            apiBaseUrl, 
237+            wsBaseUrl, 
238+            setTxsSocket, 
239+            setNewTxs, 
240+            setDialogOpen, 
241+            checkSuccessInfo : { 
242+              currency, 
243+              price, 
244+              randomSatoshis : randomSatoshis  ??  false , 
245+              disablePaymentId, 
246+              expectedAmount, 
247+              expectedOpReturn : opReturn , 
248+              expectedPaymentId : paymentId , 
249+              currencyObj, 
250+            } 
251+          } ) 
252+        } 
253+        if  ( altpaymentSocket  ===  undefined  &&  useAltpayment )  { 
254+          await  setupAltpaymentSocket ( { 
255+            addressType, 
256+            altpaymentSocket, 
257+            wsBaseUrl, 
258+            setAltpaymentSocket, 
259+            setCoins, 
260+            setCoinPair, 
261+            setLoadingPair, 
262+            setAltpaymentShift, 
263+            setLoadingShift, 
264+            setAltpaymentError, 
265+          } ) 
266+        } 
227267    } ) ( ) 
228268
229269    return  ( )  =>  { 
@@ -258,9 +298,9 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
258298
259299  useEffect ( ( )  =>  { 
260300    ( async  ( )  =>  { 
261-     if  ( isFiat ( currency )  &&  price  ===  0 )  { 
262-       await  getPrice ( ) ; 
263-     } 
301+        if  ( isFiat ( currency )  &&  price  ===  0 )  { 
302+          await  getPrice ( ) ; 
303+        } 
264304    } ) ( ) 
265305  } ,  [ currency ,  getPrice ,  to ,  price ] ) ; 
266306
@@ -282,44 +322,14 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
282322        amount  as  number , 
283323        addressType , 
284324        randomSatoshis , 
285-       ) ; 
286-       setCryptoAmount ( convertedObj . string ) ; 
325+       ) ;  setCryptoAmount ( convertedObj . string ) ; 
287326      setConvertedAmount ( convertedObj . float ) ; 
288327      setConvertedCurrencyObj ( convertedObj ) ; 
289328    }  else  if  ( ! isFiat ( currency )  &&  ! randomSatoshis )  { 
290329      setCryptoAmount ( amount ?. toString ( ) ) ; 
291330    } 
292331  } ,  [ price ,  currencyObj ,  amount ,  currency ,  randomSatoshis ,  to ] ) ; 
293332
294-   useEffect ( ( )  =>  { 
295-     if  ( fetchingPaymentId  ===  true )  { 
296-       return ; 
297-     } 
298-     if  ( ( isFiat ( currency )  &&  ! convertedAmount )  ||  ( randomSatoshis  &&  ! convertedAmount ) )  { 
299-       return 
300-     } 
301-     
302-     setFetchingPaymentId ( true ) ; 
303-     const  initializePaymentId  =  async  ( )  =>  { 
304-       if  ( ! disablePaymentId  &&  to )  { 
305-         try  { 
306-           const  amountToUse  =  ( isFiat ( currency )  ||  randomSatoshis )  ? convertedAmount  : amount ; 
307-           console . log ( 'Creating payment ID with amount:' ,  amountToUse ,  isFiat ( currency ) ) ; 
308-           const  responsePaymentId  =  await  createPayment ( amountToUse ,  to ,  apiBaseUrl ) ; 
309-           setPaymentId ( responsePaymentId ) ; 
310-           setFetchingPaymentId ( false ) ; 
311-         }  catch  ( error )  { 
312-           console . error ( 'Error creating payment ID:' ,  error ) ; 
313-           setFetchingPaymentId ( false ) ; 
314-         } 
315-       }  else  { 
316-         setFetchingPaymentId ( false ) ; 
317-       } 
318-     } ; 
319- 
320-     initializePaymentId ( ) ; 
321-   } ,  [ disablePaymentId ,  amount ,  convertedAmount ,  to ,  apiBaseUrl ] ) ; 
322- 
323333  const  theme  =  useTheme ( props . theme ,  isValidXecAddress ( to  ??  '' ) ) ; 
324334
325335  const  ButtonComponent : React . FC < ButtonProps >  =  ( 
0 commit comments