コード例 #1
0
ファイル: fedex.php プロジェクト: q0821/esportshop
 function _FEDEXrequestMethods($data, $rate)
 {
     global $fedex_methods;
     $path_to_wsdl = dirname(__FILE__) . DS . 'fedex_rate.wsdl';
     ini_set("soap.wsdl_cache_enabled", "0");
     if (!class_exists('SoapClient')) {
         $app = JFactory::getApplication();
         $app->enqueueMessage('The FEDEX shipping plugin needs the SOAP library installed but it seems that it is not available on your server. Please contact your web hosting to set it up.', 'error');
         return false;
     }
     $client = new SoapClient($path_to_wsdl, array('exceptions' => false));
     $shipment = array();
     foreach ($data['methods'] as $k => $v) {
         $request['WebAuthenticationDetail'] = array('UserCredential' => array('Key' => $data['fedex_api_key'], 'Password' => $data['fedex_api_password']));
         $request['ClientDetail'] = array('AccountNumber' => $data['fedex_account_number'], 'MeterNumber' => $data['fedex_meter_number']);
         $request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Request v10 using PHP ***');
         $request['Version'] = array('ServiceId' => 'crs', 'Major' => '10', 'Intermediate' => '0', 'Minor' => '0');
         $request['ReturnTransitAndCommit'] = true;
         $request['RequestedShipment']['DropoffType'] = 'REGULAR_PICKUP';
         // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
         $request['RequestedShipment']['ShipTimestamp'] = date('c');
         $request['RequestedShipment']['ServiceType'] = $v;
         // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
         $request['RequestedShipment']['PackagingType'] = $data['packaging_type'];
         // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
         $request['RequestedShipment']['TotalInsuredValue'] = array('Ammount' => $data['total_insured'], 'Currency' => 'USD');
         $request['RequestedPackageDetailType'] = 'PACKAGE_SUMMARY';
         $shipper = array('Contact' => array('PersonName' => $data['sender_company'], 'CompanyName' => $data['sender_company'], 'PhoneNumber' => $data['sender_phone']), 'Address' => array('StreetLines' => array($data['sender_address']), 'City' => $data['sender_city'], 'StateOrProvinceCode' => $data['sender_state'], 'PostalCode' => $data['sender_postcode'], 'CountryCode' => $data['country']));
         $recipient_StateOrProvinceCode = '';
         if (isset($data['recipient']->address_state->zone_code_2) && strlen($data['recipient']->address_state->zone_code_2) == 2) {
             $recipient_StateOrProvinceCode = $data['recipient']->address_state->zone_code_2;
         } elseif (strlen($data['recipient']->address_state->zone_code_3) == 2) {
             $recipient_StateOrProvinceCode = $data['recipient']->address_state->zone_code_3;
         }
         $recipient = array('Contact' => array('PersonName' => $data['recipient']->address_title . " " . $data['recipient']->address_firstname . " " . $data['recipient']->address_lastname, 'CompanyName' => $data['recipient']->address_company, 'PhoneNumber' => $data['recipient']->address_telephone), 'Address' => array('StreetLines' => array($data['recipient']->address_street), 'City' => $data['recipient']->address_city, 'StateOrProvinceCode' => $recipient_StateOrProvinceCode, 'PostalCode' => $data['recipient']->address_post_code, 'CountryCode' => $data['recipient']->address_country->zone_code_2, 'Residential' => true));
         if (@$rate->shipping_params->destination_type == 'res') {
             $recipient['Address']['Residential'] = true;
         }
         if (@$rate->shipping_params->destination_type == 'com' || @$rate->shipping_params->destination_type == 'auto' && $v == 'FEDEX_GROUND') {
             $recipient['Address']['Residential'] = false;
         }
         $shippingChargesPayment = array('PaymentType' => 'SENDER', 'Payor' => array('AccountNumber' => $data['fedex_account_number'], 'CountryCode' => $data['country']));
         $pkg_values = $this->xml2array('<root>' . $data['XMLpackage'] . '</root>');
         $pkg_values = $pkg_values['root'];
         $pkg_count = count($pkg_values);
         $request['RequestedShipment']['Shipper'] = $shipper;
         $request['RequestedShipment']['Recipient'] = $recipient;
         $request['RequestedShipment']['ShippingChargesPayment'] = $shippingChargesPayment;
         $request['RequestedShipment']['RateRequestTypes'] = 'ACCOUNT';
         if (empty($rate->shipping_params->rate_types) || $rate->shipping_params->rate_types != 'ACCOUNT') {
             $request['RequestedShipment']['RateRequestTypes'] = 'LIST';
         }
         $request['RequestedShipment']['PackageCount'] = $pkg_count;
         $request['RequestedShipment']['RequestedPackageLineItems'] = $this->addPackageLineItem($pkg_values);
         if (@$rate->shipping_params->debug) {
             echo "<br/> Request {$v} : <br/>";
             echo '<pre>' . var_export($request, true) . '</pre>';
         }
         $response = $client->getRates($request);
         if (isset($response->HighestSeverity) && $response->HighestSeverity == "ERROR") {
             static $notif = false;
             if (!$notif && isset($response->Notifications->Message) && $response->Notifications->Message == 'Authentication Failed') {
                 $app = JFactory::getApplication();
                 $app->enqueueMessage('FEDEX Authentication Failed');
                 $notif = true;
             }
             if (!$notif && !empty($response->Notifications->Message) && strpos($response->Notifications->Message, 'Service is not allowed') === FALSE) {
                 $app = JFactory::getApplication();
                 $app->enqueueMessage('The FedEx request failed with the message : ' . $response->Notifications->Message);
             }
         }
         if (@$rate->shipping_params->debug) {
             echo "<br/> Response {$v} : <br/>";
             echo '<pre>' . var_export($response, true) . '</pre>';
         }
         if (!empty($response->HighestSeverity) && ($response->HighestSeverity == "SUCCESS" || $response->HighestSeverity == "NOTE" || $response->HighestSeverity == "WARNING")) {
             $code = '';
             $notes = array();
             if ($response->HighestSeverity == "NOTE" || $response->HighestSeverity == "WARNING") {
                 $notes = $response->Notifications;
             }
             foreach ($this->fedex_methods as $k => $v) {
                 if ($v['code'] == $response->RateReplyDetails->ServiceType) {
                     $code = $v['code'];
                 }
             }
             $delayType = hikashop_get('type.delay');
             if (!empty($response->RateReplyDetails->DeliveryTimestamp)) {
                 $timestamp = strtotime($response->RateReplyDetails->DeliveryTimestamp);
             } else {
                 $timestamp = 0;
                 $response->RateReplyDetails->DeliveryTimestamp = 0;
             }
             $totalNetPrice = 0;
             $discountAmount = 0;
             if (is_array($response->RateReplyDetails->RatedShipmentDetails)) {
                 $totalNetPrice = $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount;
                 if ($request['RequestedShipment']['RateRequestTypes'] != 'ACCOUNT') {
                     $discountAmount = $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalFreightDiscounts->Amount;
                 }
                 $shipment[] = array('value' => $totalNetPrice + $discountAmount, 'code' => $code, 'delivery_timestamp' => $timestamp, 'day' => $response->RateReplyDetails->DeliveryTimestamp, 'delivery_day' => date("m/d/Y", $timestamp), 'delivery_delay' => parent::displayDelaySECtoDAY($timestamp - strtotime('now'), 2), 'delivery_time' => date("H:i:s", $timestamp), 'currency_code' => $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Currency, 'old_currency_code' => $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Currency, 'notes' => $notes);
             } else {
                 if (is_object($response->RateReplyDetails->RatedShipmentDetails)) {
                     $totalNetPrice = $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount;
                     if ($request['RequestedShipment']['RateRequestTypes'] != 'ACCOUNT') {
                         $discountAmount = $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalFreightDiscounts->Amount;
                     }
                     $shipment[] = array('value' => $totalNetPrice + $discountAmount, 'code' => $code, 'delivery_timestamp' => $timestamp, 'day' => $response->RateReplyDetails->DeliveryTimestamp, 'delivery_day' => date("m/d/Y", $timestamp), 'delivery_delay' => parent::displayDelaySECtoDAY($timestamp - strtotime('now'), 2), 'delivery_time' => date("H:i:s", $timestamp), 'currency_code' => $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency, 'old_currency_code' => $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency, 'notes' => $notes);
                 }
             }
         } else {
             if (!empty($response->HighestSeverity) && $response->HighestSeverity == "ERROR") {
                 static $errorsDisplayed = array();
                 $acceptedCodes = array(836);
                 if (!empty($response->Notifications)) {
                     foreach ($response->Notifications as $notif) {
                         if (!is_object($notif)) {
                             continue;
                         }
                         $errorCode = $notif->Code;
                         if (!in_array($errorCode, $acceptedCodes)) {
                             if (!isset($errorsDisplayed[$errorCode])) {
                                 $app = JFactory::getApplication();
                                 $app->enqueueMessage($notif->Message);
                             }
                             $errorsDisplayed[$errorCode] = true;
                         }
                     }
                 }
             }
         }
     }
     return $shipment;
 }
