PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/pago_shippers/ups/ups.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 187 lines | 136 code | 34 blank | 17 comment | 14 complexity | dfe670e2bce98d5ff40a292ed40cf018 MD5 | raw file
  1. <?php
  2. defined ( '_JEXEC' ) or die ( 'Restricted access' );
  3. /**
  4. * @package Pago Shipping Plugin
  5. * @author 'corePHP' LLC.
  6. * @copyright (C) 2010- 'corePHP' LLC.
  7. * @license GNU/GPL v2 http://www.gnu.org/licenses/gpl-2.0.html
  8. *
  9. * Support: http://support.corephp.com/
  10. */
  11. class plgPago_shippersUps extends JPlugin
  12. {
  13. public function __construct($subject, $plugin)
  14. {
  15. parent::__construct($subject, $plugin);
  16. // Set shipping options
  17. KDispatcher::add_filter('set_shipping_options', array($this, 'set_options'));
  18. KDispatcher::add_filter('generate_link', array($this, 'generate_link'));
  19. }
  20. public function set_options(&$shipping_options, $cart, $user_data)
  21. {
  22. require_once (dirname ( __FILE__ ) . '/ups.class.php');
  23. $path = JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_pago' . DIRECTORY_SEPARATOR . 'helpers';
  24. require_once $path . '/ShippingCalculator.php';
  25. require_once $path . '/helper.php';
  26. $ShippingCalculator = new ShippingCalculator;
  27. $PagoHelper = new PagoHelper;
  28. $config = Pago::get_instance('config')->get();
  29. $shipping_type = $config->get('checkout.shipping_type');
  30. $cred ['AccessLicenseNumber'] = $this->params->get('AccessLicenseNumber');
  31. $cred ['UserId'] = $this->params->get('UserId');
  32. $cred ['Password'] = $this->params->get('Password');
  33. $cred ['testmode'] = $this->params->get('testmode');
  34. $ups = new ups();
  35. $ups->setCredentials($cred);
  36. $ups->setShipper(array('PostalCode' => $this->params->get('PostalCode'), 'CountryCode' => $this->params->get('CountryCode')));
  37. $ups->setRecipient(array('PostalCode' => $user_data->zip, 'CountryCode' => $user_data->country));
  38. $FreeShipping = true;
  39. foreach ( $cart['items'] as $item )
  40. {
  41. $prd_id[] = $item->id;
  42. if ($shipping_type)
  43. {
  44. $isTrue = $ShippingCalculator->get_shipping_methods($item->id, $str = "ups");
  45. if ($isTrue == 2)
  46. {
  47. $options = array();
  48. //$shipping_options[ "Custom Shipping" ] = $options;
  49. return $shipping_options;
  50. }
  51. if ($isTrue)
  52. {
  53. $options = array();
  54. $options[] = array(
  55. 'code' => 0,
  56. 'name' => 'This Item is Free of Shipping',
  57. 'value' => 0
  58. );
  59. $shipping_options[ "UPS" ] = $options;
  60. return $shipping_options;
  61. }
  62. }
  63. else
  64. {
  65. $isTrue = $ShippingCalculator->checkFreeShipping($item->id);
  66. if ($isTrue)
  67. {
  68. continue;
  69. }
  70. }
  71. $FreeShipping = false;
  72. // Convert to pound
  73. $i = 1;
  74. $db = JFactory::getDBO();
  75. // Unit for length
  76. $sql = "SELECT `code` FROM #__pago_units where `default` = 1 and `type` = 'size'";
  77. $db->setQuery($sql);
  78. $sizeunit = $db->loadObject();
  79. $uol = $sizeunit->code;
  80. // Unit for weight
  81. $sql = "SELECT `code` FROM #__pago_units where `default` = 1 and `type` = 'weight'";
  82. $db->setQuery($sql);
  83. $weightunit = $db->loadObject();
  84. $uom = $weightunit->code;
  85. // Convert to weight pounds
  86. $itemWeight = $ShippingCalculator->convert_weight($item->weight, $uom, 'lb');
  87. $itemTotalWeight = ceil($itemWeight * $item->cart_qty);
  88. $itemVolData = $ShippingCalculator->getItemVolumeShipping($item);
  89. $item->length = $itemVolData['length'];
  90. $item->width = $itemVolData['width'];
  91. $item->height = $itemVolData['height'];
  92. // Convert Length, width, height to Inches
  93. $itemLength = $ShippingCalculator->convert_size($item->length, $uol, 'in');
  94. $itemWidth = $ShippingCalculator->convert_size($item->width, $uol, 'in');
  95. $itemHeight = $ShippingCalculator->convert_size($item->height, $uol, 'in');
  96. // Set Minimum and Maximun Weight
  97. if ($itemTotalWeight < 1)
  98. {
  99. $itemTotalWeight = 1;
  100. }
  101. if ($itemTotalWeight > 150)
  102. {
  103. $itemTotalWeight = 150.00;
  104. }
  105. $package = array ('Weight' => array ('Value' => $itemTotalWeight, 'Units' => $uom ),'Dimensions' => array ('Length' => $itemLength, 'Width' => $itemWidth, 'Height' => $itemHeight, 'Units' => $uol) );
  106. $ups->setPackage($package);
  107. }
  108. $shippingTypes = $this->params->get('shippingType', null);
  109. $handling_fee = $this->params->get('handling_fee', 0);
  110. if (is_string($shippingTypes) && ! empty($shippingTypes))
  111. {
  112. $shippingTypes = array($this->params->get('shippingType'));
  113. }
  114. elseif (is_string($shippingTypes) || $shippingTypes === null)
  115. {
  116. $shippingTypes = array ();
  117. }
  118. try
  119. {
  120. $shipping_options ['UPS'] = $ups->getRates($shippingTypes);
  121. $error = @$ups->get('_rateReply')->Response->Error;
  122. if($error)
  123. @$shipping_options ['UPS']['error']['error'] = (array)$error;
  124. }
  125. catch ( Exception $e )
  126. {
  127. // Try flat
  128. }
  129. if($FreeShipping)
  130. {
  131. $shipping_options['UPS'] = [];
  132. $shipping_options['UPS'][] = [
  133. 'code' => 0,
  134. 'name' => 'Free Shipping',
  135. 'value' => 0
  136. ];
  137. return $shipping_options;
  138. }
  139. foreach ($shipping_options ['UPS'] as $key => $ups) {
  140. $shipping_options ['UPS'][$key]['value'] = $ups['value'] + $handling_fee;
  141. }
  142. return $shipping_options;
  143. }
  144. public function generate_link ($shipping_method, $tracking_number)
  145. {
  146. if(trim($shipping_method) != 'UPS')
  147. {
  148. return;
  149. }
  150. $link = "<a href='http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=".$tracking_number."' target='_blank'>".$tracking_number."</a>";
  151. return $link;
  152. }
  153. }