bearer = $bearer; $this->isProduction = $isProduction; } protected function buildApiUrl(string $endpoint): string { $base = $this->isProduction ? "https://qr-acquiring.a-bank.com.ua/api/v1/cms" : "https://qr-acquiring-release.a-bank.com.ua/api/v1/cms"; return $base . $endpoint; } protected function sendRequest(string $method, string $endpoint, array $body = []): \stdClass { $headers = [ 'Content-Type' => 'application/json', 'Authorization' => "Bearer " . $this->bearer ]; $args = [ 'method' => $method, 'headers' => $headers, 'timeout' => 60, 'user-agent' => 'WooCommerce/' . WC()->version, ]; if ($method === 'POST') { $args['body'] = json_encode($body); } $response = wp_remote_request($this->buildApiUrl($endpoint), $args); if ( is_wp_error( $response ) ) { throw new \Exception( 'HTTP request error: ' . esc_html( $response->get_error_message() ) ); } $data = json_decode(wp_remote_retrieve_body($response)); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception("JSON decode error"); } return $data; } public function fetchToken(string $login, string $token): string { $body = [ 'login' => $login, 'secret_key' => $token ]; $args = [ 'method' => 'POST', 'headers' => ['Content-Type' => 'application/json'], 'body' => json_encode($body), 'timeout' => 60, 'user-agent' => 'WooCommerce/' . WC()->version ]; $response = wp_remote_request($this->buildApiUrl('/auth/get-token'), $args); if ( is_wp_error( $response ) ) { throw new \Exception( 'Auth request failed: ' . esc_html( $response->get_error_message() ) ); } $data = json_decode(wp_remote_retrieve_body($response)); if (!isset($data->access_token)) { throw new \Exception("Access token missing in response"); } $this->bearer = $data->access_token; return $data->access_token; } public function createInvoice(array $payload): \stdClass { return $this->sendRequest('POST', '/make-invoice', $payload); } public function createPreAuthInvoice(array $payload): \stdClass { return $this->sendRequest('POST', '/make-pre-auth-invoice', $payload); } public function getInvoiceStatus(string $invoice_id): \stdClass { return $this->sendRequest('GET', '/get-invoice-state?invoice_id=' . $invoice_id); } public function refund(array $payload): \stdClass { return $this->sendRequest('POST', '/refund-invoice', $payload); } public function confirmHold(array $payload): \stdClass { return $this->sendRequest('POST', '/payment/confirm', $payload); } }