How To Create Custom Function Using SOAP API

SOAP stands for Simple Object Access Protocol. It is based on XML mostly via HTTP (POST). Its basic structure is basically Envelope with Header and Body inside.

We need to create api user and role. This can be created from Admin -> System Configuration -> Web Services -> SOAP-XML/RPC User/Role. After doing this you should have “username” and “api_key” with you.

Read: How to Build REST API Using PHP

A Simple Example to List all the products in your magento store using SOAP API Function is as below:

SOAP V1 – Example

$client = new SoapClient('http://magentohost/api/soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'catalog_product.list');
var_dump($result);

SOAP V2 – Example

$proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl');  // Url will also Varies for V1 and V2
$sessionId = $proxy->login('apiUser', 'apiKey');
$result = $proxy->catalogProductList($sessionId);
var_dump($result);

Custom Module Creation in SOAP API

Scenario: Create a custom function using SOAP API to update order amount in database (function should call from ERP tool).

Function Name : soapmoduleProjectPayorder
Parameters: Order Id , Amount , Comments
Output : Boolean

Also Read: How to Create a Chart using Google API?

Step 1:

Create a Module with the name Mage_Soapmodule

Step 2:

Create an api.xml file in etc/ folder of the module. api.xml is the main configuration file for your SOAP API.

app/code/community/Mage/Soapmodule/etc/api.xml

<?xml version="1.0"?>
<config>
<api>
<resources>
<soapmodule_project translate="title" module="soapmodule">

<model>soapmodule/project_api</model>
<acl>soapmodule/project</acl><!-- acl resource -->
<methods>
<payorder translate="title" module="soapmodule">

<acl>soapmodule/project/payorder</acl> 

 // payorder is the function name where the bussiness logic defined to update the order details  in database

</payorder>
</methods>
<faults module="soapmodule">
<project_not_exists>
<code>101</code>
<message>Requested Order Details does not exist.</message>
</project_not_exists>
<invalid_data>
<code>102</code>
<message>Provided data is invalid.</message>
</invalid_data>
<save_error>
<code>103</code>
<message>Error while saving project. Details in error message.</message>
</save_error>
<remove_error>
<code>104</code>
<message>Error while removing project. Details in error message.</message>
</remove_error>
</faults>
</soapmodule_project>
</resources>
<resources_alias>
<project>soapmodule_project</project>
</resources_alias>
<v2>
<resources_function_prefix>
<project>soapmoduleProject</project>
</resources_function_prefix>
</v2>
<acl><!-- acl definition -->
<resources>
<soapmodule translate="title" module="soapmodule">

<project translate="title" module="soapmodule">

<sort_order>110</sort_order>
<list translate="title" module="soapmodule">

</list>
</project>
</soapmodule>
</resources>
</acl>
</api>
</config>

Step 3 :

Create the config.xml file in etc folder by specifying the version ,frontname etc..

app/code/community/Mage/Soapmodule/etc/config.xml

<config>
<modules>
<Mage_Soapmodule>
<version>1.2.0</version>
</Mage_Soapmodule>
</modules>
<global>
<models>
<soapmodule>
<class>Mage_Soapmodule_Model</class>
</soapmodule>
</models>
<helpers>
<soapmodule>
<class>Mage_Soapmodule_Helper</class>
</soapmodule>
</helpers>
</global>
</config>

Step 4 :

Need to create the Wsdl file. When we consider the Soap API we need to declare the parameters in xml format

app/code/community/Mage/Soapmodule/etc/wsdl.xml

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
<complexType name="soapmoduleProjectPayorderEntity"><all>
<element name="orderId" type="xsd:string" minOccurs="1" />
<element name="amount" type="xsd:string" minOccurs="0" />
<element name="comment" type="xsd:string" minOccurs="0" />
</all>
</complexType>
</schema>
</types>
<!--define messages -->
<message name="soapmoduleProjectPayorderRequest">
<part name="sessionId" type="xsd:string" />
<part name="orderId" type="xsd:string" />
<part name="amount" type="xsd:string" />
<part name="comment" type="xsd:string" />
</message>
<message name="soapmoduleProjectPayorderResponse">
 	<part name="result" type="xsd:boolean" />
</message>
<!--define messages -->
<!-- define portTypes -->
<portType name="{{var wsdl.handler}}PortType">
<operation name="soapmoduleProjectPayorder">
<documentation>Seller Payment</documentation>
<input message="typens:soapmoduleProjectPayorderRequest" />
<output message="typens:soapmoduleProjectPayorderResponse" />
</operation>
</portType>
<!--[-] define portTypes -->
<!--[+] define binding -->
<binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
		 <operation name="soapmoduleProjectPayorder">
<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
<input>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<!--[-] define portTypes -->
<service name="{{var wsdl.name}}Service">
<port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
<soap:address location="{{var wsdl.url}}" />
</port>
</service>
</definitions>

Step 5 :

In this Step we need to create the Wsi.xml file in etc/wsi.xml. Here we need to declare the necessary parameters. As mentioned above necessary params to call the function are orderId, amount and comment.

app/code/community/Mage/Soapmodule/etc/wsi.xml

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:{{var wsdl.name}}">
 <xsd:complexType name="soapmoduleProjectPayorderEntity">
<xsd:sequence>
<xsd:element name="orderId" type="xsd:string" />
<xsd:element name="amount" type="xsd:string" />
<xsd:element name="comment" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="soapmoduleProjectPayorderRequestParam">
<xsd:complexType>
<xsd:sequence>
 //Declaring the necessary parameters
<xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="orderId" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="amount" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="comment" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="soapmoduleProjectPayorderResponseParam">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
 <wsdl:message name="soapmoduleProjectPayorderRequest">
<wsdl:part name="parameters" element="typens:soapmoduleProjectPayorderRequestParam" />
</wsdl:message>
<wsdl:message name="soapmoduleProjectPayorderResponse">
<wsdl:part name="parameters" element="typens:soapmoduleProjectPayorderResponseParam"/>
</wsdl:message>
<wsdl:portType name="{{var wsdl.handler}}PortType">
<wsdl:operation name="soapmoduleProjectPayorder">
<wsdl:documentation>Seller Payment</wsdl:documentation>
<wsdl:input message="typens:soapmoduleProjectPayorderRequest" />
<wsdl:output message="typens:soapmoduleProjectPayorderResponse" />
</wsdl:operation>
<wsdl:binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="soapmoduleProjectPayorder">
<soap:operation soapAction="" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="{{var wsdl.name}}Service">
<wsdl:port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
<soap:address location="{{var wsdl.url}}" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Step 6 :

Create Model for handling the SOAP V1 requests

Create the api model file. In the api.xml file we have mentioned the model to be “soapmodule/project_api” so we need to create the file

app/code/community/Mage/Soapmodule/Model/Project/Api.php

public function payorder($orderId,$amount,$comment){
try{
/**
* Storing Transaction details
*/
$transactionDetails = 
array(‘order_id’ => $orderId, ‘amount’=> $amount,’comment’=> $comment);
$saveTransactionDetails = Mage::getModel (testmodel/testmodel' )->setData($transactionDetails);
$saveTransactionDetails->save();
}
catch (Mage_Core_Exception $e) {
$this->_fault('save_error', $e->getMessage());
}
return true;
}
}

Step 7 :

create a a new file as v2.php in Api folder

app/code/community/Mage/Soapmodule/Model/Project/Api/V2.php

class Mage_Soapmodule_Model_Project_Api_V2 extends Mage_Soapmodule_Model_Project_Api{
}

Final Step :

In your Magento root folder create soap_api.php file and call the custom Soap API function

<?php
$api_url = 'http://localhost/demostore/api/v2_soap/?wsdl';
$api_un	= ‘demo’;
$api_pw	= 'demo123';

$proxy 	= new SoapClient($api_url);
$sessionId = $proxy->login($api_un,$api_pw);

$result = $proxy->soapmoduleProjectPayorder($sessionId,'160411747',50,'by cash');
echo '<pre>';
print_r($result);
exit;
?>

Creating a custom function using SOAP API will give developers much needed flexibility. This article was created by developers to help fellow developers learn how to create a custom function using SOAP API.

Hope we have made things clear. If there is anything that you wish to know more, feel free to drop in a comment or inbox us.

Realated Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">