1

How do I add methods to the soap API? I want to add a new method to retrieve some additional information from salesOrderPaymentEntity and salesOrderEntity I have looked at several tutorials, but cannot get it to work.

2

Here is an example on how you can extend the an existing api entity so it will include more fields: https://magento.stackexchange.com/a/1331/146.
The example is for products, but the idea is the same for all entities.

If your new fields should contain some logic, you can extend the models that return them and inject your logic. Mage_Sales_Model_Order_Api_V2 for orders, items and info methods. I didn't find yet what needs to be modified for the payment. But most probably it's something in the same model

2

To add new methods you need to follow these steps:

1- Create a module

For example "ApiSales"...

In etc/modules/Namespace_ApiSales.xml

<?xml version="1.0" encoding="UTF-8"?>
 <config>
    <modules>
        <Namespace_ApiSales>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_ApiSales>
    </modules>
</config>

Create a folder structure for module in app/code/local/

Namespace
   ApiSales
      *etc
      *Model
        **Order
          ***Api

Inside the folder etc create the file config.xml with this content:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_ApiSales>
            <version>0.1.0</version>
        </Namespace_ApiSales>
    </modules>
    <global>

        <models>
            <sales>
                <rewrite>
                    <order_api_v2>Namespace_ApiSales_Model_Order_Api_V2</order_api_v2>
                </rewrite>
            </sales>
        </models>
    </global>
</config>

2- Define api.xml

