4 October, 2022

How To Create Custom Function Using SOAP API

Custom SOAP API Creation

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






Project API
soapmodule/project_api
soapmodule/project


Add Order Details
soapmodule/project/payorder 

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





101
Requested Order Details does not exist.


102
Provided data is invalid.


103
Error while saving project. Details in error message.


104
Error while removing project. Details in error message.





soapmodule_project



soapmoduleProject





soapmodule

Project
110

Update Order Details







Step 3 :

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

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




1.2.0





Mage_Soapmodule_Model




Mage_Soapmodule_Helper




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






















 	





Seller Payment








		 
















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





 









 //Declaring the necessary parameters
















 







Seller Payment





















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

login($api_un,$api_pw);

$result = $proxy->soapmoduleProjectPayorder($sessionId,'160411747',50,'by cash');
echo '
';
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.

Alex Sam is a digital marketer by choice & profession. He munches on topics relating to technology, eCommerce, enterprise mobility, Cloud Solutions and Internet of Things. His other interests lies in SEO, Online Reputation Management and Google Analytics. Follow Me Socially: Habr , Dev.to & Viblo .

Leave a Reply

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