Client.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AuthorizenetAcceptjs\Gateway\Http;
  8. use InvalidArgumentException;
  9. use Magento\AuthorizenetAcceptjs\Gateway\Config;
  10. use Magento\Framework\HTTP\ZendClient;
  11. use Magento\Framework\HTTP\ZendClientFactory;
  12. use Magento\Framework\Serialize\Serializer\Json;
  13. use Magento\Payment\Gateway\Http\ClientException;
  14. use Magento\Payment\Gateway\Http\ClientInterface;
  15. use Magento\Payment\Gateway\Http\TransferInterface;
  16. use Magento\Payment\Model\Method\Logger as PaymentLogger;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * A client that can communicate with the Authorize.net API
  20. */
  21. class Client implements ClientInterface
  22. {
  23. /**
  24. * @var PaymentLogger
  25. */
  26. private $paymentLogger;
  27. /**
  28. * @var LoggerInterface
  29. */
  30. private $logger;
  31. /**
  32. * @var ZendClientFactory
  33. */
  34. private $httpClientFactory;
  35. /**
  36. * @var Config
  37. */
  38. private $config;
  39. /**
  40. * @var Json
  41. */
  42. private $json;
  43. /**
  44. * @param PaymentLogger $paymentLogger
  45. * @param LoggerInterface $logger
  46. * @param ZendClientFactory $httpClientFactory
  47. * @param Config $config
  48. * @param Json $json
  49. */
  50. public function __construct(
  51. PaymentLogger $paymentLogger,
  52. LoggerInterface $logger,
  53. ZendClientFactory $httpClientFactory,
  54. Config $config,
  55. Json $json
  56. ) {
  57. $this->httpClientFactory = $httpClientFactory;
  58. $this->config = $config;
  59. $this->paymentLogger = $paymentLogger;
  60. $this->logger = $logger;
  61. $this->json = $json;
  62. }
  63. /**
  64. * Places request to gateway. Returns result as ENV array
  65. *
  66. * @param TransferInterface $transferObject
  67. * @return array
  68. * @throws \Magento\Payment\Gateway\Http\ClientException
  69. */
  70. public function placeRequest(TransferInterface $transferObject)
  71. {
  72. $request = $transferObject->getBody();
  73. $log = [
  74. 'request' => $request,
  75. ];
  76. $client = $this->httpClientFactory->create();
  77. $url = $this->config->getApiUrl();
  78. $type = $request['payload_type'];
  79. unset($request['payload_type']);
  80. $request = [$type => $request];
  81. try {
  82. $client->setUri($url);
  83. $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
  84. $client->setRawData($this->json->serialize($request), 'application/json');
  85. $client->setMethod(ZendClient::POST);
  86. $responseBody = $client->request()
  87. ->getBody();
  88. // Strip BOM because Authorize.net sends it in the response
  89. if ($responseBody && substr($responseBody, 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf)) {
  90. $responseBody = substr($responseBody, 3);
  91. }
  92. $log['response'] = $responseBody;
  93. try {
  94. $data = $this->json->unserialize($responseBody);
  95. } catch (InvalidArgumentException $e) {
  96. throw new \Exception('Invalid JSON was returned by the gateway');
  97. }
  98. return $data;
  99. } catch (\Exception $e) {
  100. $this->logger->critical($e);
  101. throw new ClientException(
  102. __('Something went wrong in the payment gateway.')
  103. );
  104. } finally {
  105. $this->paymentLogger->debug($log);
  106. }
  107. }
  108. }