skip to Main Content

I’m working on magento 2.3.3. I’ve stuck in one place while was doing method that receives callbacks from custom payment gateway via magento web api. The main idea is to redirect customer to my custom gateway (that I’ve done) and after paying order this custom gateway has to send callback to magento’s web api and update the order status.
The webapi.xml:

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/orders/:id/statuses" method="POST">
          <service class="TarlanPayTarlanPayApiSetorderstatus"
          method="status"/>  
    <resources>
        <resource ref="anonymous"/>
    </resources>
     <data>
        <parameter name="orderId" force="true">%reference_id%</parameter>
    </data>
</route>    

The main idea is to redirect customer to my custom gateway (that I’ve done) and after paying order this custom gateway has to send callback to magento’s web api and update the order status. For now I’ve set web api and wrote the appropriate method to receive callbacks from gateway.

namespace TarlanPayTarlanPayApi;
Interface Setorderstatus{
/**
* @api
* @param int $id
* @return string
*/
 public function status($id);
} 

The code above shows my interface that I’ve set in webapi.xml. The code below shows the class that implements this interface and has method that has to update order status.

namespace TarlanPayTarlanPayModel;
use TarlanPayTarlanPayApiSetorderstatus;
use MagentoSalesModelOrder;
use MagentoSalesApiOrderRepositoryInterface;
/**
* @api
*/
Class SetorderstatusModel implements Setorderstatus, OrderRepositoryInterface{
    public function getList(MagentoFrameworkApiSearchCriteriaInterface $searchCriteria){}
    public function get($id){}
    public function delete(MagentoSalesApiDataOrderInterface $entity){}
    public function save(MagentoSalesApiDataOrderInterface $entity){}
    /**
    * @return ModelSetorderstatusModel
    */
public function status($id){
    $tarlanResponse = file_get_contents('php://input');
    $tarlanData = json_decode($tarlanResponse, true);
    if(!empty($tarlanResponse)){
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $order = $objectManager->create('MagentoSalesApiDataOrderInterface')->load($id);
        switch($tarlanData['status']){
            case 0:
            $order->setState(MagentoSalesModelOrder::STATE_PENDING)->setStatus(MagentoSalesModelOrder::STATE_PENDING);
            $order->save();
            break;
            case 1:
            $order->setState(MagentoSalesModelOrder::STATE_COMPLETE)->setStatus(MagentoSalesModelOrder::STATE_COMPLETE);
            $order->save();
            break;
            case 3:
            $order->setState(MagentoSalesModelOrder::STATE_PROCESSING)->setStatus(MagentoSalesModelOrder::STATE_PROCESSING);
            $this->_orderRepository->save($order);
            break;
            case 4:
            $order->setState(MagentoSalesModelOrder::STATE_CANCEL)->setStatus(MagentoSalesModelOrder::STATE_CANCEL);
            $this->_orderRepository->save($order);
            break;
            case 5:
            $order->setState(MagentoSalesModelOrder::STATE_CLOSED)->setStatus(MagentoSalesModelOrder::STATE_CLOSED);
            $this->_orderRepository->save($order);
            break;
            case 6:
            $order->setState(MagentoSalesModelOrder::STATE_FAIL)->setStatus(MagentoSalesModelOrder::STATE_FAIL);
            $this->_orderRepository->save($order);
            break;
            default:
            echo 'something';
            break;
        }
    }
    return true;
}
} 

The problem is, when I try to send some status through the postman, it returns me “400 Bad Request” and “message”: “Please provide payment for the order.”. Any help will be apreciated.here is Postman’s request

2

