Skip to content

Get Automate Payment Update from PayPal

Actually, I want to get the Payment Update from Paypal automatically twice a day for my order in the status column. That Order has been in the Payment Review, declined/Failed, etc.

The Things I do I tried till now :

Cron :

<?php
namespace VendormoduleCron;


use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkControllerResultInterface;

class PaymentReview
{

    public ResultFactory $resultRedirectFactory;

    public function __construct(

        ResultFactory $resultRedirectFactory
    )
    {
        $this->resultRedirectFactory = $resultRedirectFactory;
    }
    /**
     * Return to the path
     *
     * @return ResultInterface
     */
    public function execute() {
        $isEnabled = 0;
    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('sales/order/view',['eq' => 'key']);
    return $resultRedirect;
}
}

Override this controller (MagentoSalesControllerAdminhtmlOrderReviewPayment)

<?php

declare(strict_types=1);

namespace VendormoduleControllerAdminhtmlOrder;

use MagentoFrameworkAppActionHttpGetActionInterface as HttpGetActionInterface;
use MagentoFrameworkControllerResultRedirect;
use MagentoFrameworkExceptionLocalizedException;
use MagentoSalesControllerAdminhtmlOrder;
use MagentoSalesModelResourceModelOrderCollectionFactory;

/**
 * Class MagentoSalesControllerAdminhtmlOrderReviewPayment
 */
class ReviewPayment extends Order implements HttpGetActionInterface
{

    protected $orderCollectionFactory;

    /**
     * @param CollectionFactory $orderCollectionFactory
     */
    public function __construct(
        CollectionFactory $orderCollectionFactory
    ) {
        $this->orderCollectionFactory = $orderCollectionFactory;

    }
    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Magento_Sales::review_payment';

    /**
     * Manage payment state
     *
     * Either denies or approves a payment that is in "review" state
     *
     * @return Redirect
     */
    public function execute()
    {
        $isEnabled = 0;
        $resultRedirect = $this->resultRedirectFactory->create();
        try {
            $order = $this->_initOrder();
            $ordersCollection = $this->orderCollectionFactory->create();

            foreach ($ordersCollection as $order) {

                $to = date("Y-m-d h:i:s"); // current date
                $from = strtotime('-14 day', strtotime($to));
                $from = date('Y-m-d h:i:s', $from)
                    ->addFieldToSelect('*')
                    ->addFieldToFilter('status', ['eq' => 'payment_review']
                    );
                if ($order) {
                    $action = $this->getRequest()->getParam('action', '');
                    switch ($action) {
                        case 'accept':
                            $order->getPayment()->accept();
                            $message = __('The payment has been accepted.');
                            break;
                        case 'deny':
                            $order->getPayment()->deny();
                            $message = __('The payment has been denied.');
                            break;
                        case 'update':
                            $order->getPayment()->update();
                            if ($order->getPayment()->getIsTransactionApproved()) {
                                $message = __('Transaction has been approved.');
                            } elseif ($order->getPayment()->getIsTransactionDenied()) {
                                $message = __('Transaction has been voided/declined.');
                            } else {
                                $message = __('There is no update for the transaction.');
                            }
                            break;
                        default:
                            throw new MagentoFrameworkExceptionNotFoundException(
                                __('Action "%1" is not supported.', $action)
                            );
                    }
                    $this->orderRepository->save($order);
                    $this->messageManager->addSuccessMessage($message);
                }
                // phpcs:ignore Magento2.Exceptions.ThrowCatch
            }
        }catch (LocalizedException $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
            $this->messageManager->addErrorMessage(__('We can't update the payment right now.'));
            $this->logger->critical($e);
        }

        if ($order) {
            $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getEntityId()]);
        } else {
            $resultRedirect->setPath('sales/*/');
        }

        return $resultRedirect;
    }
}