224 lines
7.6 KiB
PHP
224 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace LDC\MESBR;
|
|
|
|
use LDC\LDCCRM\Enums\Country;
|
|
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 printBasketCustomer(string $hashcode)
|
|
{
|
|
return $this->callApi("GET", '/customer/basket/print', ['refcamp' => $this->refCamp, 'hashcode' => $hashcode]);
|
|
}
|
|
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 string $crm_customer_hashcode
|
|
* @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,
|
|
string $crm_customer_hashcode,
|
|
string $ref_site,
|
|
string $coupon_id,
|
|
int $quantity = 1
|
|
): array {
|
|
$res = $this->callAPI("GET", "/updatebasket", [
|
|
'refsite' => $ref_site,
|
|
'refcamp' => $this->refCamp,
|
|
'refadherent' => $crm_customer_hashcode,
|
|
'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];
|
|
}
|
|
|
|
/** .
|
|
* 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->uuid];
|
|
}
|
|
|
|
/**
|
|
* 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', 'uuid' => $res->ref, 'coupons' => $res->coupons];
|
|
}
|
|
|
|
/**
|
|
* Translate country to MesBr Country
|
|
*
|
|
* @param \LDC\LDCCRM\Enums\Country $country
|
|
* @return inteager
|
|
*/
|
|
private function getCountry(Country $country): int
|
|
{
|
|
return match ($country) {
|
|
Country::belgium() => 56,
|
|
Country::luxembourg() => 442,
|
|
// * France
|
|
default => 250
|
|
};
|
|
}
|
|
}
|