Answers


  1. Chosen as BEST ANSWER

    I still have no idead why it asked me to provide payment for the order. But now it's gone. And also I've faced another issue. When I tried to update status to complete it didn't work. Now I solved this one as well. It was because magento didn't register my order as invoiced order, thus it couldn't be as completed order. This is final code:

    namespace TarlanPayTarlanPayModel;
     use TarlanPayTarlanPayApiSetorderstatus;
     use MagentoSalesModelOrder;
     use MagentoSalesApiOrderRepositoryInterface;
     use MagentoCheckoutModelSession;
     use MagentoSalesModelServiceInvoiceService;
    
    /**
    * @api
    */
    
    Class SetorderstatusModel implements Setorderstatus, OrderRepositoryInterface{
    
    public function getList(MagentoFrameworkApiSearchCriteriaInterface $searchCriteria){}
    public function get($id){}
    public function delete(MagentoSalesApiDataOrderInterface $entity){}
    public function save(MagentoSalesApiDataOrderInterface $entity){}
      /**
      * @return ModelSetorderstatusModel
      */
    public function status($id){
    $tarlanResponse = file_get_contents('php://input');
    $tarlanData = json_decode($tarlanResponse, true);
    if(!empty($tarlanResponse)){
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $invoice = $objectManager->create('MagentoSalesModelServiceInvoiceService');
        $order = $objectManager->create('MagentoSalesModelOrder')->load($id); // This is to correctly pass an argument that has been set in web api's url. For example my url looks like this - magento2/rest/V1/orders/12/statuses. Where 12 is order entity id.
        $invoice = $invoice->prepareInvoice($order); //this invoice is to update your status to complete
        $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE); //this invoice is to update your status to complete
        $invoice->register(); //this invoice is to update your status to complete
        switch($tarlanData['status']){
            case 0:
            $orderState = Order::STATE_PENDING_PAYMENT;
            $order->setState($orderState)->setStatus(Order::STATE_PENDING_PAYMENT);
            $order->save();
            break;
            case 1:
            $orderState = Order::STATE_COMPLETE;
            $order->setState($orderState)->setStatus(Order::STATE_COMPLETE);
            $order->save();
            break;
            case 2:
            $orderState = Order::STATE_PROCESSING;
            $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
            $order->save();
            break;
            case 3:
            $orderState = Order::STATE_PROCESSING;
            $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
            $order->save();
            break;
            case 4:
            $orderState = Order::STATE_CANCELED;
            $order->setState($orderState)->setStatus(Order::STATE_CANCELED);
            $order->save();
            break;
            case 5:
            $orderState = Order::STATE_CLOSED;
            $order->setState($orderState)->setStatus(Order::STATE_CLOSED);
            $this->_orderRepository->save($order);
            break;
            case 6:
            $orderState = Order::STATE_CANCELED;
            $order->setState($orderState)->setStatus(Order::STATE_CANCELED);
            $order->save();
            break;
            default:
            echo 'something';
            break;
        }
    }
      return true;
     }
     }                                                                             
    

  2. I tried to implement my code in your function
    I am considering here $id means $orderId

    $id = 1;
    
    public function status($id){
        $tarlanResponse = file_get_contents('php://input');
        $tarlanData = json_decode($tarlanResponse, true);
        if(!empty($tarlanResponse)){
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $order = $objectManager->create('MagentoSalesModelOrder')->load($id);
            switch($tarlanData['status']){
                case 0:
                $orderState = Order::STATE_PENDING;
                $order->setState($orderState)->setStatus(Order::STATE_PENDING);
                $order->save();
                break;
                case 1:
                $orderState = Order::STATE_COMPLETE;
                $order->setState($orderState)->setStatus(Order::STATE_COMPLETE);
                $order->save();
                break;
                case 3:
                $orderState = Order::STATE_PROCESSING;
                $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
                $this->_orderRepository->save($order);
                break;
                case 4:
                $orderState = Order::STATE_CANCEL;
                $order->setState($orderState)->setStatus(Order::STATE_CANCEL);
                $this->_orderRepository->save($order);
                break;
                case 5:
                $orderState = Order::STATE_CLOSED;
                $order->setState($orderState)->setStatus(Order::STATE_CLOSED);
                $this->_orderRepository->save($order);
                break;
                case 6:
                $orderState = Order::STATE_FAIL;
                $order->setState($orderState)->setStatus(Order::STATE_FAIL);
                $this->_orderRepository->save($order);
                break;
                default:
                echo 'something';
                break;
            }
        }
        return true;
    }
    } 
    

    Try this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search