Skip to main content
Blog Magento

SAP Magento Integration with WSDL

Vinothkumar
May 13, 2015 |
SAP-MAGENTO-INTEGRATION-WITH-WSDL

How about integrating your SAP with Magento? Let’s initiate your project with our Magneto development team.

With Omnichannel retail gaining its foothold, retailers today are looking beyond a simple eCommerce solution. The requirements vary from Inventory tracking, fulfillment, supply chain, CRM and much more. To fulfill all these needs, many Enterprise companies rely on a solid ERP platform. SAP being one of the most popular ERP systems and known for its all round capabilities, is used by a lot of merchants to handle their everyday operations.

Since the whole retail ecosystem is getting tuned to provide an Omnichannel experience to a customer, the integration between SAP and Magento becomes crucial. This blog identifies a few technical intricacies that are involved in implementing a successful SAP Magento Integration.

SAP and Magento, being two independent systems, needs a common language to communicate with each other. That common language is known as WSDL (Web Services Description Language). WSDL is typically an XML file which describes a webservice that transmits and receives the data across Magento and SAP. Let’s have a look at a small example of how this integration happens.

Three things that we should consider while Integrating Magento with SAP

  • Authentication
  • Binding XML
  • Methods and Parameters

These three details should be included with every request that is made across Magento and SAP.

1. Authentication

To begin, we need to create a module to implement this integration, use this module to configure and store the SAP authentication settings. In every request, these authentication details should be included to ensure a secure connection between the two systems.

2. Binding XML.

Like we mentioned before, the binding XML forms the medium of communication between Magento and SAP. It defines the structure of the request and response functions with parameters. Create a folder named WSDL  in your Magento root directory and store the WSDL bindings files.

3.  Methods and Parameters. 

This is the important part of integration.  The method defines the exact input parameters that we need to provide SAP and the output that’s needed. Consider the following example where we query the Customer Name from SAP using the user id from Magento.

Create a module for Magento_Sap in your local folder. Create an admin page to store authentication details in the Magento backend.

/var/www/html/magento/app/code/local/Magento/Sap/etc/System.xml

<config>
..
 <groups>
 <login>
 <label translate="label">Connection Info</label>
 <USER>
 <label translate="label">User</label>
 <frontend_type>text</frontend_type>
 </USER>
 <PASSWD>
 <label translate="label">Password</label>
 <frontend_type>obscure</frontend_type> <backend_model>adminhtml/system_config_backend_encrypted</backend_model> 
 </PASSWD>
…
…
 </login>
 </groups>
</config>

2. Create a folder as name of “wsdl” in your Magento root directory and move the binded XML files to this folder.

3. Create a config.xml file in your module etc folder.

<get_username>
 <module>getUsername</module>
 <wsdl>sap/GETUSERNAME.XML</wsdl>
 <IMPORT>
 <USERID type="alone">
 <value>
 <object>value</object>
 <path>1000</path>
 </value>
 </USERID>
 </IMPORT>
 <TABLE>
 <UNAME type="output" />
 </TABLE>
</get_username>

4. Create model file for User.php in app/code/local/Magento/Sap/Model/User.php.
This model file is used to make the request to access the  data from SAP.

Class Magento_Sap_Model_User externds Magento_Sap_Model_Api
{
    Public function getUsername () {
        $result = $this->requestSap(‘get_username’);
        Return $result;
    }
}

The get_username is used to identify config.xml tag.

5. Create API file for Api.php in app/code/local/Magento/Sap/Model/Api.php.

This API file is used to connect SAP with magento functionalities. The function name and parameter are taken from config.xml and these XML parameters are converted into wsdl array format. This would get the Username from SAP based on the user ID from Magento.

