Extending Magento API v2: Adding a New Payment Method to Sales/Orders
Dealing with Magento’s API, especially version 2, can sometimes pose challenges when trying to extend its functionality. Recently, I faced an issue while attempting to add a new payment method called “pago” (Spanish for ‘payment’) to the sales/order class using v2 of the API.
My journey began by successfully implementing this extension using v1 WSDL, but when I tried the same with v2, I encountered the following error:
Procedure 'salesOrderPago' not present in ...'
The Culprit: Issues with my api.xml File
Upon further investigation, it became clear that the issue lay somewhere within my api.xml file. Here’s a snippet of the problematic code:
<config>
<api>
...
<sales_order translate="title" module="sales">
<model>sales/order_api</model>
<title>Order API</title>
<acl>sales/order</acl>
<methods>
<pago translate="title" module="sales">
<title>Acepta un pago</title>
<acl>sales/order/create</acl>
</pago>
...
Analyzing the api/v2.php File
Next, let’s examine my api/v2.php file:
class Neurona_Pagoexpress_Model_Sales_Order_Api_V2 extends Mage_Sales_Model_Order_Api
{
public function pago($ref, $medio, $moneda, $boleta)
{
try{
//Crea invoice para la orden
$invoice = Mage::getModel('sales/order_invoice_api');
$invoice->create($ref, array(), 'PagoExpress - '.$boleta);
$aut = "4894371870891274"; //Generar con algoritmo
return array('00','Procedimiento correcto',$aut); // 00 = OK, Pago realizado
}
catch (Mage_Core_Exception $e) {
return array('99');
}
}
}
The config.xml File and Rewriting the Models
My config.xml file contains the necessary rewrites for the models:
<config>
<global>
<models>
<sales>
<rewrite>
<order_api>Neurona_Pagoexpress_Model_Sales_Order_Api</order_api>
<order_api_v2>Neurona_Pagoexpress_Model_Sales_Order_Api_V2</order_api_v2>
</rewrite>
</sales>
</models>
</global>
</config>
The WSDL.xml File
Lastly, my wsdl.xml file defines the message structure for the new payment operation:
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
...
<message name="salesOrderPagoRequest">
...
</message>
<message name="salesOrderPagoResponse">
...
</message>
<portType>
<operation name="salesOrderPago">
...
</operation>
</portType>
<binding>
...
</binding>
</definitions>
The Solution: Correcting the WSDL and XML Configuration
Upon a thorough review of my configuration files, I realized that the issue stemmed from an inconsistency in how I was defining the operation name in the wsdl.xml file. To resolve this, I needed to ensure that the operation name in the portType section matched the one used in the api/resources_alias and api/v2/resources_function_prefix sections of my main api.xml file.
Here’s how I updated the relevant sections:
In api.xml:
<resources>
...
<sales_order translate="title" module="sales">
...
<methods>
...
<pago translate="title" module="sales">
...
</pago>
...
</methods>
</sales_order>
</resources>
<resources_alias>
<order>sales_order</order>
</resources_alias>
<v2>
<resources_function_prefix>
<order>salesOrder</order> <!-- Update the operation prefix here -->
</resources_function_prefix>
</v2>
In wsdl.xml:
<portType>
<operation name="salesOrderPago"> <!-- Update the operation name to match API configuration -->
...
</operation>
</portType>
Conclusion: Successful Extension of Magento v2 API
After careful examination and correction of my XML and PHP files, I was finally able to successfully extend Magento’s v2 API with a new payment method. The key takeaway is to ensure consistency across all the configuration files when defining operation names in the WSDL and API XML.
Remember to always double-check your file structures and naming conventions for any inconsistencies that could lead to errors like the one I initially encountered.
If you’re still facing challenges, feel free to refer to our extensive documentation on Magento development at LaravelCompany, or reach out to our team of experienced developers who can provide personalized guidance for your specific use case.