コード例 #2
0
ファイル: canpar.php プロジェクト: rodhoff/MNW
 function onShippingDisplay(&$order, &$dbrates, &$usable_rates, &$messages)
 {
     if (!hikashop_loadUser()) {
         return false;
     }
     $local_usable_rates = array();
     $local_messages = array();
     $ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
     if ($ret === false) {
         return false;
     }
     $currentShippingZone = null;
     $currentCurrencyId = null;
     $found = true;
     $usableWarehouses = array();
     $zoneClass = hikashop_get('class.zone');
     $zones = $zoneClass->getOrderZones($order);
     foreach ($local_usable_rates as $k => $rate) {
         if (!empty($rate->shipping_params->warehousesList)) {
             $rate->shipping_params->warehouses = unserialize($rate->shipping_params->warehousesList);
         } else {
             $messages['no_warehouse_configured'] = 'No warehouse configured in the CANPAR shipping plugin options';
             continue;
         }
         foreach ($rate->shipping_params->warehouses as $warehouse) {
             if (empty($warehouse->zone) || $warehouse->zone == '-' || in_array($warehouse->zone, $zones)) {
                 $usableWarehouses[] = $warehouse;
             }
         }
         if (empty($usableWarehouses)) {
             $messages['no_warehouse_configured'] = 'No available warehouse found for your location';
             continue;
         }
         if (!empty($rate->shipping_params->methodsList)) {
             $rate->shipping_params->methods = unserialize($rate->shipping_params->methodsList);
         } else {
             $messages['no_shipping_methods_configured'] = 'No shipping methods configured in the CANPAR shipping plugin options';
             continue;
         }
         if ($order->weight <= 0 || $order->volume <= 0) {
             return true;
         }
         $data = null;
         if (empty($order->shipping_address)) {
             $messages['no_shipping_address_found'] = 'No shipping address entered';
             continue;
         }
         $this->shipping_currency_id = hikashop_getCurrency();
         $db = JFactory::getDBO();
         $query = 'SELECT currency_code FROM ' . hikashop_table('currency') . ' WHERE currency_id IN (' . $this->shipping_currency_id . ')';
         $db->setQuery($query);
         $this->shipping_currency_code = $db->loadResult();
         $cart = hikashop_get('class.cart');
         $null = null;
         $cart->loadAddress($null, $order->shipping_address->address_id, 'object', 'shipping');
         $currency = hikashop_get('class.currency');
         $receivedMethods = $this->_getBestMethods($rate, $order, $usableWarehouses, $null);
         if (empty($receivedMethods)) {
             $messages['no_rates'] = JText::_('NO_SHIPPING_METHOD_FOUND');
             continue;
         }
         $i = 0;
         $local_usable_rates = array();
         foreach ($receivedMethods as $method) {
             $local_usable_rates[$i] = !HIKASHOP_PHP5 ? $rate : clone $rate;
             $local_usable_rates[$i]->shipping_price += $method['value'];
             $selected_method = '';
             $name = '';
             foreach ($this->canpar_methods as $canpar_method) {
                 if ($canpar_method['name'] == $method['name']) {
                     $name = $canpar_method['name'];
                     $selected_method = $canpar_method['key'];
                 }
             }
             $local_usable_rates[$i]->shipping_name = $name;
             if (!empty($selected_method)) {
                 $local_usable_rates[$i]->shipping_id .= '-' . $selected_method;
             }
             if ($method['deliveryDate'] != 'www.canpar.ca') {
                 if (is_numeric($method['deliveryDate'])) {
                     $timestamp = strtotime($method['deliveryDate']);
                     $time = parent::displayDelaySECtoDAY($timestamp - strtotime('now'), 2);
                     $local_usable_rates[$i]->shipping_description .= 'Estimated delivery date:  ' . $time;
                 } else {
                     $time = $method['deliveryDate'];
                     $local_usable_rates[$i]->shipping_description .= 'Estimated delivery date:  ' . $time;
                 }
             } else {
                 $local_usable_rates[$i]->shipping_description .= ' ' . JText::_('NO_ESTIMATED_TIME_AFTER_SEND');
             }
             if ($rate->shipping_params->group_package == 1 && $this->nbpackage > 1) {
                 $local_usable_rates[$i]->shipping_description .= '<br/>' . JText::sprintf('X_PACKAGES', $this->nbpackage);
             }
             $i++;
         }
         foreach ($local_usable_rates as $i => $rate) {
             $usable_rates[$rate->shipping_id] = $rate;
         }
     }
 }