ldc-tregalette/app/Lib/vendor/LDCWEB/MesBr.php
Vincent BRIDIER 6d4500c360 Bump CRM V4
2024-07-16 15:55:49 +00:00

150 lines
5.1 KiB
PHP

<?php
namespace LDC\MESBR;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer;
class MesBr
{
private $endpoint;
private $refCamp;
public function __construct($endpoint, $refCamp)
{
$this->endpoint = $endpoint;
$this->refCamp = $refCamp;
}
public function createCustomer(string $hashcode, string $email, string $country)
{
return $this->callApi("POST", '/customer', ['hashcode' => $hashcode, 'email' => $email, 'country' => $country]);
}
public function updateCustomer(string $hashcode, string $email, string $country)
{
return $this->callApi("POST", '/customer', ['hashcode' => $hashcode, 'email' => $email, 'country' => $country]);
}
public function getMurCustomer(string $hashcode)
{
return $this->callApi("GET", '/customer/mur', ['refcamp' => $this->refCamp, 'hashcode' => $hashcode]);
}
public function getMur()
{
return $this->callApi("GET", '/mur', ['refcamp' => $this->refCamp]);
}
public function printCoupons(string $hashcode, array $coupons)
{
return $this->callApi("GET", '/coupons/print', [
'refcamp' => $this->refCamp, 'hashcode' => $hashcode, 'coupons' => implode(",", $coupons)
]);
}
private function callApi($method, $url, $data = [], $path = '/api/v1')
{
$url = $this->endpoint . $path . $url;
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($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 (!curl_errno($curl)) {
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (!in_array($http_code, [200, 201])) {
return (object)[
'status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed - Code HTTP : ' . $http_code
];
}
} else {
return (object)[
'status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed : ' . curl_error($curl)
];
}
curl_close($curl);
return json_decode($result);
}
// * Added METHODS
/** .
* Update panier
*
* @param \LDC\LDCCRM\Models\Customer $crm_customer
* @param integer $crm_customer_id
* @param string $ref_site
* @param string $coupon_id
* @param integer $quantity
* @return array<string,string>
* 'status' => 'OK',
* 'link' => URL,
* 'verif' => URL,
* 'basketId' => 'ID'
*/
public function updatePanier(
Customer $crm_customer,
int $crm_customer_id,
string $ref_site,
string $coupon_id,
int $quantity = 1
): array {
$res = $this->callAPI("GET", "/updatebasket", [
'refsite' => $ref_site,
'refcamp' => $this->refCamp,
'refadherent' => $crm_customer_id,
'nom' => $crm_customer->lastname,
'prenom' => $crm_customer->firstname,
'email' => $crm_customer->email,
'code_postal' => $crm_customer->postcode,
'commune' => $crm_customer->city,
'pays' => $this->getCountry($crm_customer->country),
'panier' => "({$quantity}){$coupon_id}"
], '');
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];
}
/**
* Translate country to MesBr Country
*
* @param \LDC\LDCCRM\Enums\Country $country
* @return integer
*/
private function getCountry(Country $country): int
{
return match ($country) {
Country::belgium => 56,
Country::luxembourg => 442,
// * France
default => 250
};
}
}