2

I am writing a custom API which will use a customer token in authorization header and will able to fetch the order details.

Below is my code:

File : CustomerOrderRepositoryInterface.php

<?php
namespace Vendor\Modulename\Api;


interface CustomerOrderRepositoryInterface
{
    /**
     * Get Customer Order.
     *
     * @api
     * @param int $customerId
     * @param int $orderId
     * @return \Magento\Sales\Api\Data\OrderInterface Order interface.
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getOrder($customerId, $orderId);
}

File : CustomerOrderRepository.php

<?php
namespace Vendor\Modulename\Model\Resource;

use Vendor\Modulename\Api\CustomerOrderRepositoryInterface;
use Magento\Sales\Model\OrderRepository;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerOrderRepository implements CustomerOrderRepositoryInterface
{
    /**
     * @var \Magento\Sales\Model\OrderRepository
     */
    private $orderRepository;



    /**
     * CustomerOrderRepository constructor.
     *
     * @param \Magento\Sales\Model\OrderRepository $orderRepository
     */
    public function __construct(
        \Magento\Sales\Model\OrderRepository $orderRepository
    ) {
        $this->orderRepository = $orderRepository;
    }

    /**
     * Get Customer Order.
     *
     * @param int $customerId
     * @param int $orderId
     * @return \Magento\Sales\Api\Data\OrderInterface Order interface.
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getOrder($customerId, $orderId)
    {
        /**
        * Here I have put some custom checks before proceeding to fetch the order details and 
        * based on that I am getting the $customerOrderId from my custom table.
        */

        if($customerOrderId == $orderId) {
            /** Here I am passing the correct order ID. 
              *I am able to get the details with admin token for same orderId.*/
            $orderDetails = $this->orderRepository->get($orderId);
            return $orderDetails;
        }else {
            throw new NoSuchEntityException(__('There is no order with number "%1" is associated.',$orderId));
        }
    }
}

File : 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/vendor/orders/:orderId" method="GET">
        <service class="Vendor\Modulename\Api\CustomerOrderRepositoryInterface" method="getOrder"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
    </route>
</routes>

I am getting the error below in the response. (I tried using use Magento\Sales\Api\OrderRepositoryInterface too)

{
    "message": "No such entity with %fieldName = %fieldValue",
    "parameters": {
        "fieldName": "orderId",
        "fieldValue": "17"
    }
}

How I can fetch the order details in this scenario?

10
  • you need to declare the route in the webapi.xml first ie, /rest/v1/foo/boo/:vendorId/:orderId
    – 95623
    Jul 19, 2017 at 13:08
  • I have edited my question added my webapi.xml. Btw it gives "Request does not match any route" error if I didn't specify the routes in webapi.xml. Jul 19, 2017 at 13:16
  • try something like this, :vendorId = url param, :orderId = url param vars name need to match with interface params method declaration <route url="/V1/pos/foo/:vendorId/:orderId" method="GET"> <service class="vendor_name\module_name\Api\FooInterface" method="getById" /> <resources> <resource ref="vendor_module::getById_acl" /> </resources> </route>
    – 95623
    Jul 19, 2017 at 13:20
  • I didn't get any reference link in your previous comment :) Jul 19, 2017 at 13:23
  • heres is a good example for a new api endpoint: alankent.me/2015/07/24/…
    – 95623
    Jul 19, 2017 at 13:25

1 Answer 1

1

In file vendor/magento/module-sales/Model/ResourceModel/Order/Plugin/Authorization.php below code is throwing the error.

public function aroundLoad(
    \Magento\Sales\Model\ResourceModel\Order $subject,
    \Closure $proceed,
    \Magento\Framework\Model\AbstractModel $order,
    $value,
    $field = null
) {
    $result = $proceed($order, $value, $field);
    if (!$this->isAllowed($order)) {
        throw NoSuchEntityException::singleField('orderId', $order->getId());
    }
    return $result;
}

$this->isAllowed method checks against USER_TYPE_CUSTOMER. As I am passing the different customer token which is need of my project, it's throwing the error.

1
  • I am getting order details for customer with the same customer token, But still I got the same error like mentioned above. May 17, 2018 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.