Cancel.php 2.3 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 75
<?php
/**
 * This file is part of the Klarna Order Management module
 *
 * (c) Klarna Bank AB (publ)
 *
 * For the full copyright and license information, please view the NOTICE
 * and LICENSE files that were distributed with this source code.
 */

namespace Klarna\Ordermanagement\Gateway\Command;

use Klarna\Core\Api\OrderInterface as KlarnaOrderInterface;
use Klarna\Core\Exception as KlarnaException;
use Magento\Payment\Gateway\Command;
use Magento\Sales\Api\Data\OrderInterface as MageOrderInterface;

/**
 * Class Cancel
 *
 * @package Klarna\Ordermanagement\Gateway\Command
 */
class Cancel extends AbstractCommand
{
    /**
     * Cancel command
     *
     * @param array $commandSubject
     *
     * @return null|Command\ResultInterface
     * @throws \Klarna\Core\Exception
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(array $commandSubject)
    {
        /** @var \Magento\Payment\Model\InfoInterface $payment */
        $payment = $commandSubject['payment']->getPayment();
        /** @var \Magento\Sales\Model\Order $order */
        $order = $payment->getOrder();
        $klarnaOrder = $this->getKlarnaOrder($order);

        if (!$klarnaOrder->getId() || !$klarnaOrder->getReservationId()) {
            throw new KlarnaException(__('Unable to cancel payment for this order.'));
        }

        $response = $this->processPayment($order, $klarnaOrder, $payment);

        if (!$response->getIsSuccessful()) {
            $errorMessage = __('Order cancellation failed, please try again.');
            $errorMessage = $this->getFullErrorMessage($response, $errorMessage, 'cancel');
            throw new KlarnaException($errorMessage);
        }

        if ($response->getTransactionId()) {
            $payment->setTransactionId($response->getTransactionId());
        }
        return null;
    }

    /**
     * Process cancel/refund for order
     *
     * @param MageOrderInterface   $order
     * @param KlarnaOrderInterface $klarnaOrder
     * @return \Magento\Framework\DataObject
     */
    private function processPayment($order, $klarnaOrder)
    {
        if ($order->hasInvoices()) {
            return $this->getOmApi($order)->release($klarnaOrder->getReservationId());
        }

        return $this->getOmApi($order)->cancel($klarnaOrder->getReservationId());
    }
}