ldc-gen-game-web/class/MesBR.php
Jérémy Munsch 5262489bf3
Ticket n°279
Trying to get property 'Statut' of non-object
2021-09-28 11:54:42 +02:00

120 lines
4.1 KiB
PHP

<?php
namespace LDCWeb;
class MesBR
{
private $endpoint;
private $refSite;
private $refCamp;
public function __construct($endpoint, $refSite, $refCamp)
{
$this->endpoint = $endpoint;
$this->refSite = $refSite;
$this->refCamp = $refCamp;
}
/** Update contact *
* @param $oCrmUser
* @return bool
*/
public function updateContact($oCrmUser)
{
$datas = [
'refsite' => $this->refSite,
'refadherent' => $oCrmUser->crm_id,
'nom' => $oCrmUser->lastname,
'prenom' => $oCrmUser->firstname,
'email' => $oCrmUser->email,
'date_naissance' => $oCrmUser->birth_date,
'code_postal' => $oCrmUser->postcode,
'commune' => $oCrmUser->city,
'pays' => $this->getCountry($oCrmUser->country),
'hashcode' => $oCrmUser->hashcode
];
$this->callAPI("GET", "/updatecontact", $datas);
// TODO : Verif statut du call ? (Nouvelle API MesBR)
return true;
}
/** Update panier *
* @param $oCrmUser
* @param $couponId
* @param int $quantity
* @return array
*/
public function updatePanier($oCrmUser, $couponId, $quantity = 1)
{
$datas = [
'refsite' => $this->refSite,
'refcamp' => $this->refCamp,
'refadherent' => $oCrmUser->crm_id,
'nom' => $oCrmUser->lastname,
'prenom' => $oCrmUser->firstname,
'email' => $oCrmUser->email,
'code_postal' => $oCrmUser->postcode,
'commune' => $oCrmUser->city,
'pays' => $this->getCountry($oCrmUser->country),
'panier' => "($quantity)$couponId"
];
$res = $this->callAPI("GET", "/updatebasket", $datas);
if (!$res) {
error_log("MESBR - CODE : 999 - MESSAGE : Cannot read API Response (non json)");
return ['status' => 'KO', 'code' => 999, 'message' => 'Cannot read API Response (non json)'];
}
if ($res->Statut === 'KO') {
error_log("MESBR - CODE : $res->Code - MESSAGE : $res->Message");
return ['status' => 'KO', 'code' => $res->Code, 'message' => $res->Message];
}
$panierVerification = $this->endpoint . '/showbasket?basketid=' . $res->Panier->Panier;
$panier_link = $this->endpoint . '/?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 $this->endpoint . "/?refsite=".trim($br_refsite)."&refcamp=".trim($br_refcamp)."&email=".$crmUser->email;
}
private function getCountry($country)
{
switch ($country) {
case 'Belgique':
return 56;
break;
case 'Luxembourg':
return 442;
break;
default:
return 250; //France
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, $this->endpoint . $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);
}
}