506 lines
27 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
class SoapResponse {
const CONTENT_TYPE = 'Content-Type: application/xop+xml;';
const UUID = '/--uuid:/'; //This is the separator of each part of the response
const CONTENT = 'Content-';
public $attachments = array ();
public $soapResponse = array ();
public $uuid = NULL;
public function __construct($response)
{
if(strpos($response, self::CONTENT_TYPE ) !== FALSE) {
$this->parseResponse($response);
} else {
throw new Exception('This response is not '.self::CONTENT_TYPE);
}
}
private function parseResponse($response)
{
$content = array ();
$matches = array ();
preg_match_all(self::UUID, $response, $matches, PREG_OFFSET_CAPTURE);
for($i = 0; $i < count ($matches[0]) - 1; $i++) {
if ($i + 1 < count ( $matches [0] )) {
$content[$i] = substr($response, $matches[0][$i][1], $matches[0][$i + 1][1] - $matches[0][$i][1]);
} else {
$content[$i] = substr($response, $matches[0][$i][1], strlen($response));
}
}
foreach ( $content as $part ) {
if($this->uuid == NULL){
$uuidStart = 0;
$uuidEnd = 0;
$uuidStart = strpos($part, self::UUID, 0) + strlen(self::UUID);
$uuidEnd = strpos($part, "\r\n", $uuidStart);
$this->uuid = substr($part, $uuidStart, $uuidEnd-$uuidStart);
}
$header = $this->extractHeader($part);
if(count($header) > 0){
if(strpos($header['Content-Type'], 'type="text/xml"') !== FALSE) {
$this->soapResponse['header'] = $header;
$this->soapResponse['data'] = trim(substr($part, $header['offsetEnd']));
} else {
$attachment['header'] = $header;
$attachment['data'] = trim(substr($part, $header['offsetEnd']));
array_push($this->attachments, $attachment);
}
}
}
}
/**
* Exclude the header from the Web Service response
* @param string $part
* @return array $header
*/
private function extractHeader($part)
{
$header = array();
$headerLineStart = strpos($part, self::CONTENT, 0);
$endLine = 0;
while($headerLineStart !== FALSE){
$header['offsetStart'] = $headerLineStart;
$endLine = strpos($part, "\r\n", $headerLineStart);
$headerLine = explode(': ', substr($part, $headerLineStart, $endLine - $headerLineStart));
$header[$headerLine[0]] = $headerLine[1];
$headerLineStart = strpos($part, self::CONTENT, $endLine);
}
$header['offsetEnd'] = $endLine;
return $header;
}
}
class Parcel {
public function addArticle($description, $qty, $weight, $value, $wco_nom) {}
public function getShippingNumber() {}
public function attachOrder($id_order)
{
$this->order = new Order($id_order);
if(Validate::isLoadedObject($this->order)) {
$this->product_type = '';
$dst_customer = new Customer($this->order->id_customer);
$dst_address = new Address($this->order->id_address_delivery);
$dst_country = new Country($dst_address->id_country);
$langs = array(
1 => 'EN',
2 => 'FR',
3 => 'ES',
5 => 'IT',
6 => 'EN',
);
$this->dst_address = array(
'companyName' => '',
'lastName' => '',
'firstName' => '',
'line0' => '',
'line1' => '',
'line2' => '',
'line3' => '',
'countryCode' => '',
'city' => '',
'zipCode' => '',
'phoneNumber' => '',
'mobileNumber' => '',
'doorCode1' => '',
'doorCode2' => '',
'email' => '',
'intercom' => '',
'language' => $langs[(int) $this->order->id_lang],
);
$this->prid = '';
$this->instructions = '';
$unicode_ack = json_decode('"\u0006"');
$unicode_ack2 = json_decode('"\u00ad"');
if(Module::isInstalled('socolissimo')
&& ($so_data = Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'socolissimo_delivery_info`
WHERE `id_cart` = '.(int) $this->order->id_cart.'
AND `id_customer` = '.(int) $this->order->id_customer.'
'))
&& (
(strpos(Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'carrier`
WHERE `id_carrier` = '.(int) $this->order->id_carrier.'
'), 'La Poste') !== FALSE)
|| (strpos(Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'carrier`
WHERE `id_carrier` = '.(int) $this->order->id_carrier.'
'), 'Colissimo') !== FALSE)
)) {
$this->product_type = $so_data['delivery_mode'];
if($this->product_type == 'BOM') {
$this->product_type = 'DOM';
}
if($so_data['cecountry'] == 'ES' && $this->product_type == 'DOM') {
$this->product_type = 'DOS';
}
if(!in_array($so_data['cecountry'], array('ES', 'BE', 'AD', 'MC', 'FR', 'GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) {
$this->product_type = 'DOS';
}
if($so_data['cecountry'] == 'IT'){
$this->product_type = 'COLI';
}
$this->dst_address['email'] = $so_data['ceemail'];
if(empty($this->dst_address['email'])) {
$this->dst_address['email'] = $dst_customer->email;
}
if(!empty($so_data['cename'])) {
$this->dst_address['lastName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','İ'), array(' ', '', '', '', 'a', 'a','a','A','l','i'), $so_data['cename']), 0, 35);
} else {
$this->dst_address['lastName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','İ'), array(' ', '', '', '', 'a', 'a','a','A','l','i'), $so_data['prname']), 0, 35);
}
if(!empty($so_data['cefirstname'])) {
$this->dst_address['firstName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['cefirstname']), 0, 29);
} else {
$this->dst_address['firstName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['prfirstname']), 0, 29);
}
if(!empty($so_data['cecompanyname'])) {
$this->dst_address['companyName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['cecompanyname']), 0, 35);
}
$address_lines = array();
for($i = 1; $i < 5; $i++) {
if(($line = (string) mb_substr($so_data['pradress'.$i], 0, 32)) != '') {
$address_lines[] = trim($line);
}
}
if(count($address_lines) == 1) {
$this->dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]);
} elseif(count($address_lines) == 2) {
if(strtolower($so_data['cecountry']) == 'be') {
$this->dst_address['line0'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]);
$this->dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]);
} else {
$this->dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]);
$this->dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]);
}
} elseif(count($address_lines) == 3) {
$this->dst_address['line0'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]);
$this->dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]);
$this->dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[2]);
} else {
$this->dst_address['line0'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]);
$this->dst_address['line1'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]);
$this->dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[2]);
$this->dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[3]);
}
$this->dst_address['countryCode'] = $so_data['cecountry'];
if(strtolower($so_data['cecountry']) == 'sl') {
$this->dst_address['zipCode'] = (string) str_ireplace(array('SI-', 'SL-'), '', mb_substr($so_data['przipcode'], 0, 5));
} elseif(strtolower($so_data['cecountry']) == 'pt') {
$this->dst_address['zipCode'] = (string) $so_data['przipcode'];
} else {
$this->dst_address['zipCode'] = (string) mb_substr($so_data['przipcode'], 0, 5);
}
$this->dst_address['city'] = (string) mb_substr($so_data['prtown'], 0, 35);
$this->dst_address['mobileNumber'] = (string) mb_substr(str_replace(array('.','°', '|', ' ', '’',"/"),'',$so_data['cephonenumber']), 0, 32);
if(strlen($this->dst_address['mobileNumber']) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $this->dst_address['mobileNumber'])) {
$this->dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($this->dst_address['mobileNumber'], -10), 1);
}
if(strtolower($so_data['cecountry']) == 'be' && preg_match('/^(0032|32)[0-9]{9,10}$/', $this->dst_address['mobileNumber'])) {
$this->dst_address['mobileNumber'] = '+32'.mb_substr(mb_substr($this->dst_address['mobileNumber'], -10), 1);
}
} elseif(strtolower($so_data['cecountry']) == 'fr' && preg_match('/^[0-9]{9,10}$/', $this->dst_address['mobileNumber'])) {
if(strlen($this->dst_address['mobileNumber']) == 9) {
$this->dst_address['mobileNumber'] = '0'.$this->dst_address['mobileNumber'];
} elseif(strlen($this->dst_address['mobileNumber']) == 10) {
$this->dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($this->dst_address['mobileNumber'], -10), 1);
}
} elseif(strtolower($so_data['cecountry']) == 'be' && preg_match('/^[0-9]{9,10}$/', $this->dst_address['mobileNumber'])) {
if(strlen($this->dst_address['mobileNumber']) == 9) {
$this->dst_address['mobileNumber'] = '+32'.$this->dst_address['mobileNumber'];
} elseif(strlen($this->dst_address['mobileNumber']) == 10) {
$this->dst_address['mobileNumber'] = '+32'.mb_substr(mb_substr($this->dst_address['mobileNumber'], -10), 1);
}
}
if(strtolower($so_data['cecountry']) == 'fr' && mb_substr($this->dst_address['mobileNumber'], 0, 2) != '06' && mb_substr($this->dst_address['mobileNumber'], 0, 2) != '07') {
$this->dst_address['mobileNumber'] = '0600000000';
}
if(($doorcode = (string) mb_substr($so_data['cedoorcode1'], 0, 8)) != '') {
$this->dst_address['doorCode1'] = (string) mb_substr($so_data['cedoorcode1'], 0, 8);
}
if(($doorcode = (string) mb_substr($so_data['cedoorcode2'], 8, 8)) != '') {
$this->dst_address['doorCode2'] = (string) mb_substr($so_data['cedoorcode1'], 8, 8);
}
$this->instructions = trim(str_replace(array('°', '|', ' ', '’', "\n", "\r"), ' ', (string) $so_data['cedeliveryinformation']));
$this->prid = $so_data['prid'];
} else {
$this->dst_address['companyName'] = (string) mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->company), 0, 35);
$this->dst_address['lastName'] = (string) mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->firstname), 0, 35);
$this->dst_address['firstName'] = (string) mb_substr(strtoupper(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->lastname)), 0, 29);
$this->dst_address['email'] = (string) $dst_customer->email;
if($dst_address->id_country == 193) {
$this->dst_address['zipCode'] = mb_substr((string) str_ireplace(array('SI-', 'SL-'), '', $dst_address->postcode), 0, 5);
} else {
$this->dst_address['zipCode'] = mb_substr((string) str_replace(array(' ', '-'), '', $dst_address->postcode), 0, 5);
}
$this->dst_address['city'] = (string) mb_substr(strtoupper($dst_address->city), 0, 35);
if(in_array($dst_country->iso_code, array('GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) {
$this->dst_address['countryCode'] = 'FR';
} else {
$this->dst_address['countryCode'] = (string) $dst_country->iso_code;
}
$this->dst_address['line2'] = str_replace(array('º', 'º', 'º', 'ª',), array('', '', '', 'a'), (string) $dst_address->address1);
$this->dst_address['line3'] = str_replace(array('º', 'º', 'º', 'ª',), array('', '', '', 'a'), (string) $dst_address->address2);
$this->instructions = trim(str_replace(array('°', '|', ' ', '’', "\n", "\r"), ' ', (string) $dst_address->other));
if(in_array($dst_country->iso_code, array('FR', 'AD', 'MC', 'GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) {
$dst_address->phone = ($dst_address->phone != ''? (string) str_replace(array('.','°', '|', ' ', '’',"/"),'',$dst_address->phone) : '');
$dst_address->phone_mobile = ($dst_address->phone_mobile != ''? (string) str_replace(array('.','°', '|', ' ', '’',"/"),'',$dst_address->phone_mobile) : '');
if($dst_address->phone != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone)) {
$this->dst_address['phoneNumber'] = (string) $dst_address->phone;
} elseif($dst_address->phone_mobile != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone_mobile)) {
$this->dst_address['phoneNumber'] = (string) $dst_address->phone_mobile;
}
if(strlen($this->dst_address['phoneNumber']) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $this->dst_address['phoneNumber'])) {
$this->dst_address['phoneNumber'] = '0'.mb_substr(mb_substr($this->dst_address['phoneNumber'], -10), 1);
}
}
if(strlen($this->dst_address['mobileNumber']) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $this->dst_address['mobileNumber'])) {
$this->dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($this->dst_address['mobileNumber'], -10), 1);
}
}
} else {
if($dst_country->iso_code == 'BE') {
if($dst_address->phone != '') {
$this->dst_address['phoneNumber'] = (string) $dst_address->phone;
} else {
$this->dst_address['phoneNumber'] = (string) $dst_address->phone_mobile;
}
$this->dst_address['mobileNumber'] = '';
} else {
if($dst_address->phone != '') {
$this->dst_address['phoneNumber'] = (string) $dst_address->phone;
}
if($dst_address->phone_mobile != '') {
$this->dst_address['mobileNumber'] = (string) $dst_address->phone_mobile;
}
}
}
if($dst_country->iso_code == 'FR' || $dst_country->iso_code == 'MC' || $dst_country->iso_code == 'BE') {
//$this->product_type = 'COLD'; // Amené a disparaitre
$this->product_type = 'DOM';
} else {
$this->product_type = 'COLI';
}
}
}
}
private function array_to_xml($array, $xml)
{
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml->addChild("$key");
$this->array_to_xml($value, $subnode);
}
else{
$subnode = $xml->addChild("item$key");
$this->array_to_xml($value, $subnode);
}
}
else {
$xml->addChild("$key",htmlspecialchars("$value"));
}
}
}
private function xml_to_array($xml, $array=array())
{
foreach((array) $xml as $index => $node) {
$array[$index] = (is_object($node))? $this->xml_to_array($node): $node;
}
return $array;
}
public function setWeight($weight=0)
{
$this->weight = rtrim((string) $weight, '0');
}
public function send()
{
if(Validate::isLoadedObject($this->order)) {
global $cookie;
$carrier_config = array(
'api' => array(
'wsu' => array(
'contract' => (int) Configuration::get('LAPOSTEWS_API_CONTRACT_WSU'),
'password' => Configuration::get('LAPOSTEWS_API_PASSWORD_WSU'),
)
),
'exp_commercial_name' => Configuration::get('LAPOSTEWS_COMMERCIALNAME'),
'exp_firstname' => Configuration::get('LAPOSTEWS_EXP_FIRSTNAME'),
'exp_lastname' => Configuration::get('LAPOSTEWS_EXP_LASTNAME'),
'exp_company' => Configuration::get('LAPOSTEWS_EXP_COMPANY'),
'exp_address1' => Configuration::get('LAPOSTEWS_EXP_ADDR1'),
'exp_address2' => Configuration::get('LAPOSTEWS_EXP_ADDR2'),
'exp_city' => Configuration::get('LAPOSTEWS_EXP_CITY'),
'exp_postcode' => Configuration::get('LAPOSTEWS_EXP_POSTALCODE'),
'exp_email' => Configuration::get('LAPOSTEWS_EXP_EMAIL'),
'exp_country' => Configuration::get('LAPOSTEWS_EXP_COUNTRY'),
'exp_phone' => Configuration::get('LAPOSTEWS_EXP_PHONE'),
);
$c = new SoapClient(
'https://ws.colissimo.fr/sls-ws/SlsServiceWS?wsdl',
array(
'trace' => 0,
'exceptions' => 1,
'cache_wsdl' => 0? WSDL_CACHE_MEMORY: WSDL_CACHE_NONE,
)
);
try {
$exp_country = new Country((int) $carrier_config['exp_country']);
if(in_array($exp_country->iso_code, array('GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) {
$countryCode = 'FR';
} else {
$countryCode = (string) $exp_country->iso_code;
}
$phone = $carrier_config['exp_phone'];
if(strlen($phone) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $phone)) {
$phone = '0'.mb_substr(mb_substr($phone, -10), 1);
}
}
$soap = new SimpleXMLElement('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" />');
$soap->addChild('soapenv:Header');
$children = $soap->addChild('soapenv:Body');
$children = $children->addChild('sls:generateLabel', NULL, 'http://sls.ws.coliposte.fr');
$children = $children->addChild('generateLabelRequest', NULL, '');
$this->array_to_xml(array(
'contractNumber' => $carrier_config['api']['wsu']['contract'],
'password' => $carrier_config['api']['wsu']['password'],
'outputFormat' => array(
'x' => 0,
'y' => 0,
'outputPrintingType' => 'DPL_10x15_203dpi', // DPL_10x15_300dpi / PDF_10x15_300dpi / PDF_A4_300dpi
),
'letter' => array(
'service' => array(
/*
COLD = Colissimo Access FR
COL = Colissimo Expert FR
COM = Colissimo Access FR
CDS = Colissimo Expert OM
COLI = Colissimo Expert Inter
DOM = SoCol Dom SS
DOS = SoCol Dom AS
BPR = Socol Bureau de poste FR
A2P = Socol Commerçant FR
CMT = Socol commercant FR
BDP = Socol Bureau de poste inter
*/
'productCode' => $this->product_type,
'depositDate' => date('Y-m-d'),
'transportationAmount' => (int) ($this->order->total_shipping * 100),
'orderNumber' => (string) $this->order->id,
'commercialName' => $carrier_config['exp_commercial_name'],
'returnTypeChoice' => 3, // 2 = return, 3 = do not return
),
'parcel' => array(
'weight' => 0.24,
'instructions' => $this->instructions,
'pickupLocationId' => $this->prid,
),
'customsDeclarations' => array(
),
'sender' => array(
'senderParcelRef' => 'REF'.$this->order->id,
'address' => array(
'companyName' => (string) mb_substr($carrier_config['exp_company'], 0, 35),
'line2' => (string) mb_substr(strtoupper($carrier_config['exp_address1']), 0, 35),
'line3' => (string) mb_substr(strtoupper($carrier_config['exp_address2']), 0, 35),
'countryCode' => $countryCode,
'city' => (string) mb_substr(strtoupper($carrier_config['exp_city']), 0, 35),
'zipCode' => (string) mb_substr($carrier_config['exp_postcode'], 0, 5),
'phoneNumber' => $phone,
'email' => mb_substr((string) $carrier_config['exp_email'], 0, 80),
'language' => 'FR',
),
),
'addressee' => array(
'address' => $this->dst_address,
),
),
), $children);
$authorized_ip = array(
'88.163.22.99',
'90.63.178.63',
);
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', 'BBB LOG WSU xml', serialize($soap->asXML()));
}
$response = new SoapResponse($c->__doRequest($soap->asXML(), 'https://ws.colissimo.fr/sls-ws/SlsServiceWS', 'generateLabel', '2.0', 0));
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', 'BBB LOG WSU reponse', serialize($response));
}
return array(
'data' => new SimpleXMLElement(
'<?xml version=\'1.0\' standalone=\'yes\'?>'.str_replace(
array(
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:generateLabelResponse xmlns:ns2="http://sls.ws.coliposte.fr">',
'</ns2:generateLabelResponse></soap:Body></soap:Envelope>',
),
'',
$response->soapResponse['data']
)
),
'label' => $response->attachments[0]['data'],
);
} catch(Exception $e) {
$authorized_ip = array(
'88.163.22.99',
'90.63.178.63',
);
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', 'BBB LOG WSU Exception', serialize($e));
}
return $e;
}
}
}
}