将销售订单发送到第三方API并回复

时间:2014-02-18 19:35:53

标签: magento

基本上我正在尝试从magento向第三方API发送销售订单,并接收订单是否已被接受的通知。 我用api用户和密码为其余系统创建了用户和卷。我已经阅读了多个论坛帖子,但似乎无法找到从这里开始的地方我有一段代码,我已经给了第三个 - 这里连接到API的API是以下信息

Post example (fixed)    
<form method="post" action="http://iconnect.ibacstel.com/submitorder.php">
<input name="api_key" value="Your API Key" />
<input name="api_password" value="Your API Password" />
<input name="notify_url" value="Notify URL" /> <!-Notify url is your websites url where you would like to get notification from ibacstel api -->
<input name="receipt_header" value="Header Line1%%123 Street Address, City, Zip Code" />
<input name="receipt_footer" value="Thanks for your custom..." />
<input name="printer_id" value="Your printer ID" />
<input name="order_id" value="OrderID" />
<input name="currency" value="Currency" /><!--ex. USD/GBP -->
<input name="order_type" value="1" /><!--Delivery=1, Pick up=2, Reservation=3-->
<input name="payment_status" value="6" /><!--Paid=6, Not Paid=7-->
<input name="payment_method" value="Payment Method" />
<input name="delivery_time" value="18:30 17-09-10 " /><!--Format=HH:MM DD-MM-YY-->
<input name="auth_code" value="Payment authorization code" />

<input name="cat_1" value="Category 1" />
<input name="item_1" value="Item Name 1" />
<input name="desc_1" value="Item description 1" />
<input name="qnt_1" value="1" />
<input name="price_1" value="10.50" />

<input name="cat_2" value="" />
<input name="item_2" value="Item Name 2" />
<input name="desc_2" value="Item description 2" />
<input name="qnt_2" value="1" />
<input name="price_2" value="10.50" />

<input name="cat_3" value="Category 2" />
<input name="item_3" value="Item Name 3" />
<input name="desc_3" value="Item description 3" />
<input name="qnt_3" value="1" />
<input name="price_3" value="10.50" />

---------
You can place more items here using above format.
Please note that you don't need to send category name for the items after first item if the category name is same and if you put all same category items consecutively.
---------
<input name="deliverycost" value="3.50" />
<input name="card_fee" value="0.50" />
<input name="extra_fee" value="1.50" />
<input name="total_discount" value="4.50" />
<input name="total_amount" value="56.50" /><!--Grand Total -->
<input name="cust_name" value="Customer Name" />
<input name="cust_address" value="Customer address" />
<input name="cust_phone" value="Phone number" />
<input name="cust_instruction" value="Special instruction" />
<input name="isVarified" value="4" /><!-Verified=4, Not verified=5 -->
<input name="num_prev_order" value="Number of previous order" />
---------settings---------
<input name="apply_settings " value="1" />
<input name="auto_print" value="1" />
<input name="auto_accept" value="1" />
<input name="enter_delivery_time" value="1" />
<input name="time_input_method" value="2" />
<input name="time_list" value="0-5-10-15-20-25-30-35-40-45-50-55-60" />
<input name="extra_line_feed" value="3" />
<input type="submit" value="Submit" />
</form>


Post example (free style)   
<form method="post" action="http://iconnect.ibacstel.com/submitorderfreestyle.php">
<input name="api_key" value="Your API Key" />
<input name="api_password" value="Your API Password" />
<input name="notify_url" value="Notify URL" /> <!-Notify url is your websites url where you would like to get notification from ibacstel api -->
<input name="receipt_header" value="Header Line1%%123 Street Address, City, Zip Code" />
<input name="receipt_footer" value="Thanks for your custom..." />
<input name="printer_id" value="Your printer ID" />
<input name="order_id" value="OrderID" />
<input name="currency" value="Currency" /><!--ex. USD/GBP -->
<input name="delivery_time" value="18:30 17-09-10 " /><!--Format=HH:MM DD-MM-YY-->
<input name="print_data" value="Line 1/rLine 2%%Line 3/-" />
<input name="total_amount" value="56.50" /><!--Grand Total -->
---------settings---------
<input name="apply_settings " value="1" />
<input name="auto_print" value="1" />
<input name="print_only" value="0" />
<input name="auto_accept" value="1" />
<input name="enter_delivery_time" value="1" />
<input name="time_input_method" value="2" />
<input name="time_list" value="0-5-10-15-20-25-30-35-40-45-50-55-60" />
<input name="extra_line_feed" value="3" />
<input type="submit" value="Submit" />
</form>


