Friday, September 21, 2018

Consuming Magento 1.x SOAP v2 API with .NET Core 2.x

As we know Magento 1.x offers by default different ways to consume their API: 
  1. SOAP v1 
  2. SOAP v2 
  3. XML-RPC 
  4. REST 
 In this case we are going to make a console app in .NET Core for consuming SOAP v2. 


Source code of this example can be found in Github.


Please:
Also check the problem list at the bottom of this blog. In my case I had running Magento in my Ubuntu using Docker.
Because I will demonstrate in my local environment, ALL the requests will in HTTP only. 
Be sure you don't have SSL with self signed certificate because it won't work.


Let's go! 

Prerequisites:

1. Be sure that in Magento the setting in: 
       Configuration > Services > Magento Core API
    Option WS-I Compliance is set to "Yes".

2. Be sure to have to have an API user with access to the resource to consume. 
        System > Web Services > SOAP/XML-RPC 




I will skip the part of installing .Net Core and Visual Studio Code because it depends on your system.



Hands on!

1. Create the .Net Core console app called mageSoap.
     a. Let's move to our Workspace and run this command to create the console app
dotnet new console --output mageSoap








    b. Move to the new project a open it with  Visual Studio Code.
cd mageSoap
code .



2. Let's add some dependencies to the project.
    a. Update your mageSoap.csproj like this.
<?xml version="1.0" encoding="utf-8"?>
<project sdk="Microsoft.NET.Sdk">
  <propertygroup>
    <outputtype>Exe</outputtype>
    <targetframework>netcoreapp2.1</targetframework>
  </propertygroup>
  <itemgroup>
    <dotnetclitoolreference include="dotnet-svcutil" version="1.0.3">
  </dotnetclitoolreference></itemgroup>
</project>
    
    b. Restore the project.
dotnet restore


3. Use the Microsoft WCF dotnet-svcutil tool to generate all the classes.
    a. Check that you can consume the service in Magento. 
        Go to :
            http://localhost.com/index.php/api/v2_soap/index/?wsdl=1
        If you got the XML everything is correct.

        Next, search in the XML for node "soap:address location" and check the link.
        It must be something like this:
              http://localhost.com/index.php/api/v2_soap/index/
        Follow that link and check if you don't have any error like:
             SOAP-ERROR: Parsing WSDL: Couldn't load from ...
        If so, go to the Problems section.

    b. Run this command from the terminal
dotnet svcutil  -edb -n "*,mageSoap.ServiceReference" http://localhost.com/index.php/api/v2_soap/index/\?wsdl\=1



   c. This will do two things:
        c.1- Create a folder ServiceReference1 with the file Reference.cs


        c.2- Update your mageSoap.csproj adding more dependencies.

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-svcutil" Version="1.0.3" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
  </ItemGroup>
</Project>

     c.3- Restore the project again.
dotnet restore

4. Time to play!!!
    a. Update the Program.cs with the following code:
using System;
using mageSoap.ServiceReference;

namespace mageSoap
{
    class Program
    {
        static void Main(string[] args)
        {
            Mage_Api_Model_Server_Wsi_HandlerPortTypeClient client = new Mage_Api_Model_Server_Wsi_HandlerPortTypeClient();

            //log in Magento API to get the session_id
            loginResponse session = client.loginAsync("userAPI","keyAPI").Result;
            string sessionId = session.result;
            Console.WriteLine("Session ID: " + sessionId);

            //Let's get the status from Order "100000010"
            var response = client.salesOrderInfoAsync(sessionId,"100000010").Result;
            salesOrderEntity order = response.result;
            Console.WriteLine("Status in Order: " + order.status);
        }
    }
}

5. See results
   a. Run from your terminal or F5 in Visual Studio Code
dotnet run mageSoap



6. Congrats. It's done!




Problems:
1. Problem in Magento when auto-consuming the SOAP XML.

SOAP-ERROR: Parsing WSDL: Couldn't load from ...



Solution:
This is because Magento try to auto-consume the XML from the PHP container and the container doesn't find the Web-server container in my case Nginx.
Access to the PHP container and add in the /etc/hosts file the IP of the Web-server container.

If everything is correct we should get a response as this.



References:
https://docs.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide
https://devdocs.magento.com/guides/m1x/api/soap/sales/salesOrder/sales_order.info.html

No comments:

Post a Comment