Create also the file api.xml with this content:

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <sales_order translate="title" module="sales">
                <model>sales/order_api</model>
                <title>Order API</title>
                <acl>sales/order</acl>
                <methods>
                    <list translate="title" module="sales">
                        <title>Retrieve list of orders by filters</title>
                        <method>items</method>
                        <acl>sales/order/info</acl>
                    </list>
                    <info translate="title" module="sales">
                        <title>Retrieve order information</title>
                        <acl>sales/order/info</acl>
                    </info>
                    <addComment translate="title" module="sales">
                        <title>Add comment to order</title>
                        <acl>sales/order/change</acl>
                    </addComment>
                    <hold translate="title" module="sales">
                        <title>Hold order</title>
                        <acl>sales/order/change</acl>
                    </hold>
                    <unhold translate="title" module="sales">
                        <title>Unhold order</title>
                        <acl>sales/order/change</acl>
                    </unhold>
                    <cancel translate="title" module="sales">
                        <title>Cancel order</title>
                        <acl>sales/order/change</acl>
                    </cancel>

                    <!-- Custom Method -->

                    <extendedInfo translate="title" module="sales">
                        <title>Get Extra info of order</title>
                        <method>extendedInfo</method>
                        <acl>sales/order/info</acl>
                    </extendedInfo>

                    <!-- Custom Method -->

                </methods>
                <faults module="sales">
                    <not_exists>
                        <code>100</code>
                        <message>Requested order not exists.</message>
                    </not_exists>
                    <filters_invalid>
                        <code>101</code>
                        <message>Invalid filters given. Details in error message.</message>
                    </filters_invalid>
                    <data_invalid>
                        <code>102</code>
                        <message>Invalid data given. Details in error message.</message>
                    </data_invalid>
                    <status_not_changed>
                        <code>103</code>
                        <message>Order status not changed. Details in error message.</message>
                    </status_not_changed>
                </faults>
            </sales_order>
            <sales_order_shipment>
                <title>Shipment API</title>
                <model>sales/order_shipment_api</model>
                <acl>sales/order/shipment</acl>
                <methods>
                    <list translate="title" module="sales">
                        <title>Retrieve list of shipments by filters</title>
                        <method>items</method>
                        <acl>sales/order/shipment/info</acl>
                    </list>
                    <info translate="title" module="sales">
                        <title>Retrieve shipment information</title>
                        <acl>sales/order/shipment/info</acl>
                    </info>
                    <sendInfo translate="title" module="sales">
                        <title>Send shipment info</title>
                        <acl>sales/order/shipment/send</acl>
                    </sendInfo>
                    <create translate="title" module="sales">
                        <title>Create new shipment for order</title>
                        <acl>sales/order/shipment/create</acl>
                    </create>
                    <addComment translate="title" module="sales">
                        <title>Add new comment to shipment</title>
                        <acl>sales/order/shipment/comment</acl>
                    </addComment>
                    <addTrack translate="title" module="sales">
                        <title>Add new tracking number</title>
                        <acl>sales/order/shipment/track</acl>
                    </addTrack>
                    <removeTrack translate="title" module="sales">
                        <title>Remove tracking number</title>
                        <acl>sales/order/shipment/track</acl>
                    </removeTrack>
                    <getCarriers>
                        <title>Retrieve list of allowed carriers for order</title>
                    </getCarriers>
                </methods>
                <faults module="sales">
                    <not_exists>
                        <code>100</code>
                        <message>Requested shipment not exists.</message>
                    </not_exists>
                    <filters_invalid>
                        <code>101</code>
                        <message>Invalid filters given. Details in error message.</message>
                    </filters_invalid>
                    <data_invalid>
                        <code>102</code>
                        <message>Invalid data given. Details in error message.</message>
                    </data_invalid>
                    <order_not_exists>
                        <code>103</code>
                        <message>Requested order not exists.</message>
                    </order_not_exists>
                    <track_not_exists>
                        <code>104</code>
                        <message>Requested tracking not exists.</message>
                    </track_not_exists>
                    <track_not_deleted>
                        <code>105</code>
                        <message>Tracking not deleted. Details in error message.</message>
                    </track_not_deleted>
                </faults>
            </sales_order_shipment>
            <sales_order_invoice>
                <title>Invoice API</title>
                <model>sales/order_invoice_api</model>
                <acl>sales/order/invoice</acl>
                <methods>
                    <list translate="title" module="sales">
                        <title>Retrieve list of invoices by filters</title>
                        <method>items</method>
                        <acl>sales/order/invoice/info</acl>
                    </list>
                    <info translate="title" module="sales">
                        <title>Retrieve invoice information</title>
                        <acl>sales/order/invoice/info</acl>
                    </info>
                    <create translate="title" module="sales">
                        <title>Create new invoice for order</title>
                        <acl>sales/order/invoice/create</acl>
                    </create>
                    <addComment translate="title" module="sales">
                        <title>Add new comment to shipment</title>
                        <acl>sales/order/invoice/comment</acl>
                    </addComment>
                    <capture translate="title" module="sales">
                        <title>Capture invoice</title>
                        <acl>sales/order/invoice/capture</acl>
                    </capture>
                    <void translate="title" module="sales">
                        <title>Void invoice</title>
                        <acl>sales/order/invoice/void</acl>
                    </void>
                    <cancel translate="title" module="sales">
                        <title>Cancel invoice</title>
                        <acl>sales/order/invoice/cancel</acl>
                    </cancel>
                </methods>
                <faults module="sales">
                    <not_exists>
                        <code>100</code>
                        <message>Requested invoice does not exist.</message>
                    </not_exists>
                    <invalid_filter>
                        <code>101</code>
                        <message>Invalid filter given. Details in error message.</message>
                    </invalid_filter>
                    <data_invalid>
                        <code>102</code>
                        <message>Invalid data given. Details in error message.</message>
                    </data_invalid>
                    <order_not_exists>
                        <code>103</code>
                        <message>Requested order not exists.</message>
                    </order_not_exists>
                    <status_not_changed>
                        <code>104</code>
                        <message>Invoice status not changed</message>
                    </status_not_changed>
                </faults>
           </sales_order_invoice>
            <sales_order_creditmemo>
                <title>Credit memo API</title>
                <model>sales/order_creditmemo_api</model>
                <acl>sales/order/creditmemo</acl>
                <methods>
                    <list translate="title" module="sales">
                        <title>Retrieve list of credit memos by filters</title>
                        <method>items</method>
                        <acl>sales/order/creditmemo/list</acl>
                    </list>
                    <info translate="title" module="sales">
                        <title>Retrieve credit memo information</title>
                        <acl>sales/order/creditmemo/info</acl>
                    </info>
                    <create translate="title" module="sales">
                        <title>Create new credit memo for order</title>
                        <acl>sales/order/creditmemo/create</acl>
                    </create>
                    <addComment translate="title" module="sales">
                        <title>Add new comment to credit memo</title>
                        <acl>sales/order/creditmemo/comment</acl>
                    </addComment>
                    <cancel translate="title" module="sales">
                        <title>Cancel credit memo</title>
                        <acl>sales/order/creditmemo/cancel</acl>
                    </cancel>
                </methods>
                <faults module="sales">
                    <not_exists>
                        <code>100</code>
                        <message>Requested credit memo does not exist</message>
                    </not_exists>
                    <invalid_filter>
                        <code>101</code>
                        <message>Invalid filter given. Details in error message</message>
                    </invalid_filter>
                    <data_invalid>
                        <code>102</code>
                        <message>Invalid data given. Details in error message</message>
                    </data_invalid>
                    <order_not_exists>
                        <code>103</code>
                        <message>Requested order does not exist</message>
                    </order_not_exists>
                    <status_not_changed>
                        <code>104</code>
                        <message>Credit memo status not changed</message>
                    </status_not_changed>
                    <cannot_refund_to_storecredit>
                        <code>105</code>
                        <message>Money can not be refunded to the store credit account as order was created by guest</message>
                    </cannot_refund_to_storecredit>
                    <cannot_create_creditmemo>
                        <code>106</code>
                        <message>Credit memo for requested order can not be created.</message>
                    </cannot_create_creditmemo>
                </faults>
            </sales_order_creditmemo>
        </resources>
        <resources_alias>
            <order>sales_order</order>
            <order_shipment>sales_order_shipment</order_shipment>
            <order_invoice>sales_order_invoice</order_invoice>
            <order_creditmemo>sales_order_creditmemo</order_creditmemo>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <order>salesOrder</order>
                <order_shipment>salesOrderShipment</order_shipment>
                <order_invoice>salesOrderInvoice</order_invoice>
                <order_creditmemo>salesOrderCreditmemo</order_creditmemo>
            </resources_function_prefix>
        </v2>
        <rest>
            <mapping>
                <sales_order>
                    <delete>
                        <method>cancel</method>
                    </delete>
                    <post>
                        <resource>cart</resource>
                        <method>order</method>
                    </post>
                </sales_order>
               <sales_order_invoice>
                   <delete>
                       <method>cancel</method>
                   </delete>
               </sales_order_invoice>
                <sales_order_creditmemo>
                    <delete>
                        <method>cancel</method>
                    </delete>
                </sales_order_creditmemo>
            </mapping>
        </rest>
        <acl>
            <resources>
                <sales translate="title" module="sales">
                    <title>Sales</title>
                    <sort_order>2</sort_order>
                    <order translate="title" module="sales">
                        <title>Order</title>
                        <change translate="title" module="sales">
                            <title>Change status, add comments</title>
                        </change>
                        <info translate="title" module="sales">
                            <title>Retrieve orders info</title>
                        </info>
                    </order>
                </sales>
            </resources>
        </acl>
    </api>
