82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Lib;
|
|
|
|
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 = [])
|
|
{
|
|
$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);
|
|
}
|
|
}
|