🥋 Dojo Jutsu Docs docs.dojojutsu.com
← dojojutsu.com Launch Portal →

# 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):

ProviderHeaderRequired Secret Env VarVerification Mechanism
Stripestripe-signatureSTRIPE_WEBHOOK_SECRETStripe Webhook Secret (whsec_...) constant-time HMAC-SHA256 timestamp signature (300s replay window)
Squarex-square-hmacsha256-signatureSQUARE_WEBHOOK_SIGNATURE_KEYSquare Webhook Signature Key constant-time HMAC-SHA256 over notificationUrl + rawBody
Cloverx-clover-signatureCLOVER_WEBHOOK_SECRETClover Webhook Secret constant-time HMAC-SHA256 (t=<timestamp>,v1=<hex> or hex digest)

# 2. Supported Events & Plan Mappings

GatewayEvent NamePlan Identifier (plan_id)Entitlement AppliedRing Effect
Stripecheckout.session.completedmonthly_tuitionmonths_added = 1 (+30 days to paid_until_date)🔴/🟡 ➔ 🟢 GREEN
Stripeinvoice.paidmonthly_tuitionmonths_added = 1 (+30 days to paid_until_date)🔴/🟡 ➔ 🟢 GREEN
Squarepayment.updated (COMPLETED)10_class_packcredits_added = 10 (+10 to credits_remaining)🔴/🟡 ➔ 🟢 GREEN
Cloverpayment_completedsingle_drop_incredits_added = 1 (+1 to credits_remaining)🔴/🟡 ➔ 🟢 GREEN

# 3. Sample Gateway Payloads

# 3.1 Stripe: checkout.session.completed

JSON
{
  "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

JSON
{
  "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

JSON
{
  "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

MERMAID
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_memberships matches both member_id and dojo_id before recording or crediting any event, preventing cross-tenant membership mutation.
  • Ledger Immutability: Inserts a record into public.payment_events with provider, amount_cents, and external_event_id using SUPABASE_SERVICE_ROLE_KEY (no anon-key fallback).
  • Entitlement Extension:
  • Monthly Plans: paid_until_date is extended by 30 days from greatest(current_date, existing_paid_until_date).
  • Punch Cards: credits_remaining increments by credits_added (e.g., +10).
  • Reactive Trigger Execution: The trg_membership_status trigger executes recalculate_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.