How to Retrieve Ngenius Order Reference in Magento 2 Using REST API or Custom Module

Table of Contents

  1. Introduction
  2. Understanding Magento 2 and Ngenius Online Payment
  3. The Challenge
  4. Solution 1: Using a Custom REST API Endpoint
  5. Solution 2: Using a Custom Module to Fetch Data
  6. Conclusion
  7. FAQ

Introduction

Have you ever faced challenges when trying to retrieve the Ngenius Order Reference in Magento 2? If you're working with version 2.3.5 and using Ngenius Online Payment, you might have noticed that the order reference and payment ID are visible in the Ngenius Reports. However, retrieving this information programmatically via a REST API or a custom module can be tricky. This blog post aims to provide a comprehensive guide on how to overcome this challenge.

In this detailed guide, we will explore the methods to extract the Ngenius Order Reference using a REST API or a custom module in Magento 2. By the end of this article, you will have a clear understanding of the steps involved and be equipped with the knowledge to implement this functionality seamlessly.

Understanding Magento 2 and Ngenius Online Payment

Before diving into the solutions, let's briefly discuss Magento 2 and the Ngenius Online Payment gateway. Magento 2 is a powerful e-commerce platform that allows businesses to create robust online stores. One of its many features is the ability to integrate various payment gateways, such as Ngenius Online Payment, which enhances the payment processing capabilities of your store.

The Ngenius Online Payment gateway is a reliable solution for processing transactions securely. It integrates smoothly with Magento 2, providing the capabilities to manage payments efficiently. When an order is placed using this payment method, the order reference and payment ID are stored in the Ngenius Reports within the Magento admin panel.

The Challenge

The main challenge lies in retrieving the Ngenius Order Reference for a specific order ID using either a REST API or a custom Magento module. Typically, the Magento REST API does not provide this information directly, making it necessary to implement custom solutions to fetch the required data.

Solution 1: Using a Custom REST API Endpoint

Creating a custom REST API endpoint in Magento 2 involves several key steps. Here’s a step-by-step guide to achieve this:

Step 1: Create a Custom Module

First, you need to create a custom Magento module. Let's name it YourCompany_Ngenius.

  1. Directory Structure:

    app/code/YourCompany/Ngenius/
    
  2. Registration File: Create a registration.php file in the Ngenius directory.

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'YourCompany_Ngenius',
        __DIR__
    );
    
  3. Module File: Create a module.xml file in the etc directory.

    <?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="YourCompany_Ngenius" setup_version="1.0.0"/>
    </config>
    

Step 2: Define the REST API Route

  1. Webapi File: Create a webapi.xml file in the etc directory.
    <?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/ngenius/order/reference/:orderId" method="GET">
            <service class="YourCompany\Ngenius\Api\OrderReferenceInterface" method="getOrderReference"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>
    </routes>
    

Step 3: Create the API Interface and Implementation

  1. Interface: Create an OrderReferenceInterface.php file in the Api directory.

    <?php
    namespace YourCompany\Ngenius\Api;
    
    interface OrderReferenceInterface
    {
        /**
         * Get Ngenius Order Reference by Order ID
         *
         * @param int $orderId
         * @return string
         */
        public function getOrderReference($orderId);
    }
    
  2. Implementation: Create an OrderReference.php file in the Model directory.

    <?php
    namespace YourCompany\Ngenius\Model;
    
    use YourCompany\Ngenius\Api\OrderReferenceInterface;
    use Magento\Sales\Api\OrderRepositoryInterface;
    
    class OrderReference implements OrderReferenceInterface
    {
        protected $orderRepository;
    
        public function __construct(OrderRepositoryInterface $orderRepository)
        {
            $this->orderRepository = $orderRepository;
        }
    
        public function getOrderReference($orderId)
        {
            $order = $this->orderRepository->get($orderId);
            // Assuming 'ngenius_order_reference' is a custom attribute
            return $order->getData('ngenius_order_reference');
        }
    }
    

With these steps, you have successfully created a custom REST API endpoint that fetches the Ngenius Order Reference.

Solution 2: Using a Custom Module to Fetch Data

If you prefer to implement a custom module without the REST API, follow these steps:

Step 1: Create a Custom Module

Follow the same steps as in Solution 1 to create a basic Magento module structure.

Step 2: Create a Controller to Fetch Data

  1. Controller: Create a GetOrderReference.php file in the Controller/Index directory.
    <?php
    namespace YourCompany\Ngenius\Controller\Index;
    
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\App\Action\Action;
    use Magento\Sales\Api\OrderRepositoryInterface;
    
    class GetOrderReference extends Action
    {
        protected $orderRepository;
    
        public function __construct(Context $context, OrderRepositoryInterface $orderRepository)
        {
            parent::__construct($context);
            $this->orderRepository = $orderRepository;
        }
    
        public function execute()
        {
            $orderId = $this->getRequest()->getParam('order_id');
            try {
                $order = $this->orderRepository->get($orderId);
                $ngeniusOrderReference = $order->getData('ngenius_order_reference');
                $this->getResponse()->setBody($ngeniusOrderReference);
            } catch (\Exception $e) {
                $this->getResponse()->setBody('Error: ' . $e->getMessage());
            }
        }
    }
    

Step 3: Define the Route

  1. Route: Create a routes.xml file in the etc/frontend directory.
    <?xml version="1.0"?>
    <router id="standard">
        <route id="ngenius" frontName="ngenius">
            <module name="YourCompany_Ngenius" />
        </route>
    </router>
    

With these steps, you have created a custom module that can be called via a URL to fetch the Ngenius Order Reference.

Conclusion

Retrieving the Ngenius Order Reference in Magento 2 can be made easy by implementing custom REST APIs or modules. This detailed guide provides two robust solutions to achieve this, along with code examples to help you implement them seamlessly. Whether you prefer using a REST API or a custom module, you can now confidently retrieve order references and improve your Magento 2 store's functionality.

FAQ

How do I know if the custom module is working?

After deploying your custom module, you can test it by accessing the defined endpoints or routes in your Magento store. If everything is set up correctly, you'll be able to retrieve the Ngenius Order Reference for a given order ID.

Can I enhance the custom module to include more data?

Yes, you can easily extend the custom module to include additional data fields as required. Simply modify the API interface and implementation or the controller to fetch and return more information.

What if I encounter errors during implementation?

Ensure that all the necessary files and configurations are correctly set up. Double-check the code for any typos or missing dependencies. If the issue persists, consult Magento's official documentation or seek assistance from the Magento community.