/ Cases / Restaurants / Loyalty

Loyalty in Apple and Google Wallet that updates itself

The stamp card lives on the phone, pops up when the customer arrives and the agent can credit receipts. Automatic birthday gift.

/ Restaurantes Atitlán/ 2026/ No app to download

0

apps to download

5 min

resync

/ The problem

Paper stamp cards get lost, and nobody downloads an app to collect coffees.

/ What we built

  • An Apple Wallet and Google Wallet pass signed on the server, added in one tap.
  • It updates itself on the phone when a stamp is added and shows on the lock screen on arrival.
  • The WhatsApp agent can create the card and credit a receipt; a receipt can't be credited twice.
  • Automatic birthday gift and a resync every 5 minutes.

/ How it works

  1. 01Customer adds the pass
  2. 02Buys in store
  3. 03Stamp is credited
  4. 04The pass updates itself

/ By the numbers

Loyalty cards added to Wallet (cumulative)

■ Real production data · counts per month, no amounts or personal data · Cumulative at month end.

/ Real code

src/lib/loyalty/verificar-recibo.tsts
  // ⚠️ IGUALDAD, no `.length`: ver el encabezado. Un número inexistente pero
  // bien formado devuelve la página entera sin filtrar.
  const iguales = (datos.receipts ?? []).filter((r) => digitos(r.receipt_number) === d);
  if (iguales.length === 0) return { ok: false, code: 'receipt_not_found' };
  if (iguales.length > 1) {
    // …
    // no se adivina cuál es. Que el cajero mire el guion impreso.
    return { ok: false, code: 'receipt_ambiguous', detalle: { opciones: iguales.map((r) => r.receipt_number).join(', ') } };
  }

  const r = iguales[0];
  if (r.cancelled_at || r.receipt_type === 'REFUND') return { ok: false, code: 'receipt_cancelled' };

  const tienda = tiendaDeRecibo(r.store_id);
  if (!tienda) {
    // Tienda nueva que nadie mapeó. NO se rechaza al cliente por un hueco
    // nuestro: se acredita marcado y el panel lo muestra.
    return { ok: true, estado: 'unverified', motivo: `store_id sin mapear: ${r.store_id}` };
  }
  if (tienda.sede !== o.sucursal) {
    return { ok: false, code: 'receipt_wrong_store', detalle: { tienda: tienda.nombre, sede: tienda.sede } };
  }

  const ahora = o.ahora ?? new Date();
  const horas = (ahora.getTime() - new Date(r.receipt_date).getTime()) / 3_600_000;
  if (horas > VENTANA_HORAS) {
    return { ok: false, code: 'receipt_too_old', detalle: { fecha: r.receipt_date.slice(0, 10), horas: Math.round(horas) } };
  }

■ Before granting a stamp, the receipt is checked against the POS: exact number, not voided, from that location and from the last 48 hours.

/ Stack

Apple WalletGoogle WalletAPNsCloudflare WorkersPostgres