0

Does anyone try integrating SimplyPost with Magento, Is there any plugins or giudes I can follow? So far I could find only the SimplyPost API. I'm new to magento, So have no idea where to start. I will appreciate any kick-start. Peace!

  • simplypost.asia/platform I just opened this website, i think they are providing an extension for Magento, Shopify, and WooCommerce as per the website. – inrsaurabh Nov 24 '17 at 4:09
  • I notice that, but they just mention the platform "Magento". there is nothing more details than that. Even in Google I couldn't find any extension or guides regarding this. Strange. – nanbatman Nov 24 '17 at 4:38
  • You purchased their service ,if yes then they will surely tell you ,Just contact them simplypost.asia/contact-us . I am 100% sure that they have extensions . – inrsaurabh Nov 24 '17 at 4:50
  • Otherwise you have to create your own custom module ,using their API – inrsaurabh Nov 24 '17 at 4:51
  • I haven't purchase yet, I need to confirm before subscribe to their service. Yea I send a note too. I need to research more on creating custom modules as-well, but do thing their API is good enough to do the job? – nanbatman Nov 24 '17 at 5:11
1

I manage write an custom module to connect SimplyPost with Magento via RESTful API. I Just wanna share the code. Check Github to find the complete source.

1- app/code/local/Mymodule/Simplypost/etc/config.xml

    <?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Mymodule_Simplypost>
            <version>1.0.0</version>
        </Mymodule_Simplypost>
    </modules>
    <global>
        <models>
            <simplypost>
                <class>Mymodule_Simplypost_Model</class>
            </simplypost>
        </models>
        <helpers>
            <simplypost>
                <class>Mymodule_Simplypost_Helper</class>
            </simplypost>
        </helpers>
        <blocks>
            <simplypost>
                <class>Mymodule_Simplypost_Block</class>
            </simplypost>
        </blocks>
    </global>
    <adminhtml>
        <!--call simplypost process before save shipment-->
        <events>
            <sales_order_shipment_save_before>
                <observers>
                    <create_simplypost>
                        <type>singleton</type>
                        <class>simplypost/observer</class>
                        <method>createSimplypost</method>
                    </create_simplypost>
                </observers>
            </sales_order_shipment_save_before>
        </events>
        <!--customize the grid on order view page to show the SimplyPost tracking status-->
        <events> 
            <core_block_abstract_to_html_after>
                <observers>
                    <simplypost_custom_order_view_info>
                        <class>simplypost/observer</class>
                        <method>getSalesOrderViewInfo</method>
                    </simplypost_custom_order_view_info>
                </observers>
            </core_block_abstract_to_html_after>
        </events>
        <!--create a custom grid on order view page : app/design/adminhtml/default/default/layout/mymodule_simplypost.xml -->
        <layout>
            <updates>
                <simplypost>
                    <file>mymodule_simplypost.xml</file>
                </simplypost>
            </updates>
        </layout>
    </adminhtml>
</config>

2- app/code/local/Mymodule/Simplypost/Model/Observer.php

<?php
/*
*   Observer Event : sales_order_shipment_save_before
*   This is the function which post data SimplyPost
*/
class Mymodule_Simplypost_Model_Observer {

