GatewayQueryCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Command;
  8. use Exception;
  9. use Magento\Payment\Gateway\Command\CommandException;
  10. use Magento\Payment\Gateway\Command\ResultInterface;
  11. use Magento\Payment\Gateway\CommandInterface;
  12. use Magento\Payment\Gateway\Http\ClientInterface;
  13. use Magento\Payment\Gateway\Http\TransferFactoryInterface;
  14. use Magento\Payment\Gateway\Request\BuilderInterface;
  15. use Magento\Payment\Gateway\Validator\ValidatorInterface;
  16. use Psr\Log\LoggerInterface;
  17. use Magento\Payment\Gateway\Command\Result\ArrayResult;
  18. /**
  19. * Makes a request to the gateway and returns results
  20. */
  21. class GatewayQueryCommand implements CommandInterface
  22. {
  23. /**
  24. * @var BuilderInterface
  25. */
  26. private $requestBuilder;
  27. /**
  28. * @var TransferFactoryInterface
  29. */
  30. private $transferFactory;
  31. /**
  32. * @var ClientInterface
  33. */
  34. private $client;
  35. /**
  36. * @var ValidatorInterface
  37. */
  38. private $validator;
  39. /**
  40. * @var LoggerInterface
  41. */
  42. private $logger;
  43. /**
  44. * @param BuilderInterface $requestBuilder
  45. * @param TransferFactoryInterface $transferFactory
  46. * @param ClientInterface $client
  47. * @param LoggerInterface $logger
  48. * @param ValidatorInterface $validator
  49. */
  50. public function __construct(
  51. BuilderInterface $requestBuilder,
  52. TransferFactoryInterface $transferFactory,
  53. ClientInterface $client,
  54. LoggerInterface $logger,
  55. ValidatorInterface $validator
  56. ) {
  57. $this->requestBuilder = $requestBuilder;
  58. $this->transferFactory = $transferFactory;
  59. $this->client = $client;
  60. $this->validator = $validator;
  61. $this->logger = $logger;
  62. }
  63. /**
  64. * @inheritdoc
  65. *
  66. * @throws Exception
  67. */
  68. public function execute(array $commandSubject): ResultInterface
  69. {
  70. $transferO = $this->transferFactory->create(
  71. $this->requestBuilder->build($commandSubject)
  72. );
  73. try {
  74. $response = $this->client->placeRequest($transferO);
  75. } catch (Exception $e) {
  76. $this->logger->critical($e);
  77. throw new CommandException(__('There was an error while trying to process the request.'));
  78. }
  79. $result = $this->validator->validate(
  80. array_merge($commandSubject, ['response' => $response])
  81. );
  82. if (!$result->isValid()) {
  83. throw new CommandException(__('There was an error while trying to process the request.'));
  84. }
  85. return new ArrayResult($response);
  86. }
  87. }