100 lines
4 KiB
PHP
100 lines
4 KiB
PHP
<?php
|
|
namespace LDCWeb;
|
|
|
|
class MesBR
|
|
{
|
|
private $refSite;
|
|
private $refCamp;
|
|
public function __construct($refSite, $refCamp)
|
|
{
|
|
$this->refSite = $refSite;
|
|
$this->refCamp = $refCamp;
|
|
}
|
|
/** Update contact **/
|
|
public function updateContact($oCrmUser)
|
|
{
|
|
if (!$oCrmUser->zDateNaissance || $oCrmUser->zDateNaissance === '0000-00-00'){
|
|
$newDateNaissance = '01011990';
|
|
} else {
|
|
$tmpDate = date_create_from_format('Y-m-d', $oCrmUser->zDateNaissance);
|
|
$newDateNaissance = $tmpDate->format('dmY');
|
|
}
|
|
if(!$oCrmUser->getField('pays')){
|
|
$pays = 250;
|
|
} else {
|
|
$pays = $this->getCountry($oCrmUser->getField('pays'));
|
|
}
|
|
$url = 'https://www.mesbr.eu/updatecontact?refsite='.trim($this->refSite) .'&refadherent='. urlencode($oCrmUser->iId) . '&nom=' . urlencode($oCrmUser->zNom) .
|
|
'&prenom=' . urlencode($oCrmUser->zPrenom) . '&email=' . $oCrmUser->zEmail . '&date_naissance=' . $newDateNaissance .
|
|
'&code_postal=' . $oCrmUser->zCodePostal . '&commune=' . urlencode($oCrmUser->zVille).'&pays='.$pays;
|
|
$this->callAPI("GET", $url, false);
|
|
// TODO : Verif statut du call ?
|
|
return true;
|
|
}
|
|
/** Update panier **/
|
|
public function updatePanier($oCrmUser, $couponId, $quantity = 1)
|
|
{
|
|
if(!$oCrmUser->getField('pays')){
|
|
$pays = 250;
|
|
} else {
|
|
$pays = $this->getCountry($oCrmUser->getField('pays'));
|
|
}
|
|
$url = 'https://www.mesbr.eu/updatebasket?refsite='. trim($this->refSite) .'&refcamp='. trim($this->refCamp) .
|
|
'&refadherent='. urlencode($oCrmUser->iId) .'&nom='. urlencode($oCrmUser->zNom) .
|
|
'&prenom='. urlencode($oCrmUser->zPrenom) .'&email='. $oCrmUser->zEmail .
|
|
'&code_postal='. $oCrmUser->zCodePostal .'&commune='. urlencode($oCrmUser->zVille) .'&pays='.$pays.
|
|
'&panier=('.$quantity.')'.trim($couponId);
|
|
$res = $this->callAPI("GET", $url, false);
|
|
if ($res->Statut === 'KO') {
|
|
error_log("MESBR - CODE : $res->Code - MESSAGE : $res->Message");
|
|
return ['status' => 'KO'];
|
|
}
|
|
$panierVerification = 'https://www.mesbr.eu/showbasket?basketid=' . $res->Panier->Panier;
|
|
$panier_link = 'https://www.mesbr.eu/?basketid='. $res->Panier->Panier;
|
|
return ['status' => 'OK', 'link' => $panier_link, 'verif' => $panierVerification, 'basketId' => $res->Panier->Panier];
|
|
}
|
|
/** Génère le lien vers le mur (ou panier préprogrammé) **/
|
|
public function GetMurBDRLink($br_refsite, $br_refcamp, $crmUser)
|
|
{
|
|
return "https://www.mesbr.eu/?refsite=".trim($br_refsite)."&refcamp=".trim($br_refcamp)."&email=".$crmUser->zEmail;
|
|
}
|
|
private function getCountry($country)
|
|
{
|
|
switch ($country) {
|
|
case 'Belgique':
|
|
return 56;
|
|
break;
|
|
case 'Luxembourg':
|
|
return 442;
|
|
break;
|
|
default:
|
|
return 250;
|
|
break;
|
|
}
|
|
}
|
|
private function callAPI($method, $url, $data){
|
|
$curl = curl_init();
|
|
switch ($method){
|
|
case "POST":
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
if ($data)
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
|
break;
|
|
case "PUT":
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
|
if ($data)
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
|
break;
|
|
default:
|
|
if ($data)
|
|
$url = sprintf("%s?%s", $url, http_build_query($data));
|
|
}
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
$result = curl_exec($curl);
|
|
if(!$result){die("Connection Failure");}
|
|
curl_close($curl);
|
|
return json_decode($result);
|
|
}
|
|
}
|