Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. $_RATES = array();
  2.  
  3. $actualState = $DATA['destination']['province'];
  4.  
  5. $specialStates = array('AK', 'AS', 'AA', 'AE', 'AP', 'FM', 'GU', 'HI', 'MH', 'MP', 'PW', 'PR', 'VI');
  6. $statesName = array('AK' => 'Alaska', 'AS' => 'American Samoa', 'AA' => 'Armed Forces Americas', 'AE' => 'Armed Forces Europe', 'AP' => 'Armed Forces Pacific', 'FM' => 'Federated States of Micronesia', 'GU' => 'Guam', 'HI' => 'Hawaii', 'MH' => 'Marshall Islands', 'MP' => 'Northern Mariana Islands', 'PW' => 'Palau', 'PR' => 'Puerto Rico', 'VI' => 'Virgin Islands');
  7.  
  8. $bannedCollection = getcollection(420582159);
  9. $limitedCollection = getcollection(420583375);
  10.  
  11. function limitedProductsWarningMessage($productName, $state)
  12. {
  13. return ' - *WARNING* : We can\'t include CO2 cartridges in your shipment "' . $productName . '" to ' . $state . '.';
  14. }
  15.  
  16. function discount($total_price, $percentage, $A)
  17. {
  18. return number_format((($total_price/100) * $percentage), 2) + $A;
  19. }
  20.  
  21. $ups_percentage = 20; //in % of returned shipp cost
  22. $ups_A = 150; // in cents
  23. $usps_percentage = 20; //in % of returned shipp cost
  24. $usps_A = 100; // in cents
  25.  
  26. $w = 0;
  27. foreach ($DATA['items'] as $i) {
  28. $w += $i['quantity']*$i['grams']/1000;
  29. }
  30. $w = $w*2.20462; //in pounds
  31. $isresidential = true;
  32. if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') {
  33. $isresidential = false;
  34. }
  35.  
  36. /* UPS API DETAILS */
  37. $_UPS_ACCESS = "5D211D4BF15CE548";
  38. $_UPS_USERID = "evan.rege";
  39. $_UPS_PASSWD = "P!314156s";
  40. $_UPS_ACCOUNT = "X76Y68"; //set to null if you DON'T want to use negotiated rates
  41. /* USPS API DETAILS */
  42. $_USPS_USERNAME = '304GROWL4238';
  43. $_USPS_PASSWORD = '036YO42LN442';
  44. $_USPS_CONTRACTTYPE = 'C'; //N=none(retail), C=commercial, P=commercial plus
  45.  
  46. $packages = array(array('weight'=>$w, 'length'=>5, 'width'=>5, 'height'=>5));
  47.  
  48. $ups = new UPSAPI($_UPS_USERID, $_UPS_PASSWD, $_UPS_ACCESS, $_UPS_ACCOUNT);
  49. $ups->setOrigin($DATA['origin']['city'], $DATA['origin']['province'], $DATA['origin']['postal_code'], $DATA['origin']['country']);
  50. $ups->setDestination($DATA['destination']['city'], $DATA['destination']['province'], $DATA['destination']['postal_code'], $DATA['destination']['country'], $isresidential);
  51. $ups_rates = $ups->getRate($packages, 'IMPERIAL');
  52.  
  53. $usps = new USPSAPI($_USPS_USERNAME, $_USPS_PASSWORD, $_USPS_CONTRACTTYPE);
  54. $DATA['destination']['postal_code'] = substr($DATA['destination']['postal_code'], 0, 5);
  55. $usps->setOrigin($DATA['origin']['postal_code']);
  56. $usps->setDestination($DATA['destination']['province'], $DATA['destination']['postal_code'], $DATA['destination']['country'], $isresidential);
  57. $usps_rates = $usps->getRate($packages);
  58.  
  59. /* minimised code - find a copy : 'bespoke_ups_usps_api_table.php' - more readable for developer and has commented out indications on the situations it covers */
  60. if (in_array($actualState, $specialStates)) {
  61. foreach ($DATA['items'] as $item) {
  62. if (in_array($item['product_id'], $limitedCollection)) {
  63. $warningMessage = limitedProductsWarningMessage($item['name'], $statesName[$actualState]);
  64. if ($ups_rates) {
  65. foreach ($ups_rates as $ups_r) {
  66. if ($ups_r['name'] == 'UPS Ground' || $ups_r['name'] == 'UPS Ground') {
  67. $_RATES[] = array(
  68. "service_name" => $ups_r['name'] . $warningMessage,
  69. "service_code" => $ups_r['code'],
  70. "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
  71. "currency"=>"USD",
  72. );
  73. }
  74. }
  75. }
  76. if ($usps_rates) {
  77. foreach ($usps_rates as $usps_r) {
  78. if ($usps_r['name'] == 'Priority Mail 1-Day' || $usps_r['name'] == 'Priority Mail 2-Day' || $usps_r['name'] == 'Priority Mail 3-Day' || $usps_r['name'] == 'USPS Retail Ground') {
  79. $_RATES[] = array(
  80. "service_name" => $usps_r['name'] . $warningMessage,
  81. "service_code" => $usps_r['code'],
  82. "total_price" => $usps_r['amount'] * 100 + discount($usps_r['amount'] * 100, $usps_percentage, $usps_A),
  83. "currency" => "USD",
  84. );
  85. }
  86. }
  87. }
  88. } elseif (in_array($item['product_id'], $bannedCollection)) {
  89. $_RATES = array();
  90.  
  91. return $_RATES;
  92. }
  93. }
  94. foreach ($DATA['items'] as $item) {
  95. if ((!in_array($item['product_id'], $bannedCollection)) && (!in_array($item['product_id'], $limitedCollection))) {
  96. if ($ups_rates) {
  97. foreach ($ups_rates as $ups_r) {
  98. if ($ups_r['name'] == 'UPS Ground') {
  99. $_RATES[] = array(
  100. "service_name" => $ups_r['name'],
  101. "service_code" => $ups_r['code'],
  102. "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
  103. "currency" => "USD",
  104. );
  105. }
  106. }
  107. }
  108. if ($usps_rates) {
  109. foreach ($usps_rates as $usps_r) {
  110. if ($usps_r['name'] == 'Priority Mail 1-Day' || $usps_r['name'] == 'Priority Mail 2-Day' || $usps_r['name'] == 'Priority Mail 3-Day' || $usps_r['name'] == 'First-Class Package Service') {
  111. $_RATES[] = array(
  112. "service_name" => $usps_r['name'],
  113. "service_code" => $usps_r['code'],
  114. "total_price" => $usps_r['amount'] * 100 + discount($usps_r['amount'] * 100, $usps_percentage, $usps_A),
  115. "currency" => "USD",
  116. );
  117. }
  118. }
  119. }
  120. }
  121. }
  122. } else {
  123. foreach ($DATA['items'] as $item) {
  124. if ((in_array($item['product_id'], $bannedCollection)) || (in_array($item['product_id'], $limitedCollection))) {
  125. if ($ups_rates) {
  126. foreach ($ups_rates as $ups_r) {
  127. if ($ups_r['name'] == 'UPS Ground') {
  128. $_RATES[] = array(
  129. "service_name" => $ups_r['name'],
  130. "service_code" => $ups_r['code'],
  131. "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
  132. "currency" => "USD",
  133. );
  134. }
  135. }
  136. }
  137. if ($usps_rates) {
  138. foreach ($usps_rates as $usps_r) {
  139. if ($usps_r['name']=='USPS Retail Ground') {
  140. $_RATES[]=array("service_name"=>$usps_r['name'],"service_code"=>$usps_r['code'],"total_price"=>$usps_r['amount']*100+discount($usps_r['amount']*100, $usps_percentage, $usps_A),"currency"=>"USD",);
  141. }
  142. }
  143. }
  144. return $_RATES;
  145. }
  146. }
  147. foreach ($DATA['items'] as $item) {
  148. if ((!in_array($item['product_id'], $bannedCollection))&&(!in_array($item['product_id'], $limitedCollection))) {
  149. if ($ups_rates) {
  150. foreach ($ups_rates as $ups_r) {
  151. if ($ups_r['name'] == 'UPS Ground') {
  152. $_RATES[] = array(
  153. "service_name" => $ups_r['name'],
  154. "service_code" => $ups_r['code'],
  155. "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
  156. "currency" => "USD",
  157. );
  158. }
  159. }
  160. }
  161. if ($usps_rates) {
  162. foreach ($usps_rates as $usps_r) {
  163. if ($usps_r['name'] == 'Priority Mail 1-Day' || $usps_r['name'] == 'Priority Mail 2-Day' || $usps_r['name'] == 'Priority Mail 3-Day' || $usps_r['name'] == 'First-Class Package Service' || $usps_r['name'] == 'USPS Retail Ground') {
  164. $_RATES[] = array(
  165. "service_name" => $usps_r['name'],
  166. "service_code" => $usps_r['code'],
  167. "total_price" => $usps_r['amount'] * 100 + discount($usps_r['amount']*100, $usps_percentage, $usps_A),
  168. "currency" => "USD",
  169. );
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176.  
  177. $_RATES[] = array(
  178. "service_name" => '1337',
  179. "service_code" => 'asdasd',
  180. "total_price" => 420,
  181. "currency" => "USD",
  182. );
  183.  
  184. return $_RATES;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement