0

We're using Novuna PaybyFinance (formerly Hitachi PaybyFinance) version 1.7.1 in Magento 2.4.3-p3 and need to be able to access Novuna's column 'finance_application_no' which is present in the sales_order column via the Order API.

Can anyone guide me on how to do this?

Thanks

2 Answers 2

1

I am not clear your question but as per my understanding I have added below code and please check.

  1. First, you need to create a custom module with the extension's sales_order column.
  2. In the module's di.xml file, you need to add the following code to define the sales_order column as an extension attribute:

Here is the code:-

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="finance_application_no" type="string"/>
    </extension_attributes>
</config>
  1. Next, you need to create a plugin that will add the sales_order column to the Order API. In the module's etc/webapi.xml file, you need to add the following code:

Here is the code:-

<route url="/V1/orders/:id" method="GET">
    <service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
    <data>
        <parameter name="id" force="true">%id</parameter>
    </data>
</route>
  1. Finally, you can access the sales_order column via the Order API by calling the following endpoint:

Here is the URL:-

GET /V1/orders/:id

Where :id is the order ID you want to retrieve.

Let me know if any query.

THANKS.

2
  • Hi, many thanks for your reply. I have followed the steps but get "Invalid Document Element 'extension_attributes': This element is not expected. Line: 3" re. di.xml
    – eleventy
    Jan 19 at 13:06
  • Please share your full code. Jan 19 at 13:16
0

Many thanks to Mohit Patel for his advice and setting me off in the right direction. I ended up getting it to work like this:

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::MODULE,
     'Vendor_Module',
    __DIR__
);

etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="finance_application_no" type="string" />
        <attribute code="finance_deposit" type="int" />
        <attribute code="finance_amount" type="int" />
    </extension_attributes>
</config>

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"></module>
</config>

webapi_rest/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="orderInformationUpdate" type="Vendor\Module\Plugin\Api\OrderRepository" />
    </type>
</config>

Plugin/Api/OrderRepository.php

<?php

namespace Vendor\Module\Plugin\api;

use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;


class OrderRepository
{

    const FINANCE_DEPOSIT = 'finance_deposit';
    const FINANCE_AMOUNT = 'finance_amount';
    const FINANCE_APPLICATION_NO = 'finance_application_no';
    

    /**
     * Order Extension Attributes Factory
     *
     * @var OrderExtensionFactory
     */
    protected $extensionFactory;

    /**
     * OrderRepositoryPlugin constructor
     *
     * @param OrderExtensionFactory $extensionFactory
     */
    public function __construct(OrderExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     * Add "finance_deposit", "finance_amount" and "finance_application_no" extension attributes to order data object to make them accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderInterface $order
     *
     * @return OrderInterface
     */
    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
    {
        $financeDeposit = $order->getData(self::FINANCE_DEPOSIT);
        $financeAmount = $order->getData(self::FINANCE_AMOUNT);
        $financeApplicationNo = $order->getData(self::FINANCE_APPLICATION_NO);
        $extensionAttributes = $order->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setFinanceDeposit($financeDeposit);
        $extensionAttributes->setFinanceAmount($financeAmount);
        $extensionAttributes->setFinanceApplicationNo($financeApplicationNo);
        $order->setExtensionAttributes($extensionAttributes);

        return $order;
    }

    /**
     * Add "finance_deposit", "finance_amount" and "finance_application_no" extension attributes to order data object to make them accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderSearchResultInterface $searchResult
     *
     * @return OrderSearchResultInterface
     */
    public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
    {
        $orders = $searchResult->getItems();

        foreach ($orders as &$order) {
            $financeDeposit = $order->getData(self::FINANCE_DEPOSIT);
            $financeAmount = $order->getData(self::FINANCE_AMOUNT);
            $financeApplicationNo = $order->getData(self::FINANCE_APPLICATION_NO);
            $extensionAttributes = $order->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setFinanceDeposit($financeDeposit);
            $extensionAttributes->setFinanceAmount($financeAmount);
            $extensionAttributes->setFinanceApplicationNo($financeApplicationNo);
            $order->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

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.