diff --git a/Model/Validate.php b/Model/Validate.php index ae324b2..8e27530 100644 --- a/Model/Validate.php +++ b/Model/Validate.php @@ -27,9 +27,15 @@ */ class Validate extends \Magento\Framework\App\Config\Value { - const GET_SETTINGS_API_URL = 'http://api.ekomi.de/v3/getSettings'; - const SMART_CHECK_API_URL = 'https://srr.ekomi.com/api/v1/shops/setting'; + const GET_SETTINGS_API_URL = 'http://api.ekomi.de/v3/getSettings'; + const SMART_CHECK_API_URL = 'https://srr.ekomi.com/api/v1/shops/setting'; + const CUSTOMER_SEGMENT_URL = 'https://srr.ekomi.com/api/v1/customer-segments'; const ACCESS_DENIED_RESPONSE = 'Access denied'; + const SEGMENT_STATUS_ACTIVE = "active"; + const SEGMENT_STATUS_INACTIVE = "inactive"; + const HTTP_METHOD_GET = 'GET'; + const HTTP_METHOD_PUT = 'PUT'; + const HTTP_STATUS_OK = 200; /** * @var RequestInterface @@ -44,15 +50,15 @@ class Validate extends \Magento\Framework\App\Config\Value /** * Validate constructor. * - * @param Context $context - * @param Registry $registry - * @param ScopeConfigInterface $config - * @param TypeListInterface $cacheTypeList - * @param RequestInterface $request - * @param Curl $curl + * @param Context $context + * @param Registry $registry + * @param ScopeConfigInterface $config + * @param TypeListInterface $cacheTypeList + * @param RequestInterface $request + * @param Curl $curl * @param AbstractResource|null $resource - * @param AbstractDb|null $resourceCollection - * @param array $data + * @param AbstractDb|null $resourceCollection + * @param array $data */ public function __construct( Context $context, @@ -64,9 +70,10 @@ public function __construct( AbstractResource $resource = null, AbstractDb $resourceCollection = null, array $data = [] - ) { + ) + { $this->request = $request; - $this->curl = $curl; + $this->curl = $curl; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } @@ -81,17 +88,22 @@ public function beforeSave() $postData = $this->request->getPostValue(); $postValues = $postData['groups']['general']['fields']; $shopId = $postValues['shop_id']['value']; - $shopPw = $postValues['shop_password']['value']; + $shopPassword = $postValues['shop_password']['value']; $smartCheck = $postValues['smart_check']['value']; - $server_output = $this->verifyAccount($shopId, $shopPw); + $server_output = $this->verifyAccount($shopId, $shopPassword); if ($server_output == null || $server_output == self::ACCESS_DENIED_RESPONSE) { $this->setValue(0); $errorMsg = 'Access denied, Invalid Shop ID or Password'; $phrase = new Phrase($errorMsg); throw new LocalizedException($phrase); } else { - $this->updateSmartCheck($shopId, $shopPw, $smartCheck); + $customerSegment = $this->getSrrCustomerSegment($shopId, $shopPassword); + if ($customerSegment !== false && is_array($customerSegment)) { + $this->activateSrrCustomerSegment($shopId, $shopPassword, $customerSegment["id"]); + } + + $this->updateSmartCheck($shopId, $shopPassword, $smartCheck); return parent::beforeSave(); } @@ -101,12 +113,12 @@ public function beforeSave() * Validates eKomi account credentials * * @param string $shopId - * @param string $shopPw + * @param string $shopPassword * @return string */ - private function verifyAccount($shopId, $shopPw) + private function verifyAccount($shopId, $shopPassword) { - $apiUrl = self::GET_SETTINGS_API_URL . "?auth=" . $shopId . "|" . $shopPw . + $apiUrl = self::GET_SETTINGS_API_URL . "?auth=" . $shopId . "|" . $shopPassword . "&version=cust-1.0.0&type=request&charset=iso"; $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); @@ -116,6 +128,67 @@ private function verifyAccount($shopId, $shopPw) return $server_output; } + /** + * @param string $shopId + * @param string $shopPassword + * @return bool|array + */ + public function getSrrCustomerSegment($shopId, $shopPassword) + { + $apiUrl = self::CUSTOMER_SEGMENT_URL . '?status=' . self::SEGMENT_STATUS_INACTIVE; + + $this->configureCurl($shopId, $shopPassword, self::HTTP_METHOD_GET); + $this->curl->get($apiUrl); + $responseJson = $this->curl->getBody(); + + $response = json_decode($responseJson, true); + if ($response['status_code'] !== self::HTTP_STATUS_OK) { + return false; + } + + $customerSegments = $response['data']; + $defaultSegmentKey = $this->getDefaultSegmentKey($customerSegments); + if ($defaultSegmentKey === false) { + return false; + } + + return $customerSegments[$defaultSegmentKey]; + } + + /** + * @param array $customerSegments + * @return bool|int|string + */ + public function getDefaultSegmentKey($customerSegments) + { + foreach($customerSegments as $key => $customerSegment) + { + if ( $customerSegment['is_default'] == 'true' ) + return $key; + } + + return false; + } + + /** + * @param string $shopId + * @param string $shopPassword + * @param int $segmentId + * @return string + */ + public function activateSrrCustomerSegment($shopId, $shopPassword, $segmentId) + { + $apiUrl = self::CUSTOMER_SEGMENT_URL . '/' . $segmentId . '?status=' . self::SEGMENT_STATUS_ACTIVE; + $this->configureCurl($shopId, $shopPassword, self::HTTP_METHOD_PUT); + $this->curl->post( + $apiUrl, + [] + ); + $responseJson = $this->curl->getBody(); + + return $responseJson; + } + /** * Updates Smart Check value on SRR * @@ -126,17 +199,38 @@ private function verifyAccount($shopId, $shopPw) */ private function updateSmartCheck($shopId, $shopPassword, $smartCheckOn) { - $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); - $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'PUT'); - $this->curl->setOption(CURLOPT_POSTFIELDS, json_encode(['smartcheck_on' => (bool)$smartCheckOn])); - $this->curl->addHeader('shop-id', $shopId); - $this->curl->addHeader('interface-password', $shopPassword); + $this->configureCurl( + $shopId, + $shopPassword, + self::HTTP_METHOD_PUT, + ['smartcheck_on' => (bool)$smartCheckOn] + ); $this->curl->post( self::SMART_CHECK_API_URL, json_encode(['smartcheck_on' => $smartCheckOn]) ); - $server_output = $this->curl->getBody(); + $response = $this->curl->getBody(); - return $server_output; + return $response; } -} + + /** + * @param string $shopId + * @param string $shopPassword + * @param string $httpMethod + * @param array $postFields + */ + private function configureCurl($shopId, $shopPassword, $httpMethod = self::HTTP_METHOD_GET, $postFields = null) + { + $this->curl->addHeader('shop-id', $shopId); + $this->curl->addHeader('interface-password', $shopPassword); + $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); + if ($httpMethod === self::HTTP_METHOD_PUT) { + $this->curl->setOption(CURLOPT_CUSTOMREQUEST, self::HTTP_METHOD_PUT); + } + if ($postFields !== null && is_array($postFields)) { + $this->curl->setOption(CURLOPT_POSTFIELDS, json_encode($postFields)); + } + } + +} \ No newline at end of file diff --git a/README.md b/README.md index 0b9a7ad..28925cd 100644 --- a/README.md +++ b/README.md @@ -252,3 +252,4 @@ Our eKomi headquarters in Berlin is the best place to start if you need help wit | 1.3.0 | zip (15.3 kB) | 2018-Sep-01 | | 2.0.0 | zip (20.1 kB) | 2018-Nov-12 | | 2.0.1 | zip (20.0 kB) | 2018-Dec-04 | +| 2.1.0 | zip (20.8 kB) | 2019-Jan-10 | diff --git a/composer.json b/composer.json index fdd124e..8c2253f 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "ekomiltd/ekomiintegration", "description": "eKomi Plugin for Magento allows you to integrate your Magento shop easily with eKomi system. This allows you to collect verified reviews, display eKomi seal on your website and get your seller ratings on Google. This helps you increase your website's click through rates, conversion rates and also, if you are running Google AdWord Campaigns, this helps in improving your Quality Score and hence your costs per click.", "type": "magento2-module", - "version": "2.0.1", + "version": "2.1.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index ecb269a..9175278 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file