示例#1
0
文件: dhl.php 项目: noikiy/owaspbwa
 function generate_label($order_id)
 {
     global $vmLogger;
     global $VM_LANG, $mosConfig_absolute_path;
     /* Retrieve label information from database */
     $dbl = new ps_DB();
     $q = "SELECT order_id, ship_date, service_code, special_service, ";
     $q .= "package_type, order_weight, is_international, ";
     $q .= "additional_protection_type, additional_protection_value, ";
     $q .= "duty_value, content_desc, label_is_generated ";
     $q .= "FROM #__{vm}_shipping_label ";
     $q .= "WHERE order_id = '" . $order_id . "'";
     $dbl->query($q);
     if (!$dbl->next_record()) {
         $vmLogger->err("couldn't find label info for order #" . $order_id);
         return false;
     }
     if ($dbl->f('order_weight') == 0) {
         return false;
     }
     /* If the label has already been generated, we're done */
     if ($dbl->f('label_is_generated')) {
         return true;
     }
     /* get customer shipping information */
     $db = new ps_DB();
     $q = "SELECT first_name,last_name,address_1,address_2,";
     $q .= "city,state,country,zip,phone_1,user_email,country_2_code ";
     $q .= "FROM #__{vm}_order_user_info, #__{vm}_country ";
     $q .= "WHERE order_id = '" . $order_id . "' ";
     $q .= "AND (country=country_2_code OR country=country_3_code)";
     $q .= "AND address_type='ST'";
     $db->query($q);
     if (!$db->next_record()) {
         /* If we can't find a ship-to address use bill-to */
         $q = "SELECT first_name,last_name,address_1,address_2,";
         $q .= "city,state,country,zip,phone_1,user_email,country_2_code ";
         $q .= "FROM #__{vm}_order_user_info, #__{vm}_country ";
         $q .= "WHERE order_id = '" . $order_id . "' ";
         $q .= "AND (country=country_2_code OR country=country_3_code)";
         $q .= "AND address_type='BT'";
         $db->query($q);
         if (!$db->next_record()) {
             $vmLogger->err("user info for order #" . $order_id . " not found");
             return false;
         }
     }
     $dest_country = $db->f("country_2_code");
     $dest_state = $db->f("state");
     /* Read current Configuration */
     require_once CLASSPATH . "shipping/" . __CLASS__ . ".cfg.php";
     $dhl_url = "https://eCommerce.airborne.com/";
     if (DHL_TEST_MODE == 'TRUE') {
         $dhl_url .= "ApiLandingTest.asp";
     } else {
         $dhl_url .= "ApiLanding.asp";
     }
     if (!$dbl->f('is_international')) {
         $shipping_key = DHL_DOMESTIC_SHIPPING_KEY;
     } else {
         /*
          * XXX
          * We should really walk through the list of each product in
          * the order and check for special "harmonizing descriptions"
          * to build our $content_desc variables.
          */
         $content_desc = DHL_CONTENT_DESC;
         $shipping_key = DHL_INTERNATIONAL_SHIPPING_KEY;
         /* DHL country codes are non-standard, remap them */
         $dest_country = $this->remap_country_code($dest_country, $dest_state);
     }
     /* Get our sending address information */
     $vq = "SELECT * FROM #__{vm}_vendor ";
     $vq .= "WHERE vendor_id='" . $_SESSION['ps_vendor_id'] . "'";
     $dbv = new ps_DB();
     $dbv->query($vq);
     $dbv->next_record();
     require_once $mosConfig_absolute_path . '/includes/domit/xml_domit_lite_include.php';
     $xmlReq = new DOMIT_Lite_Document();
     $xmlReq->setXMLDeclaration('<?xml version="1.0"?>');
     $root =& $xmlReq->createElement('eCommerce');
     $root->setAttribute('action', 'Request');
     $root->setAttribute('version', '1.1');
     $xmlReq->setDocumentElement($root);
     $requestor =& $xmlReq->createElement('Requestor');
     $id =& $xmlReq->createElement('ID');
     $id->setText(DHL_ID);
     $requestor->appendChild($id);
     $password =& $xmlReq->createElement('Password');
     $password->setText(DHL_PASSWORD);
     $requestor->appendChild($password);
     $root->appendChild($requestor);
     /* International Rate Estimate Request */
     if ($dbl->f('is_international')) {
         $shipment =& $xmlReq->createElement('IntlShipment');
     } else {
         $shipment =& $xmlReq->createElement('Shipment');
     }
     $shipment->setAttribute('action', 'GenerateLabel');
     $shipment->setAttribute('version', '1.0');
     $creds =& $xmlReq->createElement('ShippingCredentials');
     $ship_key =& $xmlReq->createElement('ShippingKey');
     $ship_key->setText($shipping_key);
     $creds->appendChild($ship_key);
     $an =& $xmlReq->createElement('AccountNbr');
     $an->setText(DHL_ACCOUNT_NUMBER);
     $creds->appendChild($an);
     $shipment->appendChild($creds);
     $detail =& $xmlReq->createElement('ShipmentDetail');
     $date =& $xmlReq->createElement('ShipDate');
     $date->setText(date($dbl->f('ship_date')));
     $detail->appendChild($date);
     $service =& $xmlReq->createElement('Service');
     $code =& $xmlReq->createElement('Code');
     $code->setText($dbl->f('service_code'));
     $service->appendChild($code);
     $detail->appendChild($service);
     $stype =& $xmlReq->createElement('ShipmentType');
     $code =& $xmlReq->createElement('Code');
     $code->setText($dbl->f('package_type'));
     $stype->appendChild($code);
     if ($dbl->f('additional_protection_value') > 0 && $dbl->f('additional_protection_type') != 'NR') {
         /* include additional value protection */
         $addl_prot =& $xmlReq->createElement('AdditionalProtection');
         $code =& $xmlReq->createElement('Code');
         $code->setText($dbl->f('additional_protection_type'));
         $addl_prot->appendChild($code);
         $value =& $xmlReq->createElement('Value');
         $value->setText(round($dbl->f('additional_protection_value'), 0));
         $addl_prot->appendChild($value);
         $detail->appendChild($addl_prot);
     }
     $detail->appendChild($stype);
     if ($dbl->f('is_international')) {
         $desc =& $xmlReq->createElement('ContentDesc');
         /* CDATA description */
         $desc_text =& $xmlReq->createCDATASection($dbl->f('content_desc'));
         $desc->appendChild($desc_text);
         $detail->appendChild($desc);
     }
     $weight =& $xmlReq->createElement('Weight');
     $weight->setText(round($dbl->f('order_weight'), 0));
     $detail->appendChild($weight);
     if ($dbl->f('special_service') != '') {
         $sservices =& $xmlReq->createElement('SpecialServices');
         $service =& $xmlReq->createElement('SpecialService');
         $code =& $xmlReq->createElement('Code');
         $code->setText($dbl->f('special_service'));
         $service->appendChild($code);
         $sservices->appendChild($service);
         $detail->appendChild($sservices);
     }
     $shipment->appendChild($detail);
     if ($dbl->f('is_international')) {
         $dutiable =& $xmlReq->createElement('Dutiable');
         $dflag =& $xmlReq->createElement('DutiableFlag');
         if ($dbl->f('duty_value') == 0) {
             $dflag->setText('N');
             $dutiable->appendChild($dflag);
         } else {
             $dflag->setText('Y');
             $dutiable->appendChild($dflag);
             $dval =& $xmlReq->createElement('CustomsValue');
             $dval->setText(round($dbl->f('duty_value'), 0));
             $dutiable->appendChild($dval);
             /*
              * This should probably be a configuration item,
              * but I'm harding coding it to 'N'.  The question
              * being asked is: Is a Shipment Export Declaration (SED)
              * required?
              * N - If an Export Declaration is to be created only
              *     when required by value or Self Filing details
              *     in the form of XTN number is provided.
              * Y - If an Export Declaration is to be created
              *     even when not required by value.
              * X - I will file SED information for this shipment
              *     electronically. ?need to provide XTN number.
              */
             $sed =& $xmlReq->createElement('IsSEDReqd');
             $sed->setText('N');
             $dutiable->appendChild($sed);
         }
         $shipment->appendChild($dutiable);
     }
     $billing =& $xmlReq->createElement('Billing');
     $party =& $xmlReq->createElement('Party');
     $code =& $xmlReq->createElement('Code');
     /* Always bill shipper */
     $code->setText('S');
     $party->appendChild($code);
     $billing->appendChild($party);
     if ($dbl->f('is_international')) {
         $duty_payer =& $xmlReq->createElement('DutyPaymentType');
         /* receiver pays duties */
         $duty_payer->setText('R');
         $billing->appendChild($duty_payer);
     }
     $shipment->appendChild($billing);
     $send =& $xmlReq->createElement('Sender');
     if ($dbl->f('is_international')) {
         /* International shipments require shipper address */
         $addr =& $xmlReq->createElement('Address');
         $name =& $xmlReq->createElement('CompanyName');
         $name_cdata =& $xmlReq->createCDATASection($dbv->f('vendor_name'));
         $name->appendChild($name_cdata);
         $addr->appendChild($name);
         $street =& $xmlReq->createElement('Street');
         $street_addr =& $xmlReq->createCDATASection($dbv->f('vendor_address_1') . "\n" . $dbv->f('vendor_address_2'));
         $street->appendChild($street_addr);
         $addr->appendChild($street);
         $city =& $xmlReq->createElement('City');
         $city_name =& $xmlReq->createCDATASection($dbv->f('vendor_city'));
         $city->appendChild($city_name);
         $addr->appendChild($city);
         $state =& $xmlReq->createElement('State');
         $state->setText($dbv->f('vendor_state'));
         $addr->appendChild($state);
         /*
          * DHL's XML API currently only supports international
          * shipping from the US.
          */
         $country =& $xmlReq->createElement('Country');
         $country->setText('US');
         $addr->appendChild($country);
         $pc =& $xmlReq->createElement('PostalCode');
         $pc->setText($dbv->f('vendor_zip'));
         $addr->appendChild($pc);
         $send->appendChild($addr);
         $email =& $xmlReq->createElement('Email');
         $email_cdata =& $xmlReq->createCDATASection($dbv->f('contact_email'));
         $email->appendChild($email_cdata);
         $send->appendChild($email);
     }
     $sent_by =& $xmlReq->createElement('SentBy');
     $sent_by_cdata =& $xmlReq->createCDATASection($dbv->f('contact_first_name') . " " . $dbv->f('contact_last_name'));
     $sent_by->appendChild($sent_by_cdata);
     $send->appendChild($sent_by);
     $pn =& $xmlReq->createElement('PhoneNbr');
     $pn->setText($dbv->f('contact_phone_1'));
     $send->appendChild($pn);
     $shipment->appendChild($send);
     $recv =& $xmlReq->createElement('Receiver');
     $addr =& $xmlReq->createElement('Address');
     $name =& $xmlReq->createElement('CompanyName');
     $name_cdata =& $xmlReq->createCDATASection($db->f('first_name') . " " . $db->f('last_name'));
     $name->appendChild($name_cdata);
     $addr->appendChild($name);
     $street =& $xmlReq->createElement('Street');
     $street_addr =& $xmlReq->createCDATASection($db->f('address_1'));
     $street->appendChild($street_addr);
     $addr->appendChild($street);
     $street2 =& $xmlReq->createElement('StreetLine2');
     $street2_addr =& $xmlReq->createCDATASection($db->f('address_2'));
     $street2->appendChild($street2_addr);
     $addr->appendChild($street2);
     $city =& $xmlReq->createElement('City');
     $city_name =& $xmlReq->createCDATASection($db->f('city'));
     $city->appendChild($city_name);
     $addr->appendChild($city);
     if ($db->f('state') != '') {
         $state =& $xmlReq->createElement('State');
         $state->setText($db->f('state'));
         $addr->appendChild($state);
     }
     $country =& $xmlReq->createElement('Country');
     $country->setText($dest_country);
     $addr->appendChild($country);
     if ($db->f('zip') != '') {
         $pc =& $xmlReq->createElement('PostalCode');
         $pc->setText($db->f('zip'));
         $addr->appendChild($pc);
     }
     $recv->appendChild($addr);
     $attn =& $xmlReq->createElement('AttnTo');
     $attn_cdata =& $xmlReq->createCDATASection($db->f('first_name') . " " . $db->f('last_name'));
     $attn->appendChild($attn_cdata);
     $recv->appendChild($attn);
     $pn =& $xmlReq->createElement('PhoneNbr');
     $pn->setText($db->f('phone_1'));
     $recv->appendChild($pn);
     $shipment->appendChild($recv);
     /* label image type */
     $inst =& $xmlReq->createElement('ShipmentProcessingInstructions');
     $label =& $xmlReq->createElement('Label');
     $itype =& $xmlReq->createElement('ImageType');
     /* GIF is the alternative */
     $itype->setText('PNG');
     $label->appendChild($itype);
     $inst->appendChild($label);
     $shipment->appendChild($inst);
     /* Have DHL notify the receiver */
     $notification =& $xmlReq->createElement('Notification');
     $notify =& $xmlReq->createElement('Notify');
     $email =& $xmlReq->createElement('EmailAddress');
     $email->setText($db->f('user_email'));
     $notify->appendChild($email);
     $notification->appendChild($notify);
     $shipment->appendChild($notification);
     $root->appendChild($shipment);
     //		$vmLogger->err($xmlReq->toNormalizedString());
     if (function_exists("curl_init")) {
         $CR = curl_init();
         curl_setopt($CR, CURLOPT_URL, $dhl_url);
         curl_setopt($CR, CURLOPT_POST, 1);
         curl_setopt($CR, CURLOPT_FAILONERROR, true);
         curl_setopt($CR, CURLOPT_POSTFIELDS, $xmlReq->toString());
         curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
         $xmlResult = curl_exec($CR);
         $error = curl_error($CR);
         if (!empty($error)) {
             $vmLogger->err(curl_error($CR));
             return false;
         }
         curl_close($CR);
     }
     // XML Parsing
     $xmlResp = new DOMIT_Lite_Document();
     if (!$xmlResp->parseXML($xmlResult, false, true)) {
         echo $VM_LANG->_('PHPSHOP_SHIPPING_METHOD_DHL_INVALID_XML') . $xmlResult;
         return false;
     }
     //		$vmLogger->err($xmlResp->toNormalizedString());
     // Check for success or failure.
     $result_code_list =& $xmlResp->getElementsByPath('//Result/Code');
     $result_code =& $result_code_list->item(0);
     $result_desc_list =& $xmlResp->getElementsByPath('//Result/Desc');
     $result_desc =& $result_desc_list->item(0);
     if ($result_code == NULL) {
         $vmLogger->debug($VM_LANG->_('PHPSHOP_SHIPPING_METHOD_DHL_MISSING_RESULT') . "\n" . $xmlResp->toNormalizedString());
         return false;
     }
     /* '100' is the code for success with a generated label. */
     if ($result_code->getText() != '100') {
         $err_msg = '<br /><span class="message">' . $result_desc->getText() . ' [code ' . $result_code->getText() . ']' . '</span>';
         // display an error line for each fault
         $fault_node_list =& $xmlResp->getElementsByPath('//Faults/Fault');
         if ($fault_node_list->getLength() > 0) {
             $err_msg .= '<ul>';
         }
         for ($i = 0; $i < $fault_node_list->getLength(); $i++) {
             $fault_node =& $fault_node_list->item($i);
             $fault_code_node_list =& $fault_node->getElementsByTagName('Code');
             $fault_desc_node_list =& $fault_node->getElementsByTagName('Desc');
             $fault_code_node =& $fault_code_node_list->item(0);
             $fault_desc_node =& $fault_desc_node_list->item(0);
             $err_msg .= '<li>' . $fault_desc_node->getText() . ' [code ' . $fault_code_node->getText() . ']</li>';
         }
         if ($fault_node_list->getLength() > 0) {
             $err_msg .= '</ul>';
         }
         echo $err_msg;
         return false;
     }
     $label_image_node_list =& $xmlResp->getElementsByPath('//Label/Image');
     $label_image_node =& $label_image_node_list->item(0);
     $label_image = $label_image_node->getText();
     $airbill_number_node_list =& $xmlResp->getElementsByPath('//ShipmentDetail/AirbillNbr');
     $airbill_number_node =& $airbill_number_node_list->item(0);
     $airbill_number = $airbill_number_node->getText();
     /*
      * insert label image into database and mark that the label has
      * been generated.
      */
     $q = "UPDATE #__{vm}_shipping_label ";
     $q .= "SET ";
     $q .= "label_is_generated='1', ";
     $q .= "tracking_number='" . $airbill_number . "', ";
     $q .= "label_image='" . $label_image . "' ";
     $q .= "WHERE order_id = '" . $order_id . "'";
     $dbnl = new ps_DB();
     $dbnl->query($q);
     $dbnl->next_record();
     return true;
 }