Gateway.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Payflow\Service;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\HTTP\ZendClient;
  9. use Magento\Framework\HTTP\ZendClientFactory;
  10. use Magento\Framework\Math\Random;
  11. use Magento\Payment\Model\Method\ConfigInterface;
  12. use Magento\Payment\Model\Method\Logger;
  13. use Magento\Payment\Model\Method\Online\GatewayInterface;
  14. /**
  15. * Gateway Service
  16. */
  17. class Gateway implements GatewayInterface
  18. {
  19. /**
  20. * @var ZendClientFactory
  21. */
  22. protected $httpClientFactory;
  23. /**
  24. * @var Random
  25. */
  26. protected $mathRandom;
  27. /**
  28. * @var Logger
  29. */
  30. protected $logger;
  31. /**
  32. * @param ZendClientFactory $httpClientFactory
  33. * @param Random $mathRandom
  34. * @param Logger $logger
  35. */
  36. public function __construct(
  37. ZendClientFactory $httpClientFactory,
  38. Random $mathRandom,
  39. Logger $logger
  40. ) {
  41. $this->httpClientFactory = $httpClientFactory;
  42. $this->mathRandom = $mathRandom;
  43. $this->logger = $logger;
  44. }
  45. /**
  46. * Post request into gateway
  47. *
  48. * @param DataObject $request
  49. * @param ConfigInterface $config
  50. *
  51. * @return DataObject
  52. * @throws \Zend_Http_Client_Exception
  53. */
  54. public function postRequest(DataObject $request, ConfigInterface $config)
  55. {
  56. $result = new DataObject();
  57. $clientConfig = [
  58. 'maxredirects' => 5,
  59. 'timeout' => 30,
  60. 'verifypeer' => $config->getValue('verify_peer')
  61. ];
  62. if ($config->getValue('use_proxy')) {
  63. $clientConfig['proxy'] = $config->getValue('proxy_host')
  64. . ':'
  65. . $config->getValue('proxy_port');
  66. $clientConfig['httpproxytunnel'] = true;
  67. $clientConfig['proxytype'] = CURLPROXY_HTTP;
  68. }
  69. /** @var ZendClient $client */
  70. $client = $this->httpClientFactory->create();
  71. $client->setUri(
  72. (bool)$config->getValue('sandbox_flag')
  73. ? $config->getValue('transaction_url_test_mode')
  74. : $config->getValue('transaction_url')
  75. );
  76. $client->setConfig($clientConfig);
  77. $client->setMethod(\Zend_Http_Client::POST);
  78. $client->setParameterPost($request->getData());
  79. $client->setHeaders(
  80. [
  81. 'X-VPS-VIT-CLIENT-CERTIFICATION-ID' => '33baf5893fc2123d8b191d2d011b7fdc',
  82. 'X-VPS-Request-ID' => $this->mathRandom->getUniqueHash(),
  83. 'X-VPS-CLIENT-TIMEOUT' => 45
  84. ]
  85. );
  86. $client->setUrlEncodeBody(false);
  87. try {
  88. $response = $client->request();
  89. $responseArray = [];
  90. parse_str(strstr($response->getBody(), 'RESULT'), $responseArray);
  91. $result->setData(array_change_key_case($responseArray, CASE_LOWER));
  92. $result->setData('result_code', $result->getData('result'));
  93. } catch (\Zend_Http_Client_Exception $e) {
  94. $result->addData(
  95. [
  96. 'response_code' => -1,
  97. 'response_reason_code' => $e->getCode(),
  98. 'response_reason_text' => $e->getMessage()
  99. ]
  100. );
  101. throw $e;
  102. } finally {
  103. $this->logger->debug(
  104. [
  105. 'request' => $request->getData(),
  106. 'result' => $result->getData()
  107. ],
  108. (array)$config->getValue('getDebugReplacePrivateDataKeys'),
  109. (bool)$config->getValue('debug')
  110. );
  111. }
  112. return $result;
  113. }
  114. }