#!/usr/bin/env bash
# ── نمونه اتصال cURL — درگاه پرداخت اعتباری راتک‌لند ─────────────────────────

BASE_URL="https://your-rateklend-host.example.com"
CLIENT_ID="YOUR_CLIENT_ID"
CLIENT_SECRET="YOUR_CLIENT_SECRET"
USERNAME="YOUR_USERNAME"
PASSWORD="YOUR_PASSWORD"

# ۱) دریافت Access Token
BASIC=$(printf '%s:%s' "$CLIENT_ID" "$CLIENT_SECRET" | base64)
curl --location --request POST "$BASE_URL/api/v1/auth/token" \
  --header "Authorization: Basic $BASIC" \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=password' \
  --data-urlencode 'scope=credit-gateway' \
  --data-urlencode "username=$USERNAME" \
  --data-urlencode "password=$PASSWORD"

# JWT="eyJhbGciOi..."

# ۲) بررسی امکان پرداخت
curl --location "$BASE_URL/api/v1/credit-offers/eligibility?amount=120000000&methods=INSTALLMENT" \
  --header "Authorization: Bearer $JWT"

# ۳) ایجاد جلسهٔ پرداخت
curl --location --request POST "$BASE_URL/api/v1/credit-payments/initiate" \
  --header "Authorization: Bearer $JWT" \
  --header 'Content-Type: application/json' \
  --data '{
    "amount": 120000000,
    "discountAmount": 0,
    "otherSourceAmount": 0,
    "mobile": "+989123456789",
    "callbackUrl": "https://shop.example.com/ratek/callback",
    "merchantOrderId": "order-58291",
    "orderLines": [{
      "lineId": 1,
      "shippingIncluded": true,
      "taxIncluded": true,
      "shippingFee": 0,
      "tax": 0,
      "lineTotal": 120000000,
      "items": [{
        "id": 101, "name": "گوشی موبایل", "category": "digital",
        "count": 1, "amount": 120000000, "feeCategory": 100
      }]
    }]
  }'

# ۴) Confirm
curl --location --request POST "$BASE_URL/api/v1/credit-payments/confirm" \
  --header "Authorization: Bearer $JWT" \
  --header 'Content-Type: application/json' \
  --data '{"paymentReference": "rlcp_..."}'

# ۵) Finalize
curl --location --request POST "$BASE_URL/api/v1/credit-payments/finalize" \
  --header "Authorization: Bearer $JWT" \
  --header 'Content-Type: application/json' \
  --data '{"paymentReference": "rlcp_..."}'

# ۶) Reverse / Void — بدنه یکسان
curl --location --request POST "$BASE_URL/api/v1/credit-payments/reverse" \
  --header "Authorization: Bearer $JWT" --header 'Content-Type: application/json' \
  --data '{"paymentReference": "rlcp_..."}'

curl --location --request POST "$BASE_URL/api/v1/credit-payments/void" \
  --header "Authorization: Bearer $JWT" --header 'Content-Type: application/json' \
  --data '{"paymentReference": "rlcp_..."}'

# ۷) استعلام وضعیت
curl --location "$BASE_URL/api/v1/credit-payments/status?paymentReference=rlcp_..." \
  --header "Authorization: Bearer $JWT"
