ldc-legaulois/app/Lib/MesBR.php
2021-03-21 22:51:19 +01:00

175 lines
5.2 KiB
PHP

<?php
namespace App\Lib;
use Illuminate\Support\Facades\Log;
class MesBR
{
private $refSite;
private $refCamp;
public function __construct($refSite, $refCamp)
{
$this->refSite = $refSite;
$this->refCamp = $refCamp;
}
/**
* Undocumented function
* updateContact
*
* @param [type] $oCrmUser
* @return void
*/
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;
}
/**
* Undocumented function
* Update panier
*
* @param [type] $oCrmUser
* @param [type] $couponId
* @param integer $quantity
* @return void
*/
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') {
Log::critical("MESBR - CODE : $res->Code - MESSAGE : $res->Message");
return ['status' => 'KO'];
}
$panierVerification = sprintf(
'https://www.mesbr.eu/showbasket?basketid=%s',
$res->Panier->Panier
);
$panier_link = sprintf(
'https://www.mesbr.eu/?basketid=%s',
$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é)
*
* @param string $br_refsite
* @param string $br_refcamp
* @param object $crmUser
* @return string url
*/
public function getMurBdrLink($br_refsite, $br_refcamp, $crmUser)
{
return sprintf(
"https://www.mesbr.eu/?refsite=%s&refcamp=%s&email=%s",
trim($br_refsite),
trim($br_refcamp),
$crmUser->zEmail
);
}
/**
* Undocumented function
*
* @return object
*/
public function getDetailCampaign()
{
return $this->callApi('GET', sprintf(
'https://www.mesbr.eu/getdetailcampaign?refsite=%s&refcamp=%s',
$this->refSite,
$this->refCamp
), false);
}
private function getCountry($country)
{
switch ($country) {
case 'Belgique':
return 56;
case 'Luxembourg':
return 442;
default:
return 250;
}
}
/**
* Undocumented function
*
* @param string $method
* @param string $url
* @param string $data
* @return object
*/
private function callApi(string $method, string $url, string $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, JSON_FORCE_OBJECT);
}
}