Skip to content

Commit f4473c4

Browse files
RichardIralaSamueleA
authored andcommitted
feat(onramp): add optional Transak alternate flow with default and windowed modes
1 parent bb1fdd3 commit f4473c4

File tree

4 files changed

+197
-1
lines changed

4 files changed

+197
-1
lines changed

packages/checkout/src/contexts/AddFundsModal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface AddFundsSettings {
1616
onOrderSuccessful?: (data: any) => void
1717
onOrderFailed?: (data: any) => void
1818
provider?: TransactionOnRampProvider
19+
transakOnRampKind?: 'default' | 'windowed'
1920
cryptoAmount?: string
2021
}
2122

packages/checkout/src/utils/transak.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,91 @@ import { zeroAddress } from 'viem'
33

44
export const TRANSAK_PROXY_ADDRESS = '0x4a598b7ec77b1562ad0df7dc64a162695ce4c78a'
55

6+
export const getTransakLink = (
7+
addFundsSettings: AddFundsSettings,
8+
{ transakApiUrl, transakApiKey }: { transakApiUrl: string; transakApiKey: string }
9+
) => {
10+
interface Options {
11+
[index: string]: string | undefined
12+
}
13+
14+
const url = new URL(transakApiUrl)
15+
const apiKey = transakApiKey
16+
17+
const options: Options = {
18+
apiKey: apiKey,
19+
referrerDomain: window.location.origin,
20+
walletAddress: addFundsSettings.walletAddress,
21+
fiatAmount: addFundsSettings?.fiatAmount,
22+
fiatCurrency: addFundsSettings?.fiatCurrency,
23+
disableWalletAddressForm: 'true',
24+
defaultFiatAmount: addFundsSettings?.defaultFiatAmount || '50',
25+
defaultCryptoCurrency: addFundsSettings?.defaultCryptoCurrency || 'USDC',
26+
cryptoCurrencyList: addFundsSettings?.cryptoCurrencyList,
27+
networks: addFundsSettings?.networks
28+
}
29+
30+
Object.keys(options).forEach(k => {
31+
const option = options[k]
32+
if (option) {
33+
url.searchParams.append(k, option)
34+
}
35+
})
36+
37+
return url.href
38+
}
39+
40+
export const getTransakLinkFromSequenceApi = async (
41+
addFundsSettings: AddFundsSettings,
42+
transakApiUrl: string,
43+
projectAccessKey: string
44+
) => {
45+
interface Options {
46+
[index: string]: string | boolean | undefined
47+
}
48+
49+
const options: Options = {
50+
referrerDomain: window.location.origin,
51+
walletAddress: addFundsSettings.walletAddress,
52+
fiatAmount: addFundsSettings?.fiatAmount,
53+
fiatCurrency: addFundsSettings?.fiatCurrency,
54+
disableWalletAddressForm: true,
55+
defaultCryptoCurrency: addFundsSettings?.defaultCryptoCurrency,
56+
networks: addFundsSettings?.networks
57+
}
58+
59+
const url = new URL(transakApiUrl)
60+
61+
const data = {
62+
params: {
63+
referrerDomain: options.referrerDomain,
64+
cryptoCurrencyCode: options.defaultCryptoCurrency,
65+
fiatAmount: options?.fiatAmount,
66+
fiatCurrency: options?.fiatCurrency,
67+
network: options.networks ? (options.networks as string).split(',')[0].trim() : undefined,
68+
disableWalletAddressForm: options.disableWalletAddressForm,
69+
walletAddress: options.walletAddress
70+
}
71+
}
72+
73+
try {
74+
const response = await fetch(url, {
75+
method: 'POST',
76+
headers: {
77+
'Content-Type': 'application/json',
78+
'x-access-key': projectAccessKey
79+
},
80+
body: JSON.stringify(data)
81+
})
82+
83+
const result = await response.json()
84+
85+
return result?.url
86+
} catch (error) {
87+
console.error('Error:', error)
88+
}
89+
}
90+
691
interface CountriesResult {
792
response: Country[]
893
}

packages/checkout/src/views/AddFunds.tsx

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
<<<<<<< HEAD
12
import { Spinner, Text } from '@0xsequence/design-system'
23
import { useEffect, useRef } from 'react'
4+
=======
5+
import { useProjectAccessKey } from '@0xsequence/connect'
6+
import { Button, Spinner, Text } from '@0xsequence/design-system'
7+
import { useEffect, useRef, useState } from 'react'
8+
>>>>>>> 51730908 (feat(onramp): add optional Transak alternate flow with default and windowed modes)
39

410
import { HEADER_HEIGHT } from '../constants/index.js'
511
import type { AddFundsSettings } from '../contexts/AddFundsModal.js'
612
import { useAddFundsModal } from '../hooks/index.js'
13+
<<<<<<< HEAD
714
import { useTransakWidgetUrl } from '../hooks/useTransakWidgetUrl.js'
15+
=======
16+
import { getTransakLink, getTransakLinkFromSequenceApi } from '../utils/transak.js'
17+
>>>>>>> 51730908 (feat(onramp): add optional Transak alternate flow with default and windowed modes)
818

