79 lines
2.3 KiB
PHP
Executable File
79 lines
2.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\Prology\Api;
|
|
|
|
use IpSupply\Prology\Api\Api;
|
|
|
|
class ProductErpApi 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 API_LOGIN_URL = self::HOST . '/api/login';
|
|
const TRANSFER_GET_DATA = self::HOST . '/api/transferGetData';
|
|
|
|
|
|
|
|
protected $objectManager;
|
|
protected $cache;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$this->cache = $this->objectManager->create('\Magento\Framework\App\CacheInterface');
|
|
}
|
|
|
|
private function createPostDataForLogin($email, $password)
|
|
{
|
|
return array(
|
|
"userEmail" => $email,
|
|
"password" => $password
|
|
);
|
|
}
|
|
|
|
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->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);
|
|
}
|
|
return $response;
|
|
|
|
}
|
|
|
|
public function getProductFromErp(){
|
|
$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"]);
|
|
}
|
|
$query = json_decode('{"urlAPI":"/api/magento/sync-data","filter":{"skip":0,"limit":10,"where":{"update_time_from":"2020-04-05T01:02:10.189Z","update_time_to":""}}}', true);
|
|
$response = $this->get(self::TRANSFER_GET_DATA, $query);
|
|
return $response;
|
|
}
|
|
|
|
|
|
}
|