115 lines
3.9 KiB
PHP
115 lines
3.9 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");
|
|
}
|
|
|
|
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, $lastname, $optin, $birthdate = null, $phone = null, $address = null, $address2 = null, $city = null, $postcode = null, $country = null){
|
|
$user = [
|
|
'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('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]);
|
|
}
|
|
|
|
private function callAPI($method, $url, $data = false){
|
|
$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);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
|
|
'Accept: application/json',
|
|
'Authorization: Bearer ' . $this->api_key
|
|
));
|
|
|
|
$result = curl_exec($curl);
|
|
if(!$result)
|
|
return ['status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed'];
|
|
curl_close($curl);
|
|
return json_decode($result);
|
|
}
|
|
}
|