Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Gateway.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Framework\HTTP\ZendClientFactory;
15 
19 class Gateway implements GatewayInterface
20 {
24  protected $httpClientFactory;
25 
29  protected $mathRandom;
30 
34  protected $logger;
35 
41  public function __construct(
42  ZendClientFactory $httpClientFactory,
45  ) {
46  $this->httpClientFactory = $httpClientFactory;
47  $this->mathRandom = $mathRandom;
48  $this->logger = $logger;
49  }
50 
61  {
62  $result = new DataObject();
63 
64  $clientConfig = [
65  'maxredirects' => 5,
66  'timeout' => 30,
67  'verifypeer' => $config->getValue('verify_peer')
68  ];
69 
70  if ($config->getValue('use_proxy')) {
71  $clientConfig['proxy'] = $config->getValue('proxy_host')
72  . ':'
73  . $config->getValue('proxy_port');
74  $clientConfig['httpproxytunnel'] = true;
75  $clientConfig['proxytype'] = CURLPROXY_HTTP;
76  }
77 
79  $client = $this->httpClientFactory->create();
80 
81  $client->setUri(
82  (bool)$config->getValue('sandbox_flag')
83  ? $config->getValue('transaction_url_test_mode')
84  : $config->getValue('transaction_url')
85  );
86  $client->setConfig($clientConfig);
87  $client->setMethod(\Zend_Http_Client::POST);
88  $client->setParameterPost($request->getData());
89  $client->setHeaders(
90  [
91  'X-VPS-VIT-CLIENT-CERTIFICATION-ID' => '33baf5893fc2123d8b191d2d011b7fdc',
92  'X-VPS-Request-ID' => $this->mathRandom->getUniqueHash(),
93  'X-VPS-CLIENT-TIMEOUT' => 45
94  ]
95  );
96  $client->setUrlEncodeBody(false);
97 
98  try {
99  $response = $client->request();
100 
101  $responseArray = [];
102  parse_str(strstr($response->getBody(), 'RESULT'), $responseArray);
103 
104  $result->setData(array_change_key_case($responseArray, CASE_LOWER));
105  $result->setData('result_code', $result->getData('result'));
106  } catch (\Zend_Http_Client_Exception $e) {
107  $result->addData(
108  [
109  'response_code' => -1,
110  'response_reason_code' => $e->getCode(),
111  'response_reason_text' => $e->getMessage()
112  ]
113  );
114  throw $e;
115  } finally {
116  $this->logger->debug(
117  [
118  'request' => $request->getData(),
119  'result' => $result->getData()
120  ],
121  (array)$config->getValue('getDebugReplacePrivateDataKeys'),
122  (bool)$config->getValue('debug')
123  );
124  }
125 
126  return $result;
127  }
128 }
$response
Definition: 404.php:11
$config
Definition: fraud_order.php:17
postRequest(DataObject $request, ConfigInterface $config)
__construct(ZendClientFactory $httpClientFactory, Random $mathRandom, Logger $logger)
Definition: Gateway.php:41