ldc-gen-game-web/class/LDCCRM_V1.php
2021-02-23 18:27:03 +01:00

162 lines
5.2 KiB
PHP

<?php
namespace 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);
}
}