- Update API

This commit is contained in:
Vincent BRIDIER 2021-08-16 12:27:07 +02:00
parent 46d24cc98f
commit f5d9991d90
3 changed files with 14 additions and 234 deletions

View file

@ -1,225 +0,0 @@
<?php
namespace App\Lib\LDCCRM;
class ApiV1
{
private $endpoint;
private $api_key;
public function __construct($endpoint, $api_key)
{
$this->endpoint = $endpoint;
$this->api_key = $api_key;
}
public function getCustomer($id)
{
return $this->callApi('GET', "/customer/id/$id");
}
public function getCustomerByEmail($email)
{
return $this->callApi('GET', "/customer/email/$email");
}
public function createCustomer(
$email,
$password,
$firstname,
$lastname,
$optin,
$birthdate = null,
$phone = null,
$address = null,
$address2 = null,
$city = null,
$postcode = null,
$country = null
) {
$user = [
'email' => $email,
'password' => $password,
'firstname' => $firstname,
'lastname' => $lastname,
'opt_in' => $optin,
'birth_date' => $birthdate,
'phone' => $phone,
'address' => $address,
'address2' => $address2,
'city' => $city,
'postcode' => $postcode,
'country' => $country
];
return $this->callApi('POST', '/customer/create', $user);
}
public function updateCustomer(
$crmId,
$firstname = null,
$lastname = null,
$optin = null,
$birthdate = null,
$phone = null,
$address = null,
$address2 = null,
$city = null,
$postcode = null,
$country = null
) {
$user = [];
if ($firstname !== null) {
$user['firstname'] = $firstname;
}
if ($lastname !== null) {
$user['lastname'] = $lastname;
}
if ($optin !== null) {
$user['opt_in'] = $optin;
}
if ($birthdate !== null) {
$user['birth_date'] = $birthdate;
}
if ($phone !== null) {
$user['phone'] = $phone;
}
if ($address !== null) {
$user['address'] = $address;
}
if ($address2 !== null) {
$user['address2'] = $address2;
}
if ($city !== null) {
$user['city'] = $city;
}
if ($postcode !== null) {
$user['postcode'] = $postcode;
}
if ($country !== null) {
$user['country'] = $country;
}
return $this->callApi('PUT', "/customer/$crmId", $user);
}
public function deleteCustomer($id)
{
return $this->callApi('DELETE', "/customer/$id");
}
public function changePwdCustomer($id, $password)
{
return $this->callApi('POST', "/customer/$id/change-password", ['password' => $password]);
}
public function activateCustomer($id)
{
return $this->callApi('POST', "/customer/$id/activate");
}
public function toggleOptInCustomer($id)
{
return $this->callApi('POST', "/customer/$id/toggle-optin");
}
public function loginCustomer($email, $password)
{
return $this->callApi('POST', "/customer/login", ['email' => $email, 'password' => $password]);
}
public function createSingleOptin($email)
{
return $this->callApi('POST', '/single-optin', ['email' => $email]);
}
public function arcadiaOutgoingMail($crm_id, $fields, $type)
{
return $this->callApi(
'POST',
'/arcadia/outgoing-mail',
['crm_id' => $crm_id, 'fields' => json_encode($fields), 'type' => $type]
);
}
private function callApi($method, $url, $data = false)
{
if (strpos($this->endpoint, "dev")) {
if ($data !== false) {
$data = array_merge($data, ['api_token' => $this->api_key]);
} else {
$data = ['api_token' => $this->api_key];
}
}
$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":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
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);
if (!strpos($this->endpoint, "dev")) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Authorization: Bearer ' . $this->api_key
));
}
$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);
}
}

View file

@ -7,12 +7,12 @@ class MesBR
private $endpoint;
private $api_key;
private $refCamp;
public function __construct($endpoint, $api_key)
public function __construct($endpoint, $refCamp)
{
$this->endpoint = $endpoint;
$this->api_key = $api_key;
$this->refCamp = $refCamp;
}
public function createCustomer(string $hashcode, string $email, string $country)
@ -25,14 +25,19 @@ class MesBR
return $this->callAPI("POST", '/customer', ['hashcode' => $hashcode, 'email' => $email, 'country' => $country]);
}
public function getMur(int $refCamp, string $hashcode)
public function getMurCustomer(string $hashcode)
{
return $this->callAPI("POST", '/mur', ['refcamp' => $refCamp, 'hashcode' => $hashcode]);
return $this->callAPI("GET", '/customer/mur', ['refcamp' => $this->refCamp, 'hashcode' => $hashcode]);
}
public function printCoupons(int $refCamp, string $hashcode, array $coupons)
public function getMur()
{
return $this->callAPI("POST", '/coupons/print', ['refcamp' => $refCamp, 'hashcode' => $hashcode, 'coupons' => implode(",", $coupons)]);
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 = [])

View file

@ -16,7 +16,7 @@ use Illuminate\Http\Request;
Route::prefix('v1')
->group(function () {
Route::resource('customer', 'Api\v1\CustomerController');
Route::post('/customer/mur', 'Api\v1\CustomerController@mur');
Route::get('/customer/mur', 'Api\v1\CustomerController@mur');
Route::get('/mur', 'Api\v1\MurController@show');
Route::post('/coupons/print', 'Api\v1\CouponsController@print');
Route::get('/coupons/print', 'Api\v1\CouponsController@print');
});