0

Magento 2 default paypal order after IPN completed turn to order status "Processing". This is the default order status

I need to change paypal order IPN completed after order status "new_order_recieved"

I have found vendor path model file through possible to change order status and state vendor/magento/module-sales/Model/Order/Payment/State/RegisterCaptureNotificationCommand.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Model\Order\Payment\State;

use Magento\Framework\App\ObjectManager;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\StatusResolver;

class RegisterCaptureNotificationCommand implements CommandInterface
{
    /**
     * @var StatusResolver
     */
    private $statusResolver;

    /**
     * @param StatusResolver|null $statusResolver
     */
    public function __construct(StatusResolver $statusResolver = null)
    {
        $this->statusResolver = $statusResolver
            ? : ObjectManager::getInstance()->get(StatusResolver::class);
    }

    /**
     * @param OrderPaymentInterface $payment
     * @param string|float|int $amount
     * @param OrderInterface $order
     * @return string
     */
    public function execute(OrderPaymentInterface $payment, $amount, OrderInterface $order)
    {
      
        $state = Order::STATE_PROCESSING; 
        $status =  null; 
        $message = 'Registered notification about captured amount of %1.';

        if ($payment->getIsTransactionPending()) {
            $state = Order::STATE_PAYMENT_REVIEW;
            $message = 'An amount of %1 will be captured after being approved at the payment gateway.';
        }

        if ($payment->getIsFraudDetected()) {
            $state = Order::STATE_PAYMENT_REVIEW;
            $status = Order::STATUS_FRAUD;
            $message = 'Order is suspended as its capture amount %1 is suspected to be fraudulent.';
        }

        if (!isset($status)) {
            $status = $this->statusResolver->getOrderStatusByState($order, $state);
        }

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

        return __($message, $order->getBaseCurrency()->formatTxt($amount));
    }

    /**
     * @deprecated 100.1.9 Replaced by a StatusResolver class call.
     *
     * @param Order $order
     * @param string $status
     * @param string $state
     * @return void
     */
    protected function setOrderStateAndStatus(Order $order, $status, $state)
    {
        if (!$status) {
            $status = $order->getConfig()->getStateDefaultStatus($state);
        }

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

I need to override above RegisterCaptureNotificationCommand.php only few line

    ```
    $state = Order::STATE_PROCESSING; 
    $status =  null; 
    $message = 'Registered Notification about captured amount of %1.';
    ```
    to 

    ```
    $state = Order::STATE_PROCESSING; 
    $status =  'new_order_recieved'; 
    $message = 'Notification about captured amount of %1.';
    ```

This file need to override my custom module

Myvendor/PaypalOrderstatus/etc/di.xml Myvendor/PaypalOrderstatus/Model/Order/Payment/State/Custom.php

I know etc/di.xml format possible to method below like

1, preference

 <preference for="Magento\Sales\Model\Order\Payment\State\RegisterCaptureNotificationCommand" type="Mytask\PaypalOrderstatus\Model\Order\Payment\State\Custom" />

2 plugin.

 <type name="Magento\Sales\Model\Order\Payment\State\RegisterCaptureNotificationCommand">
        <plugin name="changingorderstatus" type="Mytask\PaypalOrderstatus\Model\Order\Payment\State\Custom"  />
    </type>  

But I don't know the override Custom.php file syntax.

If you know best method vendor model file(RegisterCaptureNotificationCommand.php) to override (Custom.php) model file in custom module , please feel free to answer the question.

Thanks for advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.