httpClientFactory = $httpClientFactory; $this->config = $config; $this->paymentLogger = $paymentLogger; $this->logger = $logger; $this->json = $json; } /** * Places request to gateway. Returns result as ENV array * * @param TransferInterface $transferObject * @return array * @throws \Magento\Payment\Gateway\Http\ClientException */ public function placeRequest(TransferInterface $transferObject) { $request = $transferObject->getBody(); $log = [ 'request' => $request, ]; $client = $this->httpClientFactory->create(); $url = $this->config->getApiUrl(); $type = $request['payload_type']; unset($request['payload_type']); $request = [$type => $request]; try { $client->setUri($url); $client->setConfig(['maxredirects' => 0, 'timeout' => 30]); $client->setRawData($this->json->serialize($request), 'application/json'); $client->setMethod(ZendClient::POST); $responseBody = $client->request() ->getBody(); // Strip BOM because Authorize.net sends it in the response if ($responseBody && substr($responseBody, 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf)) { $responseBody = substr($responseBody, 3); } $log['response'] = $responseBody; try { $data = $this->json->unserialize($responseBody); } catch (InvalidArgumentException $e) { throw new \Exception('Invalid JSON was returned by the gateway'); } return $data; } catch (\Exception $e) { $this->logger->critical($e); throw new ClientException( __('Something went wrong in the payment gateway.') ); } finally { $this->paymentLogger->debug($log); } } }