How to create integration module in Magento 2

We live in a world that is increasingly focused on APIs which are used everywhere. No matter how you look on its usage, and you may not even realize it, APIs are widely popular.

Currently, there are APIs for finance, sending electronic messages and payments. Online trading, cryptocurrencies and many other interaction tasks of some applications with others are performed through software interfaces.

A curious fact on the APIs: in the last five years the most attention was devoted to interfaces for data sharing and analysis. This is the area which can determine the application value and determine its real world usage pattern.

In this manner, the main advantages of the most popular API classes are the tools they provide for applications to connect together, and to be able to exchange data with each other.

Since Magento is also an application, it may require connecting to third-party services and applications. For such task any online business can utilize those services by using the APIs on the web for integration with Magento.

Clearly, usage of integrations is an indispensable part of any internet business.

Despite the fact that executing a straightforward integration requires information on PHP or Magento, it's vital to learn on the fundamentals of options that will be worked on:

  • Magento web APIs
  • Web API authentication
  • OAuth authentication

Prior to developing the store’s integration module, all tools should be introduced. That regards to the Magento itself and its framework.

Check the five straightforward moves for anyone to be able to make integration.

Manual integration development in Magento 2

In order to manually create the integration you should follow these steps:

  1. In the Magento backend admin panel navigate to System > Extensions > Integrations.
  2. With the Add new integration button create a new integration form.
  3. Specify the integration name, for example myintegration.
  4. On the API tab use the resource tree to grant access to the resources integration requires.
  5. Press Save button to generate access keys for the integration. You will get public & secret keys.
  6. Navigate to the tab with the list of available integrations. Activate your new integration and get the access token. It will be used later by external application.

Check if you have all required keys and tokes for providing access to the integration. You should have such data: - Consumer Key - Consumer Secret - Access Token - Access Token Secret

Create an integration in Magento 2 programmatically

Integrations in Magento are managed by the means of the dedicated Magento_Integration extension. Using the class Magento\Integration\Model\ConfigBasedIntegrationManager of thi extension it is possible to create a new integration by generating XML files with required configuration data.

Make the folder structure for the module files. It is totally fine to put the module in any directory. The path should be under the Magento root path. Best practice is to locate it in magento_base_dir/app/code/{{vendor_name}}/{{module_name}} or in the /app/code/{{vendor_name}}/{{module_name}}.

Create all required subdirectories in the module’s folder:

cd mkdir -p app/code//{{vendor_name}}/{{;module_name}}/etc/integration mkdir -p app/code//{{vendor_name}}/{{module_name}}/Setup

Using the module.xml as a template, fill the configuration data of your integration module. In this configuration specify the following:

  • Module name. This is a mandatory string that should be unique and serves the identification purposes.
  • Module version. Specifies the component version the module utilizes.
  • Dependencies. Lists the dependencies required for a module to work.

The module.xml file has the following structure:

Move your {{module_name}} to a first position of a loading queue. This would assist with keeping away from a breakdown when an integration configuration of the module is loaded.

Establish the composer.json document for the module. Composer is required tool controlling the versions of components. The composer.json document should be set in {{module_name}} catalog.

Create a composer.json by using the example below as a minimal template:

{ "name": "Vendor1_Module1", "description": "create integration from config", "require": { "php": "~5.5.0|~5.6.0|~7.0.0", "magento/framework": "2.0.0", "magento/module-integration": "2.0.0" }, "type": "magento2-module", "version": "1.0", "autoload": { "files": [ "registration.php" ], "psr-4": { "Vendor1\Module1\": "" } } }

Now it is time to set a registration.php file, which is used to embed the module into Magento. Locate it in the module’s root directory. Use the code below as a template:

   <?php
     /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

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

Make an installation class InstallData.php which introduces the module design information into the Magento joining data table. Use the template below as a start for your module:

<?php namespace Vendor1\Module1\Setup;

use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Integration\Model\ConfigBasedIntegrationManager; use Magento\Framework\Setup\InstallDataInterface;

class InstallData implements InstallDataInterface { /** * @var ConfigBasedIntegrationManager */

 private $integrationManager;

 /**
  * @param ConfigBasedIntegrationManager $integrationManager
  */

 public function __construct(ConfigBasedIntegrationManager $integrationManager)
 &#123;
     $this->integrationManager = $integrationManager;
 &#125;

 /**
  * &#123;@inheritdoc&#125;
  */

 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 &#123;
     $this->integrationManager->processIntegrationConfig(['testIntegration']);
 &#125;

}

Change the last line {{ test }} so that the testIntegration refers to etc/integrations/config.xml file. The name of the integration must be identical.

Ensure that paths for the namespaces of the vendor and module are corrected according to your module.

Set files to facilitate integration

To alter the module it is required to create numerous XML documents to provide information for existing modules on what assets can be used by them. The altering process incorporates two stages: characterize assets included for the integration and preliminary configuration.

Characterize assets included for the integration

The API resources the integration is granted access are put in the list which is determined by the api.xml file located in etc/integration/. Define the resources with the file acl.xml file that is in the folder etc/.

Check how the integration can get an access to the assets in the Sales module:

Preliminary configuration

A config.xml design document can be enlisted alternatively, which would permit the integration module to be automatically configured. Simply obey the rule that the config.xml has to be in etc/integration to define the APIs the integration will have access:

Check the description of the parameters in this specific file:

  • integrations. Can contain definition for one or several integration definitions
  • integration name. Define an integration name.
  • email. The specified email will be bind to the integrated service.
  • endpoint_url. This is the optional parameter which sets a unique URL. The purpose of this link is to receive OAuth credentials.
  • identity_link_url. This optional parameter sets the URL for redirect to the user’s third-party account used in the Magento integration.

Install the module

Follow the provided steps to install newly created module:

  • Update the Magento data set information by running bin/magento setup:upgrade
  • Launch the generation process with command bin/magento setup:di:compile
  • The cache is created during the development. This cache must be cleared. The bin/magento cache:clean allows to do so

Make the integraion

Prior to integration actuating make two pages on the application to deal with OAuth. The identity_link_url location should deal with login demands. The endpoint_url link has to process OAuth tokens.

A login page for the external third-party application can be shown when a click on the Activate button in admin panel is made. The Magento after this action will send to this page such parameters as oauth_consumer_key and success_call_back. The oauth_consumer_key should be bind to the login ID. The success_call_back can be utilized to bring control once again to Magento.

The rundown of commonplace tasks that are performed by a callback page:

The request, which is done with POST method and which is a marking of a start point on the callback page. This exchange consolidates multiple data points. The list includes an URL of the Magento store, and several keys. OAuth client and secret keys are among them, as well as oauth_verifier info. These data points are delivered at the time integration is made. Issue a request for a token that will allow establishing the exchange. The API can be utilized to get such token from Magento with POST /oauth/token/request

Analyze the response, including the oauth_token and oauth_token_secret.

Issue the request for the access token. The request token can be used when it is required to obtain the access credential token. To get an incoming request token data try using an API. Simply apply POST /oauth/token/access. Scan the code you will get in the response message.

The current data in oauth_token simultaneously with oauth_token_secret will mismatch the previous. This can be seen when they are placed together with the points of acquired data when the last step was conducted. The latter regards the incoming response on request token.

Save OAuth parameters, including access token. Put this data in the Authorization header for each call to Magento.

Conclusion

As you can see, Magento 2 provides the ability to integrate with external systems; however the execution of such integration requires some experience in various aspects of coding. We hope this information will help you figure out how to make integration in Magento 2.

In case you find this article valuable let us know on Facebook.

Loading...