Success response    
<response>
   <status>OK</status>
   <details>
      <msg>Order has been stored successfully</msg>
   </details>
</response>


Error response  
<response>
   <status>FAILED</status>
   <details>
      <error> Authentication failed </error>
      <error> Other errors...</error>
   </details>
</response>


Update notification format  
When printer sends a callback response to API the API sends a notification to the Provided Notify URL with order
In your notification url you will get notification regarding the status of the order, all the information will be submitted via post method which will allow you to grab information easily.

Post variables:
"printer_id" = Printer ID
"order_id" = Order ID
"status" = (1=accepted,2=rejected)
"msg" = message from printer
"delivery_time" = confirmed delivery time from printer
"manual_update" = (1=manual update identifier)
"booking" = (1=manage booking identifier)

Please note that you will not get the notification while you are sending order to API even if the notify URL is same as where you are sending order to API. API will send response later automatically when the order will be accepted/rejected from printer to that Notify URL you provided with order. So we recommend you to use separate URL/file to send order to API and receive notification from API to avoid confusion.

Example in PHP

Lets say you are sending order the URL of your site www.example.com/sendorder.php to API
So you need to write necessary code on sendorder.php to send order information from your site to API. I am not telling you the details here how to send order to API. Please check our example html form above to send order information to API.
Lets say you have used the value of notification URL field www.example.com/receivenotification.php in orders sending form like <input name="notify_url" value="www.example.com/receivenotification.php" /> then iConnect API will send notification to www.example.com/receivenotification.php as soon as API get response from printer when an order will be accepted/rejected after printing. Here is the example code to grab the information from API notification need to be placed on receivenotification.php file.
<?php

$printer_id = $_REQUEST['printer_id'];
$order_id = $_REQUEST['order_id'];
$order_status = $_REQUEST['status'];
$message = $_REQUEST['msg'];
$delivery_time = $_REQUEST['delivery_time'];

if($order_status==1){
   //order has been accepted from printer
   //do your necessary task for accepted order like update databse, send email to customer to inform him that his order has been accepted and will be delivered on returned delivery time (variable $delivery_time).
}
else{
   //order has been rejected from printer
   //do your necessary task for rejected order like update databse, send email to customer to inform him that his order has been rejected for the returned reason (variable $message).
}

?>

基本上我需要知道在哪里放置此代码和/或我必须修改或创建的文件才能执行此操作。提前致谢

所以我已经创建了你详细说明的所有文件,我把我的第三方代码放在你说过的地方,但是,不工作,heree是我的第三方.php文件内容也许你会在哪里我出错了

<?php
class Bh_ZeroSubtotalpaymentmethod_Model_Thirdparty {

