"use client";
import { useState, useEffect } from "react";
import { RozoPayButton, useRozoPayUI } from "@rozoai/intent-pay";
import { baseUSDC } from "@rozoai/intent-common";
import { getAddress, isAddress } from "viem";
function PaymentDemo() {
const { resetPayment } = useRozoPayUI();
const [formData, setFormData] = useState({
recipientAddress: "",
tokenAddress: baseUSDC.token,
amount: "5",
});
// PayButton only visible when Address and Token Address are filled
const canShowButton =
formData.recipientAddress &&
isAddress(formData.recipientAddress) &&
formData.tokenAddress &&
isAddress(formData.tokenAddress) &&
formData.amount &&
parseFloat(formData.amount) > 0;
// IMPORTANT: Must call resetPayment() whenever toChain, toAddress, toToken, or toUnits changes
// toUnits is a human-readable amount as string (e.g., "10" for 10 USDC, no parseFloat needed)
useEffect(() => {
if (canShowButton) {
resetPayment({
toChain: baseUSDC.chainId,
toAddress: getAddress(formData.recipientAddress),
toToken: getAddress(formData.tokenAddress),
toUnits: formData.amount, // Human-readable amount as string
});
}
}, [formData, canShowButton]);
return (
<div className="p-8 max-w-md mx-auto">
<h1 className="text-2xl font-bold mb-6">Crypto Payment Demo</h1>
<div className="space-y-4 mb-6">
<div>
<label className="block text-sm font-medium mb-1">
Recipient Address *
</label>
<input
type="text"
className="w-full p-2 border rounded"
placeholder="0x742d35Cc6634C0532925a3b8D454A3fE1C11C4e2"
value={formData.recipientAddress}
onChange={(e) =>
setFormData((prev) => ({
...prev,
recipientAddress: e.target.value,
}))
}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">
Token Address *
</label>
<input
type="text"
className="w-full p-2 border rounded"
placeholder="USDC Token Address"
value={formData.tokenAddress}
onChange={(e) =>
setFormData((prev) => ({ ...prev, tokenAddress: e.target.value }))
}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Amount *</label>
<input
type="number"
step="0.01"
min="0"
className="w-full p-2 border rounded"
placeholder="5.00"
value={formData.amount}
onChange={(e) =>
setFormData((prev) => ({ ...prev, amount: e.target.value }))
}
/>
</div>
</div>
{canShowButton && (
<RozoPayButton
appId="rozoDemo" // Demo app ID
toChain={baseUSDC.chainId} // Base chain (8453)
toAddress={getAddress(formData.recipientAddress)} // Your wallet
toToken={getAddress(formData.tokenAddress)} // USDC on Base
toUnits={formData.amount} // Human-readable amount as string (e.g., "10" for 10 USDC)
intent={`Pay $${formData.amount}`} // Button text
onPaymentStarted={(event) => {
console.log("β
Payment started!", event.paymentId);
}}
onPaymentCompleted={(event) => {
console.log("π Payment completed!", event.txHash);
alert("Payment successful! π");
}}
onPaymentBounced={(event) => {
console.log("β Payment bounced!", event);
alert("Payment failed. You'll receive a refund.");
}}
/>
)}
{!canShowButton && (
<div className="p-3 bg-gray-100 rounded text-sm text-gray-600">
Please fill in all required fields to show the payment button
</div>
)}
</div>
);
}