/examples/commercelouise-toolkit/commerce/square test mode · no data leaves your browser

A card checkout, priced on the server

louise-toolkit/commerce/square wraps the Square API for Workers. The browser tokenizes the card into a sourceId (raw card data never touches your Worker); the server prices the item from the live catalog and charges the token with createPayment. Sandbox test cards approve for free.

live · running now

Cortado

Single origin · 6oz

$4.50

import { createPayment, listCatalogItems, type SquareConfig } from "louise-toolkit/commerce/square";

interface CheckoutEnv {
  SQUARE_TOKEN: string; // server-only access token (secret)
  SQUARE_LOCATION: string; // your Square location id
  SQUARE_ENV: "sandbox" | "production"; // sandbox honors test cards, moves no real money
}

// POST { sourceId, variationId } — charge the tokenized card for one catalog item.
export async function handleCheckout(request: Request, env: CheckoutEnv): Promise<Response> {
  const { sourceId, variationId } = (await request.json()) as {
    sourceId: string;
    variationId: string;
  };

  const config: SquareConfig = { accessToken: env.SQUARE_TOKEN, environment: env.SQUARE_ENV };

  // Price server-side from the live catalog — never trust an amount from the client.
  const catalog = await listCatalogItems(config);
  const variation = catalog.flatMap((item) => item.variations).find((v) => v.id === variationId);
  if (!variation) return Response.json({ error: "Unknown item" }, { status: 404 });

  // Charge the card token. `amountMoney` is the smallest unit (cents for USD); in
  // sandbox, Square's test cards approve instantly and no real money moves.
  const payment = await createPayment(config, {
    sourceId,
    locationId: env.SQUARE_LOCATION,
    amountMoney: { amount: variation.priceCents, currency: variation.currency },
  });

  return Response.json({ status: payment.status, id: payment.id, receipt: payment.receiptUrl });
}
sliced from real source — never drifts
Try itHit pay with Square's sandbox test card — watch it tokenize, then charge. The charge is simulated here; the code on the right is the real handler. Deep-dive doc View source

Stripe and Fourthwall follow the same shape — a client token or hosted checkout, then a server call — via louise-toolkit/commerce/stripe andlouise-toolkit/commerce/fourthwall.