Solana Pay is the standard for payment
requests on Solana. Your checkout puts the order’s recipient, amount, token, and
a unique reference into a
transfer request
URL and shows it as a QR code. The customer scans it, and their
walletWallets & signaturesA wallet holds a key pair: a public address that anyone can pay, and a private key that signs approvals and never leaves the device.Learn more (Wallets & signatures: A wallet holds a key pair: a public address that anyone can pay, and a private key that signs approvals and never leaves the device.) builds
the transfer, asks them to approve it, and sends it. Adding the QR code to your
own app is optional: the rest of the tutorial runs without it.
@solana/pay is currently in beta. APIs may change before the stable
release.
You need Node.js 20.18 or higher, a React or Next.js app (a fresh
create-next-app is fine), and a mobile wallet set to devnet. Fund it with
devnet USDC from the Circle faucet and devnet SOL
from faucet.solana.com to cover transaction fees.
Install Solana Pay:
npm install @solana/pay@beta @solana/kit@^6.5.0
The component creates a unique reference for the order, encodes it into a
transfer request URL, and renders the URL as a QR code. It then watches for a
transaction that includes the reference and validates the recipient, amount,
and token before marking the order paid. Set MERCHANT_WALLET to an address you
control:
Run your app, scan the QR code with your wallet, and approve the payment. Once
the
transactionTransactionsA transaction is a signed request to change the shared ledger. It either fully happens or doesn't happen at all, and once confirmed anyone can look it up.Learn more (Transactions: A transaction is a signed request to change the shared ledger. It either fully happens or doesn’t happen at all, and once confirmed anyone can look it up.) confirms
and passes validation, the component shows the transaction signature. That signature is your receipt: look it up with
getTransaction or in any explorer.
Don’t ship orders from a browser check alone. Validation in the browser
is fine for a demo, but anything that runs in the browser can be faked. In
the next step your server looks the signature up on the network and checks
the amount, the recipient, and the order itself.
A USDC payment is one TransferChecked instruction: move this many base units
of this mint from the customer’s
USDC accountToken accountsWallets don't hold tokens directly. Each token (USDC, or your own asset) gets a small account owned by the wallet, and transfers move between these accounts.Learn more (Token accounts: Wallets don’t hold tokens directly. Each token (USDC, or your own asset) gets a small account owned by the wallet, and transfers move between these accounts.) to
the store’s. The wallet that scans the QR code builds it from the URL. Here’s
the same payment with
@solana/kit, the code behind step 1.
Like the wallet, it adds the order’s reference as an extra read-only account,
following the
Solana Pay reference
convention, so your server can match the payment to its order.
checkout/pay.ts
import { AccountRole, appendTransactionMessageInstructions, assertIsTransactionWithBlockhashLifetime, createTransactionMessage, getSignatureFromTransaction, pipe, sendAndConfirmTransactionFactory, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, signTransactionMessageWithSigners, type Address, type TransactionSigner,} from "@solana/kit";import { findAssociatedTokenPda, getTransferCheckedInstruction, TOKEN_PROGRAM_ADDRESS,} from "@solana-program/token";import { rpc, rpcSubscriptions, USDC_DECIMALS, USDC_MINT } from "./config";const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions,});/** * The customer pays `amount` USDC base units (1 USDC = 1_000_000) to the * store's USDC token account. `reference` is a fresh address for this order, * added as a read-only account so the backend can tell which order a * transaction pays for (the Solana Pay convention). */export async function payStore( customer: TransactionSigner, order: { storeOwner: Address; amount: bigint; reference: Address },) { const [source] = await findAssociatedTokenPda({ owner: customer.address, mint: USDC_MINT, tokenProgram: TOKEN_PROGRAM_ADDRESS, }); const [destination] = await findAssociatedTokenPda({ owner: order.storeOwner, mint: USDC_MINT, tokenProgram: TOKEN_PROGRAM_ADDRESS, }); const transfer = getTransferCheckedInstruction({ source, mint: USDC_MINT, destination, authority: customer, amount: order.amount, decimals: USDC_DECIMALS, }); const transferWithReference = { ...transfer, accounts: [ ...transfer.accounts, { address: order.reference, role: AccountRole.READONLY }, ], }; const { value: latestBlockhash } = await rpc.getLatestBlockhash().send(); const message = pipe( createTransactionMessage({ version: 0 }), (m) => setTransactionMessageFeePayerSigner(customer, m), (m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m), (m) => appendTransactionMessageInstructions([transferWithReference], m), ); const transaction = await signTransactionMessageWithSigners(message); assertIsTransactionWithBlockhashLifetime(transaction); await sendAndConfirmTransaction(transaction, { commitment: "confirmed" }); return getSignatureFromTransaction(transaction);}
What Node prints for the order and the signature payStore() returns, on a local test network (Surfpool)Local test network (Surfpool)
The rest of the tutorial uses this code. Call payStore from your checkout when
the customer pays from a wallet connected to your app rather than by scanning a
QR code: it takes the customer’s signer and the order, and works in any
frontend or on a server.