class Magento_Sap_Model_Api extends Varien_Object
{
 /* Constant params */ 
 const XML_CONFIG_PATH_CONNECTION_INFO = 'sap_connection/login'; 
 const XML_CONFIG_PATH_API_CONFIG = 'sap_api';
 const XML_TAG_FUNCTION_NAME = 'module'; 
 const XML_TAG_IMPORT = 'IMPORT';
 const XML_TAG_PARAMS = 'params';
 const XML_TAG_WSDL ="wsdl";
 /* SAP request starts here */
 Public function requestSap($requestType) {

 $request= $this->prepareRequestDetails ($requestType);
 $result = $this->wsdlCallFunction($request); 
 return $result;
 }
 /* get request type object from config.xml */
 protected function getSapConfig($apiType=''){
 $node = Mage::getConfig()->getNode(self::XML_CONFIG_PATH_API_CONFIG . '/' .$apiType, 'default');
 return $node;
 }
 /* prepare the object for wsdl format */
 protected function prepareRequestDetails($requestType){
 if ($configData = $this->getSapConfig($requestType)) {
 $request = array();
 // Get function name from config
 if ($functionName = (string)$configData->{self::XML_TAG_FUNCTION_NAME})    {
 $request[self::XML_TAG_FUNCTION_NAME] = $functionName;
 }
 // Get wsdl url from config
 if ($wsdl = (string)$configData->{self::XML_TAG_WSDL}) {
 $request[self::XML_TAG_WSDL] = $wsdl; }
 // Prepare IMPORT parameters values
 if ($import = $configData->{self::XML_TAG_IMPORT}) { 
 foreach ($import->children() as $importNode) { 
 $importValue = array();
 foreach ($importNode->children() as $importVal) { 
 $importValue[$importVal->getName()] = $this->getConfigValue($importVal);
 }
 $request[self::XML_TAG_PARAMS][$importNode->getName()] = (string)$importVal->path;
 } 
 }
 return $request;
 }
 }
 /* get Authentication from backend */
 protected function getWsdlLoginInfo()
 {
 $attributes = Mage::getSingleton('adminhtml/config')->getSection('sap_connection');
 if (isset($attributes->groups->login->fields)) {
 $attributes = $attributes->groups->login->fields->children();
 } else {
 return false;
 }
 $result = array();
 foreach ($attributes as $node) {
 $result[$node->getName()] = Mage::getStoreConfig(self::XML_CONFIG_PATH_CONNECTION_INFO . '/' . $node->getName());
 }
 return $result;
 }
 /* connect Soap cilent */
 protected function getSoapClient($wsdl=null){ 
 $AUTH = $this->getWsdlLoginInfo();
 $SOAP_AUTH['login'] = $AUTH['USER']; 
 $SOAP_AUTH['password'] = $AUTH['PASSWD']; 
 $client = new SoapClient($wsdl,$SOAP_AUTH); 
 return $client;
 }
 /* Call SAP and get the response */
 protected function wsdlCallFunction($wsdlRequest){ 
 ini_set('soap.wsdl_cache_ttl', 0);
 $wsdlFunctionName = $wsdlRequest[self::XML_TAG_FUNCTION_NAME];
 $params = $wsdlRequest[self::XML_TAG_PARAMS]; 
 try{ 
 $client = $this->getSoapClient($wsdlRequest[self::XML_TAG_WSDL]); 
 }catch (Exception $e){ 
 Mage::log($e,null,'wsdl_error.log');
 return array('error'=>true,'message'=>$e->getMessage());
 }
 $sapResponse = $client->$wsdlFunctionName($params);
 return $sapResponse;
 } 
}

This is a simple way to achieve SAP Magento integration. Using similar methodologies, we can integrate a lot of modules and the respective data between SAP and Magento. This opens up a lot of opportunities which can leveraged by retailers to provide a complete Omnichannel experience.

Vinothkumar

Vinoth is a Certified Magento Developer Plus Software Developer at DCKAP. He has a great amount of drive and works hard . For his , coding is as much smart as it is work. Loves traveling.

More posts by Vinothkumar