    public function automatic(Varien_Event_Observer $observer) 
        {
            $orderIds = $observer->getEvent()->getOrderIds();
            if (empty($orderIds) || !is_array($orderIds)) {
             return;
             }
            foreach($orderIds as $eachOrderId){
                 $order = Mage::getModel('sales/order')->load($eachOrderId);
                <form method="post" action="http://iconnect.ibacstel.com/submitorderfreestyle.php">
<input name="api_key" value="******" />
<input name="api_password" value="************" />
<input name="notify_url" value="Notify URL" /> <!-Notify url is your websites url where you would like to get notification from ibacstel api -->
<input name="receipt_header" value="Header Line1%%Take, City, Zip Code" />
<input name="receipt_footer" value="Thanks for your custom..." />
<input name="printer_id" value="Your printer ID" />
<input name="order_id" value="OrderID" />
<input name="currency" value="Currency" /><!--ex. USD/GBP -->
<input name="delivery_time" value="18:30 17-09-10 " /><!--Format=HH:MM DD-MM-YY-->
<input name="print_data" value="Line 1/rLine 2%%Line 3/-" />
<input name="total_amount" value="56.50" /><!--Grand Total -->
---------settings---------
<input name="apply_settings " value="1" />
<input name="auto_print" value="1" />
<input name="print_only" value="0" />
<input name="auto_accept" value="1" />
<input name="enter_delivery_time" value="1" />
<input name="time_input_method" value="2" />
<input name="time_list" value="0-5-10-15-20-25-30-35-40-45-50-55-60" />
<input name="extra_line_feed" value="3" />
<input type="submit" value="Submit" />
</form>


Success response    
<response>
   <status>OK</status>
   <details>
      <msg>Order has been stored successfully</msg>
   </details>
</response>


Error response  
<response>
   <status>FAILED</status>
   <details>
      <error> Authentication failed </error>
      <error> Other errors...</error>
   </details>
</response>


            }
            return $this;
        }


}

这是对的吗?你也可以看看第三方代码并告诉我天气这将在magento中工作,还是我必须将其翻译成另一种语言? Thankks

1 个答案:

答案 0 :(得分:2)

Magento有一个有趣的功能事件 - 观察者和触发器,并使用观察者运行一些代码。 例如,nagento事件列表http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ 在下订单后运行一些 使用

创建扩展程序

在app / code / local / Bh / ZeroSubtotalpaymentmethod / etc下创建config.xml

           <?xml version="1.0" ?>
    <config>
        <modules>
            <Bh_ZeroSubtotalpaymentmethod>
                <version>1.0.1</version>
            </Bh_ZeroSubtotalpaymentmethod>
        </modules>
        <global>
            <models>
                <zerosubtotalpaymentmethod>
                <class>Bh_ZeroSubtotalpaymentmethod_Model</class>
                </zerosubtotalpaymentmethod>
                            </models>

        </global>
        <frontend>
            <events>

                <checkout_onepage_controller_success_action>
                    <observers>
                        <create_thridparty_order>
                            <class>zerosubtotalpaymentmethod/thirdparty</class>
                            <method>automatic</method>
                        </create_thridparty_order>
                    </observers>
                </checkout_onepage_controller_success_action>

            </events>
        </frontend>
    <adminhtml>
            <events>

                <checkout_submit_all_after>
                    <observers>
                        <create_thridparty_order>
                            <class>zerosubtotalpaymentmethod/thirdparty</class>
                            <method>automatic</method>
                        </create_thridparty_order>
                    </observers>
                </checkout_submit_all_after>

            </events>
        </adminhtml>
</config>

****然后在app / code / local / Bh / ZeroSubtotalpaymentmethod / Model / **下创建Thirdparty.php

<?php
class Bh_ZeroSubtotalpaymentmethod_Model_Thirdparty {

public function automatic(Varien_Event_Observer $observer) {
    $orderIds = $observer->getEvent()->getOrderIds();
    if (empty($orderIds) || !is_array($orderIds)) {
        return;
    }
    foreach ($orderIds as $eachOrderId) {
        $order = Mage::getModel('sales/order')->load($eachOrderId);
        //right our third party code
        client = new Zend_Http_Client();
        $client->setUri('http://iconnect.ibacstel.com/submitorderfreestyle.php');
        $client->>setParameterPost(array(
        'api_key' => 'yourapi',
        'apikey' => 'xxxx',
        'message' => $message,
        'order_id' => $order_id,
        ...//more params
        ));
        $response = $client->request('POST');
        // Yet another way of preforming a POST request
        $client->setMethod(Zend_Http_Client::POST);

        if (!$response->isSuccessful()) {
            Mage::log($response);
        }
    }
    return $this;
}

}

另外。在Bh_ZeroSubtotalpaymentmethod.xml下创建 应用程序的/ etc /模块/

<?xml version="1.0" ?>
<config>
<modules>
    <Bh_ZeroSubtotalpaymentmethod>
        <active>true</active>
        <codePool>local</codePool>
    </Bh_ZeroSubtotalpaymentmethod>
</modules>
</config>