AcceptPaymentStrategyCommand.php 1.93 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\AuthorizenetAcceptjs\Gateway\Command;

use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
use Magento\Payment\Gateway\Command\CommandException;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\CommandInterface;

/**
 * Chooses the best method of accepting the payment based on the status of the transaction
 */
class AcceptPaymentStrategyCommand implements CommandInterface
{
    private const ACCEPT_FDS = 'accept_fds';
    private const NEEDS_APPROVAL_STATUSES = [
        'FDSPendingReview',
        'FDSAuthorizedPendingReview'
    ];

    /**
     * @var CommandPoolInterface
     */
    private $commandPool;

    /**
     * @var SubjectReader
     */
    private $subjectReader;

    /**
     * @param CommandPoolInterface $commandPool
     * @param SubjectReader $subjectReader
     */
    public function __construct(
        CommandPoolInterface $commandPool,
        SubjectReader $subjectReader
    ) {
        $this->commandPool = $commandPool;
        $this->subjectReader = $subjectReader;
    }

    /**
     * @inheritdoc
     */
    public function execute(array $commandSubject): void
    {
        if ($this->shouldAcceptInGateway($commandSubject)) {
            $this->commandPool->get(self::ACCEPT_FDS)
                ->execute($commandSubject);
        }
    }

    /**
     * Determines if the transaction needs to be accepted in the gateway
     *
     * @param array $commandSubject
     * @return bool
     * @throws CommandException
     */
    private function shouldAcceptInGateway(array $commandSubject): bool
    {
        $details = $this->commandPool->get('get_transaction_details')
            ->execute($commandSubject)
            ->get();

        return in_array($details['transaction']['transactionStatus'], self::NEEDS_APPROVAL_STATUSES);
    }
}