magento2-docker/app/code/IpSupply/CustomerErp/Api/CustomerErpApi.php

156 lines
5.4 KiB
PHP
Executable File

<?php
namespace IpSupply\CustomerErp\Api;
use IpSupply\CustomerErp\Api\Api;
class CustomerErpApi extends Api
{
const ADMIN_EMAIL = "admin@apactech.io";
//const ADMIN_PASSWORD = "work1234@ct";
const CACHE_TOKEN = "jwt_token_";
//const HOST = 'https://api.nswteam.net';
const ADMIN_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2ludC5pcHN1cHBseS5jb20uYXUvYXBpL2xvZ2luIiwiaWF0IjoxNjg5ODYyNjAxLCJleHAiOjE3MjEzOTg2MDEsIm5iZiI6MTY4OTg2MjYwMSwianRpIjoiUElZVjNBM3ZPQVlMQ081SyIsInN1YiI6MSwicHJ2IjoiYzhlZTFmYzg5ZTc3NWVjNGM3Mzg2NjdlNWJlMTdhNTkwYjZkNDBmYyJ9.UcybIKMBjTAY9i0PfIDQMtqHyN72Ul0jC03ZDGLGpMI";
const HOST = 'https://int.ipsupply.com.au';
const API_LOGIN_URL = self::HOST . '/api/login';
const API_SET_PASSWORD = self::HOST . '/api/user/setpassword';
const API_TRANSFER_POST_DATA = self::HOST . '/api/transferPostData';
const API_FORGOT_PASSWORD = self::HOST . '/api/forgot-password';
protected $logHelper;
protected $objectManager;
protected $cache;
public function __construct()
{
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->cache = $this->objectManager->create('\Magento\Framework\App\CacheInterface');
}
public function loadCustomerErpCache($email)
{
return $this->cache->load(self::CACHE_TOKEN . $email);
}
public function setLogHelper($logHelper)
{
$this->logHelper = $logHelper;
}
private function createPostDataForLogin($email, $password)
{
return array(
"userEmail" => $email,
"password" => $password
);
}
private function getMessageDto($status, $code, $message, $body)
{
return array(
"status" => $status,
"code" => $code,
"message" => $message,
"body" => $body,
);
}
private function createCustomerCache($body){
return json_encode(array(
"token" => $body["token"],
"userId" => $body["data"]["userId"],
"userEmail" => $body["data"]["userEmail"],
"firstName" => $body["data"]["firstName"],
"lastName" => $body["data"]["lastName"],
));
}
public function loginAdmin()
{
// $response = $this->post(
// self::API_LOGIN_URL,
// array(
// "userEmail" => self::ADMIN_EMAIL,
// "password" => self::ADMIN_PASSWORD
// )
// );
// if ($response) {
// $body = null;
// if ($response->getBody()){
// $body = json_decode($response->getBody()->getContents(), true);
// }
// if ($response->getStatusCode() == 200) {
// $this->cache->save($this->createCustomerCache($body), self::CACHE_TOKEN . self::ADMIN_EMAIL, array(), null);
// }
// }
// $response->getBody()->rewind();
//return $response;
}
public function login($email, $password)
{
$data = $this->createPostDataForLogin($email, $password);
$response = $this->post(self::API_LOGIN_URL, $data);
if ($response->getStatusCode() == 200) {
$body = null;
if ($response->getBody()){
$body = json_decode($response->getBody()->getContents(), true);
}
$this->cache->save($this->createCustomerCache($body), self::CACHE_TOKEN . $email, array(), null);
}
$response->getBody()->rewind();
return $response;
}
public function createPostDataForAddCustomer($email, $firstName, $lastName, $password)
{
$raw_data_str = '{"urlAPI":"/api/user/data-save","data":{"firstName":"","lastName":"","password":"","orgId":"","type":"staff_of_partner","signature":null,"tels":[],"emails":[{"email":""}],"skypes":[],"group":"","positions":[null],"countryId":"","address1":"","address2":"","cityId":"","stateId":"","zipCode":"","postCode":"","notes":{"content":""},"shipTo":"0","billTo":"0","accessPortal":"1"}}';
$post_data = json_decode($raw_data_str, true);
$post_data["data"]["emails"][0]["email"] = $email;
$post_data["data"]["firstName"] = $firstName;
$post_data["data"]["lastName"] = $lastName;
$post_data["data"]["password"] = $password;
return $post_data;
}
public function createCustomer($email, $firstName, $lastName, $password)
{
//$customer_cache = $this->cache->load(self::CACHE_TOKEN . self::ADMIN_EMAIL);
// if (!empty($customer_cache)) {
// $customer_cache = json_decode($customer_cache, true);
// $this->setToken($customer_cache["token"]);
// }
$token = self::ADMIN_TOKEN;
$this->setToken($token);
$data = $this->createPostDataForAddCustomer($email, $firstName, $lastName, $password);
$response = $this->post(self::API_TRANSFER_POST_DATA, $data);
return $response;
}
public function setPassword($id, $password, $token)
{
$this->setToken($token);
$data = array(
"password" => $password
);
$response = $this->post(self::API_SET_PASSWORD."/".$id, $data);
return $response;
}
public function sendMailForgotPassword($email) {
$data = array(
"email" => $email
);
$response = $this->post(self::API_FORGOT_PASSWORD, $data);
return $response;
}
}