$amount, 'description' => 'Pesanan ' . $orderId, 'fee_bearer' => 'merchant', ], JSON_THROW_ON_ERROR); $ch = curl_init(GATEWAY_URL . '/api/v1/create-payment'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 20, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'X-Api-Key: ' . API_KEY, 'Idempotency-Key: ' . $idempotencyKey, ], CURLOPT_POSTFIELDS => $payload, ]); $raw = curl_exec($ch); if ($raw === false) { throw new RuntimeException('Gagal menghubungi gateway: ' . curl_error($ch)); } $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $result = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); if ($httpCode !== 201 || empty($result['success'])) { http_response_code(502); exit('Pembayaran gagal dibuat: ' . ($result['message'] ?? 'kesalahan gateway')); } $trxId = $result['data']['trx_id']; // Simpan trx_id dan total_paid pada pesanan sebelum redirect. $_SESSION['loket_trx_id'] = $trxId; header('Location: ' . $result['data']['payment_url']); exit;