Skip to content
Johan Peña

Published · 1 min read

A payment button the browser can't bargain with

Santiago Entrenador is a landing page for a coach, with a button to pay for a reserved spot. The page is static and has no database. The question was how to charge money without letting the browser decide how much.

What could go wrong

If the amount travels in the link or sits in the page's JavaScript, anyone can edit it before paying. And if the page shows “payment approved” because the return address says so, anyone can type that address.

The server sets the price and signs it

The button links to a serverless function with only a product id. The function checks the id against a catalog that lives on the server, takes the amount from there, builds a unique reference, and signs reference, amount and currency with a secret the browser never sees. Then it redirects to the payment provider's hosted checkout.

Santiago Entrenador · api/checkout.ts
if (!isCheckoutProductId(productId)) {
  return new Response("Producto no válido", { status: 400 });
}

const product = CHECKOUT_PRODUCTS[productId];
// ...
const amountInCents = product.amountCop * 100;
const currency = "COP";
const reference = `${productId}-${Date.now()}-${randomBytes(4).toString("hex")}`;
const integrity = createHash("sha256")
  .update(`${reference}${amountInCents}${currency}${integritySecret}`)
  .digest("hex");

Changing the amount in the checkout address breaks the signature, and the provider rejects the transaction.

The page asks instead of believing

When the visitor comes back, the page calls a second function with the transaction id. That function validates the id's format, asks the provider for the real state, and returns it. Test keys talk to the sandbox and live keys to production, decided by the key's prefix.

Santiago Entrenador · api/payment-status.ts
if (!/^[\w-]{1,64}$/.test(id)) {
  return json({ error: "Transacción no válida" }, 400);
}
// ...
// Las llaves de prueba (pub_test_) viven en el sandbox; las reales, en producción.
const apiBase = publicKey.startsWith("pub_test_") ? "https://sandbox.wompi.co/v1" : "https://production.wompi.co/v1";

const response = await fetch(`${apiBase}/transactions/${id}`);

When payments are not configured

If the keys are missing, the checkout function does not fail. It redirects to WhatsApp with a message naming the product, so the reservation still reaches the coach.

One limit remains: payment state is checked when the visitor returns. There is no server-side webhook recording payments, so a visitor who closes the tab before returning is only visible in the provider's dashboard.

← All writing