Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sardine checkout integration #33

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 96 additions & 7 deletions examples/react/src/components/Homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
validateEthProof,
useTheme as useKitTheme,
useWaasFeeOptions,
useIndexerClient
useIndexerClient,
getModalPositionCss
} from '@0xsequence/kit'
import { useOpenWalletModal } from '@0xsequence/kit-wallet'
import { useCheckoutModal } from '@0xsequence/kit-checkout'
Expand Down Expand Up @@ -36,9 +37,12 @@ import {
Switch,
Select,
IconButton,
CheckmarkIcon
CheckmarkIcon,
Modal,
TextInput
} from '@0xsequence/design-system'
import { allNetworks } from '@0xsequence/network'
import { AnimatePresence } from 'framer-motion'
import { Footer } from './Footer'
import { messageToSign } from '../constants'
import { delay, formatAddress, getCheckoutSettings } from '../utils'
Expand All @@ -63,6 +67,12 @@ export const Homepage = () => {
const { data: walletClient } = useWalletClient()
const { switchChain } = useSwitchChain()

const [isCheckoutInfoModalOpen, setIsCheckoutInfoModalOpen] = React.useState(false)

const [checkoutOrderId, setCheckoutOrderId] = React.useState('')
const [checkoutTokenContractAddress, setCheckoutTokenContractAddress] = React.useState('')
const [checkoutTokenId, setCheckoutTokenId] = React.useState('')

const connections = useConnections()

const isWaasConnection = connections.find(c => c.connector.id.includes('waas')) !== undefined
Expand Down Expand Up @@ -324,7 +334,23 @@ export const Homepage = () => {
}

const onClickCheckout = () => {
triggerCheckout(getCheckoutSettings(address))
setIsCheckoutInfoModalOpen(true)
}

const onCheckoutInfoConfirm = () => {
setIsCheckoutInfoModalOpen(false)
if (checkoutOrderId !== '' && checkoutTokenContractAddress !== '' && checkoutTokenId !== '') {
const checkoutSettings = getCheckoutSettings(
checkoutOrderId,
address || '',
checkoutTokenContractAddress,
checkoutTokenId,
137,
1,
true
)
triggerCheckout(checkoutSettings)
}
}

const SwitchThemeButton = () => {
Expand Down Expand Up @@ -370,11 +396,11 @@ export const Homepage = () => {
description="Connect a Sequence wallet to view, swap, send, and receive collections"
onClick={() => setOpenWalletModal(true)}
/>
{/* <ClickableCard
title="Checkout"
description="Checkout screen before placing a purchase on coins or collections"
<ClickableCard
title="Sardine Checkout"
description="Set orderbook order id, token contract address and token id to test checkout (on Polygon)"
onClick={onClickCheckout}
/> */}
/>
<ClickableCard
title="Send transaction"
description="Send a transaction with your wallet"
Expand Down Expand Up @@ -615,6 +641,69 @@ export const Homepage = () => {
</Box>

{!isMobile && <Footer />}

<AnimatePresence>
{isCheckoutInfoModalOpen && (
<Modal
contentProps={{
style: {
maxWidth: '400px',
height: 'auto',
...getModalPositionCss('center')
}
}}
scroll={false}
backdropColor="backgroundBackdrop"
onClose={() => setIsCheckoutInfoModalOpen(false)}
>
<Box id="sequence-kit-checkout-info-modal">
<Box paddingTop="16" paddingBottom="8" paddingX="6" gap="2" flexDirection="column">
<Text variant="medium" color="text50">
Order ID
</Text>
<TextInput
autoFocus
name="orderId"
value={checkoutOrderId}
onChange={ev => setCheckoutOrderId(ev.target.value)}
placeholder="Order Id"
data-1p-ignore
/>
<Text variant="medium" color="text50">
Token Contract Address
</Text>
<TextInput
autoFocus
name="tokenContractAddress"
value={checkoutTokenContractAddress}
onChange={ev => setCheckoutTokenContractAddress(ev.target.value)}
placeholder="Token Contract Address"
data-1p-ignore
/>
<Text variant="medium" color="text50">
Token ID
</Text>
<TextInput
autoFocus
name="tokenId"
value={checkoutTokenId}
onChange={ev => setCheckoutTokenId(ev.target.value)}
placeholder="Token Id"
data-1p-ignore
/>

<Button
marginTop="4"
onClick={() => {
onCheckoutInfoConfirm()
}}
label="Trigger checkout"
/>
</Box>
</Box>
</Modal>
)}
</AnimatePresence>
</Box>
)
}
Expand Down
36 changes: 36 additions & 0 deletions examples/react/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Address } from 'viem'
import { CheckoutSettings } from '@0xsequence/kit-checkout'

export const truncateAtMiddle = (text: string, truncateAt: number) => {
let finalText = text

Expand All @@ -15,3 +18,36 @@ export const formatAddress = (text: string) => {
export const delay = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}

export const getCheckoutSettings = (
blockchainNftId: string,
recipientAddress: string | Address,
tokenContractAddress: string,
tokenId: string,
chainId: number,
quantity: number,
isDev?: boolean
) => {
const checkoutSettings: CheckoutSettings = {
sardineCheckout: {
chainId,
defaultPaymentMethodType: 'us_debit',
platform: 'horizon',
contractAddress: '0xB537a160472183f2150d42EB1c3DD6684A55f74c',
blockchainNftId: blockchainNftId,
recipientAddress: recipientAddress,
quantity,
isDev
},
orderSummaryItems: [
{
chainId,
contractAddress: tokenContractAddress,
tokenId,
quantityRaw: String(quantity)
}
]
}

return checkoutSettings
}
1 change: 0 additions & 1 deletion examples/react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './settings'
export * from './helpers'
30 changes: 0 additions & 30 deletions examples/react/src/utils/settings.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/checkout/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xsequence/kit-checkout",
"version": "2.4.0",
"version": "2.4.1",
"description": "Checkout UI for Sequence Kit",
"repository": "https://github.com/0xsequence/kit/tree/master/packages/checkout",
"main": "dist/0xsequence-kit-checkout.cjs.js",
Expand Down
Loading