The checkout in the browser gives you a signature. Your server fetches that
transaction and checks that it paid you the right amount of USDC for this order
before marking the order paid. This step builds that server and runs it
against a local test network or devnet.
1. Create the project
You need Node.js 20.18 or later. Create a project folder,solana-payments,
with a checkout folder inside it. Every file in this tutorial goes in
checkout/, and you run every command from solana-payments. (The Paywall an
endpoint tutorial adds a paywall-api/ folder next to it.)
Terminal
"type": "module" lets the scripts use top-level await. tsx runs the
TypeScript files directly. The examples are tested with @solana/kit 8.4 and
@solana-program/token 0.17.
Your server reads the network over RPC:
The code reads its network settings from environment variables and defaults
to devnet. Save this as checkout/config.ts:
checkout/config.ts
checkout/pay.ts:
checkout/pay.ts
What Node prints for the order and the signature payStore() returns, on a local test network (Surfpool)Local test network (Surfpool)
order
{
id:
'order-1001',storeOwner:
'DVxMtF9M6EAQ8CjqH9omYUTXdJU7hu4M8G94udRyr1m6',amount: 1000000n,
reference:
'47venn787GZLWUcgUU3QM1CpT11JHQMs8hyYQMPXKN2W'}
const signature = await payStore(customer, order)
signature
'paxK3zbseZEZpGrmGrzm3p3VYF5XN6aTAGKS1sVt2CmkcJ2afLCTB5C84rFT8VFX6LboNFty2pzcTCiVJw5DAtJ'Payment sent · not verified yet
2. Verify a payment
Given the signature and the order, fetch the transaction and check three things: it succeeded, it carries the order’s reference, and the store’s USDC balance went up by exactly the order amount. The balance is read for the USDC mint only, so a payment in any other token doesn’t count. Save this ascheckout/verify-payment.ts:
checkout/verify-payment.ts
Output of verifyPayment() with the right and a wrong amountLocal test network (Surfpool)
await verifyPayment({ ...order, signature, mint: USDC_MINT }){ paid: true }
await verifyPayment({ ...order, amount: 2_000_000n, signature, mint: USDC_MINT }){
paid: false,
reason:
'amount mismatch: expected 2000000, received 1000000'}
Right amount: paidWrong amount: refused
confirmed commitment:
3. Add a confirm endpoint
The endpoint looks the order up, so it needs somewhere to keep orders. This in-memory store stands in for your database. Save it ascheckout/orders.ts:
checkout/orders.ts
markPaidOnce a single database transaction with a unique
constraint on the signature and on each order’s paid signature, so two
concurrent requests can’t both succeed.
The endpoint receives { "signature": "…" } for an order, runs
verifyPayment, and marks the order paid. It refuses a signature that was
already used, so one payment can’t pay for two orders. Save the Node.js version
as checkout/server.ts. It uses no framework, so the handler ports directly to
Express, Fastify, or Hono. The Next.js tab is the same handler as an App Router
route: save it at app/api/orders/[orderId]/confirm/route.ts and point its
imports at your config, orders, and verify-payment files.
checkout/server.ts (Node.js)
reference is optional in verifyPayment, but keep it. Transactions are
public, so without it anyone who sees a payment to your store could submit
its signature for their own order of the same amount. If your checkout can’t
attach a reference, also check who paid, for example that the signer is the
customer’s wallet.- 200 for a confirmed payment of the right amount.
- 402
already usedwhen the same signature is sent again. - 402
amount mismatchwhen a 2 USDC order was paid with 1 USDC.
4. Call it from the checkout
Build the customer’s payment withpayStore, which attaches the order’s
reference that this endpoint requires.
- When the customer checks out, create the order on your server with a fresh
reference (
(await generateKeyPairSigner()).address) and send the order’sstoreOwner,amount, andreferenceto the browser. - In the browser, call
payStore(customer, order), wherecustomeris the wallet’sTransactionSigner(for example fromuseWalletAccountTransactionSignerin@solana/react). It resolves to the transaction signature once the payment is confirmed. POSTthat signature to/api/orders/<order-id>/confirmwith a JSON body of{ "signature": "…" }.
200 with
"paid": true. Treat any other response as unpaid, and let the customer
retry.
The QR code component from step 2 makes its own reference in the browser, so
verifyPayment answers reference not found for its payments and the
endpoint returns 402. To confirm QR code payments, give encodeURL and
watchReference the order’s reference from your server, then post the
signature that watchReference returns.5. Run it on your machine
You now have five files incheckout/: config.ts, pay.ts,
verify-payment.ts, orders.ts, and server.ts. This script plays both
sides: it starts the server on port 3000, creates a store wallet and its USDC
account, creates order order-1001 for 1 USDC, pays it as the customer with
payStore, and posts the signature to the confirm endpoint twice. Save it as
checkout/run.ts:
checkout/run.ts
Output of npx tsx checkout/run.tsLocal test network (Surfpool)
npx tsx checkout/run.ts
Store wallet: DVxMtF9M6EAQ8CjqH9omYUTXdJU7hu4M8G94udRyr1m6 USDC account: 5iE6nCcXGFAXzQRrBCPbFaNm8hXVBPkndqutjJKyVr9S
Paid order-1001: 1 USDC (1000000 base units)
Reference: 47venn787GZLWUcgUU3QM1CpT11JHQMs8hyYQMPXKN2W
Signature: paxK3zbseZEZpGrmGrzm3p3VYF5XN6aTAGKS1sVt2CmkcJ2afLCTB5C84rFT8VFX6LboNFty2pzcTCiVJw5DAtJ
Fee: 5000 lamports
POST /api/orders/order-1001/confirm -> 200
{"paid":true,"orderId":"order-1001","signature":"paxK3zbseZEZpGrmGrzm3p3VYF5XN6aTAGKS1sVt2CmkcJ2afLCTB5C84rFT8VFX6LboNFty2pzcTCiVJw5DAtJ"}POST /api/orders/order-1001/confirm -> 402
{"paid":false,"reason":"already used"}Paid · 200Reused signature · 402
CUSTOMER_KEYPAIR, or the Solana CLI default ~/.config/solana/id.json. If
you don’t have one yet, save this as keygen.ts in solana-payments. It
writes a new key file in the same format and prints its address:
keygen.ts
Terminal
-
Install the Surfpool CLI
and start a local network that copies accounts from devnet, so the devnet
USDC mint the code uses exists locally. It airdrops SOL to
~/.config/solana/id.jsonwhen it starts (create the key file first):Terminal -
In another terminal, in
solana-payments, give the customer 10 test USDC with Surfpool’ssurfnet_setTokenAccountcheatcode. Replace<customer address>with the address of your key file:Terminal -
Point the code at Surfpool:
Terminal
Terminal
200 with "paid": true for the payment, then 402 with
already used for the same signature sent again. Each run creates a new store
wallet, so you can run it as often as the customer has USDC.
Port 3000 in use? Set another one for the run, for example
PORT=3001 npx tsx checkout/run.ts.Run the tests instead
The code on this page and its tests live in the docs repository underpackages/docs-examples/paths/payments/checkout. The test starts its own
Surfpool network and funds a test customer, so you need no Surfpool CLI, keys,
SOL, or USDC. It also checks that verifyPayment rejects a wrong amount, mint,
recipient, or reference:
Terminal
The full example is in
packages/docs-examples/paths/payments/checkout.