    public function createSimplypost(Varien_Event_Observer $observer) {

        $shipment = $observer->getEvent()->getShipment();
        $order = $shipment->getOrder();

        //This can be serve from admin cms, check the Github link for more details
        $spEnable = true;
        $spAuthEndpoint = https://app.simplypost.asia/api/gateway/v1/auth/login/;
        $spLoginToken = 'Basic weloYUBasd313WEdfsf23423429tOnhwbzEyMw==';
        $spEndpoint = 'https://app.simplypost.asia/api/gateway/v1/deliveries/';
        $spMerchantCode = 'W2134';
        $spServiceCode = 'DOMN';

        if($spEnable){

            //check all configurations exists
            if($spAuthEndpoint == null || $spLoginToken == null || $spEndpoint == null || $spMerchantCode == null || $spServiceCode == null){

                Mage::getSingleton('core/session')->addError('SimplyPost failed, Configurations Error.');
                //stop the process
                Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER']);
                Mage::app()->getResponse()->sendResponse();
                exit;

            }
            else{

                // get all order items and make an array
                $orderitems = $order->getAllVisibleItems();
                $arrItem = array();
                $arrItems = array();
                foreach ($orderitems as $orderitem): 
                    $product = $orderitem->getProduct();
                    $arrItem['weight'] = ($product->getData('weight')) ? $product->getData('weight') : 0;
                    $arrItem['description'] = ($product->getData('short_description')) ? $product->getData('short_description') : 0;
                    $arrItem['weight_unit'] = "Kg";
                    $arrItems[] = $arrItem;
                    unset($arrItem);
                endforeach;

                /*
                 * Send data to Simplypost via Zend_Http_Client
                */
                //genarate new token, because this token is only valid for 1 hour
                $auth = $spLoginToken;
                $clientLogin = new Zend_Http_Client();
                $clientLogin->setHeaders(array(
                    'Authorization' => $auth
                ));
                $clientLogin->setUri($spAuthEndpoint);
                $clientLogin->setMethod(Zend_Http_Client::POST);
                $responseLogin = $clientLogin->request();
                if ($responseLogin->isSuccessful()) {

                    $responseLogin = json_decode($responseLogin->getBody());
                    //our new token
                    $token = "JWT ".$responseLogin->token;

                    //end point post order details to simplypost
                    $url = $spEndpoint;
                    //obj Zend_Http_Client
                    $client = new Zend_Http_Client();
                    //set Authentication and other headers
                    $client->setHeaders(array(
                        'Content-Type' => 'application/json',
                        'Authorization' => $token
                    ));
                    //set endpoint url
                    $client->setUri($url);

                    //create post data into SimplyPost array format
                    $customerFName = ($order->getShippingAddress()->getData('firstname')) ? $order->getShippingAddress()->getData('firstname') : null;
                    $customerLName = ($order->getShippingAddress()->getData('lastname')) ? $order->getShippingAddress()->getData('lastname') : null;
                    $customerAddressStreet = ($order->getShippingAddress()->getData('street')) ? $order->getShippingAddress()->getData('street') : null;
                    $customerAddressCity = ($order->getShippingAddress()->getData('city')) ? $order->getShippingAddress()->getData('city') : null;
                    $customerAddressCountry = ($order->getShippingAddress()->getData('country_id')) ? Mage::app()->getLocale()->getCountryTranslation($order->getShippingAddress()->getData('country_id')) : null;
                    $data = array(
                        'merchant_code'  => $spMerchantCode, // copied from SimplyPost Mymodule account
                        'reference_number'   => ($order->getData('increment_id')) ? $order->getData('increment_id') : null, // order number
                        'order_source'   => 'mage_test', //random string
                        'service_code'   => $spServiceCode, //Delievery stype code
                        "pickup_details" => array( //Pickup details from admin cms
                            "contact_name" => "Kevin",
                            "phone_number" => '12344321',
                            "email" => 'kev@gmail.com',
                            "address" => 'Address goes here',
                            "postcode" => '123443',
                            ),
                        'consignee_details' => array( //Customer details
                            "contact_name" => $customerFName." ".$customerLName,
                            "phone_number" => ($order->getShippingAddress()->getData('telephone')) ? $order->getShippingAddress()->getData('telephone') : null,
                            "email" => ($order->getShippingAddress()->getData('email')) ? $order->getShippingAddress()->getData('email') : null,
                            "address" => $customerAddressStreet." ".$customerAddressCity." ".$customerAddressCountry,
                            "postcode" => ($order->getShippingAddress()->getData('postcode')) ? $order->getShippingAddress()->getData('postcode') : null,
                            ),
                        'item_details' => $arrItems,
                    );
                    //set the parameters
                    $client->setParameterPost($data);

                    // POST request
                    $client->setMethod(Zend_Http_Client::POST);
                    $response = $client->request();
                    if ($response->isSuccessful()) {
                        //decode the response
                        $responseBody = json_decode($response->getBody());

                        // update the tracking code into shipment in our system
                        $shipment = $observer->getEvent()->getShipment();
                        $track = Mage::getModel('sales/order_shipment_track')
                            ->setNumber($responseBody->tracking_id) //tracking number / awb number
                            ->setCarrierCode('simplypost') //carrier code
                            ->setTitle('SimplyPost'); //carrier title
                        $shipment->addTrack($track);
                        Mage::getSingleton('core/session')->addSuccess('Order added to the SimplyPost.');


                    }
                    else{
                        //log error
                        Mage::log($response);
                        $responseBody = json_decode($response->getBody());
                        Mage::getSingleton('core/session')->addError('SimplyPost Failed: '.$responseBody->error->message);
                        //stop the process
                        Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER']);
                        Mage::app()->getResponse()->sendResponse();
                        exit;
                    }
                }
                else{
                    //log error
                    Mage::log($responseLogin);
                    $responseLogin = json_decode($responseLogin->getBody());
                    Mage::getSingleton('core/session')->addError('SimplyPost Authentication Failed: '.$responseLogin->errors);
                    //stop the process
                    Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER']);
                    Mage::app()->getResponse()->sendResponse();
                    exit;
                }
            }
        }


    }

    // This function is called on core_block_abstract_to_html_after event
    // We will append our block to the html in the adminhtml under order page
    public function getSalesOrderViewInfo(Varien_Event_Observer $observer) {
        $block = $observer->getBlock();
        // layout name should be same as used in app/design/adminhtml/default/default/layout/mymodule.xml
        if (($block->getNameInLayout() == 'order_info') && ($child = $block->getChild('simplypost.order.info.custom.block'))) {
            $transport = $observer->getTransport();
            if ($transport) {
                $html = $transport->getHtml();
                $html .= $child->toHtml();
                $transport->setHtml($html);
            }
        }
    }

}
  • Great to see that you did it – inrsaurabh Mar 31 '18 at 12:36
  • Thanks man, as you say I just follow their API and build a custom module :) – nanbatman Apr 3 '18 at 6:17

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.