# UPIDirect integration guide (for developers and AI coding agents) UPIDirect lets a business collect UPI payments straight into its own bank account (0% per-transaction fee). UPIDirect never holds money: the payer's UPI app pays the merchant's UPI ID directly. UPIDirect creates payment requests and hosted payment pages, tracks status, confirms payments and sends signed webhooks. Human-readable guide: https://upidirect.codepeacock.com/docs/ · AI agents page: https://upidirect.codepeacock.com/docs/ai/ · Support: info@codepeacock.com ## Key facts - Base URL: https://upidirect.codepeacock.com/api - Auth: header `Authorization: Bearer `. Keys: `sk_test_...` (simulated payments) and `sk_live_...` (real money; works only after the merchant is approved and has a UPI ID). - Secret keys are SERVER-SIDE ONLY. Store in env vars (UPIDIRECT_SECRET_KEY). Never in browser/mobile code or git. - Amounts are in rupees, up to 2 decimals: 2000, 2000.50 or "99.99". Min ₹1, max ₹5,00,000. Never send paise. - A payment request is `paid` only after server-side confirmation. Fulfil orders ONLY on the webhook `payment_request.paid`, never on a browser redirect. - Errors: `{ "error": { "code": "...", "message": "..." } }` with HTTP 400/401/404/409/429/5xx. ## Option A: one payment per order (website checkout) POST https://upidirect.codepeacock.com/api/v1/payment_requests Headers: Authorization, Content-Type: application/json, Idempotency-Key: (retries return the same request) Body: ```json { "amount": 2000.50, "purpose": "Order #1001", // required, max 80 chars, shown to payer "customer": { "name": "Ravi Kumar", "phone": "9876543210", "email": "ravi@example.com", "external_ref": "cust_42" }, "external_ref": "1001", // your order/fee id, returned in webhooks "return_url": "https://yoursite.com/orders/1001",// payer can continue here after payment "expires_at": "2026-12-31T18:30:00Z", // optional, default 30 days, max 180 "metadata": { "plan": "gold" } // optional, up to 20 keys } ``` Response 201 (200 if the Idempotency-Key was used before): ```json { "id": "uuid", "object": "payment_request", "reference": "UPD-7K2M9QX4", "mode": "test", "status": "pending", "amount": 2000.50, "currency": "INR", "purpose": "Order #1001", "customer": {...}, "external_ref": "1001", "payment_page_id": null, "metadata": {...}, "payment_url": "https://upidirect.codepeacock.com/r/UPD-7K2M9QX4", "upi_uri": null, "awaiting_confirmation": false, "confirmation": null, "return_url": "...", "expires_at": "...", "paid_at": null, "created_at": "..." } ``` Redirect the customer to `payment_url` (HTTP 303). `upi_uri` (live mode only) is a raw upi://pay link for native apps. Node.js (Express): ```js app.post('/pay/:orderId', async (req, res) => { const order = await getOrder(req.params.orderId) const r = await fetch('https://upidirect.codepeacock.com/api/v1/payment_requests', { method: 'POST', headers: { Authorization: `Bearer ${process.env.UPIDIRECT_SECRET_KEY}`, 'Content-Type': 'application/json', 'Idempotency-Key': `order-${order.id}` }, body: JSON.stringify({ amount: order.total, purpose: `Order #${order.id}`, customer: { name: order.name, phone: order.phone }, external_ref: String(order.id), return_url: `https://yoursite.com/orders/${order.id}` }), }) if (!r.ok) return res.status(502).send('Could not start payment') res.redirect(303, (await r.json()).payment_url) }) ``` PHP: ```php $ch = curl_init('https://upidirect.codepeacock.com/api/v1/payment_requests'); curl_setopt_array($ch, [CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('UPIDIRECT_SECRET_KEY'), 'Content-Type: application/json', 'Idempotency-Key: order-' . $order['id']], CURLOPT_POSTFIELDS => json_encode(['amount' => $order['total'], 'purpose' => 'Order #' . $order['id'], 'external_ref' => (string) $order['id']])]); $pr = json_decode(curl_exec($ch), true); header('Location: ' . $pr['payment_url'], true, 303); ``` Python (requests): ```python pr = requests.post("https://upidirect.codepeacock.com/api/v1/payment_requests", headers={"Authorization": f"Bearer {os.environ['UPIDIRECT_SECRET_KEY']}", "Idempotency-Key": f"order-{order.id}"}, json={"amount": order.total, "purpose": f"Order #{order.id}", "external_ref": str(order.id)}, timeout=15).json() return redirect(pr["payment_url"], code=303) ``` ## Option B: reusable payment page / Pay button (no per-order code) Create once (dashboard → Payment pages, or API), then link to it forever. Every payer fills in name/phone (and the amount, if the page has no fixed amount) and gets their own payment request. POST https://upidirect.codepeacock.com/api/v1/payment_pages body: `{ "title": "December fee", "amount": 2000.50 }` optional: description, slug, amount (null = payer chooses) with min_amount/max_amount, collect_phone (default true), collect_email, return_url Response includes `url` (https://upidirect.codepeacock.com/p/). Other: GET /v1/payment_pages, GET/PATCH /v1/payment_pages/:id (PATCH `{ "active": false }` to stop). Payments from a page carry `payment_page_id` and `metadata.payment_page` = slug. Pay button HTML (no JavaScript needed): ```html Pay with UPI ``` Or the styled script button: ```html ``` ## Webhooks Register: dashboard → Developers, or POST https://upidirect.codepeacock.com/api/v1/webhook_endpoints `{ "url": "https://yoursite.com/upidirect/webhook" }` (response contains the signing secret `whsec_...`; test and live have separate endpoints). Events: payment_request.paid (fulfil here), payment_request.utr_submitted, payment_request.expired, payment_request.cancelled. Request: POST JSON `{ id, type, mode, created_at, data: }`, headers UPD-Signature, UPD-Event-Id, UPD-Event-Type. Reply 2xx quickly. Failures are retried with backoff for 24 h, so de-duplicate on UPD-Event-Id and make updates idempotent. Signature: `UPD-Signature: t=,v1=` where hex = HMAC-SHA256(secret, "."). Use the RAW body. Reject if |now - t| > 300 s. ```js import { createHmac, timingSafeEqual } from 'node:crypto' export function verifyUpiDirect(rawBody, header, secret) { const m = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header ?? ''); if (!m) return false if (Math.abs(Date.now() / 1000 - Number(m[1])) > 300) return false const expected = createHmac('sha256', secret).update(`${m[1]}.${rawBody}`).digest('hex') return timingSafeEqual(Buffer.from(expected), Buffer.from(m[2])) } ``` ```php function verify_upidirect(string $raw, ?string $h, string $secret): bool { if (!preg_match('/^t=(\d+),v1=([0-9a-f]{64})$/', $h ?? '', $m)) return false; if (abs(time() - (int) $m[1]) > 300) return false; return hash_equals(hash_hmac('sha256', $m[1] . '.' . $raw, $secret), $m[2]); } // $raw = file_get_contents('php://input'); $h = $_SERVER['HTTP_UPD_SIGNATURE'] ?? null; ``` ```python def verify_upidirect(raw: bytes, header: str | None, secret: str) -> bool: m = re.fullmatch(r"t=(\d+),v1=([0-9a-f]{64})", header or "") if not m or abs(time.time() - int(m[1])) > 300: return False expected = hmac.new(secret.encode(), f"{m[1]}.".encode() + raw, hashlib.sha256).hexdigest() return hmac.compare_digest(expected, m[2]) ``` Supabase Edge Functions (Deno): the Node snippet works with `import { Buffer } from 'node:buffer'`; read `await req.text()` before JSON.parse. ## Other endpoints GET /v1/payment_requests/:id · GET /v1/payment_requests?status=paid&external_ref=...&limit=20&starting_after= POST /v1/payment_requests/:id/cancel · POST /v1/payment_requests/:id/confirm `{ "utr": "412345678901" }` (optional) GET /v1/webhook_endpoints · DELETE /v1/webhook_endpoints/:id · GET /v1/me (check key and mode) ## How payments get confirmed today UPI does not notify a merchant's UPI ID automatically. A request becomes paid when the payer enters the 12-digit UTR and the merchant presses "Received" in the dashboard, or the merchant marks it paid (dashboard or POST /confirm). Statement matching and bank callbacks are coming; integrations do not change (always listen for payment_request.paid). ## Test plan 1. Use an sk_test_ key. 2. Create a payment request (or open a test payment page). 3. Open payment_url and press "Simulate successful payment". 4. Check your webhook received payment_request.paid with a valid signature and your order is paid. 5. For live: merchant approved + UPI ID set in Settings, create an sk_live_ key and a live webhook endpoint, make a ₹1 test payment. ## MCP server (for AI agents) Remote MCP endpoint (Streamable HTTP): https://upidirect.codepeacock.com/mcp with header `Authorization: Bearer sk_test_...` Claude Code: `claude mcp add --transport http upidirect https://upidirect.codepeacock.com/mcp --header "Authorization: Bearer sk_test_..."` JSON config (Cursor, Claude Desktop via mcp-remote, others): `{ "mcpServers": { "upidirect": { "url": "https://upidirect.codepeacock.com/mcp", "headers": { "Authorization": "Bearer sk_test_..." } } } }` Tools: get_account, create_payment_request, get_payment_request, list_payment_requests, cancel_payment_request, confirm_payment_request, create_payment_page, list_payment_pages, update_payment_page, list_webhook_endpoints, create_webhook_endpoint, delete_webhook_endpoint, get_integration_guide.