#!/usr/bin/env bash # ───────────────────────────────────────────────────────────────────────────── # moyasar_pilot_test.sh — G2 gate: end-to-end Moyasar 1 SAR pilot test # ───────────────────────────────────────────────────────────────────────────── # # Validates the full funnel: # 1. POST /api/v1/checkout → creates Moyasar invoice for pilot_1sar plan # 2. Captures payment_url + invoice_id # 3. Fires PostHog CHECKOUT_STARTED event (in the API) # 4. Prompts you to open payment_url and pay with Moyasar test card # 5. Verifies webhook was received + idempotency + DLQ stats stable # 6. Fires PostHog PAYMENT_SUCCEEDED event (in the webhook handler) # # Prerequisites (set in /opt/dealix/.env + service restarted): # MOYASAR_SECRET_KEY=sk_test_XXXX # MOYASAR_WEBHOOK_SECRET= # also registered in Moyasar dashboard # POSTHOG_API_KEY=phc_XXXX # POSTHOG_HOST=https://us.i.posthog.com # # Moyasar test cards: https://docs.moyasar.com/testing # * Success: 4111 1111 1111 1111 | any future expiry | any CVV | any 3DS pwd # * Fail: 4000 0000 0000 0002 # ───────────────────────────────────────────────────────────────────────────── set -euo pipefail API_BASE="${API_BASE:-http://127.0.0.1:8001}" API_KEY="${API_KEY:?API_KEY is required (from /opt/dealix/.env API_KEYS)}" TEST_EMAIL="${TEST_EMAIL:-pilot-$(date +%s)@dealix.me}" TEST_LEAD_ID="${TEST_LEAD_ID:-pilot_$(date +%s)}" hdr=(-H "X-API-Key: $API_KEY") echo "=========================================" echo " Dealix — G2 Moyasar Pilot Test (1 SAR)" echo "=========================================" echo # ── Step 0: preflight — check required keys present ────────────────────────── echo "[0/6] Preflight — checking .env for required keys" MISSING=() for key in MOYASAR_SECRET_KEY MOYASAR_WEBHOOK_SECRET POSTHOG_API_KEY; do if ! grep -q "^$key=" /opt/dealix/.env 2>/dev/null; then MISSING+=("$key") fi done if [[ ${#MISSING[@]} -gt 0 ]]; then echo "❌ Missing in /opt/dealix/.env: ${MISSING[*]}" echo " Add them and 'systemctl restart dealix-api' before running this." exit 1 fi echo "✅ All required keys present" echo # ── Step 1: baseline DLQ + approvals ───────────────────────────────────────── echo "[1/6] Baseline: DLQ stats + approvals stats" DLQ_BEFORE=$(curl -s "${hdr[@]}" "$API_BASE/api/v1/admin/dlq/stats" || echo "{}") echo "DLQ before: $DLQ_BEFORE" APP_BEFORE=$(curl -s "${hdr[@]}" "$API_BASE/api/v1/admin/approvals/stats" || echo "{}") echo "Approvals before: $APP_BEFORE" echo # ── Step 2: create checkout ────────────────────────────────────────────────── echo "[2/6] Creating Moyasar invoice for pilot_1sar plan" CHECKOUT_BODY=$(cat <