function fn_get_can_rates($code, $weight_data, $location, &$auth, $shipping_settings, $package_info, $origination) { static $cached_rates = array(); if ($shipping_settings['can_enabled'] != 'Y') { return false; } $cached_rate_id = fn_generate_cached_rate_id($weight_data, $origination); if (!empty($cached_rates[$cached_rate_id])) { if (!empty($cached_rates[$cached_rate_id][$code])) { return array('cost' => $cached_rates[$cached_rate_id][$code]); } else { return false; } } $merchant_id = $shipping_settings['can']['merchant_id']; $length = $shipping_settings['can']['length']; $width = $shipping_settings['can']['width']; $height = $shipping_settings['can']['height']; $origination_postal = $origination['zipcode']; $destination_postal = $location['zipcode']; $destination_country = $location['country']; $destination_city = $location['city']; $destination_state = $location['state']; $total_cost = $package_info['C']; $weight = $weight_data['full_pounds'] * 0.4536; $amount = '1'; $lang = CART_LANGUAGE == 'FR' ? 'fr' : 'en'; $request[] = <<<XML <?xml version="1.0" ?> <eparcel> \t<language>{$lang}</language> \t<ratesAndServicesRequest> \t\t<merchantCPCID>{$merchant_id}</merchantCPCID> \t\t<fromPostalCode>{$origination_postal}</fromPostalCode> \t\t<turnAroundTime> 24 </turnAroundTime> \t\t<itemsPrice>{$total_cost}</itemsPrice> \t\t<lineItems> \t\t\t<item> \t\t\t\t<quantity>{$amount}</quantity> \t\t\t\t<weight>{$weight}</weight> \t\t\t\t<length>{$length}</length> \t\t\t\t<width>{$width}</width> \t\t\t\t<height>{$height}</height> \t\t\t\t<description>ggrtye</description> \t\t\t\t<readyToShip/> \t\t\t</item> \t\t</lineItems> \t\t<city>{$destination_city}</city> \t\t<provOrState>{$destination_state}</provOrState> \t\t<country>{$destination_country}</country> \t\t<postalCode>{$destination_postal}</postalCode> \t</ratesAndServicesRequest> </eparcel> XML; list($header, $result) = fn_http_request('POST', 'http://sellonline.canadapost.ca:30000', $request); $rates = fn_can_get_rates($result); if (empty($cached_rates[$cached_rate_id]) && !empty($rates['cost'])) { $cached_rates[$cached_rate_id] = $rates['cost']; } if (!empty($rates['cost'][$code])) { return array('cost' => $rates['cost'][$code]); } else { if (defined('SHIPPING_DEBUG')) { return array('error' => !empty($rates['error']) ? $rates['error'] : fn_get_lang_var('service_not_available')); } } return false; }
function fn_get_ups_rates($code, $weight_data, $location, &$auth, $shipping_settings, $package_info, $origination) { static $cached_rates = array(); if ($shipping_settings['ups_enabled'] != 'Y') { return false; } $cached_rate_id = fn_generate_cached_rate_id($weight_data, $origination); if (!empty($cached_rates[$cached_rate_id])) { if (!empty($cached_rates[$cached_rate_id][$code])) { return array('cost' => $cached_rates[$cached_rate_id][$code]); } else { return false; } } if ($shipping_settings['ups']['test_mode'] == 'Y') { $url = "https://wwwcie.ups.com:443/ups.app/xml/Rate"; } else { $url = "https://www.ups.com:443/ups.app/xml/Rate"; } // Prepare data for UPS request $username = $shipping_settings['ups']['username']; $password = $shipping_settings['ups']['password']; $access_key = $shipping_settings['ups']['access_key']; $origination_postal = $origination['zipcode']; $origination_country = $origination['country']; $height = $shipping_settings['ups']['height']; $width = $shipping_settings['ups']['width']; $length = $shipping_settings['ups']['length']; $pickup_type = $shipping_settings['ups']['pickup_type']; $package_type = $shipping_settings['ups']['package_type']; $destination_postal = $location['zipcode']; $destination_country = $location['country']; // define weight unit and value $weight = $weight_data['full_pounds']; if (in_array($origination_country, array('US', 'DO', 'PR'))) { $weight_unit = 'LBS'; $measure_unit = 'IN'; } else { $weight_unit = 'KGS'; $measure_unit = 'CM'; $weight = $weight * 0.4536; } $customer_classification = ''; if ($origination_country == 'US' && $pickup_type == '11') { $customer_classification = <<<EOT \t<CustomerClassification> \t\t<Code>04</Code> \t</CustomerClassification> EOT; } $request = <<<EOT <?xml version="1.0"?> <AccessRequest xml:lang="en-US"> \t<AccessLicenseNumber>{$access_key}</AccessLicenseNumber> \t\t<UserId>{$username}</UserId> \t\t<Password>{$password}</Password> </AccessRequest> <?xml version="1.0"?> <RatingServiceSelectionRequest xml:lang='en-US'> <Request> \t<TransactionReference> \t <CustomerContext>Rate Request</CustomerContext> \t <XpciVersion>1.0</XpciVersion> \t</TransactionReference> \t<RequestAction>Rate</RequestAction> \t<RequestOption>shop</RequestOption> </Request> \t<PickupType> \t<Code>{$pickup_type}</Code> </PickupType> {$customer_classification} <Shipment> \t<Shipper> \t\t<Address> \t\t\t<PostalCode>{$destination_postal}</PostalCode> \t\t\t<CountryCode>{$destination_country}</CountryCode> \t\t</Address> \t</Shipper>\t \t<ShipTo> \t\t<Address> \t\t\t<PostalCode>{$destination_postal}</PostalCode> \t\t\t<CountryCode>{$destination_country}</CountryCode> \t\t\t<ResidentialAddressIndicator/> \t\t</Address> \t</ShipTo> \t<ShipFrom> \t\t<Address> \t\t\t<PostalCode>{$origination_postal}</PostalCode> \t\t\t<CountryCode>{$origination_country}</CountryCode> \t\t</Address> \t</ShipFrom> \t<Package> \t\t<PackagingType> \t\t\t<Code>{$package_type}</Code> \t\t</PackagingType> \t\t\t<Dimensions> \t\t\t\t<UnitOfMeasurement> \t\t\t\t <Code>{$measure_unit}</Code> \t\t\t\t</UnitOfMeasurement> \t\t\t\t<Length>{$length}</Length> \t\t\t\t<Width>{$width}</Width> \t\t\t\t<Height>{$height}</Height> \t\t\t</Dimensions> \t\t<PackageWeight> \t\t\t<UnitOfMeasurement> \t\t\t\t <Code>{$weight_unit}</Code> \t\t\t</UnitOfMeasurement> \t\t\t<Weight>{$weight}</Weight> \t\t</PackageWeight> \t</Package> </Shipment> </RatingServiceSelectionRequest> EOT; $post = explode("\n", $request); list($header, $result) = fn_https_request('POST', $url, $post, '', '', 'text/xml'); $rates = fn_ups_get_rates($result); if (empty($cached_rates[$cached_rate_id]) && !empty($rates)) { $cached_rates[$cached_rate_id] = $rates; } if (!empty($rates[$code])) { return array('cost' => $rates[$code]); } else { if (defined('SHIPPING_DEBUG')) { return array('error' => fn_ups_get_error($result)); } } return false; }
function fn_get_usps_rates($code, $weight_data, $location, &$auth, $shipping_settings, $package_info, $origination) { static $cached_rates = array(); if ($shipping_settings['usps_enabled'] != 'Y') { return false; } $cached_rate_id = fn_generate_cached_rate_id($weight_data, $origination); if (!empty($cached_rates[$cached_rate_id])) { if (!empty($cached_rates[$cached_rate_id][$code])) { return array('cost' => $cached_rates[$cached_rate_id][$code]); } else { return false; } } if ($shipping_settings['usps']['test_mode'] == 'Y') { $url = 'http://testing.shippingapis.com/ShippingAPI.dll'; } else { $url = 'http://production.shippingapis.com/ShippingAPI.dll'; } $username = $shipping_settings['usps']['username']; $machinable = $shipping_settings['usps']['machinable']; $container_priority = $shipping_settings['usps']['container_priority']; $container_express = $shipping_settings['usps']['container_express']; $mailtype = $shipping_settings['usps']['mailtype']; $package_size = $shipping_settings['usps']['package_size']; $first_class_mail_type = $shipping_settings['usps']['first_class_mail_type']; $pounds = $weight_data['pounds']; $ounces = $weight_data['ounces']; // The zip code should be in 5 digit format so we cut all digits after "-" $origination_postal = preg_replace('/-\\d*/i', '', trim($origination['zipcode'])); $destination_postal = preg_replace('/-\\d*/i', '', trim($location['zipcode'])); $origination_country = $origination['country']; $destination_country = $location['country']; $size_parameters = ''; if ($package_size == 'Large') { $_width = $shipping_settings['usps']['priority_width']; $_length = $shipping_settings['usps']['priority_length']; $_height = $shipping_settings['usps']['priority_height']; $size_parameters = <<<EOT \t\t\t<Width>{$_width}</Width> \t\t\t<Length>{$_length}</Length> \t\t\t<Height>{$_height}</Height> EOT; if ($container_priority == 'NonRectangular') { $_priority_girth = $shipping_settings['usps']['priority_girth']; $size_parameters .= "<Girth>{$_priority_girth}</Girth>"; } } $us_dependent_territories = array('AS', 'VI', 'PR', 'GU', 'MP', 'FM', 'MH', 'PW'); if (in_array($destination_country, $us_dependent_territories)) { $destination_country = 'US'; } if ($origination_country == $destination_country) { // Domestic rate calculation $query = <<<EOT \t\t<RateV3Request USERID="{$username}"> \t\t <Package ID="0"> \t\t\t<Service>EXPRESS</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Container>{$container_express}</Container> \t\t\t<Size>{$package_size}</Size> \t\t </Package> \t\t <Package ID="1"> \t\t\t<Service>FIRST CLASS</Service> \t\t\t<FirstClassMailType>{$first_class_mail_type}</FirstClassMailType> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Size>{$package_size}</Size> \t\t\t<Machinable>{$machinable}</Machinable> \t\t </Package> \t\t <Package ID="2"> \t\t\t<Service>PRIORITY</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Container>{$container_priority}</Container> \t\t\t<Size>{$package_size}</Size> \t\t\t{$size_parameters} \t\t </Package> \t\t <Package ID="3"> \t\t\t<Service>PARCEL</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Size>{$package_size}</Size> \t\t\t<Machinable>{$machinable}</Machinable> \t\t </Package> \t\t <Package ID="4"> \t\t\t<Service>BPM</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Size>{$package_size}</Size> \t\t </Package> \t\t <Package ID="5"> \t\t\t<Service>LIBRARY</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Size>{$package_size}</Size> \t\t </Package> \t\t <Package ID="6"> \t\t\t<Service>MEDIA</Service> \t\t\t<ZipOrigination>{$origination_postal}</ZipOrigination> \t\t\t<ZipDestination>{$destination_postal}</ZipDestination> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<Size>{$package_size}</Size> \t\t </Package> \t\t</RateV3Request> EOT; $get = array('API' => 'RateV3', 'XML' => $query); $is_domestic = true; } else { // International rate calculation $destination_country = fn_get_usps_country($destination_country); if (empty($destination_country)) { return false; } $query = <<<EOT \t\t<IntlRateRequest USERID="{$username}"> \t\t <Package ID="0"> \t\t\t<Pounds>{$pounds}</Pounds> \t\t\t<Ounces>{$ounces}</Ounces> \t\t\t<MailType>{$mailtype}</MailType> \t\t\t<Country>{$destination_country}</Country> \t\t </Package> \t\t</IntlRateRequest> EOT; $get = array('API' => 'IntlRate', 'XML' => $query); $is_domestic = false; } list($header, $result) = fn_http_request('GET', $url, $get); $rates = fn_usps_get_rates($result, $is_domestic); if (empty($cached_rates[$cached_rate_id])) { $cached_rates[$cached_rate_id] = $rates; } if (!empty($rates[$code])) { return array('cost' => $rates[$code]); } else { if (defined('SHIPPING_DEBUG')) { return array('error' => fn_usps_get_error($result)); } } return false; }