bebeboutik/modules/lapostews/webservice.php
2016-05-18 12:14:17 +02:00

367 lines
13 KiB
PHP
Raw Permalink 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.

This file contains Unicode characters that might be confused with other characters. 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 WS_ServiceCallContextVO {
public $dateDeposite; // dateTime
//~ public $dateValidation; // dateTime
public $returnType = 'CreatePDFFile'; // RequestTypeVO, CreatePDFFile / ReturnPDFInResponse
public $serviceType; // string
public $crbt = FALSE; // bool
//~ public VATCode; // int
//~ public VATPercentage; // int
//~ public VATAmount; // int
//~ public transportationAmount; // int
//~ public totalAmount; // int, oblig pour Andorre
//public $portPaye = FALSE; // bool
//~ public FTD; // bool
//~ public FTDAmount; // int
//~ public returnOption; // bool
//~ public returnOptionAmount; // int
public function __construct() {
$this->serviceType = (Module::isInstalled('socolissimo')? 'SO': 'DOM');
$this->dateDeposite = date('Y-m-d\TH:i:s.000P');
}
}
class WS_ExpEnvVO {
public $alert = 'none'; // AlertTypeVO
public $addressVO; // AddressVO
}
class WS_AddressVO {
//~ public $companyName; // string
//~ public $Civility; // string
//~ public $Name; // string
//~ public $Surname; // string
//~ public $line0; // string
//~ public $line1; // string
public $line2 = ''; // string
//~ public $line3; // string
//~ public $phone; // string, not required for DOM or DOS only
//~ public $MobileNumber; // string
//~ public $DoorCode1; // string
//~ public $DoorCode2; // string
//~ public $Interphone; // string
//~ public $country; // string
public $countryCode; // string
public $city; // string
public $email; // string
public $postalCode; // string
}
class WS_DestEnvVO {
public $alert = 'none'; // AlertTypeVO
public $addressVO; // AddressVO
public $codeBarForreference = FALSE; // bool
public $deliveryError = FALSE; // bool
}
class WS_ServiceCallContextV2 extends WS_ServiceCallContextVO {
//~ public $commandNumber; // string
public $commercialName; // string
public function __construct() {
parent::__construct();
$this->commercialName = Configuration::get('LAPOSTEWS_COMMERCIALNAME', 'ONLINE SHOP');
}
}
class WS_Letter {
public $password; // string, not null
public $contractNumber; // int, not null
// public $profil; // string
public $service; // ServiceCallContextV2
public $parcel; // ParcelVO
public $dest; // DestEnvVO
public $exp; // ExpEnvVO
public function __construct() {
$this->contractNumber = (int) Configuration::get('LAPOSTEWS_API_CONTRACT');
$this->password = Configuration::get('LAPOSTEWS_API_PASSWORD');
}
}
class WS_GetLetterColissimoRequest {
public $letter; // Letter
public function __construct($letter=NULL) {
$this->letter = $letter;
}
}
class WS_ParcelVO {
public $insuranceRange = '00'; // string, not null
//~ public $typeGamme; // string, not null
//~ public $parcelNumber; // string, not null
//~ public $returnTypeChoice; // int, not null
//~ public $insuranceValue; // int, not null
//~ public $recommendationLevel; // string, not null
//~ public $RecommendationAmount; // int
public $weight = 0.01; // float, not null, /!\ décimales si > 0 uniquement
public $horsGabarit = FALSE;
//~ public $HorsGabaritAmount; // int
public $DeliveryMode = 'DOM'; // DeliveryModeVO: DOM, RDV, BPR, ACP, CDI, A2P, MRL, CIT, DOS
//public $ReturnReceipt = FALSE; // bool
//public $Recommendation = FALSE; // bool
//~ public $Instructions; // string
//public $RegateCode = ''; // string, vide si domicile, sinon point de livraison
//~ public $contents; // ContentsVO, not null, required if Andorre
}
class Parcel {
public $mode;
public $parcelVO;
public $getLetterColissimoRequest;
public $letter;
public $serviceCallContextV2;
public $order;
public function __construct() {
$this->mode = (int) Configuration::get('LAPOSTEWS_MODE'); // 0: test, 1: production
$this->serviceCallContextV2 = new WS_ServiceCallContextV2();
$this->parcelVO = new WS_ParcelVO();
$this->letter = new WS_Letter();
$this->letter->parcel = $this->parcelVO;
$this->letter->service = $this->serviceCallContextV2;
$this->letter->dest = new WS_DestEnvVO();
$this->letter->exp = new WS_ExpEnvVO();
}
public function attachOrder($id_order) {
global $cookie;
$this->order = new Order($id_order);
if(Validate::isLoadedObject($this->order)) {
$this->serviceCallContextV2->commandNumber = $this->order->id;
$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);
$dst_address_ws = new WS_AddressVO();
if($dst_address->company != '') {
$dst_address_ws->companyName = mb_substr($dst_address->company, 0, 32);
}
$dst_address_ws->Name = (string) mb_substr($dst_address->firstname, 0, 32);
$dst_address_ws->Surname = (string) mb_substr(strtoupper($dst_address->lastname), 0, 32);
$dst_address_ws->email = (string) $dst_customer->email;
$dst_address_ws->postalCode = (string) $dst_address->postcode;
$dst_address_ws->city = (string) mb_substr(strtoupper($dst_address->city), 0, 32);
if(strtolower($dst_country->name[$cookie->id_lang]) == 'monaco') {
$dst_address_ws->countryCode = 'FR';
} else {
$dst_address_ws->countryCode = (string) $dst_country->iso_code;
}
$dst_address_ws->country = (string) mb_substr($dst_country->name[$cookie->id_lang], 0, 32);
if((Module::isInstalled('socolissimo') || Module::isInstalled('soflexibilite')) && ($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.'
'))) {
$unicode_0 = json_decode('"\u0006"');
$unicode_1 = json_decode('"\u200b"');
$unicode_2 = json_decode('"\u00ad"');
$dst_address_ws->email = $so_data['ceemail'];
$dst_address_ws->Name = str_replace(
array('Á', '°', 'º', 'º', 'º', 'ª', "\t", "\n", "\r", ' ', '', '', $unicode_0, $unicode_1, $unicode_2,),
array('A', '', '', '', '', 'a', ' ', ' ', ' ', ' ', '-', '', '', '', '',),
mb_substr($so_data['prfirstname'], 0, 32)
);
$dst_address_ws->Surname = str_replace(
array('Á', '°', 'º', 'º', 'º', 'ª', "\t", "\n", "\r", ' ', '', '', $unicode_0, $unicode_1, $unicode_2,),
array('A', '', '', '', '', 'a', ' ', ' ', ' ', ' ', '-', '', '', '', '',),
mb_substr($so_data['prname'], 0, 32)
);
$address_lines = array();
for($i = 1; $i < 5; $i++) {
if(($line = (string) mb_substr($so_data['pradress'.$i], 0, 32)) != '') {
$address_lines[] = str_replace(
array('Á', '°', 'º', 'º', 'º', 'ª', "\t", "\n", "\r", ' ', '', '', $unicode_0, $unicode_1, $unicode_2,),
array('A', '', '', '', '', 'a', ' ', ' ', ' ', ' ', '-', '', '', '', '',),
trim($line)
);
}
}
if(count($address_lines) == 1) {
$dst_address_ws->line2 = $address_lines[0];
} elseif(count($address_lines) == 2) {
$dst_address_ws->line2 = $address_lines[0];
$dst_address_ws->line3 = $address_lines[1];
} elseif(count($address_lines) == 3) {
$dst_address_ws->line0 = $address_lines[0];
$dst_address_ws->line2 = $address_lines[1];
$dst_address_ws->line3 = $address_lines[2];
} else {
$dst_address_ws->line0 = $address_lines[0];
$dst_address_ws->line1 = $address_lines[1];
$dst_address_ws->line2 = $address_lines[2];
$dst_address_ws->line3 = $address_lines[3];
}
$dst_address_ws->postalCode = (string) mb_substr($so_data['przipcode'], 0, 32);
$dst_address_ws->city = (string) mb_substr($so_data['prtown'], 0, 32);
$dst_address_ws->MobileNumber = (string) mb_substr($so_data['cephonenumber'], 0, 32);
if(strlen($dst_address_ws->MobileNumber) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address_ws->MobileNumber)) {
$dst_address_ws->MobileNumber = '0'.mb_substr(mb_substr($dst_address_ws->MobileNumber, -10), 1);
}
}
if(mb_substr($dst_address_ws->MobileNumber, 0, 2) != '06' && mb_substr($dst_address_ws->MobileNumber, 0, 2) != '07') {
$dst_address_ws->MobileNumber = '0600000000';
}
if(($doorcode = (string) mb_substr($so_data['cedoorcode1'], 0, 32)) != '') {
$dst_address_ws->DoorCode1 = $doorcode;
}
if(($doorcode = (string) mb_substr($so_data['cedoorcode2'], 0, 32)) != '') {
$dst_address_ws->DoorCode2 = $doorcode;
}
$this->letter->parcel->Instructions = $address_lines[] = str_replace(
array('Á', '°', 'º', 'º', 'º', 'ª', "\t", "\n", "\r", ' ', '', '', $unicode_0, $unicode_1, $unicode_2,),
array('A', '', '', '', '', 'a', ' ', ' ', ' ', ' ', '-', '', '', '', '',),
(string) $so_data['cedeliveryinformation']
);
$this->letter->parcel->DeliveryMode = (string) $so_data['delivery_mode'];
if(!in_array($this->letter->parcel->DeliveryMode, array('DOM', 'DOS', 'RDV'))) {
$this->letter->parcel->RegateCode = $so_data['prid']; //(string) ltrim($so_data['prid'], '0');
error_log($this->letter->parcel->RegateCode);
}
} else {
// $this->serviceCallContextV2->serviceType = 'DOM';
$dst_address_ws->line2 = (string) $dst_address->address1;
$dst_address_ws->line3 = (string) $dst_address->address2;
if($dst_country->iso_code == 'FR' || $dst_country->iso_code == 'AD' || $dst_country->iso_code == 'MO') {
if($dst_address->phone != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone)) {
$dst_address_ws->phone = (string) $dst_address->phone;
} elseif($dst_address->phone_mobile != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone_mobile)) {
$dst_address_ws->phone = (string) $dst_address->phone_mobile;
}
if(strlen($dst_address_ws->phone) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address_ws->phone)) {
$dst_address_ws->phone = '0'.mb_substr(mb_substr($dst_address_ws->phone, -10), 1);
}
}
if(strlen($dst_address_ws->phone_mobile) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address_ws->phone_mobile)) {
$dst_address_ws->phone_mobile = '0'.mb_substr(mb_substr($dst_address_ws->phone_mobile, -10), 1);
}
}
} else {
if($dst_address->phone != '') {
$dst_address_ws->phone = (string) $dst_address->phone;
}
if($dst_address->phone_mobile != '') {
$dst_address_ws->MobileNumber = (string) $dst_address->phone_mobile;
}
}
}
$this->letter->dest->addressVO = $dst_address_ws;
$exp_country = new Country((int) Configuration::get('LAPOSTEWS_EXP_COUNTRY'));
$exp_address_ws = new WS_AddressVO();
$exp_address_ws->companyName = (string) mb_substr(Configuration::get('LAPOSTEWS_EXP_COMPANY', ''), 0, 32);
$exp_address_ws->Name = (string) mb_substr(Configuration::get('LAPOSTEWS_EXP_FIRSTNAME', ''), 0, 32);
$exp_address_ws->Surname = (string) mb_substr(strtoupper(Configuration::get('LAPOSTEWS_EXP_LASTNAME', '')), 0, 32);
$exp_address_ws->email = (string) Configuration::get('LAPOSTEWS_EXP_EMAIL', '');
$exp_address_ws->postalCode = (string) Configuration::get('LAPOSTEWS_EXP_POSTALCODE', '');
$exp_address_ws->city = (string) mb_substr(strtoupper(Configuration::get('LAPOSTEWS_EXP_CITY', '')), 0, 32);
$exp_address_ws->line2 = (string) mb_substr(strtoupper(Configuration::get('LAPOSTEWS_EXP_ADDR1', '')), 0, 32);
$exp_address_ws->line3 = (string) mb_substr(strtoupper(Configuration::get('LAPOSTEWS_EXP_ADDR2', '')), 0, 32);
if(strtolower($exp_country->name[$cookie->id_lang]) == 'monaco') {
$exp_address_ws->countryCode = 'FR';
} else {
$exp_address_ws->countryCode = (string) $dst_country->iso_code;
}
$exp_address_ws->country = (string) mb_substr($exp_country->name[$cookie->id_lang], 0, 32);
$exp_address_ws->phone = (string) Configuration::get('LAPOSTEWS_EXP_PHONE', '');
if(strlen($exp_address_ws->phone) > 10) {
if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $exp_address_ws->phone)) {
$exp_address_ws->phone = '0'.mb_substr(mb_substr($exp_address_ws->phone, -10), 1);
}
}
$this->letter->exp->addressVO = $exp_address_ws;
}
}
public function setWeight($weight=0) {
$this->parcelVO->weight = rtrim((string) $weight, '0');
}
public function send() {
if(Validate::isLoadedObject($this->order)) {
global $cookie;
$c = new SoapClient(
(
/*$this->mode
? 'http://ws.colissimo.fr/soap.shippingclpV2/services/WSColiPosteLetterService?wsdl'
: 'http://217.108.161.162/soap.shippingclpV2/services/WSColiPosteLetterService?wsdl'*/
'http://ws.colissimo.fr/soap.shippingclpV2/services/WSColiPosteLetterService?wsdl'
),
array(
'trace' => $this->mode,
'exceptions' => !$this->mode,
'cache_wsdl' => $this->mode? WSDL_CACHE_MEMORY: WSDL_CACHE_NONE,
)
);
$this->getLetterColissimoRequest = new WS_GetLetterColissimoRequest($this->letter);
try {
//$x = $c->getLetterColissimo(array('in0' => $this->letter));
$x = $c->getLetterColissimo($this->getLetterColissimoRequest);
if($_SERVER['REMOTE_ADDR'] == '78.226.56.137') {
mail('perron@antadis.com', 'test belgique bbb', serialize($this->getLetterColissimoRequest)."\n".serialize($x));
}
// if($_SERVER['REMOTE_ADDR'] == '88.120.248.124') {
// mail('thibault@antadis.com', 'test point retrait', json_encode($this->getLetterColissimoRequest) ."\n".json_encode($x));
// }
return $x;
} catch(Exception $e) {
if($_SERVER['REMOTE_ADDR'] == '78.226.56.137') {
mail('perron@antadis.com', 'test belgique bbb err', serialize($x));
}
return $e;
}
} else {
return FALSE;
}
}
}