* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT * @license https://opensource.org/licenses/MIT MIT License */ require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_ServiceCache.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_JsonRPCClient.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_PasswordManager.php'; class TNTOfficiel_AddressHelper { /** * @var Singleton */ private static $_instance = null; /** * @var TNTOfficiel_Logger */ private $logger; /** * Constructor. */ public function __construct() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $this->logger = new TNTOfficiel_Logger(); } /** * Creates a singleton. * * @param void * * @return TNTOfficiel_AddressHelper */ public static function getInstance() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); if (is_null(TNTOfficiel_AddressHelper::$_instance)) { TNTOfficiel_AddressHelper::$_instance = new TNTOfficiel_AddressHelper(); } return TNTOfficiel_AddressHelper::$_instance; } /** * Get cities from the middleware for the given CP * Store the result in cache until midnight. * * @param string $postcode the postal code * @param int $idShop the shop id * * @return array */ public function getCitiesFromMiddleware($postcode, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objCache = Cache::getInstance(); // get params without timestamp $params = $this->_getCitiesParams($postcode, $idShop); $cache_key = 'middleware::getCities_'.md5(serialize($params)); //check if already in cache if (!$objCache->exists($cache_key)) { $client = new TNTOfficiel_JsonRPCClient(Configuration::get('TNT_CARRIER_MIDDLEWARE_URL')); try { $cities = $client->execute('getCities', $params); $this->logger->logMessageTnt('getCities', null, 'JSON', true, 'SUCCESS'); } catch (Exception $objException) { $strStatus = ($objException->getCode() == 500) ? 'FATAL' : 'ERROR'; $strMsg = $objException->getMessage(); $this->logger->logMessageTnt('getCities', $strMsg, 'JSON', false, $strStatus); return false; } //add in cache until midnight $objCache->set($cache_key, Tools::jsonEncode($cities), TNTOfficiel_ServiceCache::getSecondsUntilMidnight()); return $cities; } $cities = Tools::jsonDecode($objCache->get($cache_key), true); return $cities; } /** * Get the data needed by the middleware. * * @param string $postcode the postal code * @param int $idShop the shop id * * @return array */ private function _getCitiesParams($postcode, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $data = array( 'store' => $idShop, 'merchant' => array( 'identity' => Configuration::get('TNT_CARRIER_USERNAME'), 'password' => TNTOfficiel_PasswordManager::getHash(), 'merchant_number' => Configuration::get('TNT_CARRIER_ACCOUNT'), ), 'postcode' => $postcode, ); return $data; } /** * ask the middleware to check if the city match the postcode. * * @param $postcode * @param $city * @param $idShop * * @return mixed */ public function checkPostcodeCityFromMiddleware($postcode, $city, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objCache = Cache::getInstance(); // get params without timestamp $params = $this->_getTestCityParams($postcode, $city, $idShop); $cache_key = 'middleware::testCity_'.md5(serialize($params)); //check if already in cache if (!$objCache->exists($cache_key)) { $client = new TNTOfficiel_JsonRPCClient(Configuration::get('TNT_CARRIER_MIDDLEWARE_URL')); try { $result = $client->execute('testCity', $params); $this->logger->logMessageTnt('testCity', null, 'JSON', true, 'SUCCESS'); } catch (Exception $objException) { $strStatus = ($objException->getCode() == 500) ? 'FATAL' : 'ERROR'; $strMsg = $objException->getMessage(); $this->logger->logMessageTnt('testCity', $strMsg, 'JSON', false, $strStatus); return false; } //add in cache until midnight $objCache->set($cache_key, Tools::jsonEncode($result), TNTOfficiel_ServiceCache::getSecondsUntilMidnight()); return $result; } $result = Tools::jsonDecode($objCache->get($cache_key), true); return $result; } public function getAddressData($addressId, $cartId) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $strSQL = ' SELECT a.*, tc.* FROM '._DB_PREFIX_.'orders o INNER JOIN '._DB_PREFIX_.'address a on a.id_address = o.id_address_delivery INNER JOIN '._DB_PREFIX_.'tntofficiel_cart tc on tc.id_cart = o.id_cart WHERE a.id_address = '.(int)$addressId.' and tc.id_cart = '.(int)$cartId; $result = Db::getInstance()->getRow($strSQL, false); if (!$result || count($result) == 0) { throw new Exception('No address was found for the address '.$addressId.' - '.$cartId); } return $result; } /** * Get the data needed by the middleware to check the city/postcode. * * @param $postcode * @param $city * @param $idShop * * @return array */ private function _getTestCityParams($postcode, $city, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $data = $this->_getCitiesParams($postcode, $idShop); $data['city'] = $city; return $data; } /** * Check the postcode/city validity for a cart. * * @param $cart * * @return bool */ public function checkPostCodeCityForCart($cart) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $deliveryAddress = new Address($cart->id_address_delivery); $countryIsoCode = Country::getIsoById($deliveryAddress->id_country); $idShop = (int) Context::getContext()->shop->id; //do not check if the address is not in france if ($countryIsoCode == 'FR') { $result = $this->checkPostcodeCityFromMiddleware( $deliveryAddress->postcode, $deliveryAddress->city, $idShop ); return $result['response']; } return true; } }