PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/kohana_core/modules/shipping/libraries/drivers/Shipping/tnt.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 300 lines | 224 code | 50 blank | 26 comment | 20 complexity | 7f0597d7cf61a4c95f34507dcd94f8b3 MD5 | raw file
  1. <?php
  2. class TNTShipping {
  3. public $package;
  4. function __construct(){
  5. $this->package = new stdClass();
  6. $this->company = 'cioshiper'; // Your license number
  7. $this->password = 'qwer6789'; // Username
  8. $this->accountID = '276048'; // Password
  9. $this->postal_code = '02145'; // Zipcode you are shipping FROM
  10. }
  11. public function setData($package){
  12. $this->package = $package;
  13. }
  14. public function pickupNotification(){
  15. if(date("H") >= 15)
  16. $shipDate = date("d/m/Y", mktime(0, 0, 0, date("m"), date("d")+1, date("Y"))); //next day pickup
  17. else
  18. $shipDate = date("d/m/Y"); //same day
  19. $Xml = '<ESHIPPER>
  20. <LOGIN>
  21. <COMPANY>'.$this->company.'</COMPANY>
  22. <PASSWORD>'.$this->password.'</PASSWORD>
  23. <APPID>IN</APPID>
  24. <APPVERSION>2.1</APPVERSION>
  25. </LOGIN>
  26. <CONSIGNMENTBATCH>
  27. <SENDER>
  28. <COMPANYNAME>CIO</COMPANYNAME>
  29. <STREETADDRESS1>492 Old Connecticut Path</STREETADDRESS1>
  30. <STREETADDRESS2></STREETADDRESS2>
  31. <STREETADDRESS3></STREETADDRESS3>
  32. <CITY>Framingham</CITY>
  33. <PROVINCE>MA</PROVINCE>
  34. <POSTCODE>01701</POSTCODE>
  35. <COUNTRY>US</COUNTRY>
  36. <ACCOUNT>000276048</ACCOUNT>
  37. <VAT>GB78687</VAT>
  38. <CONTACTNAME>Deb Stark</CONTACTNAME>
  39. <CONTACTDIALCODE>1</CONTACTDIALCODE>
  40. <CONTACTTELEPHONE>5089354068</CONTACTTELEPHONE>
  41. <CONTACTEMAIL>dstark@cio.com</CONTACTEMAIL>
  42. <COLLECTION>
  43. <SHIPDATE>'.$shipDate.'</SHIPDATE>
  44. <PREFCOLLECTTIME>
  45. <FROM>1300</FROM>
  46. <TO>1500</TO>
  47. </PREFCOLLECTTIME>
  48. <ALTCOLLECTTIME>
  49. <FROM>1500</FROM>
  50. <TO>1700</TO>
  51. </ALTCOLLECTTIME>
  52. <COLLINSTRUCTIONS>Enter through visitors entrance at the intersection of Speen St and 492 Old Connecticut Path. Sign states "Framingham Corporate Center" and "IDG Enterprise" . Drive in under the canopy and go in the main door. Packages will be left outside the mailroom, first office on the left.</COLLINSTRUCTIONS>
  53. </COLLECTION>
  54. </SENDER>
  55. <CONSIGNMENT>
  56. <CONREF>ref 1</CONREF>
  57. <DETAILS>
  58. <RECEIVER>
  59. <COMPANYNAME>'.$this->package->shipping->company.'</COMPANYNAME>
  60. <STREETADDRESS1>'.$this->package->shipping->address1.'</STREETADDRESS1>
  61. <STREETADDRESS2></STREETADDRESS2>
  62. <STREETADDRESS3></STREETADDRESS3>
  63. <CITY>'.$this->package->shipping->city.'</CITY>
  64. <PROVINCE>'.$this->package->shipping->state.'</PROVINCE>
  65. <POSTCODE>'.$this->package->shipping->zip.'</POSTCODE>
  66. <COUNTRY>'.$this->package->shipping->country.'</COUNTRY>
  67. <VAT></VAT>
  68. <CONTACTNAME>'.$this->package->shipping->firstname .' '.$this->package->shipping->lastname.'</CONTACTNAME>
  69. <CONTACTDIALCODE>1 800</CONTACTDIALCODE>
  70. <CONTACTTELEPHONE>555555</CONTACTTELEPHONE>
  71. </RECEIVER>
  72. <PACKAGE>
  73. <ITEMS>'.$this->package->items.'</ITEMS>
  74. <DESCRIPTION>box</DESCRIPTION>
  75. <LENGTH>'.$this->package->length .'</LENGTH>
  76. <HEIGHT>'.$this->package->height .'</HEIGHT>
  77. <WIDTH>'.$this->package->width .'</WIDTH>
  78. <WEIGHT>'.$this->package->weight.'</WEIGHT>
  79. </PACKAGE>
  80. </DETAILS>
  81. <CONSIGNMENT>
  82. </CONSIGNMENTBATCH>
  83. </ESHIPPER>';
  84. $Request = rawurlencode($Xml);
  85. $CurlURL = 'http://iconnection.tnt.com:81/ShipperGate2.asp';
  86. $CurlQuery = 'xml_in=' . $Request;
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_URL, $CurlURL);
  89. curl_setopt($ch, CURLOPT_POST, 1);
  90. curl_setopt($ch, CURLOPT_POSTFIELDS, $CurlQuery);
  91. curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
  92. curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
  93. curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
  94. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Needs to be included if no *.crt is available to verify SSL certificates
  95. curl_setopt($ch, CURLOPT_SSLVERSION,3);
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  97. $result = curl_exec($ch);
  98. curl_close($ch);
  99. if ($result) {
  100. return ' - TNT Notification ID: '. $result; //$id = explode(":", $result);
  101. } else {
  102. echo ' - TNT Notification ID: Error in response from TNT.';
  103. }
  104. }
  105. private function tnt(){
  106. $Xml = '<?xml version="1.0" standalone="no"?>
  107. <PRICEREQUEST>
  108. <LOGIN>
  109. <COMPANY>'.$this->company.'</COMPANY>
  110. <PASSWORD>'.$this->password.'</PASSWORD>
  111. <APPID>PC</APPID>
  112. </LOGIN>
  113. <packageSETS>
  114. <COUNTRY>1.0</COUNTRY>
  115. <CURRENCY>1.0</CURRENCY>
  116. <POSTCODEMASK>1.0</POSTCODEMASK>
  117. <TOWNGROUP>1.0</TOWNGROUP>
  118. <SERVICE>1.0</SERVICE>
  119. <OPTION>1.0</OPTION>
  120. </packageSETS>
  121. <PRICECHECK>
  122. <RATEID>RATE1</RATEID>
  123. <ORIGINCOUNTRY>US</ORIGINCOUNTRY>
  124. <ORIGINTOWNNAME></ORIGINTOWNNAME>
  125. <ORIGINPOSTCODE>'.$this->postal_code.'</ORIGINPOSTCODE>
  126. <ORIGINTOWNGROUP></ORIGINTOWNGROUP>
  127. <DESTCOUNTRY>'.$this->package->to_country.'</DESTCOUNTRY>
  128. <DESTTOWNNAME></DESTTOWNNAME>
  129. <DESTPOSTCODE>'.$this->package->to_zip.'</DESTPOSTCODE>
  130. <DESTTOWNGROUP></DESTTOWNGROUP>
  131. <CONTYPE>N</CONTYPE>
  132. <CURRENCY>USD</CURRENCY>
  133. <WEIGHT>'.$this->package->weight.'</WEIGHT>
  134. <VOLUME>'.$this->package->volume.'</VOLUME>
  135. <ACCOUNT>'.$this->accountID.'</ACCOUNT>
  136. <ITEMS>'.$this->package->items.'</ITEMS>
  137. </PRICECHECK>
  138. </PRICEREQUEST>';
  139. $Request = rawurlencode($Xml);
  140. $CurlURL = 'http://iConnection.tnt.com:81/Pricegate.asp';
  141. $CurlQuery = 'xml_in=' . $Request;
  142. $ch = curl_init();
  143. curl_setopt($ch, CURLOPT_URL, $CurlURL);
  144. curl_setopt($ch, CURLOPT_POST, 1);
  145. curl_setopt($ch, CURLOPT_POSTFIELDS, $CurlQuery);
  146. curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
  147. curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
  148. curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Needs to be included if no *.crt is available to verify SSL certificates
  150. curl_setopt($ch, CURLOPT_SSLVERSION,3);
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  152. $result = curl_exec($ch);
  153. curl_close($ch);
  154. if ($result) {
  155. ## catch libxml errors
  156. libxml_use_internal_errors(true);
  157. $this->package->quote_xml = @simplexml_load_string($result);
  158. if ($this->package->quote_xml) {
  159. return true;
  160. }else {
  161. echo 'Error: '. $quote_xml->ERROR->DESCRIPTION;
  162. return false;
  163. }
  164. } else {
  165. echo 'Error in response from TNT.';
  166. return false;
  167. }
  168. }
  169. public function getRates(){
  170. $this->package->to_country = (!empty($this->package->to_country))? $this->package->to_country : 'US';
  171. if($this->package->weight <= 0)
  172. return 0;
  173. if(empty($this->package->length) OR empty($this->package->width) OR empty($this->package->height)
  174. OR empty($this->package->to_zip) OR empty($this->package->to_country))
  175. exit('tnt: shipping data missing');
  176. $this->package->length = $this->convertInchToMeter($this->package->length);
  177. $this->package->width = $this->convertInchToMeter($this->package->width);
  178. $this->package->height = $this->convertInchToMeter($this->package->height);
  179. $this->package->weight = $this->convertPoundToKg($this->package->weight);
  180. $this->package->volume = $this->package->length * $this->package->width * $this->package->height;
  181. if($this->tnt()){
  182. if($this->package->quote_xml->PRICE){
  183. foreach($this->package->quote_xml->PRICE as $rate){
  184. if($rate->RATE > 0){
  185. if( !$this->isTNTExepction($rate) ){
  186. $id = (string)'tnt '. $rate->SERVICE .' '. $rate->SERVICEDESC.' '.$rate->OPTIONDESC;
  187. $id = strtolower(str_replace(" ","_", $id));
  188. $r[$id]['name'] = (string)'TNT '. ucfirst(strtolower($rate->SERVICEDESC.' - '.$rate->OPTIONDESC));
  189. $r[$id]['amount'] = (float)$rate->RATE;
  190. }
  191. }
  192. }
  193. return $r;
  194. }else{
  195. //print_r($this->package->quote_xml);
  196. }
  197. }
  198. }
  199. function isTNTExepction($rate){
  200. $dimensionalWeight = $this->package->volume / 167;
  201. //5 pounds = 2.26796185 kilograms
  202. if(($dimensionalWeight >= 2.2) || ($this->package->weight >= 2.2)){
  203. if( (($rate->SERVICEDESC == 'EXPRESS') OR ($rate->SERVICEDESC == 'ECONOMY EXPRESS')) AND $rate->OPTIONDESC == 'NONE'){
  204. return false;
  205. }else{
  206. return true;
  207. }
  208. }elseif($rate->SERVICEDESC == 'EXPRESS' AND $rate->OPTIONDESC == 'NONE'){
  209. return false;
  210. }else{
  211. return true;
  212. }
  213. }
  214. function getRate($method){
  215. /*
  216. $this->package->to_country = (!empty($this->package->country))? $this->package->country : 'US';
  217. $this->package->method_id = $method;
  218. if(empty($this->package->length) OR empty($this->package->width) OR empty($this->package->height) OR empty($this->package->weight)
  219. OR empty($this->package->to_zip) OR empty($this->package->to_country) OR empty($this->package->method_id))
  220. exit('tnt: shipping package missing');
  221. $service = ''; //get all rates that apply
  222. if(isset($method))
  223. $service = '<SERVICE>'.$this->package->method_id.'</SERVICE>'; //get onyl the rate for this service
  224. if($this->tnt()){
  225. if($rate->RATE > 0){
  226. return $rate->RATE;
  227. }else{
  228. return -1;
  229. }
  230. }
  231. */
  232. //return 42;
  233. }
  234. public function getTrackingUrl($track_number){
  235. }
  236. private function convertInchToMeter($i){
  237. //1 inch = 0.0254 meters
  238. return $i * 0.025;
  239. }
  240. private function convertPoundToKg($i){
  241. //1 pound = 0.45359237 kilograms
  242. return $i * 0.453;
  243. }
  244. }
  245. ?>