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
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:I tried to implement my code in your function
I am considering here
$id
means$orderId
Try this.