ldc-mesbr-app/clients/MesBR_V1.php
2025-12-09 15:06:15 +01:00

162 lines
5.1 KiB
PHP

<?php
namespace LDCWEB;
use LDC\LDCCRM\Models\Customer;
class MesBR
{
private $endpoint;
private $refCamp;
private $refSite;
public function __construct($endpoint, $refCamp, $refSite)
{
$this->endpoint = $endpoint;
$this->refCamp = $refCamp;
$this->refSite = $refSite;
}
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)]);
}
public function getPrintMurCouponsUrl(string $hashcode, array $coupons)
{
return sprintf(
"%s/api/v1/coupons/print?type=dl&refcamp=%s&hashcode=%s&coupons=%s",
$this->endpoint,
$this->refCamp,
$hashcode,
implode(",", $coupons)
);
}
private function callAPI($method, $url, $data = [])
{
$url = $this->endpoint . '/api/v1' . $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);
}
// -------- New methods related to baskets (2025/10)
/** .
* Update basket
*
* @param \LDC\LDCCRM\Models\Customer $crm_customer
* @param string $coupon_id
* @param integer $quantity
* @return array<string,string>
* 'status' => 'OK',
* 'ref' => Basket REF,
*/
public function updateBasket(
Customer $crm_customer,
string $coupon_id,
int $quantity = 1
): array {
$res = $this->callAPI("GET", "/customer/basket/update", [
'refsite' => $this->refSite,
'refcamp' => $this->refCamp,
'customer_hashcode' => $crm_customer->hashcode,
'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];
}
return ['status' => 'OK', 'ref' => $res->Panier->ref];
}
/**
* Get customer basket
*
* @param \LDC\LDCCRM\Models\Customer $crm_customer
* @return array
*/
public function getBasket(
Customer $crm_customer,
): array {
$res = $this->callApi("GET", '/customer/basket', [
'refcamp' => $this->refCamp,
'hashcode' => $crm_customer->hashcode
]);
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->status === 'KO') {
error_log("MESBR - CODE : $res->code - MESSAGE : $res->message");
return ['status' => 'KO', 'code' => $res->code, 'message' => $res->message];
}
return ['status' => 'OK', 'coupons' => $res->coupons];
}
}