{
  "info": {
    "name": "RatekLend Credit Gateway",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "description": "کالکشن رسمی اتصال پذیرندگان به درگاه پرداخت اعتباری راتک‌لند.\nترتیب اجرا: ۱) Authentication ۲) Eligibility ۳) Initiate Payment ۴) هدایت کاربر به checkoutUrl ۵) پس از بازگشت کاربر: Confirm ۶) Finalize"
  },
  "item": [
    {
      "name": "Authentication",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "exec": [
              "let keys = pm.environment.get(\"client-id\") + \":\" + pm.environment.get(\"client-secret\");",
              "let encoded = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(keys));",
              "pm.environment.set(\"base64-authorization\", encoded);"
            ],
            "type": "text/javascript"
          }
        },
        {
          "listen": "test",
          "script": {
            "exec": [
              "let response = pm.response.json();",
              "let jwt = response.data.access_token;",
              "pm.collectionVariables.set(\"jwt\", jwt);"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Authorization", "value": "Basic {{base64-authorization}}" },
          { "key": "Content-Type", "value": "application/x-www-form-urlencoded" }
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            { "key": "grant_type", "value": "password", "type": "text" },
            { "key": "scope", "value": "credit-gateway", "type": "text" },
            { "key": "username", "value": "{{username}}", "type": "text" },
            { "key": "password", "value": "{{password}}", "type": "text" }
          ]
        },
        "url": {
          "raw": "{{url}}/api/v1/auth/token",
          "host": ["{{url}}"],
          "path": ["api", "v1", "auth", "token"]
        }
      }
    },
    {
      "name": "Eligibility",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{url}}/api/v1/credit-offers/eligibility?amount={{amount}}&methods=INSTALLMENT,FINANCING",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-offers", "eligibility"],
          "query": [
            { "key": "amount", "value": "{{amount}}" },
            { "key": "methods", "value": "INSTALLMENT,FINANCING" }
          ]
        }
      }
    },
    {
      "name": "Initiate Payment",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "exec": ["pm.environment.set(\"merchant-order-id\", `order_${Date.now()}`);"],
            "type": "text/javascript"
          }
        },
        {
          "listen": "test",
          "script": {
            "exec": [
              "let respBody = pm.response.json();",
              "pm.collectionVariables.set(\"payment-reference\", respBody.data.paymentReference);",
              "console.log('Checkout page:', respBody.data.checkoutUrl);"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"amount\": {{amount}},\n  \"orderLines\": [\n    {\n      \"lineId\": 1,\n      \"items\": [\n        {\n          \"amount\": {{amount}},\n          \"category\": \"digital\",\n          \"count\": 1,\n          \"id\": 101,\n          \"name\": \"محصول نمونه\",\n          \"feeCategory\": 100\n        }\n      ],\n      \"shippingIncluded\": true,\n      \"taxIncluded\": true,\n      \"shippingFee\": 0,\n      \"tax\": 0,\n      \"lineTotal\": {{amount}}\n    }\n  ],\n  \"discountAmount\": 0,\n  \"otherSourceAmount\": 0,\n  \"mobile\": \"{{mobile}}\",\n  \"callbackUrl\": \"{{callback-url}}\",\n  \"merchantOrderId\": \"{{merchant-order-id}}\"\n}"
        },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/initiate",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "initiate"]
        }
      }
    },
    {
      "name": "Confirm",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": { "mode": "raw", "raw": "{\n  \"paymentReference\": \"{{payment-reference}}\"\n}" },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/confirm",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "confirm"]
        }
      }
    },
    {
      "name": "Finalize",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": { "mode": "raw", "raw": "{\n  \"paymentReference\": \"{{payment-reference}}\"\n}" },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/finalize",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "finalize"]
        }
      }
    },
    {
      "name": "Reverse",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": { "mode": "raw", "raw": "{\n  \"paymentReference\": \"{{payment-reference}}\"\n}" },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/reverse",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "reverse"]
        }
      }
    },
    {
      "name": "Void",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": { "mode": "raw", "raw": "{\n  \"paymentReference\": \"{{payment-reference}}\"\n}" },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/void",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "void"]
        }
      }
    },
    {
      "name": "Amend",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"paymentReference\": \"{{payment-reference}}\",\n  \"amount\": {{amount}},\n  \"orderLines\": [\n    {\n      \"lineId\": 1,\n      \"items\": [\n        {\n          \"amount\": {{amount}},\n          \"category\": \"digital\",\n          \"count\": 1,\n          \"id\": 101,\n          \"name\": \"محصول نمونه\",\n          \"feeCategory\": 100\n        }\n      ],\n      \"shippingIncluded\": true,\n      \"taxIncluded\": true,\n      \"shippingFee\": 0,\n      \"tax\": 0,\n      \"lineTotal\": {{amount}}\n    }\n  ],\n  \"discountAmount\": 0,\n  \"otherSourceAmount\": 0\n}"
        },
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/amend",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "amend"]
        }
      }
    },
    {
      "name": "Payment Status",
      "request": {
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{jwt}}", "type": "string" }] },
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{url}}/api/v1/credit-payments/status?paymentReference={{payment-reference}}",
          "host": ["{{url}}"],
          "path": ["api", "v1", "credit-payments", "status"],
          "query": [{ "key": "paymentReference", "value": "{{payment-reference}}" }]
        }
      }
    }
  ],
  "variable": [
    { "key": "url", "value": "https://your-rateklend-host.example.com" },
    { "key": "jwt", "value": "" },
    { "key": "payment-reference", "value": "" }
  ]
}
