Skip to content

Commit 3b1bd34

Browse files
committed
fixed transak message listening for checkout ui
1 parent 4ac4a5e commit 3b1bd34

File tree

3 files changed

+22
-51
lines changed

3 files changed

+22
-51
lines changed

examples/react/src/components/CustomCheckout/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const CustomCheckout = () => {
147147
}
148148

149149
const CryptoPayment = () => {
150-
if (cryptoPayment.cryptoOptions.isLoading) {
150+
if (cryptoPayment.cryptoOptions.data.length === 0) {
151151
return <Spinner />
152152
}
153153

@@ -172,6 +172,7 @@ export const CustomCheckout = () => {
172172
disabled={option.isInsufficientFunds}
173173
/>
174174
))}
175+
{cryptoPayment.cryptoOptions.isLoading && <Spinner />}
175176
<Button onClick={cryptoPayment.purchaseAction.action} disabled={!cryptoPayment.purchaseAction.isReady}>
176177
Purchase
177178
</Button>

packages/checkout/src/hooks/useCheckoutUI/useCreditCardPayment.tsx

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TransactionStatus } from '@0xsequence/indexer'
55
import { ContractInfo, TokenMetadata } from '@0xsequence/metadata'
66
import { findSupportedNetwork } from '@0xsequence/network'
77
import pako from 'pako'
8-
import React, { useEffect } from 'react'
8+
import React, { useEffect, useRef } from 'react'
99
import { Hex, formatUnits, zeroAddress } from 'viem'
1010
import { 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

276281
const 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 {
342328
const 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'))

packages/checkout/src/hooks/useCheckoutUI/useCryptoPayment.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const useCryptoPayment = ({
7979
const isNativeCurrency = compareAddress(currencyAddress, zeroAddress)
8080
const currencySymbol = isNativeCurrency ? network?.nativeToken.symbol : currencyInfo?.symbol
8181
const currencyDecimals = isNativeCurrency ? network?.nativeToken.decimals : currencyInfo?.decimals
82+
const isMainCurrencySelected = compareAddress(selectedCurrencyAddress || '', currencyAddress)
8283

8384
const { data: walletClient } = useWalletClient({
8485
chainId
@@ -151,7 +152,7 @@ export const useCryptoPayment = ({
151152
significantDigits: 6
152153
})
153154

154-
const mainCurrencyOption = currencyBalanceIsLoading
155+
const mainCurrencyOption = !currencyBalanceIsLoading
155156
? [
156157
{
157158
chainId,
@@ -214,9 +215,8 @@ export const useCryptoPayment = ({
214215
throw new Error('Connector is not connected')
215216
}
216217

217-
const isMainCurrency = compareAddress(selectedCurrencyAddress, currencyAddress)
218218
try {
219-
if (isMainCurrency) {
219+
if (isMainCurrencySelected) {
220220
const walletClientChainId = await walletClient.getChainId()
221221
if (walletClientChainId !== chainId) {
222222
await walletClient.switchChain({ id: chainId })
@@ -361,7 +361,8 @@ export const useCryptoPayment = ({
361361
},
362362
purchaseAction: {
363363
action: purchaseAction,
364-
isReady: !!selectedCurrencyAddress && !isLoadingSwapQuote && (!allowanceIsLoading || isNativeCurrency),
364+
isReady:
365+
!!selectedCurrencyAddress && (!isLoadingSwapQuote || isMainCurrencySelected) && (!allowanceIsLoading || isNativeCurrency),
365366
selectedCurrencyAddress,
366367
setSelectedCurrencyAddress
367368
}

0 commit comments

Comments
 (0)