@@ -5,7 +5,7 @@ import { TransactionStatus } from '@0xsequence/indexer'
55import { ContractInfo , TokenMetadata } from '@0xsequence/metadata'
66import { findSupportedNetwork } from '@0xsequence/network'
77import pako from 'pako'
8- import React , { useEffect } from 'react'
8+ import React , { useEffect , useRef } from 'react'
99import { Hex , formatUnits , zeroAddress } from 'viem'
1010import { usePublicClient } from 'wagmi'
1111
@@ -93,7 +93,7 @@ export const useCreditCardPayment = ({
9393 const isNativeCurrency = compareAddress ( currencyAddress , zeroAddress )
9494 const currencySymbol = isNativeCurrency ? network ?. nativeToken . symbol : currencyInfo ?. symbol || 'POL'
9595 const currencyDecimals = isNativeCurrency ? network ?. nativeToken . decimals : currencyInfo ?. decimals || 18
96-
96+ const iframeRef = useRef < HTMLIFrameElement | null > ( null )
9797 const tokenMetadata = tokenMetadatas ?. [ 0 ]
9898
9999 const {
@@ -198,6 +198,7 @@ export const useCreditCardPayment = ({
198198 < div className = "flex items-center justify-center" style = { { height : '770px' } } >
199199 < iframe
200200 id = "transakIframe"
201+ ref = { iframeRef }
201202 allow = "camera;microphone;payment"
202203 src = { transakLink }
203204 style = { {
@@ -216,6 +217,8 @@ export const useCreditCardPayment = ({
216217 chainId = { network ?. chainId || 137 }
217218 onSuccess = { onSuccess }
218219 onError = { onError }
220+ isLoading = { isLoading }
221+ iframeRef = { iframeRef }
219222 />
220223 )
221224 } ,
@@ -271,26 +274,25 @@ interface TransakEventListenerProps {
271274 onError ?: ( error : Error ) => void
272275 chainId : number
273276 transactionConfirmations ?: number
277+ isLoading : boolean
278+ iframeRef : React . RefObject < HTMLIFrameElement | null >
274279}
275280
276281const TransakEventListener = ( {
277282 onSuccess,
278283 onError,
279284 chainId,
280- transactionConfirmations = TRANSACTION_CONFIRMATIONS_DEFAULT
285+ transactionConfirmations = TRANSACTION_CONFIRMATIONS_DEFAULT ,
286+ isLoading,
287+ iframeRef
281288} : TransakEventListenerProps ) => {
282- const publicClient = usePublicClient ( { chainId } )
283- const indexerClient = useIndexerClient ( chainId )
284-
285289 useEffect ( ( ) => {
286- const transakIframeElement = document . getElementById ( TRANSAK_IFRAME_ID ) as HTMLIFrameElement
287- if ( ! transakIframeElement ) {
290+ const transakIframe = iframeRef . current ?. contentWindow
291+ if ( ! transakIframe ) {
288292 return
289293 }
290- const transakIframe = transakIframeElement . contentWindow
291294
292295 const readMessage = async ( message : any ) => {
293- console . log ( 'message' , message )
294296 if ( message . source !== transakIframe ) {
295297 return
296298 }
@@ -299,23 +301,7 @@ const TransakEventListener = ({
299301 console . log ( 'Order Data: ' , message ?. data ?. data )
300302 const txHash = message ?. data ?. data ?. transactionHash || ''
301303
302- if ( ! publicClient ) {
303- onError ?.( new Error ( 'Public client not available' ) )
304- return
305- }
306-
307- const { txnStatus } = await waitForTransactionReceipt ( {
308- txnHash : txHash ,
309- indexerClient,
310- publicClient,
311- confirmations : transactionConfirmations
312- } )
313-
314- if ( txnStatus === TransactionStatus . FAILED ) {
315- onError ?.( new Error ( 'Transak transaction failed' ) )
316- } else {
317- onSuccess ?.( txHash )
318- }
304+ onSuccess ?.( txHash )
319305 }
320306
321307 if ( message ?. data ?. event_id === 'TRANSAK_ORDER_FAILED' ) {
@@ -326,7 +312,7 @@ const TransakEventListener = ({
326312 window . addEventListener ( 'message' , readMessage )
327313
328314 return ( ) => window . removeEventListener ( 'message' , readMessage )
329- } , [ ] )
315+ } , [ isLoading ] )
330316
331317 return null
332318}
@@ -342,15 +328,9 @@ interface SardineEventListenerProps {
342328const SardineEventListener = ( { onSuccess, onError, chainId, orderId, transactionConfirmations } : SardineEventListenerProps ) => {
343329 const { env } = useConfig ( )
344330 const projectAccessKey = useProjectAccessKey ( )
345- const indexerClient = useIndexerClient ( chainId )
346- const publicClient = usePublicClient ( { chainId } )
347331
348332 const pollForOrderStatus = async ( ) => {
349333 try {
350- if ( ! indexerClient || ! publicClient ) {
351- onError ?.( new Error ( 'Indexer or public client not available' ) )
352- return
353- }
354334 console . log ( 'Polling for transaction status' )
355335 const pollResponse = await fetchSardineOrderStatus ( orderId , projectAccessKey , env . apiUrl )
356336 const status = pollResponse . resp . status
@@ -359,18 +339,7 @@ const SardineEventListener = ({ onSuccess, onError, chainId, orderId, transactio
359339 console . log ( 'transaction status poll response:' , status )
360340
361341 if ( status === 'Complete' ) {
362- const { txnStatus } = await waitForTransactionReceipt ( {
363- txnHash : transactionHash ,
364- indexerClient,
365- publicClient,
366- confirmations : transactionConfirmations
367- } )
368-
369- if ( txnStatus === TransactionStatus . FAILED ) {
370- onError ?.( new Error ( 'Sardine transaction failed' ) )
371- } else {
372- onSuccess ?.( transactionHash )
373- }
342+ onSuccess ?.( transactionHash )
374343 }
375344 if ( status === 'Declined' || status === 'Cancelled' ) {
376345 onError ?.( new Error ( 'Failed to transfer collectible' ) )
0 commit comments