3

I have followed a tutorial to add custom shipping method to magento 2.1.7 and i can't seem to understand how to implement NinjaVan Courier API. Could anyone help ??

API

$headers = array();
$headers[0] = 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxx';
$headers[1] = 'Content-type: application/json';

$order_data = array();
$order_data['from_postcode'] = "159363";
$order_data['from_address1'] = "30 JALAN KILANG BARAT";
$order_data['from_address2'] = "KEWALRAM HOUSE";
$order_data['from_city'] = "SG";
$order_data['from_country'] = "SG";
$order_data['from_email'] = "yasserghost90@gmail.com";
$order_data['from_name'] = "LEONARD NIMOY";
$order_data['from_contact'] = "99001122";
$order_data['to_postcode'] = "318993";
$order_data['to_address1'] = "BLOCK 998";
$order_data['to_address2'] = "TOA PAYOH NORTH";
$order_data['to_locality'] = "TOA PAYOH";
$order_data['to_city'] = "SG";
$order_data['to_country'] = "SG";
$order_data['to_email'] = "yasserghost90@gmail.com";
$order_data['to_name'] = "JAMES T KIRK";
$order_data['to_contact'] = "99110022";
$order_data['delivery_date'] = "2016-03-10";
$order_data['pickup_date'] = "2016-03-09";
$order_data['pickup_weekend'] = true;
$order_data['delivery_weekend'] = false;
$order_data['staging'] = false;
$order_data['pickup_timewindow_id'] = 1;
$order_data['delivery_timewindow_id'] = 2;
$order_data['max_delivery_days'] = 1;
$order_data['cod_goods'] = 35.50;
$order_data['pickup_instuction'] = "Park at the loading bay.";
$order_data['delivery_instuction'] = "Doorbell broken; call once you arrive.";
$order_data['requested_tracking_id'] = "90843A";
$order_data['order_ref_no'] = "9012";
$order_data['type'] = "NORMAL";
$order_data['parcel_size'] = 1;
$order_data['parcel_volume'] = 4000;
$order_data['parcel_weight'] = 1200;

$order_json = array();
$order_json[] = $order_data;

Note that this is the production API endpoint

$ch = curl_init("https://api.ninjavan.sg/3.0/orders");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order_json));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

The Order was successfully received and is being processed. Order ID is returned.

if($http_code == 200){
    $body = json_decode($body);
    print_r($body);

The Order ID/ Order reference no along with the order creation status is returned. This can be stored in db if needed.

    $parcel_id = $body[0]->id;
    $parcel_creation_status = $body[0]->status;
    $parcel_message = $body[0]->message;
    $parcel_order_ref_no = $body[0]->order_ref_no;

    if($parcel_message == "ERROR"){
        print_r($parcel_message);
    } else{

Get Order by ID API

        $header = array();
        $header[0] = 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxx';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.ninjavan.sg/2.0/orders/".$parcel_id);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        $body = curl_exec($ch);

        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        print_r($body);

Order was created successfully and the Order object is returned, hence $body will not be null

        if($http_code == 200 && $body != ""){

The created Order's tracking ID and other details can be stored in db if needed

            $order_tracking_id = json_decode($body)->tracking_id;
            $order_shipper_ref_no = json_decode($body)->shipper_ref_no;
            $order_customer_ref_no = json_decode($body)->customer_ref_no;
            return array('httpCode' => $http_code, 'Created Tracking ID: ' => json_decode($body)->tracking_id);

Order was not created successfully. Check the Order JSON that you sent for order creation.

        } else if($http_code == 200 && $body == ""){
            return array('httpCode' => $http_code, 'body' => "Order not created successfully!");
            echo 'hassan';
        }
    }
} else{
    return array('httpCode' => $http_code, 'body' => "Order not created successfully!");
    echo 'hassan';
}

Customshipping.php in my Module

class Customshipping extends AbstractCarrier implements CarrierInterface { /** * Carrier's code * * @var string */ protected $_code = 'mpcustomshipping';

/**
 * Whether this carrier has fixed rates calculation
 *
 * @var bool
 */
protected $_isFixed = true;

/**
 * @var ResultFactory
 */
protected $_rateResultFactory;

/**
 * @var MethodFactory
 */
protected $_rateMethodFactory;

/**
 * @param ScopeConfigInterface $scopeConfig
 * @param ErrorFactory $rateErrorFactory
 * @param LoggerInterface $logger
 * @param ResultFactory $rateResultFactory
 * @param MethodFactory $rateMethodFactory
 * @param array $data
 */
public function __construct(

    ScopeConfigInterface $scopeConfig,
    ErrorFactory $rateErrorFactory,
    LoggerInterface $logger,
    ResultFactory $rateResultFactory,
    MethodFactory $rateMethodFactory,
    array $data = []
) {
    $this->_rateResultFactory = $rateResultFactory;
    $this->_rateMethodFactory = $rateMethodFactory;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

/**
 * Generates list of allowed carrier`s shipping methods
 * Displays on cart price rules page
 *
 * @return array
 * @api
 */
public function getAllowedMethods()
{
    return [$this->getCarrierCode() => __($this->getConfigData('name'))];
}

/**
 * Collect and get rates for storefront
 *
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 * @param RateRequest $request
 * @return DataObject|bool|null
 * @api
 */

public function collectRates(RateRequest $request)
{
    /**
     * Make sure that Shipping method is enabled
     */
    if (!$this->isActive()) {
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->_rateResultFactory->create();

    $shippingPrice = $this->getConfigData('price');

    $method = $this->_rateMethodFactory->create();

    /**
     * Set carrier's method data
     */
    $method->setCarrier($this->getCarrierCode());
    $method->setCarrierTitle($this->getConfigData('title'));

    /**
     * Displayed as shipping method under Carrier
     */
    $method->setMethod($this->getCarrierCode());
    $method->setMethodTitle($this->getConfigData('name'));

    $method->setPrice($shippingPrice);
    $method->setCost($shippingPrice);

    $result->append($method);

    return $result;
}

}

Your Answer

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

Browse other questions tagged or ask your own question.