RefundTransactionStrategyCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 returning the payment based on the status of the transaction
  14. */
  15. class RefundTransactionStrategyCommand implements CommandInterface
  16. {
  17. private const REFUND = 'refund_settled';
  18. private const VOID = 'void';
  19. /**
  20. * @var CommandPoolInterface
  21. */
  22. private $commandPool;
  23. /**
  24. * @var SubjectReader
  25. */
  26. private $subjectReader;
  27. /**
  28. * @param CommandPoolInterface $commandPool
  29. * @param SubjectReader $subjectReader
  30. */
  31. public function __construct(
  32. CommandPoolInterface $commandPool,
  33. SubjectReader $subjectReader
  34. ) {
  35. $this->commandPool = $commandPool;
  36. $this->subjectReader = $subjectReader;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function execute(array $commandSubject): void
  42. {
  43. $command = $this->getCommand($commandSubject);
  44. $this->commandPool->get($command)
  45. ->execute($commandSubject);
  46. }
  47. /**
  48. * Determines the command that should be used based on the status of the transaction
  49. *
  50. * @param array $commandSubject
  51. * @return string
  52. * @throws CommandException
  53. */
  54. private function getCommand(array $commandSubject): string
  55. {
  56. $details = $this->commandPool->get('get_transaction_details')
  57. ->execute($commandSubject)
  58. ->get();
  59. if ($details['transaction']['transactionStatus'] === 'capturedPendingSettlement') {
  60. return self::VOID;
  61. } elseif ($details['transaction']['transactionStatus'] !== 'settledSuccessfully') {
  62. throw new CommandException(__('This transaction cannot be refunded with its current status.'));
  63. }
  64. return self::REFUND;
  65. }
  66. }