PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/gocart/packages/shipping/usps_international/libraries/usps_international.php

https://bitbucket.org/admin_malppy/malppy
PHP | 344 lines | 242 code | 78 blank | 24 comment | 14 complexity | d4647dcf2fea4366b10cecbe34b440f6 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class usps_international
  3. {
  4. var $CI;
  5. var $liveserver = 'http://production.shippingapis.com/ShippingAPI.dll';
  6. var $service_list;
  7. function usps_international()
  8. {
  9. //we're going to have this information in the back end for editing eventually
  10. //username password, origin zip code etc.
  11. $this->CI =& get_instance();
  12. $this->CI->load->model('Settings_model');
  13. $this->service_list = array(
  14. "USPS GXG&lt;sup&gt;&amp;trade;&lt;/sup&gt; Envelopes**",
  15. "Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International",
  16. "Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Flat Rate Envelope",
  17. "Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Legal Flat Rate Envelope",
  18. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International",
  19. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Large Flat Rate Box",
  20. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Medium Flat Rate Box",
  21. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Small Flat Rate Box**",
  22. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International DVD Flat Rate Box**",
  23. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Large Video Flat Rate Box**",
  24. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Flat Rate Envelope**",
  25. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Legal Flat Rate Envelope**",
  26. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Padded Flat Rate Envelope**",
  27. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Gift Card Flat Rate Envelope**",
  28. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Small Flat Rate Envelope**",
  29. "Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Window Flat Rate Envelope**",
  30. "First-Class Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Package**",
  31. "First-Class Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt; International Large Envelope**"
  32. );
  33. }
  34. function rates()
  35. {
  36. $rates = array();
  37. $this->CI->load->library('session');
  38. // get customer info
  39. $customer = $this->CI->go_cart->customer();
  40. $dest_zip = $customer['ship_address']['zip'];
  41. $dest_country = $customer['ship_address']['country'];
  42. //grab this information from the config file
  43. $country = $this->CI->config->item('country');
  44. $orig_zip = $this->CI->config->item('zip');
  45. // retrieve settings
  46. $settings = $this->CI->Settings_model->get_settings('usps_international');
  47. //check if we're enabled
  48. if(!$settings['enabled'] || $settings['enabled'] < 1)
  49. {
  50. return array();
  51. }
  52. $user = $settings['username'];
  53. $pass = $settings['password'];
  54. $service = explode(',',$settings['service']);
  55. $mailtype = $settings['mailtype'];
  56. $container = $settings['container'];
  57. $size = $settings['size'];
  58. $machinable = $settings['machinable'];
  59. $handling_method = $settings['handling_method'];
  60. $handling_amount = $settings['handling_amount'];
  61. // build allowed service list
  62. foreach($service as $s)
  63. {
  64. $service_list[] = $this->service_list[$s];
  65. }
  66. //set the weight
  67. $weight = $this->CI->go_cart->order_weight();
  68. // value of contents
  69. $total = $this->CI->go_cart->order_insurable_value();
  70. //strip the decimal
  71. $oz = ($weight-(floor($weight)))*100;
  72. //set pounds
  73. $lbs = floor($weight);
  74. //set ounces based on decimal
  75. $oz = round(($oz*16)/100);
  76. // no foreign support
  77. if($country!="US")
  78. {
  79. return array();
  80. }
  81. if($dest_country=='United States')
  82. {
  83. return array();
  84. }
  85. //$str = '<IntlRateV2Request USERID="'. $user . '" PASSWORD="' . $pass . '">';
  86. $str = '<IntlRateV2Request USERID="'. $user . '">';
  87. $str .= '<Package ID="1">';
  88. $str .= '<Pounds>'.$lbs.'</Pounds><Ounces>'.$oz.'</Ounces>';
  89. $str .= '<MailType>ALL</MailType>';
  90. $str .= '<ValueOfContents>'.$total.'</ValueOfContents>';
  91. $str .= '<Country>'.$dest_country.'</Country>';
  92. $str .= '<Container>' . $container .'</Container><Size>'.$size.'</Size>';
  93. $str .= '<Width>10</Width><Length>5</Length><Height>5</Height><Girth>5</Girth>';
  94. $str .= '</Package></IntlRateV2Request>';
  95. $str = $this->liveserver .'?API=IntlRateV2&XML='. urlencode($str);
  96. $ch = curl_init();
  97. // set URL and other appropriate options
  98. curl_setopt($ch, CURLOPT_URL, $str);
  99. curl_setopt($ch, CURLOPT_HEADER, 0);
  100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  101. // grab URL and pass it to the browser
  102. $ats = curl_exec($ch);
  103. // close curl resource, and free up system resources
  104. curl_close($ch);
  105. //$xmlParser = new xmlparser();
  106. $this->CI->load->library('xmlparser');
  107. $array = $this->CI->xmlparser->GetXMLTree($ats);
  108. if(isset($array['INTLRATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR']))
  109. {
  110. // var_dump($array);
  111. return array(); // if the request failed, just send back an empty set
  112. }
  113. foreach ($array['INTLRATEV2RESPONSE'][0]['PACKAGE'][0]['SERVICE'] as $value)
  114. {
  115. if(in_array($value['SVCDESCRIPTION'][0]['VALUE'],$service_list))
  116. {
  117. $amount = $value['POSTAGE'][0]['VALUE'];
  118. if(is_numeric($handling_amount)) // valid entry?
  119. {
  120. if($handling_method=='$')
  121. {
  122. $amount += $handling_amount;
  123. }
  124. elseif($handling_method=='%')
  125. {
  126. $amount += $amount * ($handling_amount/100);
  127. }
  128. }
  129. $rates[html_entity_decode($value['SVCDESCRIPTION'][0]['VALUE'])] = $amount;
  130. }
  131. }
  132. return $rates;
  133. }
  134. function install()
  135. {
  136. $default_settings = array(
  137. 'username'=>'',
  138. 'password'=>'',
  139. 'mailtype'=>'ALL',
  140. 'container'=>'RECTANGULAR',
  141. 'service' => implode(',', array_keys($this->service_list)),
  142. 'size'=>'LARGE',
  143. 'length'=>'',
  144. 'width'=>'',
  145. 'height'=>'',
  146. 'girth'=>'',
  147. 'machinable'=>'true',
  148. 'handling_method'=>'$',
  149. 'handling_amount'=>5,
  150. 'enabled'=>'0'
  151. );
  152. //set a default blank setting for flatrate shipping
  153. $this->CI->Settings_model->save_settings('usps_international', $default_settings);
  154. }
  155. function uninstall()
  156. {
  157. $this->CI->Settings_model->delete_settings('usps_international');
  158. }
  159. function form($post = false)
  160. {
  161. $this->CI->load->helper('form');
  162. //this same function processes the form
  163. if(!$post)
  164. {
  165. $settings = $this->CI->Settings_model->get_settings('usps_international');
  166. $mailtype = $settings['mailtype'];
  167. $container = $settings['container'];
  168. $service = explode(',', $settings['service']);
  169. $username = $settings['username'];
  170. $password = $settings['password'];
  171. $enabled = $settings['enabled'];
  172. $size = $settings['size'];
  173. $length = $settings['length'];
  174. $width = $settings['width'];
  175. $height = $settings['height'];
  176. $girth = $settings['girth'];
  177. $machinable = $settings['machinable'];
  178. $handling_method = $settings['handling_method'];
  179. $handling_amount = $settings['handling_amount'];
  180. }
  181. else
  182. {
  183. $mailtype = $post['mailtype'];
  184. $container = $post['container'];
  185. $service = $post['service'];
  186. $username = $post['username'];
  187. $password = $post['password'];
  188. $enabled = $post['enabled'];
  189. $size = $post['size'];
  190. $length = $post['length'];
  191. $width = $post['width'];
  192. $height = $post['height'];
  193. $girth = $post['girth'];
  194. $machinable = $post['machinable'];
  195. $handling_method = $post['handling_method'];
  196. $handling_amount = $post['handling_amount'];
  197. }
  198. $form = '<table><tr><td>Username: </td><td>'.form_input('username', $username) .'</td></tr>';
  199. //<tr><td>Password: </td><td>'.form_input('password', $password) .'</td></tr>';
  200. $form .= '</td></tr><tr><td valign="top">Services To Offer: </td><td>';
  201. foreach($this->service_list as $id=>$opt)
  202. {
  203. $form .= "<input type='checkbox' name='service[]' value='$id' ";
  204. if(in_array($id, $service)) $form .= "checked='checked'";
  205. $form .= "> ". htmlspecialchars_decode(html_entity_decode(stripslashes($opt))) ." <br />";
  206. }
  207. $form .= '</td></tr><tr><td>Mailtype: </td><td>';
  208. $opts = array(
  209. 'ALL'=>'All',
  210. 'PACKAGE'=>'Package',
  211. 'ENVELOPE'=>'envelope'
  212. );
  213. $form .= form_dropdown('mailtype', $opts, $mailtype);
  214. $form .= '</td></tr><tr><td>Container: </td><td>';
  215. $opts = array(
  216. 'RECTANGULAR'=>'Rectangular',
  217. 'NONRECTANGULAR'=>'Non Rectangular'
  218. );
  219. $form .= form_dropdown('container', $opts, $container);
  220. $form .= '</td></tr><tr><td>Size: </td><td>';
  221. $opts = array('REGULAR'=>'Regular',
  222. 'LARGE'=>'Large',
  223. 'OVERSIZE'=>'Oversize'
  224. );
  225. $form .= form_dropdown('size', $opts, $size);
  226. $form .= '</td></tr><tr><td>Pkg Length: </td><td>';
  227. $form .= form_input('length', $length);
  228. $form .= '</td></tr><tr><td>Pkg Width: </td><td>';
  229. $form .= form_input('width', $width);
  230. $form .= '</td></tr><tr><td>Pkg Height: </td><td>';
  231. $form .= form_input('height', $height);
  232. $form .= '</td></tr><tr><td>Pkg Girth: </td><td>';
  233. $form .= form_input('girth', $girth);
  234. $form .= '</td></tr><tr><td>Machinable: </td><td>';
  235. $opts = array('TRUE'=>'True', 'FALSE'=>'False');
  236. $form .= form_dropdown('machinable', $opts, $machinable);
  237. $form .= '</td></tr><tr><td>Handling Fee: </td><td>';
  238. $form .= form_dropdown('handling_method', array('$'=>'$', '%'=>'%'), $handling_method);
  239. $form .= ' '. form_input('handling_amount', $handling_amount);
  240. $form .= '</td></tr><tr><td>Module Status: </td><td>';
  241. $opts = array('Disabled', 'Enabled');
  242. $form .= form_dropdown('enabled', $opts, $enabled);
  243. $form .= '</td></tr></table>';
  244. return $form;
  245. }
  246. function check()
  247. {
  248. $save = $_POST;
  249. //count the errors
  250. if(empty($save['service']))
  251. {
  252. return 'You must choose at least one service';
  253. }
  254. else if(empty($save['username']))
  255. {
  256. return 'You must provide a username';
  257. }
  258. else
  259. {
  260. $save['service'] = implode(',', $save['service']);
  261. //we save the settings if it gets here
  262. $this->CI->Settings_model->save_settings('usps_international', $save);
  263. return false;
  264. }
  265. }
  266. }