|
1 | 1 | import { SequenceAPIClient } from '@0xsequence/api'
|
2 | 2 | import { TokenMetadata } from '@0xsequence/metadata'
|
3 |
| -import { ChainId, networks } from '@0xsequence/network' |
| 3 | +import { ChainId, networks, findSupportedNetwork } from '@0xsequence/network' |
4 | 4 |
|
5 | 5 | import { CreditCardCheckout } from '../contexts'
|
6 | 6 |
|
@@ -233,3 +233,137 @@ export const fetchSardineOnRampLink = async ({
|
233 | 233 |
|
234 | 234 | return url.href
|
235 | 235 | }
|
| 236 | + |
| 237 | +export interface FetchForteAccessTokenReturn { |
| 238 | + accessToken: string |
| 239 | + expiresIn: number |
| 240 | + tokenType: string |
| 241 | +} |
| 242 | + |
| 243 | +//TODO: remove once part of the sequence api |
| 244 | +const FORTE_URL = 'https://staging-api.pti-dev.cloud' |
| 245 | + |
| 246 | +export const fetchForteAccessToken = async (): Promise<FetchForteAccessTokenReturn> => { |
| 247 | + const clientId = '5tpnj5869vs3jpgtpif2ci8v08' |
| 248 | + const clientSecret = 'jpkbg3e2ho9rbd0959qe5l6ke238d4bca2nptstfga2i9hant5e' |
| 249 | + |
| 250 | + const url = `${FORTE_URL}/auth/v1/oauth2/token` |
| 251 | + |
| 252 | + const res = await fetch(url, { |
| 253 | + method: 'POST', |
| 254 | + headers: { |
| 255 | + 'Content-Type': 'application/json' |
| 256 | + }, |
| 257 | + body: JSON.stringify({ |
| 258 | + client_id: clientId, |
| 259 | + client_secret: clientSecret |
| 260 | + }) |
| 261 | + }) |
| 262 | + |
| 263 | + const { data } = await res.json() |
| 264 | + |
| 265 | + return { |
| 266 | + accessToken: data.access_token, |
| 267 | + expiresIn: data.expires_in, |
| 268 | + tokenType: data.token_type |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +export interface CreateFortePaymentIntentArgs { |
| 273 | + accessToken: string |
| 274 | + tokenType: string |
| 275 | + nonce: string |
| 276 | + nftQuantity: string |
| 277 | + recipientAddress: string |
| 278 | + chainId: string |
| 279 | + signature: string |
| 280 | + tokenAddress: string |
| 281 | + protocolAddress: string |
| 282 | + nftName: string |
| 283 | + imageUrl: string |
| 284 | + tokenId: string |
| 285 | +} |
| 286 | + |
| 287 | +export interface CreateFortePaymentIntentReturn { |
| 288 | + errorCode: string | null |
| 289 | + flow: string |
| 290 | + notes: string[] |
| 291 | + paymentIntentId: string |
| 292 | + widgetData: string |
| 293 | +} |
| 294 | + |
| 295 | +export const createFortePaymentIntent = async (args: CreateFortePaymentIntentArgs): Promise<CreateFortePaymentIntentReturn> => { |
| 296 | + const { |
| 297 | + accessToken, |
| 298 | + tokenType, |
| 299 | + recipientAddress, |
| 300 | + chainId, |
| 301 | + signature, |
| 302 | + protocolAddress, |
| 303 | + nftName, |
| 304 | + nftQuantity, |
| 305 | + imageUrl, |
| 306 | + tokenId, |
| 307 | + nonce |
| 308 | + } = args |
| 309 | + const network = findSupportedNetwork(chainId) |
| 310 | + |
| 311 | + if (!network) { |
| 312 | + throw new Error('Invalid chainId') |
| 313 | + } |
| 314 | + |
| 315 | + const url = `${FORTE_URL}/payments/v2/intent` |
| 316 | + const forteBlockchainName = network.name.toLowerCase().replace('-', '_') |
| 317 | + const idempotencyKey = `${recipientAddress}-${tokenId}-${protocolAddress}-${nftName}-${new Date().getTime()}` |
| 318 | + |
| 319 | + const res = await fetch(url, { |
| 320 | + method: 'POST', |
| 321 | + headers: { |
| 322 | + 'Content-Type': 'application/json', |
| 323 | + Authorization: `${tokenType} ${accessToken}` |
| 324 | + }, |
| 325 | + body: JSON.stringify({ |
| 326 | + blockchain: forteBlockchainName, |
| 327 | + currency: 'USD', |
| 328 | + idempotency_key: idempotencyKey, |
| 329 | + transaction_type: 'BUY_NFT_MINT', |
| 330 | + buyer: { |
| 331 | + id: recipientAddress, |
| 332 | + wallet: { |
| 333 | + address: recipientAddress, |
| 334 | + blockchain: forteBlockchainName |
| 335 | + } |
| 336 | + }, |
| 337 | + items: [ |
| 338 | + { |
| 339 | + name: nftName, |
| 340 | + quantity: nftQuantity, |
| 341 | + price: { |
| 342 | + amount: nftQuantity, |
| 343 | + image_url: imageUrl, |
| 344 | + title: nftName, |
| 345 | + mint_data: { |
| 346 | + nonce, |
| 347 | + signature: signature, |
| 348 | + token_ids: [tokenId], |
| 349 | + protocol_address: protocolAddress, |
| 350 | + protocol: 'protocol-mint' |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + ] |
| 355 | + }) |
| 356 | + }) |
| 357 | + |
| 358 | + const { |
| 359 | + data: { error_code, flow, notes, payment_intent_id, widget_data } |
| 360 | + } = await res.json() |
| 361 | + |
| 362 | + return { |
| 363 | + errorCode: error_code, |
| 364 | + flow, |
| 365 | + notes, |
| 366 | + paymentIntentId: payment_intent_id, |
| 367 | + widgetData: widget_data |
| 368 | + } |
| 369 | +} |
0 commit comments