Skip to content

Commit

Permalink
Merge pull request #15 from ekomi-ltd/Feature/IT-12113-update-custome…
Browse files Browse the repository at this point in the history
…r-segment

IT-12113 Updating customer segment in SRR
  • Loading branch information
SyedAbdullahShah authored Jan 11, 2019
2 parents 8c27e61 + f52dc52 commit ce266fd
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 28 deletions.
146 changes: 120 additions & 26 deletions Model/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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);
}

Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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
*
Expand All @@ -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));
}
}

}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Ekomi_EkomiIntegration" setup_version="2.0.1" />
<module name="Ekomi_EkomiIntegration" setup_version="2.1.0" />
</config>

0 comments on commit ce266fd

Please sign in to comment.