Here we will see how to integrate Bluedart Courier API so we can send products to our customers and pick products from the customer.
we will Generate BlueDart WayBill also. In this tutorial, we are using CodeIgniter.
NOTE: It’s SOAP base API so never forget to extend it’s class in php, e.g.
class DebugSoapClient extends SoapClient.
this class is added in the given controller code.
STEP 1:
Create a file in application/controllers/Bluedart.php and put the given code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Bluedart extends CI_Controller { public function __construct(){ error_reporting(0); parent::__construct(); $this->load->library(array('form_validation','session','cart')); //$this->load->database(); } public function index(){ $ordernumber = 'ORDER1234'; // Unique Order ID $ActualWeight = '0.5'; //kgs $CommodityDetail1 = 'CodyPaste T-shirt'; //Product Name $DeclaredValue = '2000'; //Price $Breadth = '14.1'; //in cms (centimeter) $Count = '1'; // Number of products $Height = '5.1'; //in cms (centimeter) $Length = '32.8'; //in cms (centimeter) $CustomerAddress1 = '401, Living Homes'; $CustomerAddress2 = 'Frineds Enclave'; $CustomerAddress3 = 'Noida'; $SpecialInstruction = 'N/A'; $PickupDate = '2023-01-02'; // YY-MM-DD $CustomerEmailID = 'customer@email.com'; $CustomerMobile = '9989898989'; $CustomerName = 'Vipul Rai'; $CustomerPincode = '201009'; $ProductCode = 'D'; // D: Domestic, A: Air Apex, E: Express/surface #echo "Start of Soap 1.2 version (ws_http_Binding) setting"; $soap = new DebugSoapClient('https://netconnect.bluedart.com/Ver1.10/ShippingAPI/WayBill/WayBillGeneration.svc?wsdl', array( 'trace' => 1, 'style' => SOAP_DOCUMENT, 'use' => SOAP_LITERAL, 'soap_version' => SOAP_1_2 )); $soap->__setLocation("https://netconnect.bluedart.com/Ver1.10/ShippingAPI/WayBill/WayBillGeneration.svc"); $soap->sendRequest = true; $soap->printRequest = false; $soap->formatXML = true; $actionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','Action','http://tempuri.org/IWayBillGeneration/GenerateWayBill',true); $soap->__setSoapHeaders($actionHeader); #echo "end of Soap 1.2 version (WSHttpBinding) setting"; $params = array( 'Request' => array ( 'Consignee' => array ( 'ConsigneeAddress1' => 'Plot 23-24', 'ConsigneeAddress2' => 'Greno West', 'ConsigneeAddress3' => 'Noida', 'ConsigneeAttention' => 'A', 'ConsigneeMobile' => '9999999999', 'ConsigneeName' => 'Cody Paste', 'ConsigneePincode' => '201009', 'ConsigneeTelephone' => '9999999999', ), 'Services' => array ( 'ActualWeight' => $ActualWeight, 'CollectableAmount' => '0', 'Commodity' => array ( 'CommodityDetail1' => $CommodityDetail1 ), 'CreditReferenceNo' => $ordernumber, 'DeclaredValue' => $DeclaredValue, 'Dimensions' => array ( 'Dimension' => array ( 'Breadth' => $Breadth, 'Count' => $Count, 'Height' => $Height, 'Length' => $Length ), ), 'RegisterPickup' => true, 'InvoiceNo' => substr(str_replace("-", "", $ordernumber), -10), 'PackType' => '', 'PickupDate' => $PickupDate, 'PickupTime' => '1800', 'PieceCount' => '1', 'ProductCode' => $ProductCode, 'ProductType' => 'Dutiables', 'SpecialInstruction' => $SpecialInstruction, 'SubProductCode' => '' ), 'Shipper' => array( 'CustomerAddress1' => $CustomerAddress1, 'CustomerAddress2' => $CustomerAddress2, 'CustomerAddress3' => $CustomerAddress3, 'CustomerCode' => '123456', // BlueDart will share Customer Code 'CustomerEmailID' => $CustomerEmailID, 'CustomerMobile' => $CustomerMobile, 'CustomerName' => $CustomerName, 'CustomerPincode' => $CustomerPincode, 'CustomerTelephone' => $CustomerMobile, 'IsToPayCustomer' => true, // IsToPayCustomer if blank menas 'forward', if 'true' then it's reverse pickup. 'OriginArea' => 'DEL', 'Sender' => $CustomerName, 'VendorCode' => '' ) ), 'Profile' => array( 'Api_type' =>'S', 'LicenceKey'=>'XXXXkjhgkjhgjkhg87658765jkhg', // BlueDart will share 'LoginID' =>'MOZ1234', // BlueDart will share 'Version' =>'1.3' ) ); // Here I call my external function $result = $soap->__soapCall('GenerateWayBill',array($params)); $blueDartResponse = $this->Bluedartmodel->blueDartResponse($result); if($blueDartResponse=="1"){ //Generate Waybill PDF $x = $result->GenerateWayBillResult; $AWBPrintContent = $x->AWBPrintContent; if($AWBPrintContent==""){$AWBPrintContent="none";} ?> <object data="data:application/pdf;base64,<?php echo base64_encode($AWBPrintContent); ?>" type="application/pdf" style="height:100%;width:100%"></object> <?php } } } class DebugSoapClient extends SoapClient { public $sendRequest = true; public $printRequest = true; public $formatXML = true; public function __doRequest($request, $location, $action, $version, $one_way=0) { if ( $this->printRequest ) { if ( !$this->formatXML ) { $out = $request; } else { $doc = new DOMDocument; $doc->preserveWhiteSpace = false; $doc->loadxml($request); $doc->formatOutput = true; $out = $doc->savexml(); } echo $out; } if ( $this->sendRequest ) { return parent::__doRequest($request, $location, $action, $version, $one_way); } else { return ''; } } } |
CHANGE / UPDATE these fields with real values, which you will get from BlueDart:
1 2 3 4 5 6 7 8 | 'Profile' => array( 'Api_type' =>'S', 'LicenceKey'=>'XXXXkjhgkjhgjkhg87658765jkhg', // BlueDart will share 'LoginID' =>'MOZ1234', // BlueDart will share 'Version' =>'1.3' ) ); |
STEP 2:
In Above Controller we are using MODEL ‘blueDartResponse()’ to save vluedart response to Database table, so we have to create a ‘model’ file here.
Create a file in application/models/Bluedartmodel.php and put the given code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Bluedartmodel extends CI_Model { public function __construct(){ $this->load->library('session'); $this->load->library('email'); $this->load->database(); } public function blueDartResponse($result){ date_default_timezone_set("Asia/Kolkata"); $x = $result->GenerateWayBillResult; //$y = $x->AWBPrintContent; $AWBNo = $x->AWBNo; if($AWBNo==""){$AWBNo="none";} $AWBPrintContent = $x->AWBPrintContent; if($AWBPrintContent==""){$AWBPrintContent="none";} $AvailableAmountForBooking = $x->AvailableAmountForBooking; if($AvailableAmountForBooking==""){$AvailableAmountForBooking="none";} $AvailableBalance = $x->AvailableBalance; if($AvailableBalance==""){$AvailableBalance="none";} $CCRCRDREF = $x->CCRCRDREF; if($CCRCRDREF==""){$CCRCRDREF="none";} $DestinationArea = $x->DestinationArea; if($DestinationArea==""){$DestinationArea="none";} $DestinationLocation = $x->DestinationLocation; if($DestinationLocation==""){$DestinationLocation="none";} $IsError = $x->IsError; if($IsError==""){$IsError="none";} $IsErrorInPU = $x->IsErrorInPU; if($IsErrorInPU==""){$IsErrorInPU="none";} $ShipmentPickupDate = $x->ShipmentPickupDate; if($ShipmentPickupDate==""){$ShipmentPickupDate="none";} $Status_WayBill_StatusCode = $x->Status->WayBillGenerationStatus->StatusCode; if($Status_WayBill_StatusCode==""){$Status_WayBill_StatusCode="none";} $Status_Pickup_StatusCode = $x->Status->WayBillGenerationStatus->StatusInformation; if($Status_Pickup_StatusCode==""){$Status_Pickup_StatusCode="none";} $TokenNumber = $x->TokenNumber; if($TokenNumber==""){$TokenNumber="none";} $TransactionAmount = $x->TransactionAmount; if($TransactionAmount==""){$TransactionAmount="none";} $rmanumber = $CCRCRDREF; if($rmanumber==""){$rmanumber="none";} $date = date("Y-m-d H:i:sA"); $status = '1'; $datas = array( 'AWBNo' => $AWBNo, 'AWBPrintContent' => $AWBPrintContent, 'AvailableAmountForBooking' => $AvailableAmountForBooking, 'AvailableBalance' => $AvailableBalance, 'CCRCRDREF' => $CCRCRDREF, 'DestinationArea' => $DestinationArea, 'DestinationLocation' => $DestinationLocation, 'IsError' => $IsError, 'IsErrorInPU' => $IsErrorInPU, 'ShipmentPickupDate' => $ShipmentPickupDate, 'Status_WayBill_StatusCode' => $Status_WayBill_StatusCode, 'Status_Pickup_StatusCode' => $Status_Pickup_StatusCode, 'TokenNumber' => $TokenNumber, 'TransactionAmount' => $TransactionAmount, 'rmanumber' => $rmanumber, 'date' => $date, 'status' => $status, 'full_result' => serialize($result) ); if($this->db->insert('bluedart_response',$datas)){ return 1; } } } |