AcceptPaymentStrategyCommand.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
  9. use Magento\Payment\Gateway\Command\CommandException;
  10. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  11. use Magento\Payment\Gateway\CommandInterface;
  12. /**
  13. * Chooses the best method of accepting the payment based on the status of the transaction
  14. */
  15. class AcceptPaymentStrategyCommand implements CommandInterface
  16. {
  17. private const ACCEPT_FDS = 'accept_fds';
  18. private const NEEDS_APPROVAL_STATUSES = [
  19. 'FDSPendingReview',
  20. 'FDSAuthorizedPendingReview'
  21. ];
  22. /**
  23. * @var CommandPoolInterface
  24. */
  25. private $commandPool;
  26. /**
  27. * @var SubjectReader
  28. */
  29. private $subjectReader;
  30. /**
  31. * @param CommandPoolInterface $commandPool
  32. * @param SubjectReader $subjectReader
  33. */
  34. public function __construct(
  35. CommandPoolInterface $commandPool,
  36. SubjectReader $subjectReader
  37. ) {
  38. $this->commandPool = $commandPool;
  39. $this->subjectReader = $subjectReader;
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function execute(array $commandSubject): void
  45. {
  46. if ($this->shouldAcceptInGateway($commandSubject)) {
  47. $this->commandPool->get(self::ACCEPT_FDS)
  48. ->execute($commandSubject);
  49. }
  50. }
  51. /**
  52. * Determines if the transaction needs to be accepted in the gateway
  53. *
  54. * @param array $commandSubject
  55. * @return bool
  56. * @throws CommandException
  57. */
  58. private function shouldAcceptInGateway(array $commandSubject): bool
  59. {
  60. $details = $this->commandPool->get('get_transaction_details')
  61. ->execute($commandSubject)
  62. ->get();
  63. return in_array($details['transaction']['transactionStatus'], self::NEEDS_APPROVAL_STATUSES);
  64. }
  65. }