0

While creating a module i want to create the integration for accessing the API's. I have found two ways to do that in

The first way is pretty straight forward to use and I know it will not be rejected by Magento Marketplace.

$this->integrationManager->processIntegrationConfig([Constants::INTEGRATION_NAME]);

The above code is added in the InstallData.php and the integration is created but the problem is that the Magento Admin have to manually do the activation by going to the Integration page and click activate. After click on the activate the endpoint is then able to generate the indefinite access_token.

I wanted to skip this step so that there is no such additional step. So, i found the other way.

The second way is to create the integration and activate there and then in the InstallData.php. Here is the code snippet:

$integrationExists = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create()->load($name, 'name')->getData();
    if (empty($integrationExists)) {
        $integrationData = array(
            'name' => $name,
            'email' => $email,
            'status' => '1',
            'endpoint' => $endpoint,
            'setup_type' => '0'
        );
        try {
            // Code to create Integration
            $integrationFactory = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create();
            $integration = $integrationFactory->setData($integrationData);
            $integration->save();
            $integrationId = $integration->getId();
            $consumerName = 'Integration' . $integrationId;
           
            // Code to create consumer
            $oauthService = $objectManager->get('Magento\Integration\Model\OauthService');
            $consumer = $oauthService->createConsumer(['name' => $consumerName]);
            $consumerId = $consumer->getId();
            $integration->setConsumerId($consumer->getId());
            $integration->save();
            // Code to grant permission
            $authrizeService = $objectManager->get('Magento\Integration\Model\AuthorizationService');
            $authrizeService->grantAllPermissions($integrationId);

            // Code to Activate and Authorize
            $token = $objectManager->get('Magento\Integration\Model\Oauth\Token');
            $uri = $token->createVerifierToken($consumerId);
            $token->setType('access');
            $token->save();
            return $uri;
        } catch (Exception $e) {
            $this->_logger->debug('Error creating token :: ' .  $e->getMessage());
            return null;
        }
    }

Using the second way i am able to get the access_token and then i can share this token to external application which can then use this access_token to fetch data. I tried that and it's working. Please note that i can use dependency injection in the constructor to not make use of object manager. But the point remains the same.

The problem is now that I want to know if using this code the Marketplace would reject my extension for publishing? If this is allowed are there any other things that I need to do?

2 Answers 2

1

This looks relatively ok.
Apart from the use of object manager. :).
The problem I can see with it is that if you reach the catch section, so the integration is not created you will have to create it manually.
Since this is the installData script, it will never run again after the first time.
Code can be improved also.
Of course use DI and the part for creating the integration can be done via Magento\Integration\Model\IntegrationService::create() method.

2
  • Thanks Marius that is true that if it's not created during intall it will never work. I would take care of the Exception part by probably adding the first method. But my worst issue is this be acceptable for passing the marketplace norms?
    – Sudhir
    Jun 18, 2020 at 16:42
  • OH. I cannot answer that. I don't know the marketplace norms :)
    – Marius
    Jun 19, 2020 at 7:36
0

I had got the response from the market place support and according to them you can use the above method to auto integrate unless you provide in detail to the customer that you are going to do that.

You would put all this information into the Marketing section of your submission in the long description. However, it cannot be hidden, you have to tell people that this functionality exists in the listing so they are aware of it.

The info will go onto the long description.

Your Answer

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

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