</config>

3- Define de wsdl.xml

An last create the file wsdl.xml In this file you need create the information structure about the information you need and you will show with: definitions, message and bindings. In this case I will show you one example:

<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/"/>

        <!-- Information about order -->
         <complexType name="pedidosTransmisionData">
             <all>
                <!-- Order attributes -->
                <element name="increment_id" type="xsd:string" minOccurs="0" />
                <element name="created_at" type="xsd:string" minOccurs="0" />
                <element name="tax_amount" type="xsd:string" minOccurs="0" />
                <element name="shipping_amount" type="xsd:string" minOccurs="0" />
                <element name="fee_amount" type="xsd:string" minOccurs="0" />
                <element name="discount_amount" type="xsd:string" minOccurs="0" />
                <element name="subtotal" type="xsd:string" minOccurs="0" />
                <element name="grand_total" type="xsd:string" minOccurs="0" />
                <element name="shipping_method" type="xsd:string" minOccurs="0" />
                <element name="customer_firstname" type="xsd:string" minOccurs="0" />
                <element name="customer_lastname" type="xsd:string" minOccurs="0" />
                <element name="order_id" type="xsd:string" minOccurs="0" />
                <element name="num_operacion_tpv" type="xsd:string" minOccurs="0" />
                <element name="zona_geografica" type="xsd:string" minOccurs="0" />
                <element name="store_name" type="xsd:string" minOccurs="0" />
                <element name="code_payment" type="xsd:string" minOccurs="0" />
                <element name="code_payment_bank" type="xsd:string" minOccurs="0" />



                <!-- Special attributes-->
                <element name="dedicatoria" type="xsd:string" minOccurs="0" />
                <element name="latitud" type="xsd:string" minOccurs="0" />
                <element name="longitud" type="xsd:string" minOccurs="0" />


                <!-- Items and address of order-->

                <element name="items" type="typens:pedidosTransmisionItemDataArray" minOccurs="0" />
                <element name="shipping_address" type="typens:pedidosTransmisionAddressData" minOccurs="0" />
                <element name="billing_address" type="typens:pedidosTransmisionAddressData" minOccurs="0" />
             </all>
         </complexType>

         <complexType name="pedidosTransmisionAddressData">
            <all>
               <element name="firstname" type="xsd:string" minOccurs="0" />
                <element name="lastname" type="xsd:string" minOccurs="0" />
                <element name="street" type="xsd:string" minOccurs="0" />
                <element name="city" type="xsd:string" minOccurs="0" />
                <element name="postcode" type="xsd:string" minOccurs="0" />
                <element name="country_id" type="xsd:string" minOccurs="0" /> 
                <element name="telephone" type="xsd:string" minOccurs="0" />
                <element name="region_id" type="xsd:string" minOccurs="0" />
                <element name="vat_id" type="xsd:string" minOccurs="0" />
            </all>
        </complexType>
        <complexType name="pedidosTransmisionItemData">
            <all>
                <element name="item_id" type="xsd:string" minOccurs="0" />
                <element name="product_id" type="xsd:string" minOccurs="0" />
                <element name="es_tarjeta" type="xsd:string" minOccurs="0" />
                <element name="qty_ordered" type="xsd:string" minOccurs="0" />
                <element name="tipo_impuestos" type="xsd:string" minOccurs="0" />
                <element name="subTotal" type="xsd:string" minOccurs="0" />
                <element name="Impuestos" type="xsd:string" minOccurs="0" />
                <element name="descuentoTotal" type="xsd:string" minOccurs="0" />
                <element name="PrecioIvaIncl" type="xsd:string" minOccurs="0" />
            </all>
        </complexType>

         <complexType name="pedidosTransmisionItemDataArray">
            <complexContent>
                <restriction base="soapenc:Array">
                    <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:pedidosTransmisionItemData[]" />
                </restriction>
            </complexContent>
        </complexType>
        <!-- FIN PEDIDOS DE TRANSMISIÓN (Datos para enviar a AX) --> 

        <complexType name="interfloraItemData">
            <all>
                <element name="item_id" type="xsd:string" minOccurs="0" />
                <element name="product_id" type="xsd:string" minOccurs="0" />
                <element name="subTotal" type="xsd:string" minOccurs="0" />
                <element name="Impuestos" type="xsd:string" minOccurs="0" />
                <element name="descuentoTotal" type="xsd:string" minOccurs="0" />
                <element name="PrecioIvaIncl" type="xsd:string" minOccurs="0" />
            </all>
        </complexType>

        <complexType name="interfloraItemDataArray">
            <complexContent>
                <restriction base="soapenc:Array">
                    <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:interfloraItemData[]" />
                </restriction>
            </complexContent>
        </complexType>

    </schema>

