# Universal Payment Webhook Adapter API
The Dojo Jutsu Universal Payment Webhook Adapter (payment-webhook-adapter) is a resilient cloud Edge Function that normalizes external card reader, point-of-sale, and online subscription webhooks from Stripe, Square, and Clover into a unified, tenant-isolated ledger format.
When a student pays tuition online, buys a 10-class pack from their phone, or taps their card on a physical terminal, this endpoint ingests the event, updates public.payment_events, advances public.student_memberships, and automatically flips the student's status ring to GREEN before they step onto the mat.
# 1. Webhook Endpoint Specification
- Endpoint:
POST /functions/v1/payment-webhook-adapter - Gateway Config (
supabase/config.toml):[functions.payment-webhook-adapter] verify_jwt = false - Authentication: Mandatory fail-closed provider-specific HMAC-SHA256 signature verification headers.
- Content-Type:
application/json
# Required Signature Headers & Environment Secrets
Every incoming webhook request must present one of the following cryptographic signature headers, and the corresponding signing secret must be configured in the Edge Function environment. Requests with missing/invalid signatures or unconfigured provider secrets are immediately rejected with HTTP 401 Unauthorized (fail-closed):
| Provider | Header | Required Secret Env Var | Verification Mechanism |
|---|---|---|---|
| Stripe | stripe-signature | STRIPE_WEBHOOK_SECRET | Stripe Webhook Secret (whsec_...) constant-time HMAC-SHA256 timestamp signature (300s replay window) |
| Square | x-square-hmacsha256-signature | SQUARE_WEBHOOK_SIGNATURE_KEY | Square Webhook Signature Key constant-time HMAC-SHA256 over notificationUrl + rawBody |
| Clover | x-clover-signature | CLOVER_WEBHOOK_SECRET | Clover Webhook Secret constant-time HMAC-SHA256 (t=<timestamp>,v1=<hex> or hex digest) |
# 2. Supported Events & Plan Mappings
| Gateway | Event Name | Plan Identifier (plan_id) | Entitlement Applied | Ring Effect |
|---|---|---|---|---|
| Stripe | checkout.session.completed | monthly_tuition | months_added = 1 (+30 days to paid_until_date) | 🔴/🟡 ➔ 🟢 GREEN |
| Stripe | invoice.paid | monthly_tuition | months_added = 1 (+30 days to paid_until_date) | 🔴/🟡 ➔ 🟢 GREEN |
| Square | payment.updated (COMPLETED) | 10_class_pack | credits_added = 10 (+10 to credits_remaining) | 🔴/🟡 ➔ 🟢 GREEN |
| Clover | payment_completed | single_drop_in | credits_added = 1 (+1 to credits_remaining) | 🔴/🟡 ➔ 🟢 GREEN |
# 3. Sample Gateway Payloads
# 3.1 Stripe: checkout.session.completed
{
"id": "evt_1P0000000000000000000001",
"type": "checkout.session.completed",
"data": {
"object": {
"id": "cs_test_a1b2c3d4e5",
"amount_total": 15000,
"currency": "usd",
"payment_status": "paid",
"metadata": {
"dojo_id": "10000000-0000-4000-8000-000000000001",
"member_id": "40000000-0000-4000-8000-000000000004",
"plan_id": "monthly_tuition"
}
}
}
}# 3.2 Square: payment.updated
{
"event_id": "sq_evt_20261016_001",
"type": "payment.updated",
"data": {
"object": {
"payment": {
"id": "sq_pay_99887766",
"status": "COMPLETED",
"total_money": {
"amount": 12000,
"currency": "USD"
},
"note": "{\"dojo_id\":\"10000000-0000-4000-8000-000000000001\",\"member_id\":\"40000000-0000-4000-8000-000000000005\",\"plan_id\":\"10_class_pack\"}"
}
}
}
}# 3.3 Clover: payment_completed
{
"id": "clv_evt_44332211",
"type": "payment_completed",
"amount": 2000,
"metadata": {
"dojo_id": "10000000-0000-4000-8000-000000000001",
"member_id": "40000000-0000-4000-8000-000000000006",
"plan_id": "single_drop_in"
}
}# 4. Status-Flipping Lifecycle & Database Guarantees
sequenceDiagram
autonumber
actor Student
participant Gateway as Stripe / Square / Clover
participant Webhook as payment-webhook-adapter
participant DB as Supabase PostgreSQL
participant App as Mobile Mat Mode (Drift SQLite)
Student->>Gateway: Complete payment online or card reader tap
Gateway->>Webhook: POST /functions/v1/payment-webhook-adapter (with signature header)
Webhook->>Webhook: Validate signature & extract metadata (dojo_id, member_id, plan_id)
Webhook->>DB: Verify student_memberships row exists for (member_id, dojo_id)
Webhook->>DB: INSERT into public.payment_events (external_event_id, amount_cents)
Webhook->>DB: UPDATE public.student_memberships (paid_until_date or credits_remaining)
Note over DB: PostgreSQL trigger trg_membership_status automatically calculates status = 'GREEN'
Webhook-->>Gateway: HTTP 200 OK {"success": true, "status": "GREEN"}
DB-->>App: Realtime sync / Drift local pull
Note over App: Student avatar ring flips to GREEN on Mat Mode roster# Database Atomic Updates:
- Strict Tenant & Member Binding: Verifies that
student_membershipsmatches bothmember_idanddojo_idbefore recording or crediting any event, preventing cross-tenant membership mutation. - Ledger Immutability: Inserts a record into
public.payment_eventswithprovider,amount_cents, andexternal_event_idusingSUPABASE_SERVICE_ROLE_KEY(no anon-key fallback). - Entitlement Extension:
- Monthly Plans:
paid_until_dateis extended by 30 days fromgreatest(current_date, existing_paid_until_date). - Punch Cards:
credits_remainingincrements bycredits_added(e.g., +10). - Reactive Trigger Execution: The
trg_membership_statustrigger executesrecalculate_membership_status(), computing the new status as'GREEN'with 0ms application delay. - Shoulder-Surfing Privacy: Webhook payloads, amounts, and transaction IDs remain strictly in administrative tables and private student receipts. The instructor's Mat Mode roster displays solely the high-contrast Green avatar ring.