Zend certified PHP/Magento developer

Plugin to change order status after payment capture

Currently Magento2 sets the order status to the default status of the state STATE_PROCESSING when a credit card payment is captured. This is done in vendor/magento/module-sales/Model/Order/Payment/State/CaptureCommand.php in the excute function.

I am trying to make after payment capture status as “processing_invoiced” so the history item in the order saying it has capture uses the processing_invoiced status instead of resetting to the default.

I have it working with an aroundExecute:

< ?php

namespace VendorModulePlugin;

use MagentoSalesApiDataOrderInterface;
use MagentoSalesApiDataOrderPaymentInterface;
use MagentoSalesModelOrder;
use MagentoSalesModelOrderPaymentStateCommandInterface as BaseCommandInterface;

class InvoicedOrderStatus
{
    /**
     * Set pending order status on order place
     * see https://github.com/magento/magento2/issues/5860
     *
     * @todo Refactor this when another option becomes available
     *
     * @param BaseCommandInterface $subject
     * @param Closure $proceed
     * @param OrderPaymentInterface $payment
     * @param $amount
     * @param OrderInterface $order
     * @return mixed
     */
    public function aroundExecute(
        BaseCommandInterface $subject,
        Closure $proceed,
        OrderPaymentInterface $payment,
        $amount,
        OrderInterface $order)
    {

      $result = $proceed($payment, $amount, $order);

        if($order->getState() == Order::STATE_PROCESSING) {
                $state = Order::STATE_PROCESSING;
                $status = 'processing_invoiced';

                $order->setState($state);
                $order->setStatus($status);
            }

        return $result;
    }
}

But can’t get it working with afterExecute. How can I rewrite this to be afterExcute as Magento cautions against around functions when it’s not needed?