Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $_RATES = array();
- $actualState = $DATA['destination']['province'];
- $specialStates = array('AK', 'AS', 'AA', 'AE', 'AP', 'FM', 'GU', 'HI', 'MH', 'MP', 'PW', 'PR', 'VI');
- $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');
- $bannedCollection = getcollection(420582159);
- $limitedCollection = getcollection(420583375);
- function limitedProductsWarningMessage($productName, $state)
- {
- return ' - *WARNING* : We can\'t include CO2 cartridges in your shipment "' . $productName . '" to ' . $state . '.';
- }
- function discount($total_price, $percentage, $A)
- {
- return number_format((($total_price/100) * $percentage), 2) + $A;
- }
- $ups_percentage = 20; //in % of returned shipp cost
- $ups_A = 150; // in cents
- $usps_percentage = 20; //in % of returned shipp cost
- $usps_A = 100; // in cents
- $w = 0;
- foreach ($DATA['items'] as $i) {
- $w += $i['quantity']*$i['grams']/1000;
- }
- $w = $w*2.20462; //in pounds
- $isresidential = true;
- if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') {
- $isresidential = false;
- }
- /* UPS API DETAILS */
- $_UPS_ACCESS = "5D211D4BF15CE548";
- $_UPS_USERID = "evan.rege";
- $_UPS_PASSWD = "P!314156s";
- $_UPS_ACCOUNT = "X76Y68"; //set to null if you DON'T want to use negotiated rates
- /* USPS API DETAILS */
- $_USPS_USERNAME = '304GROWL4238';
- $_USPS_PASSWORD = '036YO42LN442';
- $_USPS_CONTRACTTYPE = 'C'; //N=none(retail), C=commercial, P=commercial plus
- $packages = array(array('weight'=>$w, 'length'=>5, 'width'=>5, 'height'=>5));
- $ups = new UPSAPI($_UPS_USERID, $_UPS_PASSWD, $_UPS_ACCESS, $_UPS_ACCOUNT);
- $ups->setOrigin($DATA['origin']['city'], $DATA['origin']['province'], $DATA['origin']['postal_code'], $DATA['origin']['country']);
- $ups->setDestination($DATA['destination']['city'], $DATA['destination']['province'], $DATA['destination']['postal_code'], $DATA['destination']['country'], $isresidential);
- $ups_rates = $ups->getRate($packages, 'IMPERIAL');
- $usps = new USPSAPI($_USPS_USERNAME, $_USPS_PASSWORD, $_USPS_CONTRACTTYPE);
- $DATA['destination']['postal_code'] = substr($DATA['destination']['postal_code'], 0, 5);
- $usps->setOrigin($DATA['origin']['postal_code']);
- $usps->setDestination($DATA['destination']['province'], $DATA['destination']['postal_code'], $DATA['destination']['country'], $isresidential);
- $usps_rates = $usps->getRate($packages);
- /* 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 */
- if (in_array($actualState, $specialStates)) {
- foreach ($DATA['items'] as $item) {
- if (in_array($item['product_id'], $limitedCollection)) {
- $warningMessage = limitedProductsWarningMessage($item['name'], $statesName[$actualState]);
- if ($ups_rates) {
- foreach ($ups_rates as $ups_r) {
- if ($ups_r['name'] == 'UPS Ground' || $ups_r['name'] == 'UPS Ground') {
- $_RATES[] = array(
- "service_name" => $ups_r['name'] . $warningMessage,
- "service_code" => $ups_r['code'],
- "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
- "currency"=>"USD",
- );
- }
- }
- }
- if ($usps_rates) {
- foreach ($usps_rates as $usps_r) {
- 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') {
- $_RATES[] = array(
- "service_name" => $usps_r['name'] . $warningMessage,
- "service_code" => $usps_r['code'],
- "total_price" => $usps_r['amount'] * 100 + discount($usps_r['amount'] * 100, $usps_percentage, $usps_A),
- "currency" => "USD",
- );
- }
- }
- }
- } elseif (in_array($item['product_id'], $bannedCollection)) {
- $_RATES = array();
- return $_RATES;
- }
- }
- foreach ($DATA['items'] as $item) {
- if ((!in_array($item['product_id'], $bannedCollection)) && (!in_array($item['product_id'], $limitedCollection))) {
- if ($ups_rates) {
- foreach ($ups_rates as $ups_r) {
- if ($ups_r['name'] == 'UPS Ground') {
- $_RATES[] = array(
- "service_name" => $ups_r['name'],
- "service_code" => $ups_r['code'],
- "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
- "currency" => "USD",
- );
- }
- }
- }
- if ($usps_rates) {
- foreach ($usps_rates as $usps_r) {
- 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') {
- $_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",
- );
- }
- }
- }
- }
- }
- } else {
- foreach ($DATA['items'] as $item) {
- if ((in_array($item['product_id'], $bannedCollection)) || (in_array($item['product_id'], $limitedCollection))) {
- if ($ups_rates) {
- foreach ($ups_rates as $ups_r) {
- if ($ups_r['name'] == 'UPS Ground') {
- $_RATES[] = array(
- "service_name" => $ups_r['name'],
- "service_code" => $ups_r['code'],
- "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
- "currency" => "USD",
- );
- }
- }
- }
- if ($usps_rates) {
- foreach ($usps_rates as $usps_r) {
- if ($usps_r['name']=='USPS Retail Ground') {
- $_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",);
- }
- }
- }
- return $_RATES;
- }
- }
- foreach ($DATA['items'] as $item) {
- if ((!in_array($item['product_id'], $bannedCollection))&&(!in_array($item['product_id'], $limitedCollection))) {
- if ($ups_rates) {
- foreach ($ups_rates as $ups_r) {
- if ($ups_r['name'] == 'UPS Ground') {
- $_RATES[] = array(
- "service_name" => $ups_r['name'],
- "service_code" => $ups_r['code'],
- "total_price" => $ups_r['amount'] * 100 + discount($ups_r['amount'] * 100, $ups_percentage, $ups_A),
- "currency" => "USD",
- );
- }
- }
- }
- if ($usps_rates) {
- foreach ($usps_rates as $usps_r) {
- 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') {
- $_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",
- );
- }
- }
- }
- }
- }
- }
- $_RATES[] = array(
- "service_name" => '1337',
- "service_code" => 'asdasd',
- "total_price" => 420,
- "currency" => "USD",
- );
- return $_RATES;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement