ec_ship.php (23089B)
1 <?php 2 class ModelExtensionShippingECShip extends Model { 3 function getQuote($address) { 4 $this->load->language('extension/shipping/ec_ship'); 5 6 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('shipping_ec_ship_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"); 7 8 if (!$this->config->get('shipping_ec_ship_geo_zone_id')) { 9 $status = true; 10 } elseif ($query->num_rows) { 11 $status = true; 12 } else { 13 $status = false; 14 } 15 16 //convert iso_code_3 to ec-ship country code 17 $country_codes = array( 18 'AFG' => 'AFA', 19 'ALB' => 'ALA', 20 'DZA' => 'DZA', 21 'AND' => 'ADA', 22 'AGO' => 'AOA', 23 'AIA' => 'AIA', 24 'ATG' => 'AGA', 25 'ARG' => 'ARA', 26 'ARM' => 'AMA', 27 'ASC' => 'ACA', 28 'ABW' => 'AWA', 29 'AUT' => 'ATA', 30 'AZE' => 'AZA', 31 'BHS' => 'BSA', 32 'BHR' => 'BHA', 33 'BGD' => 'BDA', 34 'BRB' => 'BBA', 35 'BLR' => 'BYA', 36 'BEL' => 'BEA', 37 'BLZ' => 'BZA', 38 'BEN' => 'BJA', 39 'BMU' => 'BMA', 40 'BTN' => 'BTA', 41 'BOL' => 'BOA', 42 'BIH' => 'BAA', 43 'BWA' => 'BWA', 44 'BRA' => 'BRA', 45 'IOT' => 'IOA', 46 'BRN' => 'BNA', 47 'BGR' => 'BGA', 48 'BFA' => 'BFA', 49 'BDI' => 'BIA', 50 'KHM' => 'KHA', 51 'CMR' => 'CMA', 52 'CAN' => 'CAA', 53 'CPV' => 'CVA', 54 'CYM' => 'KYA', 55 'CAF' => 'CFA', 56 'TCD' => 'TDA', 57 'CHL' => 'CLA', 58 'CXR' => 'CXA', 59 'CCK' => 'CCA', 60 'COL' => 'COA', 61 'COM' => 'KMA', 62 'COG' => 'CGA', 63 'CRI' => 'CRA', 64 'CIV' => 'CIA', 65 'HRV' => 'HRA', 66 'CUB' => 'CUA', 67 'CYP' => 'CYA', 68 'CZE' => 'CZA', 69 'DNK' => 'DKA', 70 'DJI' => 'DJA', 71 'DMA' => 'DMA', 72 'DOM' => 'DOA', 73 'TLS' => 'TPA', 74 'ECU' => 'ECA', 75 'EGY' => 'EGA', 76 'SLV' => 'SVA', 77 'GNQ' => 'GQA', 78 'ERI' => 'ERA', 79 'EST' => 'EEA', 80 'ETH' => 'ETA', 81 'FLK' => 'FKA', 82 'FRO' => 'FOA', 83 'FJI' => 'FJA', 84 'FIN' => 'FIA', 85 'FRA' => 'FRA', 86 'GUF' => 'GFA', 87 'PYF' => 'PFA', 88 'GAB' => 'GAA', 89 'GMB' => 'GMA', 90 'GEO' => 'GEA', 91 'DEU' => 'DEA', 92 'GHA' => 'GHA', 93 'GIB' => 'GIA', 94 'GRC' => 'GRA', 95 'GRL' => 'GLA', 96 'GRD' => 'GDA', 97 'GUM' => 'GUA', 98 'GTM' => 'GTA', 99 'GIN' => 'GNA', 100 'GNB' => 'GWA', 101 'GUY' => 'GYA', 102 'HTI' => 'HTA', 103 'HND' => 'HNA', 104 'HKG' => 'HKG', 105 'HUN' => 'HUA', 106 'ISL' => 'ISA', 107 'IND' => 'INA', 108 'IDN' => 'IDA', 109 'IRN' => 'IRA', 110 'IRQ' => 'IQA', 111 'IRL' => 'IEA', 112 'ISR' => 'ILA', 113 'ITA' => 'ITA', 114 'JAM' => 'JMA', 115 'JPN' => 'JPA', 116 'JOR' => 'JOA', 117 'KAZ' => 'KZA', 118 'KEN' => 'KEA', 119 'KIR' => 'KIA', 120 'PRK' => 'KPA', 121 'KOR' => 'KRA', 122 'KWT' => 'KWA', 123 'KGZ' => 'KGA', 124 'LAO' => 'LAA', 125 'LVA' => 'LVA', 126 'LBN' => 'LBA', 127 'LSO' => 'LSA', 128 'LBR' => 'LRA', 129 'LBY' => 'LYA', 130 'LIE' => 'LIA', 131 'LTU' => 'LTA', 132 'LUX' => 'LUA', 133 'MAC' => 'MOA', 134 'MDG' => 'MGA', 135 'MWI' => 'MWA', 136 'MDV' => 'MVA', 137 'MLI' => 'MLA', 138 'MLT' => 'MTA', 139 'MHL' => 'MHA', 140 'MRT' => 'MRA', 141 'MUS' => 'MUA', 142 'MEX' => 'MXA', 143 'MDA' => 'MDA', 144 'MCO' => 'MCA', 145 'MNG' => 'MNA', 146 'MNE' => 'MEA', 147 'MSR' => 'MSA', 148 'MAR' => 'MAA', 149 'MOZ' => 'MZA', 150 'MMR' => 'MMA', 151 'NAM' => 'NAA', 152 'NRU' => 'NRA', 153 'NPL' => 'NPA', 154 'NLD' => 'NLA', 155 'ANT' => 'ANA', 156 'NCL' => 'NCA', 157 'NZL' => 'NZA', 158 'NIC' => 'NIA', 159 'NER' => 'NEA', 160 'NGA' => 'NGA', 161 'NFK' => 'NFA', 162 'NOR' => 'NOA', 163 'OMN' => 'OMA', 164 'PAK' => 'PKA', 165 'PAN' => 'PAA', 166 'PNG' => 'PGA', 167 'PRY' => 'PYA', 168 'PER' => 'PEA', 169 'PHL' => 'PHA', 170 'PCN' => 'PNA', 171 'POL' => 'PLA', 172 'PRT' => 'PTA', 173 'PRI' => 'PRA', 174 'QAT' => 'QAA', 175 'REU' => 'REA', 176 'ROM' => 'ROA', 177 'RUS' => 'RUA', 178 'RWA' => 'RWA', 179 'ASM' => 'ASA', 180 'SMR' => 'SMA', 181 'STP' => 'STA', 182 'SAU' => 'SAA', 183 'SEN' => 'SNA', 184 'SRB' => 'RSA', 185 'SYC' => 'SCA', 186 'SLE' => 'SLA', 187 'SGP' => 'SGA', 188 'SVK' => 'SKA', 189 'SVN' => 'SIA', 190 'SLB' => 'SBA', 191 'SOM' => 'SOA', 192 'ZAF' => 'ZAA', 193 'ESP' => 'ESA', 194 'LKA' => 'LKA', 195 'KNA' => 'KNA', 196 'SHN' => 'SHA', 197 'LCA' => 'LCA', 198 'SPM' => 'PMA', 199 'VCT' => 'VCA', 200 'SDN' => 'SDA', 201 'SUR' => 'SRA', 202 'SWZ' => 'SZA', 203 'SWE' => 'SEA', 204 'CHE' => 'CHA', 205 'SYR' => 'SYA', 206 'TWN' => 'TWA', 207 'TJK' => 'TJA', 208 'TZA' => 'TZA', 209 'THA' => 'THA', 210 'TGO' => 'TGA', 211 'TON' => 'TOA', 212 'TTO' => 'TTA', 213 'SHN' => 'TAA', 214 'TUN' => 'TNA', 215 'TUR' => 'TRA', 216 'TKM' => 'TMA', 217 'TCA' => 'TCA', 218 'TUV' => 'TVA', 219 'UGA' => 'UGA', 220 'UKR' => 'UAA', 221 'ARE' => 'AEA', 222 'GBR' => 'GBA', 223 'URY' => 'UYA', 224 'UZB' => 'UZA', 225 'VUT' => 'VUA', 226 'VAT' => 'VAA', 227 'VEN' => 'VEA', 228 'VNM' => 'VNA', 229 'VIA' => 'VIA', 230 'WLF' => 'WFA', 231 'YEM ' => 'YEA', 232 'ZMB' => 'ZMA', 233 'ZWE' => 'ZWA', 234 'AUS' => array( 235 'WA' => 'AUA', 236 'OTHERS' => 'AUB', 237 ), 238 'CHN' => array( 239 'BE' => 'CNA', 240 'FU' => 'CNB', 241 'GU' => 'CNC', 242 'ZH' => 'CND', 243 'YU' => 'CNE', 244 'SG' => 'CNF', 245 'OTHERS' => 'CNG', 246 'TI' => 'CNH', 247 'FU' => 'CNJ', 248 ), 249 'MYS' => array( 250 'OTHERS' => 'MYA', 251 'MY-12' => 'MYB', 252 'MY-13' => 'MYC', 253 ), 254 'USA' => array( 255 'HI' => 'USA', 256 'NY' => 'USB', 257 'OTHERS' => 'USC', 258 ) 259 ); 260 261 $service = array( 262 'ARM' => $this->config->get('shipping_ec_ship_air_registered_mail'), 263 'APL' => $this->config->get('shipping_ec_ship_air_parcel'), 264 'AEP' => $this->config->get('shipping_ec_ship_e_express_service_to_us'), 265 'AEC' => $this->config->get('shipping_ec_ship_e_express_service_to_canada'), 266 'AEG' => $this->config->get('shipping_ec_ship_e_express_service_to_united_kingdom'), 267 'AER' => $this->config->get('shipping_ec_ship_e_express_service_to_russia'), 268 'AE1' => $this->config->get('shipping_ec_ship_e_express_service_one'), 269 'AE2' => $this->config->get('shipping_ec_ship_e_express_service_two'), 270 'EMS' => $this->config->get('shipping_ec_ship_speed_post'), 271 'SMP' => $this->config->get('shipping_ec_ship_smart_post'), 272 'LCP' => $this->config->get('shipping_ec_ship_local_courier_post'), 273 'LPL' => $this->config->get('shipping_ec_ship_local_parcel') 274 ); 275 276 //Countries available service 277 $shipCode = array( 278 'AUS' => array( 279 'AE2' => $this->language->get('text_e_express_service_two'), 280 'ARM' => $this->language->get('text_air_registered_mail'), 281 'APL' => $this->language->get('text_air_parcel'), 282 'EMS' => $this->language->get('text_speed_post') 283 ), 284 285 'NZL' => array( 286 'AE2' => $this->language->get('text_e_express_service_two'), 287 'ARM' => $this->language->get('text_air_registered_mail'), 288 'APL' => $this->language->get('text_air_parcel'), 289 'EMS' => $this->language->get('text_speed_post') 290 ), 291 292 'KOR' => array( 293 'AE2' => $this->language->get('text_e_express_service_two'), 294 'ARM' => $this->language->get('text_air_registered_mail'), 295 'APL' => $this->language->get('text_air_parcel'), 296 'EMS' => $this->language->get('text_speed_post') 297 ), 298 299 'SGP' => array( 300 'AE2' => $this->language->get('text_e_express_service_two'), 301 'ARM' => $this->language->get('text_air_registered_mail'), 302 'APL' => $this->language->get('text_air_parcel'), 303 'EMS' => $this->language->get('text_speed_post') 304 ), 305 306 'VNM' => array( 307 'AE2' => $this->language->get('text_e_express_service_two'), 308 'ARM' => $this->language->get('text_air_registered_mail'), 309 'APL' => $this->language->get('text_air_parcel'), 310 'EMS' => $this->language->get('text_speed_post') 311 ), 312 313 'DEU' => array( 314 'AE1' => $this->language->get('text_e_express_service_one'), 315 'ARM' => $this->language->get('text_air_registered_mail'), 316 'APL' => $this->language->get('text_air_parcel'), 317 'EMS' => $this->language->get('text_speed_post') 318 ), 319 320 'FRA' => array( 321 'AE1' => $this->language->get('text_e_express_service_one'), 322 'ARM' => $this->language->get('text_air_registered_mail'), 323 'APL' => $this->language->get('text_air_parcel'), 324 'EMS' => $this->language->get('text_speed_post') 325 ), 326 327 'NOR' => array( 328 'AE1' => $this->language->get('text_e_express_service_one'), 329 'ARM' => $this->language->get('text_air_registered_mail'), 330 'APL' => $this->language->get('text_air_parcel'), 331 'EMS' => $this->language->get('text_speed_post') 332 ), 333 334 'RUS' => array( 335 'AER' => $this->language->get('text_e_express_service_to_russia'), 336 'ARM' => $this->language->get('text_air_registered_mail'), 337 'APL' => $this->language->get('text_air_parcel'), 338 'EMS' => $this->language->get('text_speed_post') 339 ), 340 341 'GBR' => array( 342 'AEG' => $this->language->get('text_e_express_service_to_united_kingdom'), 343 'ARM' => $this->language->get('text_air_registered_mail'), 344 'APL' => $this->language->get('text_air_parcel'), 345 'EMS' => $this->language->get('text_speed_post') 346 ), 347 348 'CAN' => array( 349 'AEC' => $this->language->get('text_e_express_service_to_canada'), 350 'ARM' => $this->language->get('text_air_registered_mail'), 351 'APL' => $this->language->get('text_air_parcel'), 352 'EMS' => $this->language->get('text_speed_post') 353 ), 354 355 'USA' => array( 356 'AEP' => $this->language->get('text_e_express_service_to_us'), 357 'ARM' => $this->language->get('text_air_registered_mail'), 358 'APL' => $this->language->get('text_air_parcel'), 359 'EMS' => $this->language->get('text_speed_post') 360 ), 361 362 'HKG' => array( 363 'SMP' => $this->language->get('text_smart_post'), 364 'LCP' => $this->language->get('text_local_courier_post'), 365 'LPL' => $this->language->get('text_local_parcel') 366 ), 367 368 'OTHERS' => array( 369 'ARM' => $this->language->get('text_air_registered_mail'), 370 'APL' => $this->language->get('text_air_parcel'), 371 'EMS' => $this->language->get('text_speed_post') 372 ) 373 ); 374 375 $method_data = array(); 376 $error = ''; 377 378 if ($status) { 379 $weight = $this->weight->convert($this->cart->getWeight(), $this->config->get('config_weight_class_id'), $this->config->get('shipping_ec_ship_weight_class_id')); 380 $weight_code = strtolower($this->weight->getUnit($this->config->get('shipping_ec_ship_weight_class_id'))); 381 382 $weight = ($weight < 0.1 ? 0.1 : $weight); 383 384 $address_from = array( 385 'country' => "HKG", 386 'contact_name' => $this->config->get('config_owner'), 387 'phone' => $this->config->get('config_telephone'), 388 'email' => $this->config->get('config_email'), 389 'company_name' => $this->config->get('config_name') 390 ); 391 392 $address_to = array( 393 'country' => '', 394 'contact_name' => $address['firstname'] . ' ' . $address['lastname'], 395 'company_name' => $address['company'], 396 'code' => '' 397 ); 398 399 foreach ($shipCode as $key => $value) { 400 if ($address['iso_code_3'] == $key) { 401 $address_to['code'] = $shipCode[$key]; 402 break; 403 } else { 404 $address_to['code'] = $shipCode['OTHERS']; 405 } 406 } 407 408 foreach ($service as $key => $value) { 409 if (!isset($service[$key])) { 410 unset($address_to['code'][$key]); 411 } 412 } 413 414 if (isset($country_codes[$address['iso_code_3']]) && is_array($country_codes[$address['iso_code_3']])) { 415 foreach ($country_codes[$address['iso_code_3']] as $key => $value) { 416 if ($address['zone_code'] == $key) { 417 $address_to['country'] = $country_codes[$address['iso_code_3']][$address['zone_code']]; 418 break; 419 } else { 420 $address_to['country'] = $country_codes[$address['iso_code_3']]['OTHERS']; 421 } 422 } 423 } elseif (isset($country_codes[$address['iso_code_3']])) { 424 $address_to['country'] = $country_codes[$address['iso_code_3']]; 425 } else { 426 $error = $this->language->get('text_unavailable'); 427 } 428 429 if (!$this->config->get('shipping_ec_ship_test')) { 430 $url = 'https://www.ec-ship.hk/API/services/Calculator?wsdl'; 431 } else { 432 $url = 'http://www.ec-ship.hk/API-trial/services/Calculator?wsdl'; 433 } 434 435 // Creating date using yyyy-mm-ddThh:mm:ssZ format 436 $tm_created = gmdate('Y-m-d\TH:i:s\Z'); 437 $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180); 438 439 440 // Generating, packing and encoding a random number 441 $simple_nonce = mt_rand(); 442 $encoded_nonce = base64_encode(pack('H*', $simple_nonce)); 443 444 445 $username = $this->config->get('shipping_ec_ship_api_username'); 446 $password = $this->config->get('shipping_ec_ship_api_key'); 447 $passdigest = base64_encode(pack('H*',sha1(pack('H*', $simple_nonce) . pack('a*', $tm_created) . pack('a*', $password)))); 448 449 450 // Initializing namespaces 451 $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; 452 $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; 453 $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'; 454 $encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'; 455 456 // Creating WSS identification header using SimpleXML 457 $root = new SimpleXMLElement('<root/>'); 458 459 $security = $root->addChild('wsse:Security', null, $ns_wsse); 460 461 $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse); 462 $usernameToken->addChild('wsse:Username', $username, $ns_wsse); 463 $usernameToken->addChild('wsse:Password', $passdigest, $ns_wsse)->addAttribute('Type', $password_type); 464 $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse)->addAttribute('EncodingType', $encoding_type); 465 $usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu); 466 467 // Recovering XML value from that object 468 $root->registerXPathNamespace('wsse', $ns_wsse); 469 $full = $root->xpath('/root/wsse:Security'); 470 $auth = $full[0]->asXML(); 471 472 $objSoapVarWSSEHeader = new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true); 473 474 $objClient = new SoapClient($url); 475 476 $objClient->__setSoapHeaders(array($objSoapVarWSSEHeader)); 477 478 $request = array( 479 'ecshipUsername' => $this->config->get('shipping_ec_ship_username'), 480 'integratorUsername' => $this->config->get('shipping_ec_ship_api_username'), 481 'countryCode' => $address_to['country'], 482 'shipCode' => '', 483 'weight' => $weight 484 ); 485 486 $objResponseArray = array(); 487 488 foreach ($address_to['code'] as $key => $value) { 489 $api01Req = new api01Req($request['ecshipUsername'], $request['integratorUsername'], $request['countryCode'], $key, $request['weight']); 490 $params = array("api01Req" => $api01Req); 491 $objResponse = $objClient->__soapCall("getTotalPostage", array($params)); 492 $objResponse = json_decode(json_encode($objResponse), true); 493 $objResponse['getTotalPostageReturn']['serviceName'] = $value; 494 array_push($objResponseArray, $objResponse); 495 } 496 497 if ($objResponseArray){ 498 $code = 'ec_ship'; 499 $quote_data = array(); 500 501 foreach ($objResponseArray as $key => $value) { 502 if ($value['getTotalPostageReturn']['status'] == 0) { 503 $quote_data[$key] = array( 504 'code' => 'ec_ship.' . $key, 505 'title' => $value['getTotalPostageReturn']['serviceName'], 506 'cost' => $value['getTotalPostageReturn']['totalPostage'], 507 'tax_class_id' => $this->config->get('shipping_ec_ship_tax_class_id'), 508 'text' => $this->currency->format($this->tax->calculate($this->currency->convert($value['getTotalPostageReturn']['totalPostage'], 'HKD', $this->session->data['currency']), $this->config->get('shipping_ec_ship_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000) 509 ); 510 } 511 } 512 } 513 if ($quote_data || $error) { 514 $method_data = array( 515 'code' => 'ec_ship', 516 'title' => $this->language->get('text_title'), 517 'quote' => $quote_data, 518 'sort_order' => $this->config->get('shipping_ec_ship_sort_order'), 519 'error' => $error 520 ); 521 } 522 } 523 return $method_data; 524 } 525 } 526 527 class api01Req { 528 private $ecshipUsername; 529 private $integratorUsername; 530 private $countryCode; 531 private $shipCode; 532 private $weight; 533 534 function __construct($ecshipUsername, $integratorUsername, $countryCode, $shipCode, $weight) { 535 $this->ecshipUsername = $ecshipUsername; 536 $this->integratorUsername = $integratorUsername; 537 $this->countryCode = $countryCode; 538 $this->shipCode = $shipCode; 539 $this->weight = $weight; 540 } 541 }