Exemple #1
0
 /**
  * Post request to gateway and return response
  *
  * @param \Magento\Authorizenet\Model\Authorizenet\Request $request
  * @return \Magento\Authorizenet\Model\Authorizenet\Result
  * @throws \Magento\Framework\Model\Exception
  */
 protected function _postRequest(\Magento\Framework\Object $request)
 {
     $debugData = array('request' => $request->getData());
     $result = $this->_resultFactory->create();
     $client = new \Magento\Framework\HTTP\ZendClient();
     $uri = $this->getConfigData('cgi_url');
     $client->setUri($uri ? $uri : self::CGI_URL);
     $client->setConfig(array('maxredirects' => 0, 'timeout' => 30));
     foreach ($request->getData() as $key => $value) {
         $request->setData($key, str_replace(self::RESPONSE_DELIM_CHAR, '', $value));
     }
     $request->setXDelimChar(self::RESPONSE_DELIM_CHAR);
     $client->setParameterPost($request->getData());
     $client->setMethod(\Zend_Http_Client::POST);
     try {
         $response = $client->request();
     } catch (\Exception $e) {
         $result->setResponseCode(-1)->setResponseReasonCode($e->getCode())->setResponseReasonText($e->getMessage());
         $debugData['result'] = $result->getData();
         $this->_debug($debugData);
         throw new \Magento\Framework\Model\Exception($this->_wrapGatewayError($e->getMessage()));
     }
     $responseBody = $response->getBody();
     $r = explode(self::RESPONSE_DELIM_CHAR, $responseBody);
     if ($r) {
         $result->setResponseCode((int) str_replace('"', '', $r[0]))->setResponseSubcode((int) str_replace('"', '', $r[1]))->setResponseReasonCode((int) str_replace('"', '', $r[2]))->setResponseReasonText($r[3])->setApprovalCode($r[4])->setAvsResultCode($r[5])->setTransactionId($r[6])->setInvoiceNumber($r[7])->setDescription($r[8])->setAmount($r[9])->setMethod($r[10])->setTransactionType($r[11])->setCustomerId($r[12])->setMd5Hash($r[37])->setCardCodeResponseCode($r[38])->setCAVVResponseCode(isset($r[39]) ? $r[39] : null)->setSplitTenderId($r[52])->setAccNumber($r[50])->setCardType($r[51])->setRequestedAmount($r[53])->setBalanceOnCard($r[54]);
     } else {
         throw new \Magento\Framework\Model\Exception(__('Something went wrong in the payment gateway.'));
     }
     $debugData['result'] = $result->getData();
     $this->_debug($debugData);
     return $result;
 }
 /**
  * Post request to gateway and return response
  *
  * @param \Magento\Authorizenet\Model\Request $request
  * @return \Magento\Authorizenet\Model\Response
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function postRequest(\Magento\Authorizenet\Model\Request $request)
 {
     $result = $this->responseFactory->create();
     $client = new \Magento\Framework\HTTP\ZendClient();
     $url = $this->getConfigData('cgi_url') ?: self::CGI_URL;
     $debugData = ['url' => $url, 'request' => $request->getData()];
     $client->setUri($url);
     $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
     foreach ($request->getData() as $key => $value) {
         $request->setData($key, str_replace(self::RESPONSE_DELIM_CHAR, '', $value));
     }
     $request->setXDelimChar(self::RESPONSE_DELIM_CHAR);
     $client->setParameterPost($request->getData());
     $client->setMethod(\Zend_Http_Client::POST);
     try {
         $response = $client->request();
         $responseBody = $response->getBody();
         $debugData['response'] = $responseBody;
     } catch (\Exception $e) {
         $result->setXResponseCode(-1)->setXResponseReasonCode($e->getCode())->setXResponseReasonText($e->getMessage());
         throw new \Magento\Framework\Exception\LocalizedException($this->dataHelper->wrapGatewayError($e->getMessage()));
     } finally {
         $this->_debug($debugData);
     }
     $r = explode(self::RESPONSE_DELIM_CHAR, $responseBody);
     if ($r) {
         $result->setXResponseCode((int) str_replace('"', '', $r[0]))->setXResponseReasonCode((int) str_replace('"', '', $r[2]))->setXResponseReasonText($r[3])->setXAvsCode($r[5])->setXTransId($r[6])->setXInvoiceNum($r[7])->setXAmount($r[9])->setXMethod($r[10])->setXType($r[11])->setData('x_MD5_Hash', $r[37])->setXAccountNumber($r[50]);
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Something went wrong in the payment gateway.'));
     }
     return $result;
 }