</types>

<!-- Messages -->    

 <message name="salesOrderExtendedInfoRequest">
    <part name="sessionId" type="xsd:string"/> <!-- The information we Need -->
    <part name="order_id" type="xsd:string"/> <!-- The information we Need -->
</message>
<message name="salesOrderExtendedInfoResponse">
    <part name="result_info" type="typens:pedidosTransmisionData"/> <!-- The result of WS -->
</message>
<!-- / Messages -->   

<portType name="{{var wsdl.handler}}PortType">

    <operation name="salesOrderExtendedInfo">
        <documentation>Extra info Order</documentation>
        <input message="typens:salesOrderExtendedInfoRequest"/>
        <output message="typens:salesOrderExtendedInfoResponse"/>
   </operation>
</portType>


<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="salesOrderExtendedInfo">
        <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>

<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>

4- Define Bussines Logic of your WS

And finally inside the folder Model/Order/Api/We need create the file V2.php with this content based on example:

<?php
class Namespace_ApiSales_Model_Order_Api_V2 extends Mage_Sales_Model_Order_Api_V2
{

    /**
     * Extended info about order
     */
    public function extendedInfo($orderId)
    {
        $order = Mage::getModel('sales/order');
        $order->load($orderId);

        // Information about order, get attributes of order inside the $result
        $result = $this->_getAttributes($order, 'order');

        // Addresses
        $result['shipping_address'] = $this->_getAttributes($order->getShippingAddress(), 'order_address');
        $result['billing_address']  = $this->_getAttributes($order->getBillingAddress(), 'order_address');

        //Specials attributes
        $customOrder = Mage::getResourceModel('custom/custom_order_collection')
                ->addFieldToFilter('order_id',array('eq'=>$orderId))->getFirstItem();

        $result['dedicatoria'] = $customOrder->getDedicatoria();
        $result['latitud'] = $customOrder->getLatitud();
        $result['longitud'] = $customOrder->getLongitud();

        $code_payment = $order->getPayment()->getMethodInstance()->getCode();
        $params = Mage::helper('serviredpro')->getOrderServiredParams($order);
        if( $code_payment == "serviredpro") {
            $result['code_payment'] = $code_payment;
            $result['code_payment_bank'] = Mage::helper('serviredpro')->getBank();
            $result['num_operacion_tpv'] = $params["Ds_Order"];
            $result['num_comercio'] =  $params["Ds_MerchantCode"];;
        } elseif ($code_payment == "paypal_express") {
            $result['code_payment'] = $code_payment;
            $result['code_payment_bank'] = "paypal";
            $result['num_operacion_tpv'] = $order->getPayment()->getLastTransId();  
        }
        // ITEMS
        $result['items'] = array();
        $store_id = $order->getStoreId();

        foreach ($order->getAllItems() as $item) 
        {   
            $dataItem = $this->_getAttributes($item, 'order_item');
            $prod = Mage::getModel('catalog/product')->setStoreId($store_id)->load($dataItem['product_id']);
            // Campos del item
            $newItem = array();
            $newItem['es_tarjeta'] = $prod->getData('cod_tarjeta');
            $newItem['item_id'] = $dataItem['item_id'];
            $newItem['product_id'] = $dataItem['product_id'];
            $newItem['qty_ordered'] = $dataItem['qty_ordered'];
            $newItem['subTotal'] = $dataItem['base_original_price'];
            $newItem['tipo_impuestos'] =  $dataItem['tax_percent'];
            $newItem['Impuestos'] =  $dataItem['base_tax_amount'];
            $newItem['descuentoTotal'] = $dataItem['discount_amount'];
            $newItem['PrecioIvaIncl'] = $dataItem['base_price_incl_tax'];

            $result['items'][] = $newItem;
        }     
        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.