919
const EventTypeOrderCreated = 'TRANSAK_ORDER_CREATED'
1020
const EventTypeOrderSuccessful = 'TRANSAK_ORDER_SUCCESSFUL'
@@ -17,6 +27,7 @@ export const AddFundsContent = () => {
1727

1828
export const AddFundsContentTransak = () => {
1929
const { addFundsSettings = {} as AddFundsSettings } = useAddFundsModal()
30+
<<<<<<< HEAD
2031

2132
const {
2233
data: transakLinkData,
@@ -32,7 +43,15 @@ export const AddFundsContentTransak = () => {
3243
defaultCryptoCurrency: addFundsSettings?.defaultCryptoCurrency || 'USDC',
3344
cryptoCurrencyList: addFundsSettings?.cryptoCurrencyList
3445
})
46+
=======
47+
const { transakApiUrl, transakApiKey, sequenceTransakApiUrl } = useEnvironmentContext()
48+
>>>>>>> 51730908 (feat(onramp): add optional Transak alternate flow with default and windowed modes)
3549
const iframeRef = useRef<HTMLIFrameElement | null>(null)
50+
const projectAccessKey = useProjectAccessKey()
51+
const [isLoading, setIsLoading] = useState(false)
52+
const [creationLinkFailed, setCreationLinkFailed] = useState(false)
53+
const { transakOnRampKind = 'default' } = addFundsSettings
54+
const isTransakOnRampKindWindowed = transakOnRampKind === 'windowed'
3655

3756
useEffect(() => {
3857
const handleMessage = (message: MessageEvent<any>) => {
@@ -79,6 +98,95 @@ export const AddFundsContentTransak = () => {
7998
)
8099
}
81100

101+
async function handleTransakLink({
102+
addFundsSettings,
103+
sequenceTransakApiUrl,
104+
projectAccessKey,
105+
setCreationLinkFailed,
106+
setIsLoading
107+
}: {
108+
addFundsSettings: AddFundsSettings
109+
sequenceTransakApiUrl: string
110+
projectAccessKey: string
111+
setCreationLinkFailed: (value: boolean) => void
112+
setIsLoading: (value: boolean) => void
113+
}) {
114+
try {
115+
setCreationLinkFailed(false)
116+
setIsLoading(true)
117+
const link = await getTransakLinkFromSequenceApi(addFundsSettings, sequenceTransakApiUrl, projectAccessKey)
118+
119+
if (link) {
120+
window.open(link, '_blank')
121+
} else {
122+
setCreationLinkFailed(true)
123+
}
124+
setIsLoading(false)
125+
} catch (error) {
126+
console.error(`The creation of the Transak link has failed. Error: `, error)
127+
setCreationLinkFailed(true)
128+
setIsLoading(false)
129+
}
130+
}
131+
132+
useEffect(() => {
133+
if (!isTransakOnRampKindWindowed) {
134+
return
135+
}
136+
137+
handleTransakLink({ addFundsSettings, sequenceTransakApiUrl, projectAccessKey, setIsLoading, setCreationLinkFailed })
138+
}, [])
139+
140+
if (isLoading) {
141+
return (
142+
<div
143+
className="flex items-center justify-center w-full px-4 pb-4 h-full"
144+
style={{
145+
height: '600px',
146+
paddingTop: HEADER_HEIGHT
147+
}}
148+
>
149+
<Spinner size="lg" />
150+
</div>
151+
)
152+
}
153+
154+
if (isTransakOnRampKindWindowed) {
155+
return (
156+
<div
157+
className="flex items-center justify-center w-full px-4 pb-4 h-full"
158+
style={{
159+
height: '600px',
160+
paddingTop: HEADER_HEIGHT
161+
}}
162+
>
163+
{creationLinkFailed ? (
164+
<div className="flex flex-col gap-2 items-center">
165+
<Text color="text100">The creation of the Transak link failed.</Text>
166+
<Button
167+
className="w-fit"
168+
onClick={() => {
169+
handleTransakLink({
170+
addFundsSettings,
171+
sequenceTransakApiUrl,
172+
projectAccessKey,
173+
setIsLoading,
174+
setCreationLinkFailed
175+
})
176+
}}
177+
>
178+
Try Again
179+
</Button>
180+
</div>
181+
) : (
182+
<div className="flex gap-2 items-center text-center">
183+
<Text color="text100">Once you've added funds, you can close this window and try buying with crypto again.</Text>
184+
</div>
185+
)}
186+
</div>
187+
)
188+
}
189+
82190
return (
83191
<div
84192
className="flex items-center w-full px-4 pb-4 h-full"

packages/connect/src/styles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,4 +2522,6 @@ export const styles = String.raw`
25222522
--tw-gradient-to-position: 100%;
25232523
}
25242524
}
2525-
}`
2525+
}
2526+
2527+
`

0 commit comments

Comments
 (0)