createOrder(['amount' => '499.00', 'client_order_id' => 'order_1024', * 'redirect_url' => 'https://yoursite.com/thanks']); * header('Location: ' . $order['payment_url']); // or embed with /checkout.js * * // webhook.php * $raw = file_get_contents('php://input'); * if (!SwiftUPI::verifyWebhook($raw, $_SERVER['HTTP_X_GATEWAY_TIMESTAMP'] ?? '', $_SERVER['HTTP_X_GATEWAY_SIGNATURE'] ?? '', 'whsec_xxx')) { * http_response_code(400); exit; * } * $event = json_decode($raw, true); // $event['event'] === 'payment.success' → mark order paid * http_response_code(200); */ class SwiftUPI { private $key; private $base; public function __construct($apiKey, $baseUrl = 'https://app.swiftupi.com') { $this->key = $apiKey; $this->base = rtrim($baseUrl, '/'); } public function createOrder(array $data, $idempotencyKey = null) { return $this->request('POST', '/api/orders', $data, $idempotencyKey ?: ($data['client_order_id'] ?? null)); } public function getOrder($orderIdOrClientId) { return $this->request('GET', '/api/orders/' . rawurlencode($orderIdOrClientId)); } public function refund($orderId, $amount, $reason = '', $reference = '') { return $this->request('POST', '/api/orders/' . rawurlencode($orderId) . '/refunds', ['amount' => $amount, 'reason' => $reason, 'reference' => $reference]); } public static function verifyWebhook($rawBody, $timestamp, $signature, $secret, $tolerance = 300) { if (!$timestamp || !$signature || abs(time() - (int) $timestamp) > $tolerance) return false; $expected = hash_hmac('sha256', $timestamp . '.' . $rawBody, $secret); return hash_equals($expected, $signature); } private function request($method, $path, $body = null, $idem = null) { $ch = curl_init($this->base . $path); $headers = ['X-API-Key: ' . $this->key, 'Accept: application/json']; if ($idem) $headers[] = 'Idempotency-Key: ' . $idem; if ($body !== null) { $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); } curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => 20]); $res = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $err = curl_error($ch); curl_close($ch); if ($res === false) throw new Exception('SwiftUPI request failed: ' . $err); $json = json_decode($res, true); if ($code >= 400) throw new Exception('SwiftUPI error ' . $code . ': ' . ($json['error'] ?? $res)); return $json; } }