Fiat payments
In this guide, we will enable our users to complete an onchain transaction using a credit/debit card. While you are free to use any fiat payment processor, we'll use Stripe in this guide.
At a high-level, the payment flow will look like this:
- You setup a "sponsor wallet" on Glide that will be used to pay for onchain transactions.
- Once the user completes the fiat payment, you complete the onchain transaction for the user using the sponsor wallet.
When to use this integration
This integration is perfect for you if:
- You want to enable your users to pay for onchain transactions using credit/debit cards.
- The onchain transactions span across multiple chains or tokens.
- You want a reliable and secure way to complete onchain transactions for your users.
- You don't want to maintain wallets on multiple chains with various token balances.
For example, if you are building an NFT marketplace with NFT contracts deployed on multiple chains, you can use this integration to enable your users to pay for minting NFTs using fiat and Glide. Glide will help you execute transactions across all chains and tokens.
Setup
Before you start, make sure you have the following ready:
- Setup the Glide client using the Installation guide.
- Glide API Key for backend API calls.
- A Stripe account and its API key.
Steps
1. Create a sponsor wallet
Create a sponsor wallet that will hold funds to complete the transaction on behalf of the user. You can create this wallet on any preferred chain.
For example, to create a sponsor wallet on Base, you can use the following Glide API call:
curl --request POST \
--url https://api.paywithglide.xyz/wallets \
--header 'Content-Type: application/json' \
--header 'X-Glide-Project-ID: <Your Glide Project ID>' \
--header 'Authorization: Bearer <Your Glide API Key>' \
--data '{
"chainId": "eip155:8453",
"secret": "<a password-like secret, at least 8 characters>"
}'
You will get a response similar to:
{
"walletId": "b5b4a294-99ed-4d70-b1be-57c78108d241",
"chainId": "eip155:8453",
"address": "0xA697030e8A39D33b7691f195087fc16F165E3bc8",
"createdAt": "2024-08-27T22:56:38.092937Z",
}
Take a note of the chainId
and address
as you will need it later.
2. Fund the sponsor wallet
Send some tokens to the sponsor wallet on the same chain you created it on. This wallet will be used to complete the transaction on behalf of the user.
For the purpose of this guide, we will send ETH on Base chain to the sponsor wallet.
3. Create Stripe payment intent
When a user wants to pay for a transaction using fiat (for example, in USD), you need to:
- Estimate the amount in USD required to complete the transaction.
- Use the USD amount to create a payment intent on Stripe.
You can use Glide to estimate the amount in USD required to complete the transaction.
const { paymentAmountUSD } = await estimatePaymentAmount(glideConfig, {
account: SPONSOR_WALLET_ADDRESS,
// The currency and the chain that the sponsor wallet holds
paymentCurrency: currencies.eth.on(chains.base),
// The chain on which the transaction will be executed
chainId: chains.base.id,
// Optional: Add an extra commission to the transaction to account
// for the payment processor fees
commissionUSD: 1,
// A sample NFT mint transaction
abi: hypersubAbi,
address: "0x70cd0c150bf6c952b93ce6481b571e55ba383003",
function: "mintFor",
args: [userWalletAddress, 3000000000000n],
value: 3000000000000n,
});
// Use the paymentAmountUSD to create a stripe payment intent
const paymentIntent = await stripe.paymentIntents.create({
amount: Number(paymentAmountUSD) * 100,
currency: "usd",
});
4. Configure Stripe webhook
Stripe sends events to your server using webhooks. More specifically, they send the payment_intent.succeeded
event when a payment is successful.
Follow the Stripe Webhook Guide to setup a webhook endpoint on your server.
5. Complete the onchain transaction
Once the user completes the payment and your server recieves the webhook, you can complete the onchain transaction for the user using the sponsor wallet.
const { sessionId } = await createSession(glideConfig, {
account: SPONSOR_WALLET_ADDRESS, // The wallet address from Step 1
walletSecret: SPONSOR_WALLET_SECRET, // The secret used to create the wallet in Step 1
// The currency and the chain that the sponsor wallet holds
paymentCurrency: currencies.eth.on(chains.base),
// The chain on which the transaction will be executed
chainId: chains.base.id,
// Optional: Add an extra commission to the transaction to account for the payment
// processor fees
commissionUSD: 1,
// A sample NFT mint transaction
abi: hypersubAbi,
address: "0x70cd0c150bf6c952b93ce6481b571e55ba383003",
function: "mintFor",
args: [userWalletAddress, 3000000000000n],
value: 3000000000000n,
})
const { sponsoredTransactionHash } = await waitForSession(glideConfig, sessionId);
6. That's it!
You have successfully enabled your users to pay with fiat for completing onchain transactions.
To recap:
- We created a sponsor wallet that holds funds to complete onchain transactions on behalf of the user.
- The sponsor wallet can be on any chain and hold any token. Glide will handle the conversion to the right token for the onchain transaction.
- We funded the sponsor wallet with the required amount of crypto to complete the onchain transaction.
- Once we receive a successful payment from Stripe, we complete the onchain transaction for the user using the sponsor wallet.