SweOptipricer - Version 0.1.1

Version Notes

Testing candidate

Download this release

Release Info

Developer Rui Mendes
Extension SweOptipricer
Version 0.1.1
Comparing to
See all releases


Code changes from version 0.1.0 to 0.1.1

app/code/{local → community}/SWE/Optipricer/Block/Button.php RENAMED
@@ -4,16 +4,18 @@
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
- * @copyright 2014 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.2
10
  */
11
  class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
12
  {
13
  const URL_ENDPOINT_CONTACT = 'contact/email';
14
 
15
  /**
16
- * Set template
 
 
17
  */
18
  protected function _construct()
19
  {
@@ -24,8 +26,9 @@ class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_For
24
  /**
25
  * Return element html
26
  *
27
- * @param Varien_Data_Form_Element_Abstract $element
28
- * @return string
 
29
  */
30
  protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
31
  {
@@ -35,19 +38,20 @@ class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_For
35
  /**
36
  * Return ajax url for button
37
  *
38
- * @return string
39
  */
40
  public function getAjaxCheckUrl()
41
  {
42
  $endPoint = Mage::getStoreConfig('swe/swe_group_activation/swe_endpoint', Mage::app()->getStore());
43
  $uriPageView = $endPoint.self::URL_ENDPOINT_CONTACT;
 
44
  return $uriPageView;
45
  }
46
 
47
  /**
48
  * Generate button html
49
  *
50
- * @return string
51
  */
52
  public function getButtonHtml()
53
  {
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
+ * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.1
10
  */
11
  class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
12
  {
13
  const URL_ENDPOINT_CONTACT = 'contact/email';
14
 
15
  /**
16
+ * Construct command
17
+ *
18
+ * @return void
19
  */
20
  protected function _construct()
21
  {
26
  /**
27
  * Return element html
28
  *
29
+ * @param Varien_Data_Form_Element_Abstract $element Element
30
+ *
31
+ * @return String
32
  */
33
  protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
34
  {
38
  /**
39
  * Return ajax url for button
40
  *
41
+ * @return String
42
  */
43
  public function getAjaxCheckUrl()
44
  {
45
  $endPoint = Mage::getStoreConfig('swe/swe_group_activation/swe_endpoint', Mage::app()->getStore());
46
  $uriPageView = $endPoint.self::URL_ENDPOINT_CONTACT;
47
+
48
  return $uriPageView;
49
  }
50
 
51
  /**
52
  * Generate button html
53
  *
54
+ * @return String
55
  */
56
  public function getButtonHtml()
57
  {
app/code/{local → community}/SWE/Optipricer/Block/Discount.php RENAMED
@@ -4,31 +4,75 @@
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
- * @copyright 2014 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.2
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
13
  /**
14
- * A model to serialize attributes
15
- * @var Varien_Object
16
  */
17
- protected $_serializer = null;
18
-
19
  private $token;
 
 
 
 
20
  private $key;
 
 
 
 
21
  private $enabledGlobal;
 
 
 
 
22
  private $enabledLocal;
 
 
 
 
23
  private $minDiscount;
 
 
 
 
24
  private $maxDiscount;
25
- private $text;
 
 
 
26
  private $endPoint;
 
 
 
 
27
  private $pageView;
 
 
 
 
28
  private $renderView;
 
 
 
 
29
  private $locale;
 
 
 
 
30
  private $expiryOffset;
 
 
 
 
31
  private $backgroundColor;
 
 
 
 
32
  private $colorFont;
33
 
34
  const HTTP_OK = 200;
@@ -36,7 +80,9 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
36
  const URL_ENDPOINT_PAGEVIEW = 'pageview';
37
 
38
  /**
39
- * Initialization
 
 
40
  */
41
  protected function _construct()
42
  {
@@ -46,17 +92,20 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
46
  $this->enabledGlobal = Mage::getStoreConfig('swe/swe_group_activation/swe_enable', Mage::app()->getStore());
47
  $this->minDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_min', Mage::app()->getStore());
48
  $this->maxDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_max', Mage::app()->getStore());
49
- $this->text = '';
50
  $this->pageView = Mage::getStoreConfig('swe/swe_group_parameters/swe_pageview', Mage::app()->getStore());
51
  $this->renderView = Mage::getStoreConfig('swe/swe_group_parameters/swe_renderview', Mage::app()->getStore());
52
  $this->expiryOffset = Mage::getStoreConfig('swe/swe_group_parameters/swe_expiryoffset', Mage::app()->getStore());
53
  $this->backgroundColor = Mage::getStoreConfig('swe/swe_group_parameters/swe_background_color', Mage::app()->getStore());
54
  $this->colorFont = Mage::getStoreConfig('swe/swe_group_parameters/swe_font_color', Mage::app()->getStore());
55
  $this->locale = Mage::app()->getLocale()->getLocaleCode();
56
- $this->_serializer = new Varien_Object();
57
  parent::_construct();
58
  }
59
 
 
 
 
 
 
60
  protected function _prepareLayout() {
61
  $this->getLayout()->getBlock('head')->addJs('swe/optipricer.min.js');
62
  $this->getLayout()->getBlock('head')->addJs('swe/optispin.min.js');
@@ -64,7 +113,7 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
64
  }
65
 
66
  /**
67
- * Produces links list html
68
  *
69
  * @return string
70
  */
@@ -72,12 +121,8 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
72
  {
73
  $this->loadLocalParameters();
74
 
75
- //ToDo: remove Debug
76
- $this->printDebug(false);
77
-
78
  $result = $html = '';
79
- //Global parameter of the service ($enabled)
80
- if (!$this->enabledGlobal || !$this->enabledLocal || !$this->token || !$this->key) {
81
  return $html;
82
  }
83
 
@@ -85,16 +130,14 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
85
  $data = $this->getProductDetails();
86
  $data['min'] = $this->minDiscount;
87
  $data['max'] = $this->maxDiscount;
88
- $data['text'] = $this->text;
89
  $data['discount_render'] = $this->renderView;
90
  $data['expiry_offset'] = $this->expiryOffset;
91
- //ToDo: get FacebookId if exists any info about it
92
  $data['social_credentials'] = array('facebookId' => '', 'facebookToken' => '');
93
-
94
  $securedData['data'] = $this->secureContent(json_encode($data));
95
  $securedData['social_credentials'] = $data['social_credentials'];
96
 
97
- //Check if pageView parameter is enabled
98
  if ($this->pageView) {
99
  $result = $this->updatePageView($securedData, $this->renderView);
100
  }
@@ -132,6 +175,14 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
132
  return parent::_toHtml();
133
  }
134
 
 
 
 
 
 
 
 
 
135
  private function updatePageView($data, $renderView = false)
136
  {
137
  $uriPageView = $this->endPoint.self::URL_ENDPOINT_PAGEVIEW;
@@ -147,6 +198,16 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
147
  return $response;
148
  }
149
 
 
 
 
 
 
 
 
 
 
 
150
  private function executeApiRequest($uri, $method, $headers = array(), $rawData = '')
151
  {
152
  $curl = curl_init();
@@ -171,7 +232,6 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
171
  $content = curl_exec($curl);
172
  if(curl_errno($curl)) {
173
  curl_close($curl);
174
- //ToDo: send an email to alert the error
175
  return false;
176
  }
177
  $info = curl_getinfo($curl);
@@ -179,17 +239,24 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
179
  $result['content'] = $content;
180
  // Close request to clear up some resources
181
  curl_close($curl);
 
182
  return $result;
183
  }
184
 
 
 
 
 
 
185
  private function getProductDetails()
186
  {
187
  $product = Mage::registry('current_product');
 
188
  $productDetails = array();
189
  $productDetails['product_id'] = $product->getId();
190
  $productDetails['name'] = $product->getName();
191
  $productDetails['description'] = $product->description;
192
- $productDetails['price'] = $product->getPrice();
193
  $productDetails['product_barcode'] = $product->getBarcode();
194
  $productDetails['image_url'] = $product->getImageUrl();
195
  $productDetails['link'] = $product->getProductUrl();
@@ -204,14 +271,19 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
204
  }
205
  }
206
  $productDetails['categories'] = $categoriesAux;
207
- $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
208
  $currentCurrencySymbol = Mage::app()->getLocale()->currency($currentCurrencyCode)->getSymbol();
209
  $productDetails['currency'] = $currentCurrencySymbol;
210
- $productDetails['formatted_price'] = Mage::helper('core')->formatPrice($product->getPrice(), false);
211
 
212
  return $productDetails;
213
  }
214
 
 
 
 
 
 
215
  private function loadLocalParameters()
216
  {
217
  $this->enabledLocal = $this->getData('enable_service');
@@ -241,6 +313,14 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
241
  }
242
  }
243
 
 
 
 
 
 
 
 
 
244
  private function secureContent($content, $task = 'encrypt')
245
  {
246
  if ($task == 'decrypt') {
@@ -294,27 +374,4 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
294
 
295
  return $content;
296
  }
297
-
298
- //ToDo: remove
299
- private function printDebug($debug) {
300
- /*************** ToDo: remove *************/
301
- if ($debug) {
302
- echo "<br />--------- DEBUG -------<br/>";
303
- echo "<b>Enabled Global</b>: ".$this->enabledGlobal;
304
- echo "<br /><b>Enabled Local</b>: ".$this->enabledLocal;
305
- echo "<br /><b>EndPoint</b>: ".$this->endPoint;
306
- echo "<br /><b>Token</b>: ".$this->token;
307
- echo "<br /><b>Key</b>: ".$this->key;
308
- echo "<br /><b>Min</b>: ".$this->minDiscount;
309
- echo "<br /><b>Max</b>: ".$this->maxDiscount;
310
- echo "<br /><b>PageView</b>: ".$this->pageView;
311
- echo "<br /><b>renderView</b>: ".$this->renderView;
312
- echo "<br /><b>ExpiryOffset</b>: ".$this->expiryOffset;
313
- echo "<br /><b>Locale</b>: ".$this->locale;
314
- echo "<br /><b>ColorFont</b>: ".$this->colorFont;
315
- echo "<br /><b>Background Color</b>: ".$this->backgroundColor;
316
- echo "<br />-------- END DEBUG -----<br /><br />";
317
- }
318
- /*************** ToDo: remove *************/
319
- }
320
  }
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
+ * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.1
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
13
  /**
14
+ * @var String Token Store
 
15
  */
 
 
16
  private $token;
17
+
18
+ /**
19
+ * @var String Encrypt Key
20
+ */
21
  private $key;
22
+
23
+ /**
24
+ * @var bool EnabledGlobal flag
25
+ */
26
  private $enabledGlobal;
27
+
28
+ /**
29
+ * @var bool EnabledLocal flag
30
+ */
31
  private $enabledLocal;
32
+
33
+ /**
34
+ * @var Int Minimum Discount
35
+ */
36
  private $minDiscount;
37
+
38
+ /**
39
+ * @var Int Maximum Discount
40
+ */
41
  private $maxDiscount;
42
+
43
+ /**
44
+ * @var String EndPoint
45
+ */
46
  private $endPoint;
47
+
48
+ /**
49
+ * @var bool PageView flag
50
+ */
51
  private $pageView;
52
+
53
+ /**
54
+ * @var bool RenderView flag
55
+ */
56
  private $renderView;
57
+
58
+ /**
59
+ * @var String Locale
60
+ */
61
  private $locale;
62
+
63
+ /**
64
+ * @var Int Expiry Offset time (minutes)
65
+ */
66
  private $expiryOffset;
67
+
68
+ /**
69
+ * @var String Background Color of the Button
70
+ */
71
  private $backgroundColor;
72
+
73
+ /**
74
+ * @var String Font Color of the Button
75
+ */
76
  private $colorFont;
77
 
78
  const HTTP_OK = 200;
80
  const URL_ENDPOINT_PAGEVIEW = 'pageview';
81
 
82
  /**
83
+ * Initialization command
84
+ *
85
+ * @return void
86
  */
87
  protected function _construct()
88
  {
92
  $this->enabledGlobal = Mage::getStoreConfig('swe/swe_group_activation/swe_enable', Mage::app()->getStore());
93
  $this->minDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_min', Mage::app()->getStore());
94
  $this->maxDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_max', Mage::app()->getStore());
 
95
  $this->pageView = Mage::getStoreConfig('swe/swe_group_parameters/swe_pageview', Mage::app()->getStore());
96
  $this->renderView = Mage::getStoreConfig('swe/swe_group_parameters/swe_renderview', Mage::app()->getStore());
97
  $this->expiryOffset = Mage::getStoreConfig('swe/swe_group_parameters/swe_expiryoffset', Mage::app()->getStore());
98
  $this->backgroundColor = Mage::getStoreConfig('swe/swe_group_parameters/swe_background_color', Mage::app()->getStore());
99
  $this->colorFont = Mage::getStoreConfig('swe/swe_group_parameters/swe_font_color', Mage::app()->getStore());
100
  $this->locale = Mage::app()->getLocale()->getLocaleCode();
 
101
  parent::_construct();
102
  }
103
 
104
+ /**
105
+ * Prepare Layout
106
+ *
107
+ * @return void
108
+ */
109
  protected function _prepareLayout() {
110
  $this->getLayout()->getBlock('head')->addJs('swe/optipricer.min.js');
111
  $this->getLayout()->getBlock('head')->addJs('swe/optispin.min.js');
113
  }
114
 
115
  /**
116
+ * Produces the widget html
117
  *
118
  * @return string
119
  */
121
  {
122
  $this->loadLocalParameters();
123
 
 
 
 
124
  $result = $html = '';
125
+ if (!$this->enabledGlobal || !$this->enabledLocal) {
 
126
  return $html;
127
  }
128
 
130
  $data = $this->getProductDetails();
131
  $data['min'] = $this->minDiscount;
132
  $data['max'] = $this->maxDiscount;
133
+ $data['text'] = '';
134
  $data['discount_render'] = $this->renderView;
135
  $data['expiry_offset'] = $this->expiryOffset;
 
136
  $data['social_credentials'] = array('facebookId' => '', 'facebookToken' => '');
 
137
  $securedData['data'] = $this->secureContent(json_encode($data));
138
  $securedData['social_credentials'] = $data['social_credentials'];
139
 
140
+ //PageView feature
141
  if ($this->pageView) {
142
  $result = $this->updatePageView($securedData, $this->renderView);
143
  }
175
  return parent::_toHtml();
176
  }
177
 
178
+ /**
179
+ * Update Page View feature
180
+ *
181
+ * @param String $data Data
182
+ * @param Bool $renderView RenderView flag
183
+ *
184
+ * @return mixed
185
+ */
186
  private function updatePageView($data, $renderView = false)
187
  {
188
  $uriPageView = $this->endPoint.self::URL_ENDPOINT_PAGEVIEW;
198
  return $response;
199
  }
200
 
201
+ /**
202
+ * Request execution
203
+ *
204
+ * @param String $uri URL endpoint
205
+ * @param String $method Method
206
+ * @param Array $headers Headers
207
+ * @param String $rawData Raw Data
208
+ *
209
+ * @return mixed
210
+ */
211
  private function executeApiRequest($uri, $method, $headers = array(), $rawData = '')
212
  {
213
  $curl = curl_init();
232
  $content = curl_exec($curl);
233
  if(curl_errno($curl)) {
234
  curl_close($curl);
 
235
  return false;
236
  }
237
  $info = curl_getinfo($curl);
239
  $result['content'] = $content;
240
  // Close request to clear up some resources
241
  curl_close($curl);
242
+
243
  return $result;
244
  }
245
 
246
+ /**
247
+ * get Product details
248
+ *
249
+ * @return array
250
+ */
251
  private function getProductDetails()
252
  {
253
  $product = Mage::registry('current_product');
254
+ $price = $product->getFinalPrice();;
255
  $productDetails = array();
256
  $productDetails['product_id'] = $product->getId();
257
  $productDetails['name'] = $product->getName();
258
  $productDetails['description'] = $product->description;
259
+ $productDetails['price'] = $price;
260
  $productDetails['product_barcode'] = $product->getBarcode();
261
  $productDetails['image_url'] = $product->getImageUrl();
262
  $productDetails['link'] = $product->getProductUrl();
271
  }
272
  }
273
  $productDetails['categories'] = $categoriesAux;
274
+ $currentCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
275
  $currentCurrencySymbol = Mage::app()->getLocale()->currency($currentCurrencyCode)->getSymbol();
276
  $productDetails['currency'] = $currentCurrencySymbol;
277
+ $productDetails['formatted_price'] = Mage::helper('core')->formatPrice($price, false);
278
 
279
  return $productDetails;
280
  }
281
 
282
+ /**
283
+ * Load Local Parameters of the Widget
284
+ *
285
+ * @return void
286
+ */
287
  private function loadLocalParameters()
288
  {
289
  $this->enabledLocal = $this->getData('enable_service');
313
  }
314
  }
315
 
316
+ /**
317
+ * Method to secure content
318
+ *
319
+ * @param String $content Content
320
+ * @param String $task Task to be done
321
+ *
322
+ * @return String
323
+ */
324
  private function secureContent($content, $task = 'encrypt')
325
  {
326
  if ($task == 'decrypt') {
374
 
375
  return $content;
376
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  }
app/code/{local → community}/SWE/Optipricer/Helper/Data.php RENAMED
@@ -1,12 +1,12 @@
1
  <?php
2
  /**
3
- * Default helper of the module
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
- * @copyright 2014 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.2
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
1
  <?php
2
  /**
3
+ * Widget to share the product in Facebook and get a personalized discount
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
+ * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.1
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
app/code/{local → community}/SWE/Optipricer/Model/Config.php RENAMED
@@ -1,12 +1,12 @@
1
  <?php
2
  /**
3
- * Class SWE_Optipricer_Model_Observer
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
- * @copyright 2014 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.2
10
  */
11
  class SWE_Optipricer_Model_Config
12
  {
1
  <?php
2
  /**
3
+ * Widget to share the product in Facebook and get a personalized discount
4
  *
5
  * @package SWE_Optipricer
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
+ * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.1
10
  */
11
  class SWE_Optipricer_Model_Config
12
  {
app/code/community/SWE/Optipricer/Model/Observer.php ADDED
@@ -0,0 +1,590 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Widget to share the product in Facebook and get a personalized discount
5
+ *
6
+ * @package SWE_Optipricer
7
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
+ * @copyright 2015 be.ubi
9
+ * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.1
11
+ */
12
+ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
+ {
14
+ const STATE_CANCELED = 'canceled';
15
+ const URL_ENDPOINT_COUPON = 'coupon';
16
+ const URL_ENDPOINT_REDEEM = 'redeem';
17
+ const URL_SEPARATOR = '/';
18
+ const SWE_CUSTOM_OPTION_KEY = 'SWE_Optipricer_Coupon';
19
+ const LOG_KEY = 'OPTIPRICER# ';
20
+
21
+ /**
22
+ * @var String Token
23
+ */
24
+ private $token;
25
+
26
+ /**
27
+ * @var String Encrypt Key
28
+ */
29
+ private $key;
30
+
31
+ /**
32
+ * @var bool EnabledGlobal flag
33
+ */
34
+ private $enabledGlobal;
35
+
36
+ /**
37
+ * @var Int Minimum Discount
38
+ */
39
+ private $minDiscount;
40
+
41
+ /**
42
+ * @var Int Maximum Discount
43
+ */
44
+ private $maxDiscount;
45
+
46
+ /**
47
+ * @var @var String EndPoint
48
+ */
49
+ private $endPoint;
50
+
51
+ /**
52
+ * @var bool PageView flag
53
+ */
54
+ private $pageView;
55
+
56
+ /**
57
+ * @var bool RenderView flag
58
+ */
59
+ private $renderView;
60
+
61
+ /**
62
+ * @var String Locale
63
+ */
64
+ private $locale;
65
+
66
+ /**
67
+ * @var Int Expiry Offset time (minutes)
68
+ */
69
+ private $expiryOffset;
70
+
71
+ /**
72
+ * @var Array Products for Redeem Request
73
+ */
74
+ private $products;
75
+
76
+ /**
77
+ * Initialization command
78
+ */
79
+ protected function _construct()
80
+ {
81
+ $this->token = Mage::getStoreConfig('swe/swe_group_activation/swe_token',Mage::app()->getStore());
82
+ $this->key = Mage::getStoreConfig('swe/swe_group_activation/swe_key', Mage::app()->getStore());
83
+ $this->enabledGlobal = Mage::getStoreConfig('swe/swe_group_activation/swe_enable',Mage::app()->getStore());
84
+ $this->endPoint = Mage::getStoreConfig('swe/swe_group_activation/swe_endpoint',Mage::app()->getStore());
85
+ $this->minDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_min',Mage::app()->getStore());
86
+ $this->maxDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_max',Mage::app()->getStore());
87
+ $this->pageView = Mage::getStoreConfig('swe/swe_group_parameters/swe_pageview',Mage::app()->getStore());
88
+ $this->renderView = Mage::getStoreConfig('swe/swe_group_parameters/swe_renderview',Mage::app()->getStore());
89
+ $this->expiryOffset = Mage::getStoreConfig('swe/swe_group_parameters/swe_expiryoffset',Mage::app()->getStore());
90
+ $this->locale = Mage::app()->getLocale()->getLocaleCode();
91
+ $this->products = array();
92
+ }
93
+
94
+ /**
95
+ * Cart validation
96
+ * Verify each price for each discount given
97
+ *
98
+ * @param Varien_Event_Observer $observer Observer
99
+ *
100
+ * @return void
101
+ */
102
+ public function validateCart(Varien_Event_Observer $observer)
103
+ {
104
+ $this->_construct();
105
+ if (!$this->enabledGlobal) {
106
+ return;
107
+ }
108
+ $session = Mage::getSingleton('checkout/session');
109
+ /** @var Mage_Sales_Model_Quote $quote */
110
+ $quote = $session->getQuote();
111
+ $quote->setIsMultiShipping(false);
112
+ try {
113
+ //Validate all quote items (check if is necessary validate an Optipricer discount)
114
+ foreach($quote->getAllItems() as $item) {
115
+ $prodSessionItems = $this->getProductSessionItems(self::SWE_CUSTOM_OPTION_KEY.'_'.$item->getProductId());
116
+ $error = $this->validateQuoteItem($item, $prodSessionItems);
117
+ if ($error) {
118
+ $this->logOptipricer(self::LOG_KEY.'_validateCart_'.$error);
119
+ }
120
+ }
121
+ $quote->setTotalsCollectedFlag(false);
122
+ $quote->save();
123
+ } catch (Exception $e) {
124
+ $this->logOptipricer(self::LOG_KEY.'_validateCart_'.$e);
125
+ }
126
+ return;
127
+ }
128
+
129
+ /**
130
+ * Send Order status to Optipricer System
131
+ *
132
+ * @param Varien_Event_Observer $observer Observer
133
+ *
134
+ * @return void
135
+ */
136
+ public function sendOrderStatus(Varien_Event_Observer $observer)
137
+ {
138
+ $this->_construct();
139
+ if (!$this->enabledGlobal) {
140
+ return;
141
+ }
142
+ $cookiesToRemove = array();
143
+ $session = Mage::getSingleton('checkout/session');
144
+ /** @var Mage_Sales_Model_Quote $quote */
145
+ $quote = $session->getQuote();
146
+ $errors = array();
147
+
148
+ try {
149
+ $this->products = $this->getAllQuoteProducts($quote);
150
+ //Validate each product (that has a coupon assigned to it)
151
+ foreach($quote->getAllItems() as $item)
152
+ {
153
+ $productId = $item->getProductId();
154
+ $sessionKey = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
155
+ $prodSessionItems = $this->getProductSessionItems($sessionKey);
156
+ if ($prodSessionItems) {
157
+ //Check if the product needs to be validated (has an Optipricer discount)
158
+ $error = $this->validateQuoteItem($item, $prodSessionItems, false);
159
+ if ($error) {
160
+ $errors[] = $item->getName();
161
+ } else {
162
+ //Try to Redeem coupon
163
+ $cookieParams = $this->getCookieParams($this->token, $productId);
164
+ $error = $this->tryRedeem($item, $cookieParams);
165
+ if ($error) {
166
+ $errors[] = $item->getName();
167
+ }
168
+ }
169
+ }
170
+ }
171
+ } catch (Exception $e){
172
+ $this->logOptipricer(self::LOG_KEY.'_sendOrderStatus_'.$e);
173
+ return;
174
+ }
175
+ $this->processCheckoutResult($errors, $cookiesToRemove);
176
+ }
177
+
178
+ /************************* AUXILIARY METHODS ***************************/
179
+
180
+ /**
181
+ * Check if the product has a discount and validate it if exists
182
+ *
183
+ * @param Mage_Sales_Model_Quote_Item $item Item
184
+ * @param Array $prodSessionItems Session Items
185
+ * @param Boolean $cart Add to Cart action
186
+ *
187
+ * @return string
188
+ */
189
+ private function validateQuoteItem($item, $prodSessionItems, $cart = true)
190
+ {
191
+ $error = null;
192
+ try{
193
+ $product = $item->getProduct();
194
+ $productId = $item->getProductId();
195
+ //Check if ProductId is in Session Checkout (SWE_Optipricer_Coupon_ID)
196
+ $sessionKey = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
197
+ if ($prodSessionItems) {
198
+ //Validate Session Parameters matching with the Cookie
199
+ list($item, $error) = $this->validateSessionParamsWithCookie($item, $prodSessionItems,$sessionKey);
200
+ } else {
201
+ if ($cart && $this->hasCookie($this->token, $productId)) {
202
+ // Optipricer discount was activated and this is the first time adding to the cart
203
+ $cookieParams = $this->getCookieParams($this->token, $productId);
204
+ if (count($cookieParams)) {
205
+ $item = $this->changeItemPrice($item, $product->getFinalPrice(), $cookieParams['finalPrice']);
206
+ $item = $this->createSessionParameter($item);
207
+ } else {
208
+ $this->removeCookie('discount_'.$this->token.'_'.$productId);
209
+ }
210
+ }
211
+ }
212
+ $item->save();
213
+ } catch (Exception $e) {
214
+ $this->logOptipricer(self::LOG_KEY.'_validateQuoteItem_'.$e);
215
+ }
216
+
217
+ return $error;
218
+ }
219
+
220
+ /**
221
+ * Method to redeem a given coupon
222
+ *
223
+ * @param String $discountToken Discount token
224
+ *
225
+ * @return bool
226
+ */
227
+ private function redeemCoupon($discountToken)
228
+ {
229
+
230
+ $url = $this->endPoint.self::URL_ENDPOINT_COUPON.
231
+ self::URL_SEPARATOR.$discountToken.self::URL_SEPARATOR.self::URL_ENDPOINT_REDEEM;
232
+
233
+ $client = new Zend_Http_Client($url);
234
+ $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, 'application/json');
235
+ $client->setHeaders('Authorization', 'Token '.$this->token);
236
+ $data = array(
237
+ 'tokens' => array($discountToken),
238
+ 'notes' => $this->products,
239
+ 'location' => ''
240
+ );
241
+ $securedData['data'] = $this->secureContent(json_encode($data));
242
+ $client->setRawData(json_encode($securedData));
243
+ try {
244
+ $response = $client->request('PUT');
245
+ return $response->isSuccessful();
246
+ } catch (Exception $e) {
247
+ $this->logOptipricer(self::LOG_KEY.'_redeemCoupon_'.$e);
248
+ return false;
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Method to collect all products from the quote
254
+ *
255
+ * @param Mage_Sales_Model_Service_Quote $quote Quote
256
+ *
257
+ * @throws Exception
258
+ *
259
+ * @return array
260
+ */
261
+ private function getAllQuoteProducts($quote)
262
+ {
263
+ $products = array();
264
+ foreach($quote->getAllItems() as $item)
265
+ {
266
+ $productId = $item->getProductId();
267
+ $product = $item->getProduct();
268
+ //Check if ProductId is in Session Checkout (SWE_Optipricer_Coupon_ID)
269
+ $sessionItem = Mage::getSingleton('checkout/session')->getData(self::SWE_CUSTOM_OPTION_KEY.'_'.$productId);
270
+ $productAux = array();
271
+ $productAux['productId'] = $productId;
272
+ $productAux['name'] = $product->getName();
273
+ if ($sessionItem) {
274
+ $productAux['hasOptipricerDiscount'] = 'Yes';
275
+ }
276
+ $brand = $product->getAttributeText('manufacturer');
277
+ $productAux['product_brand'] = $brand ? $brand : '';
278
+ $categories = $product->getCategoryIds();
279
+ $categoriesAux = array();
280
+ foreach ($categories as $category_id) {
281
+ $_cat = Mage::getModel('catalog/category')->load($category_id);
282
+ if ($_cat->getName() && !in_array($_cat->getName(), $categoriesAux)) {
283
+ $categoriesAux[] = $_cat->getName();
284
+ }
285
+ }
286
+ $productAux['categories'] = $categoriesAux;
287
+ $productAux['finalValue'] = $product->getFinalPrice();
288
+ $products[] = $productAux;
289
+ }
290
+ return $products;
291
+ }
292
+
293
+ /**
294
+ * Get original price stored in the session
295
+ *
296
+ * @param String $optiKeySession Optipricer Key Session
297
+ *
298
+ * @return string
299
+ */
300
+ private function getOriginalPriceBySession($optiKeySession)
301
+ {
302
+ $sessionItem = Mage::getSingleton('checkout/session')->getData($optiKeySession);
303
+ $price = '';
304
+ if ($sessionItem) {
305
+ $productItems = unserialize($sessionItem);
306
+ $price = $productItems['originalPrice'];
307
+ }
308
+ return $price;
309
+ }
310
+
311
+ /**
312
+ * Remove cookie
313
+ *
314
+ * @param String $cookie Cookie Name
315
+ *
316
+ * @return void
317
+ */
318
+ private function removeCookie($cookie)
319
+ {
320
+ setcookie($cookie, null, -1, '/');
321
+ }
322
+
323
+ /**
324
+ * Process Checkout Errors
325
+ *
326
+ * @param Array $errors Errors
327
+ *
328
+ * @return void
329
+ */
330
+ private function processCheckoutResult($errors)
331
+ {
332
+ if (count($errors)) {
333
+ $result['success'] = false;
334
+ $result['error'] = true;
335
+ if (count($errors) > 1) {
336
+ $message = 'Products ' . implode(', ', $errors) . ' are invalid! Please, edit your cart. (Optipricer)';
337
+ } else {
338
+ $message = 'Product ' . $errors[0] . ' is invalid! Please, edit your cart. (Optipricer)';
339
+ }
340
+ $result['error_messages'] = $message;
341
+ $response = Mage::app()->getResponse();
342
+ $response->setBody(Mage::helper('core')->jsonEncode($result));
343
+ $response->sendResponse();
344
+ exit();
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Try to redeem coupon
350
+ *
351
+ * @param Mage_Sales_Model_Quote_Item $item Item
352
+ * @param Array $couponParams Coupon Parameters
353
+ *
354
+ * @return null|String
355
+ */
356
+ private function tryRedeem($item, $couponParams)
357
+ {
358
+ $error = null;
359
+ $productId = $item->getProductId();
360
+ $optiKeySession = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
361
+
362
+ //Try to Redeem coupon
363
+ if (!$this->redeemCoupon($couponParams['couponToken'])) {
364
+ $error = $item->getName();
365
+ }
366
+
367
+ $item = $this->unvalidateItemSession($item, $this->getOriginalPriceBySession($optiKeySession), $optiKeySession);
368
+ $item->save();
369
+ $this->removeCookie('discount_'.$this->token.'_'.$productId);
370
+
371
+ return $error;
372
+ }
373
+
374
+ /**
375
+ * Create Session Parameter
376
+ *
377
+ * @param Mage_Sales_Model_Quote_Item $item Item
378
+ *
379
+ * @return Mage_Sales_Model_Quote_Item
380
+ */
381
+ private function createSessionParameter($item)
382
+ {
383
+ $product = $item->getProduct();
384
+ $productId = $item->getProductId();
385
+ //Check if ProductId is in Session Checkout (SWE_Optipricer_Coupon_ID)
386
+ $sessionKey = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
387
+ $parms = array(
388
+ 'cookieName' => 'discount_'.$this->token.'_'.$productId,
389
+ 'originalPrice' => $product->getFinalPrice()
390
+ );
391
+ Mage::getSingleton('checkout/session')->setData($sessionKey, serialize($parms));
392
+
393
+ return $item;
394
+ }
395
+
396
+ /**
397
+ * Validate Session Parameters with Cookie
398
+ *
399
+ * @param Mage_Sales_Model_Quote_Item $item Item
400
+ * @param Array $productSessionItems Product Session items
401
+ * @param String $optiKeySession Api Key Session
402
+ *
403
+ * @return array
404
+ */
405
+ private function validateSessionParamsWithCookie($item, $productSessionItems, $optiKeySession)
406
+ {
407
+ $error = null;
408
+ $productId = $item->getProductId();
409
+ $originalPrice = $productSessionItems['originalPrice'];
410
+ //Check if has cookie for this product
411
+ if ($this->hasCookie($this->token, $productId)) {
412
+ //Validate Cookie
413
+ $cookieParams = $this->getCookieParams($this->token, $productId);
414
+ //Product doesn't have a valid Cookie
415
+ if (!$cookieParams) {
416
+ $item = $this->unvalidateItemSession($item, $originalPrice, $optiKeySession);
417
+ $this->removeCookie('discount_'.$this->token.'_'.$productId);
418
+ $error = $item->getName();
419
+ }
420
+ } else {
421
+ $item = $this->unvalidateItemSession($item, $originalPrice, $optiKeySession);
422
+ $error = $item->getName();
423
+ }
424
+ return array($item, $error);
425
+ }
426
+
427
+ /**
428
+ * Method to change the price of the item/product
429
+ *
430
+ * @param Mage_Sales_Model_Quote_Item $item Item
431
+ * @param Double $originalPrice Final Price
432
+ * @param Double $finalValue Final Price
433
+ *
434
+ * @return Mage_Sales_Model_Quote_Item
435
+ */
436
+ private function changeItemPrice($item, $originalPrice, $finalValue)
437
+ {
438
+ try {
439
+ if ($finalValue > 0) {
440
+ $item->setOriginalPrice($originalPrice);
441
+ $item->setCustomPrice($finalValue);
442
+ $item->setOriginalCustomPrice($finalValue);
443
+ }
444
+ } catch (Exception $e) {
445
+ $this->logOptipricer(self::LOG_KEY.'_changeItemPrice_'.$e);
446
+ }
447
+ return $item;
448
+ }
449
+
450
+ /**
451
+ * Unvalidate item session (restore price and unset session parameter of the product)
452
+ *
453
+ * @param Mage_Sales_Model_Quote_Item $item Item Quote
454
+ * @param Float $originalPrice Original Price
455
+ * @param String $optiKeySession Key for the session
456
+ *
457
+ * @return Mage_Sales_Model_Quote_Item
458
+ */
459
+ private function unvalidateItemSession($item, $originalPrice, $optiKeySession)
460
+ {
461
+ $item = $this->changeItemPrice($item, $originalPrice, $originalPrice);
462
+ Mage::getSingleton('checkout/session')->unsetData($optiKeySession);
463
+
464
+ return $item;
465
+ }
466
+
467
+ /**
468
+ * Method to validate and get the DiscountToken and the Value from the cookie
469
+ *
470
+ * @param String $token Token Store
471
+ * @param Int $productId Product Id
472
+ *
473
+ * @return Array
474
+ */
475
+ private function getCookieParams($token, $productId)
476
+ {
477
+ $result = array();
478
+ $cookie = $_COOKIE['discount_'.$token.'_'.$productId];
479
+ $dataAux = explode(':', $cookie);
480
+ $data = $this->secureContent(json_encode($dataAux[0]), 'decrypt');
481
+ $coupon = json_decode($data, true);
482
+ if (is_array($coupon)) {
483
+ //Get CouponToken
484
+ $result['couponToken'] = array_key_exists('token', $coupon) ? $coupon['token'] : '';
485
+ //Get Final Price
486
+ $result['finalPrice'] = array_key_exists('value', $coupon) ? $coupon['value'] : '';
487
+ }
488
+ return $result;
489
+ }
490
+
491
+ /**
492
+ * Check if a cookie exists
493
+ *
494
+ * @param String $token TokenStore
495
+ * @param String $productId ProductId
496
+ *
497
+ * @return bool
498
+ */
499
+ private function hasCookie($token, $productId)
500
+ {
501
+ return isset($_COOKIE['discount_'.$token.'_'.$productId]);
502
+ }
503
+
504
+ /**
505
+ * Get Session parameter with a given key
506
+ *
507
+ * @param String $key Key to check
508
+ *
509
+ * @return array|null
510
+ */
511
+ private function getProductSessionItems($key)
512
+ {
513
+ $sessionItem = Mage::getSingleton('checkout/session')->getData($key);
514
+ $productItems = $sessionItem ? unserialize($sessionItem) : null;
515
+
516
+ return $productItems;
517
+ }
518
+
519
+ /**
520
+ * Log to Optipricer file log
521
+ *
522
+ * @param String $message Message
523
+ * @param String $level Level
524
+ */
525
+ public function logOptipricer($message, $level = null)
526
+ {
527
+ Mage::log($message, $level, 'OptipricerLogFile.log');
528
+ }
529
+
530
+ /**
531
+ * Method to manage secure content
532
+ *
533
+ * @param string $content Content
534
+ * @param string $task Task
535
+ *
536
+ * @return string
537
+ */
538
+ private function secureContent($content, $task = 'encrypt')
539
+ {
540
+ if ($task == 'decrypt') {
541
+ $contentSecured = $this->decryptContent($this->key, $content);
542
+ } else {
543
+ $contentSecured = $this->encryptContent($this->key, $content);
544
+ }
545
+
546
+ return $contentSecured;
547
+ }
548
+
549
+ /**
550
+ * Encrypt content
551
+ *
552
+ * @param String $key Key
553
+ * @param String $content Content
554
+ *
555
+ * @return String
556
+ */
557
+ private function encryptContent($key, $content)
558
+ {
559
+ $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
560
+ $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM);
561
+ $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_CBC, $iv);
562
+ $ciphertextArr = array('cipher' => base64_encode($ciphertext), 'iv' => base64_encode($iv));
563
+ $ciphertextArr = json_encode($ciphertextArr);
564
+ $ciphertextBase64 = base64_encode($ciphertextArr);
565
+
566
+ return $ciphertextBase64;
567
+ }
568
+
569
+ /**
570
+ * Decrypt content
571
+ *
572
+ * @param String $key Key
573
+ * @param String $cipher Cipher
574
+ *
575
+ * @return String
576
+ */
577
+ private function decryptContent($key, $cipher)
578
+ {
579
+ $ciphertextDec = base64_decode($cipher);
580
+ $ciphertextDec = json_decode($ciphertextDec);
581
+ $content = trim(mcrypt_decrypt(
582
+ MCRYPT_RIJNDAEL_128,
583
+ $key,
584
+ base64_decode($ciphertextDec->cipher),
585
+ MCRYPT_MODE_CBC,
586
+ base64_decode($ciphertextDec->iv)
587
+ ));
588
+ return $content;
589
+ }
590
+ }
app/code/{local → community}/SWE/Optipricer/controllers/Adminhtml/SweoptipricerController.php RENAMED
@@ -1,5 +1,13 @@
1
  <?php
2
-
 
 
 
 
 
 
 
 
3
  class SWE_Optipricer_Adminhtml_SweoptipricerController extends Mage_Adminhtml_Controller_Action
4
  {
5
  /**
1
  <?php
2
+ /**
3
+ * Widget to share the product in Facebook and get a personalized discount
4
+ *
5
+ * @package SWE_Optipricer
6
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
+ * @copyright 2015 be.ubi
8
+ * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.1
10
+ */
11
  class SWE_Optipricer_Adminhtml_SweoptipricerController extends Mage_Adminhtml_Controller_Action
12
  {
13
  /**
app/code/community/SWE/Optipricer/etc/config.xml ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Widget to share the product in Facebook and get a personalized discount
5
+ *
6
+ * @package SWE_Optipricer
7
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
+ * @copyright 2015 be.ubi
9
+ * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.1
11
+ */
12
+ -->
13
+ <config>
14
+ <modules>
15
+ <SWE_Optipricer>
16
+ <version>1.0.0</version>
17
+ </SWE_Optipricer>
18
+ </modules>
19
+ <global>
20
+ <helpers>
21
+ <optipricer>
22
+ <class>SWE_Optipricer_Helper</class>
23
+ </optipricer>
24
+ </helpers>
25
+ <blocks>
26
+ <optipricer>
27
+ <class>SWE_Optipricer_Block</class>
28
+ </optipricer>
29
+ </blocks>
30
+ <models>
31
+ <optipricer>
32
+ <class>SWE_Optipricer_Model</class>
33
+ </optipricer>
34
+ </models>
35
+ </global>
36
+ <frontend>
37
+ <events>
38
+ <checkout_controller_onepage_save_shipping_method>
39
+ <observers>
40
+ <swe_optipricer_observer>
41
+ <type>singleton</type>
42
+ <class>optipricer/observer</class>
43
+ <method>validateCart</method>
44
+ </swe_optipricer_observer>
45
+ </observers>
46
+ </checkout_controller_onepage_save_shipping_method>
47
+ <controller_action_predispatch_checkout_cart_index>
48
+ <observers>
49
+ <swe_optipricer_observer>
50
+ <type>singleton</type>
51
+ <class>optipricer/observer</class>
52
+ <method>validateCart</method>
53
+ </swe_optipricer_observer>
54
+ </observers>
55
+ </controller_action_predispatch_checkout_cart_index>
56
+ <sales_order_save_before>
57
+ <observers>
58
+ <swe_optipricer_observer>
59
+ <type>singleton</type>
60
+ <class>optipricer/observer</class>
61
+ <method>sendOrderStatus</method>
62
+ </swe_optipricer_observer>
63
+ </observers>
64
+ </sales_order_save_before>
65
+ </events>
66
+ <translate>
67
+ <modules>
68
+ <SWE_Optipricer>
69
+ <files>
70
+ <default>SWE_Optipricer.csv</default>
71
+ </files>
72
+ </SWE_Optipricer>
73
+ </modules>
74
+ </translate>
75
+ </frontend>
76
+ <default>
77
+ <swe>
78
+ <swe_group_activation>
79
+ <swe_endpoint>http://www.optipricer.com/api/</swe_endpoint>
80
+ <swe_enable>0</swe_enable>
81
+ </swe_group_activation>
82
+ <swe_group_parameters>
83
+ <swe_pageview>1</swe_pageview>
84
+ <swe_renderview>0</swe_renderview>
85
+ <swe_expiryoffset>2880</swe_expiryoffset>
86
+ <swe_min>5</swe_min>
87
+ <swe_max>30</swe_max>
88
+ <swe_background_color>660066</swe_background_color>
89
+ <swe_font_color>FFFFFF</swe_font_color>
90
+ </swe_group_parameters>
91
+ </swe>
92
+ </default>
93
+ <adminhtml>
94
+ <acl>
95
+ <resources>
96
+ <all>
97
+ <title>Allow Everything</title>
98
+ </all>
99
+ <admin>
100
+ <children>
101
+ <system>
102
+ <children>
103
+ <config>
104
+ <children>
105
+ <swe>
106
+ <title>SWE - All</title>
107
+ </swe>
108
+ </children>
109
+ </config>
110
+ </children>
111
+ </system>
112
+ </children>
113
+ </admin>
114
+ </resources>
115
+ </acl>
116
+ <translate>
117
+ <modules>
118
+ <SWE_Optipricer>
119
+ <files>
120
+ <default>SWE_Optipricer.csv</default>
121
+ </files>
122
+ </SWE_Optipricer>
123
+ </modules>
124
+ </translate>
125
+ </adminhtml>
126
+ </config>
app/code/{local → community}/SWE/Optipricer/etc/system.xml RENAMED
@@ -1,4 +1,15 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
 
 
 
 
 
 
 
 
 
 
 
2
  <config>
3
  <tabs>
4
  <swe translate="label" module="optipricer">
@@ -63,6 +74,7 @@
63
  <label>Store Token </label>
64
  <comment>Store Token (given by be.ubi)</comment>
65
  <frontend_type>text</frontend_type>
 
66
  <sort_order>24</sort_order>
67
  <show_in_default>1</show_in_default>
68
  <show_in_website>1</show_in_website>
@@ -72,6 +84,7 @@
72
  <label>Store Key </label>
73
  <comment>Key for encrypt/decrypt data (given by be.ubi)</comment>
74
  <frontend_type>text</frontend_type>
 
75
  <sort_order>30</sort_order>
76
  <show_in_default>1</show_in_default>
77
  <show_in_website>1</show_in_website>
@@ -155,7 +168,7 @@
155
  </swe_renderview>
156
  <swe_background_color translate="label">
157
  <label>Background color (Button)</label>
158
- <comment>If empty, the color 660066 (purple) will be used</comment>
159
  <frontend_type>text</frontend_type>
160
  <sort_order>100</sort_order>
161
  <show_in_default>1</show_in_default>
@@ -164,7 +177,7 @@
164
  </swe_background_color>
165
  <swe_font_color translate="label">
166
  <label>Font color (Button)</label>
167
- <comment>If empty, the color FFFFFF (white) will be used</comment>
168
  <frontend_type>text</frontend_type>
169
  <sort_order>110</sort_order>
170
  <show_in_default>1</show_in_default>
1
  <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Widget to share the product in Facebook and get a personalized discount
5
+ *
6
+ * @package SWE_Optipricer
7
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
+ * @copyright 2015 be.ubi
9
+ * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.1
11
+ */
12
+ -->
13
  <config>
14
  <tabs>
15
  <swe translate="label" module="optipricer">
74
  <label>Store Token </label>
75
  <comment>Store Token (given by be.ubi)</comment>
76
  <frontend_type>text</frontend_type>
77
+ <validate>required-entry</validate>
78
  <sort_order>24</sort_order>
79
  <show_in_default>1</show_in_default>
80
  <show_in_website>1</show_in_website>
84
  <label>Store Key </label>
85
  <comment>Key for encrypt/decrypt data (given by be.ubi)</comment>
86
  <frontend_type>text</frontend_type>
87
+ <validate>required-entry</validate>
88
  <sort_order>30</sort_order>
89
  <show_in_default>1</show_in_default>
90
  <show_in_website>1</show_in_website>
168
  </swe_renderview>
169
  <swe_background_color translate="label">
170
  <label>Background color (Button)</label>
171
+ <comment>By default, the color 660066 (purple) will be used</comment>
172
  <frontend_type>text</frontend_type>
173
  <sort_order>100</sort_order>
174
  <show_in_default>1</show_in_default>
177
  </swe_background_color>
178
  <swe_font_color translate="label">
179
  <label>Font color (Button)</label>
180
+ <comment>By default, the color FFFFFF (white) will be used</comment>
181
  <frontend_type>text</frontend_type>
182
  <sort_order>110</sort_order>
183
  <show_in_default>1</show_in_default>
app/code/{local → community}/SWE/Optipricer/etc/widget.xml RENAMED
@@ -1,20 +1,19 @@
1
  <?xml version="1.0"?>
 
 
 
 
 
 
 
 
 
 
 
2
  <widgets>
3
  <optipricer_discount type="optipricer/discount" translate="Optipricer Discount" module="optipricer">
4
  <name>Optipricer Discount</name>
5
-
6
  <description>Get a personalized discount</description>
7
-
8
- <!-- Additional javascript files to be loaded
9
- on the widget configuration edit form if needed. -->
10
- <!--<js>mage/adminhtml/first.js,mage/adminhtml/second.js</js>-->
11
-
12
- <!--
13
- It should contain comma separated list
14
- of javascript files paths under the /js directory.
15
- This tag is optional.
16
- -->
17
-
18
  <parameters>
19
  <enable_service translate="label">
20
  <label>Enable Service</label>
@@ -28,14 +27,13 @@
28
  <label>Font color (Button)</label>
29
  <visible>1</visible>
30
  <type>text</type>
31
- <description>If empty, the color FFFFFF (white) will be used</description>
32
  </swe_font_color>
33
  <swe_background_color translate="label">
34
  <label>Background color (Button)</label>
35
  <visible>1</visible>
36
  <type>text</type>
37
- <!-- <value>5D2D87</value> -->
38
- <description>If empty, the color 660066 (purple) will be used</description>
39
  </swe_background_color>
40
  <swe_expiryoffset translate="label">
41
  <label>Expiry Offset (in minutes)</label>
1
  <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Widget to share the product in Facebook and get a personalized discount
5
+ *
6
+ * @package SWE_Optipricer
7
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
+ * @copyright 2015 be.ubi
9
+ * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.1
11
+ */
12
+ -->
13
  <widgets>
14
  <optipricer_discount type="optipricer/discount" translate="Optipricer Discount" module="optipricer">
15
  <name>Optipricer Discount</name>
 
16
  <description>Get a personalized discount</description>
 
 
 
 
 
 
 
 
 
 
 
17
  <parameters>
18
  <enable_service translate="label">
19
  <label>Enable Service</label>
27
  <label>Font color (Button)</label>
28
  <visible>1</visible>
29
  <type>text</type>
30
+ <description>By default, the color FFFFFF (white) will be used</description>
31
  </swe_font_color>
32
  <swe_background_color translate="label">
33
  <label>Background color (Button)</label>
34
  <visible>1</visible>
35
  <type>text</type>
36
+ <description>By default, the color 660066 (purple) will be used</description>
 
37
  </swe_background_color>
38
  <swe_expiryoffset translate="label">
39
  <label>Expiry Offset (in minutes)</label>
app/code/local/SWE/Optipricer/Model/Observer.php DELETED
@@ -1,408 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * Class SWE_Optipricer_Model_Observer
5
- *
6
- * @package SWE_Optipricer
7
- * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
- * @copyright 2014 be.ubi
9
- * @license GNU Lesser General Public License (LGPL)
10
- * @version v.0.2
11
- */
12
- class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
- {
14
- const STATE_CANCELED = 'canceled';
15
- const SCENARIO_ONE_PAGE_CHECKOUT = 'one_page';
16
- const SCENARIO_MULTIPLE_ADDRESSES_CHECKOUT = 'multiple_addresses';
17
- const URL_ENDPOINT_COUPON = 'coupon';
18
- const URL_ENDPOINT_REDEEM = 'redeem';
19
- const URL_SEPARATOR = '/';
20
-
21
- protected $_serializer = null;
22
- private $token;
23
- private $key;
24
- private $enabledGlobal;
25
- private $minDiscount;
26
- private $maxDiscount;
27
- private $text;
28
- private $endPoint;
29
- private $pageView;
30
- private $renderView;
31
- private $locale;
32
- private $expiryOffset;
33
-
34
- /**
35
- * Initialization
36
- */
37
- protected function _construct()
38
- {
39
- $this->token = Mage::getStoreConfig('swe/swe_group_activation/swe_token',Mage::app()->getStore());
40
- $this->key = Mage::getStoreConfig('swe/swe_group_activation/swe_key', Mage::app()->getStore());
41
- $this->enabledGlobal = Mage::getStoreConfig('swe/swe_group_activation/swe_enable',Mage::app()->getStore());
42
- $this->endPoint = Mage::getStoreConfig('swe/swe_group_activation/swe_endpoint',Mage::app()->getStore());
43
- $this->minDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_min',Mage::app()->getStore());
44
- $this->maxDiscount = Mage::getStoreConfig('swe/swe_group_parameters/swe_max',Mage::app()->getStore());
45
- $this->text = '';
46
- $this->pageView = Mage::getStoreConfig('swe/swe_group_parameters/swe_pageview',Mage::app()->getStore());
47
- $this->renderView = Mage::getStoreConfig('swe/swe_group_parameters/swe_renderview',Mage::app()->getStore());
48
- $this->expiryOffset = Mage::getStoreConfig('swe/swe_group_parameters/swe_expiryoffset',Mage::app()->getStore());
49
- $this->locale = Mage::app()->getLocale()->getLocaleCode();
50
- $this->_serializer = new Varien_Object();
51
- }
52
-
53
- /**
54
- * Verify each price for each discount given
55
- *
56
- * @param Varien_Event_Observer $observer Observer
57
- *
58
- * @return void
59
- */
60
- public function validateCart(Varien_Event_Observer $observer)
61
- {
62
- $session = Mage::getSingleton('checkout/session');
63
- /** @var Mage_Sales_Model_Quote $quote */
64
- $quote = $session->getQuote();
65
- $quote->setIsMultiShipping(false);
66
- $scenario = $this->getCurrentScenario($quote);
67
- $this->_construct();
68
- if (!$this->token) {
69
- return;
70
- }
71
- try {
72
- $errors = array();
73
- foreach($quote->getAllItems() as $item) {
74
- $error = $this->validateItem($item, $scenario);
75
- if ($error) {
76
- $errors[] = $error;
77
- }
78
- }
79
- $quote->setTotalsCollectedFlag(false);
80
-
81
- } catch (Exception $e) {
82
- //ToDo: send email with the error
83
- return;
84
- }
85
- }
86
-
87
- /**
88
- * Send Order status to Optipricer System
89
- *
90
- * @param Varien_Event_Observer $observer Observer
91
- *
92
- * @return void
93
- */
94
- public function sendOrderStatus(Varien_Event_Observer $observer)
95
- {
96
- $this->_construct();
97
- if (!$this->token) {
98
- return;
99
- }
100
- $cookiesToRemove = array();
101
- $session = Mage::getSingleton('checkout/session');
102
- /** @var Mage_Sales_Model_Quote $quote */
103
- $quote = $session->getQuote();
104
- $scenario = $this->getCurrentScenario($quote);
105
- $errors = array();
106
- try {
107
- //Validate each product (that has a coupon assigned to it)
108
- foreach($quote->getAllItems() as $item)
109
- {
110
- $error = $this->validateItem($item, $scenario);
111
- if ($error) {
112
- $errors[] = $item->getName();
113
- } else {
114
- $productId = $item->getProductId();
115
- $oldProduct = Mage::getModel('catalog/product')->load($productId);
116
- $originalPrice = round($oldProduct->getData('price'), 2);
117
- list($discountToken, $priceWithDiscount) = $this->getDiscountTokenAndValueCookie($this->token, $productId);
118
- // Price has changed and a Cookie exists
119
- if ($discountToken) {
120
- $products = $this->getAllQuoteProducts($scenario, $quote);
121
- //Try to Redeem coupon
122
- if (!$this->redeemCoupon($discountToken, $products)) {
123
- $errors[] = $item->getName();
124
- //Remove invalid cookie
125
- setcookie('discount_'.$this->token.'_'.$productId, null, -1, '/');
126
- $this->changeItemPrice($item, $originalPrice, $scenario);
127
- } else {
128
- $cookiesToRemove[] = 'discount_'.$this->token.'_'.$productId;
129
- $this->changeItemPrice($item, $priceWithDiscount, $scenario);
130
- }
131
- }
132
- }
133
- }
134
- } catch (Exception $e){
135
- //ToDo: send email with the error
136
- return;
137
- }
138
-
139
- //Process the error regarding the scenario
140
- if (count($errors)) {
141
- $result['success'] = false;
142
- $result['error'] = true;
143
- if (count($errors) > 1) {
144
- $message = 'Products '.implode(', ', $errors).' are invalid!';
145
- } elseif (count($errors) == 1) {
146
- $message = 'Product '.$errors[0].' is invalid!';
147
- }
148
- $result['error_messages'] = $message;
149
- $response = Mage::app()->getResponse();
150
- $response->setBody(Mage::helper('core')->jsonEncode($result));
151
- $response->sendResponse();
152
- exit();
153
- } else {
154
- //Remove cookies of used coupons
155
- foreach ($cookiesToRemove as $cookie) {
156
- setcookie($cookie, null, -1, '/');
157
- }
158
- }
159
- }
160
-
161
- /************************* AUXILIARY METHODS ***************************/
162
-
163
- /**
164
- * Method to manage secure content
165
- *
166
- * @param string $content Content
167
- * @param string $task Task
168
- *
169
- * @return string
170
- */
171
- private function secureContent($content, $task = 'encrypt')
172
- {
173
- if ($task == 'decrypt') {
174
- $contentSecured = $this->decryptContent($this->key, $content);
175
- } else {
176
- $contentSecured = $this->encryptContent($this->key, $content);
177
- }
178
-
179
- return $contentSecured;
180
- }
181
-
182
- /**
183
- * Encrypt content
184
- *
185
- * @param String $key Key
186
- * @param String $content Content
187
- *
188
- * @return string
189
- */
190
- private function encryptContent($key, $content)
191
- {
192
- $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
193
- $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM);
194
- $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_CBC, $iv);
195
- $ciphertextArr = array('cipher' => base64_encode($ciphertext), 'iv' => base64_encode($iv));
196
- $ciphertextArr = json_encode($ciphertextArr);
197
- $ciphertextBase64 = base64_encode($ciphertextArr);
198
-
199
- return $ciphertextBase64;
200
- }
201
-
202
- /**
203
- * Decrypt content
204
- *
205
- * @param String $key Key
206
- * @param String $cipher Cipher
207
- *
208
- * @return string
209
- */
210
- private function decryptContent($key, $cipher)
211
- {
212
- $ciphertextDec = base64_decode($cipher);
213
- $ciphertextDec = json_decode($ciphertextDec);
214
- $content = trim(mcrypt_decrypt(
215
- MCRYPT_RIJNDAEL_128,
216
- $key,
217
- base64_decode($ciphertextDec->cipher),
218
- MCRYPT_MODE_CBC,
219
- base64_decode($ciphertextDec->iv)
220
- ));
221
- return $content;
222
- }
223
-
224
- /**
225
- * Method to get the DiscountToken and the Value from the cookie
226
- *
227
- * @param string $token Token Store
228
- * @param int $productId Product Id
229
- *
230
- * @return array
231
- */
232
- private function getDiscountTokenAndValueCookie($token, $productId)
233
- {
234
- $discountToken = '';
235
- $finalPrice = 0;
236
- if (isset($_COOKIE['discount_'.$token.'_'.$productId])) {
237
- $cookie = $_COOKIE['discount_'.$token.'_'.$productId];
238
- $dataAux = explode(':', $cookie);
239
- $data = $this->secureContent(json_encode($dataAux[0]), 'decrypt');
240
- $coupon = json_decode($data, true);
241
- if (is_array($coupon)) {
242
- if (array_key_exists('token', $coupon)) {
243
- $discountToken = $coupon['token'];
244
- }
245
- } else {
246
- //ToDo: alert that a cookie was violated
247
- setcookie($cookie, null, -1, '/');
248
- }
249
- if (is_array($coupon)) {
250
- if (array_key_exists('value', $coupon)) {
251
- $finalPrice = $coupon['value'];
252
- }
253
- }
254
- }
255
- return array($discountToken, $finalPrice);
256
- }
257
-
258
- /**
259
- * Method to redeem a given coupon
260
- *
261
- * @param string $discountToken Discount token
262
- * @param array $products Products
263
- * @param string $discountTokens
264
- *
265
- * @return bool
266
- */
267
- private function redeemCoupon($discountToken, $products, $discountTokens = '')
268
- {
269
- $info = array();
270
- if ($products) {
271
- $info['products'] = $products;
272
- }
273
- $url = $this->endPoint.self::URL_ENDPOINT_COUPON.
274
- self::URL_SEPARATOR.$discountToken.self::URL_SEPARATOR.self::URL_ENDPOINT_REDEEM;
275
-
276
- $client = new Zend_Http_Client($url);
277
- $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, 'application/json');
278
- $client->setHeaders('Authorization', 'Token '.$this->token);
279
- if (!$discountTokens) {
280
- $discountTokens = array($discountToken);
281
- }
282
- $data = array(
283
- 'tokens' => $discountTokens,
284
- 'notes' => $info,
285
- 'location' => ''
286
- );
287
- $securedData['data'] = $this->secureContent(json_encode($data));
288
- $client->setRawData(json_encode($securedData));
289
- try {
290
- $response = $client->request('PUT');
291
- if ($response->isSuccessful()) {
292
- //$resp = $response->getBody();
293
- $result = true;
294
- } else {
295
- $result = false;
296
- }
297
- } catch (Exception $e) {
298
- //ToDo: send an email
299
- $result = false;
300
- }
301
- return $result;
302
- }
303
-
304
- /**
305
- * Method to change the price of the item/product
306
- *
307
- * @param Mage_Sales_Model_Quote_Item $item
308
- * @param double $finalValue Final Price
309
- * @param string $scenario Scenario
310
- *
311
- * @throws Exception
312
- */
313
- private function changeItemPrice($item, $finalValue, $scenario='other')
314
- {
315
- try {
316
- if ($finalValue > 0) {
317
- $product = $item->getProduct();
318
- $oldProduct = Mage::getModel('catalog/product')->load($product->getId());
319
- $originalPrice = $oldProduct->getData('price');
320
- //ToDo: discount field (amount)
321
- $item->setOriginalPrice($product->getPrice());
322
- $item->setCustomPrice($finalValue);
323
- $item->setOriginalCustomPrice($finalValue);
324
- $price = $product->getPrice();
325
- $discount = $price - $finalValue;
326
- $item->setBaseDiscountAmount($discount);
327
- $perc = $discount / $price * 100;
328
- $item->save();
329
- }
330
- } catch (Exception $e) {
331
- throw $e;
332
- }
333
- }
334
-
335
- /**
336
- * Method to collect all products from the quote
337
- *
338
- * @param string $scenario Current Scenario
339
- * @param Mage_Sales_Model_Service_Quote $quote Quote
340
- *
341
- * @throws Exception
342
- *
343
- * @return array
344
- */
345
- private function getAllQuoteProducts($scenario, $quote)
346
- {
347
- $products = array();
348
- foreach($quote->getAllItems() as $item)
349
- {
350
- $productId = $item->getProductId();
351
- list($discountToken, $finalValue) = $this->getDiscountTokenAndValueCookie($this->token, $productId);
352
- $productAux = array();
353
- $productAux['productId'] = $productId;
354
- $productAux['discountToken'] = $discountToken;
355
- $productAux['finalValue'] = $finalValue;
356
- $products[] = $productAux;
357
- }
358
- return $products;
359
- }
360
-
361
- /**
362
- * Method to return current Scenario
363
- *
364
- * @return string
365
- */
366
- private function getCurrentScenario($quote)
367
- {
368
- if ($quote->getIsMultiShipping()) {
369
- $scenario = self::SCENARIO_MULTIPLE_ADDRESSES_CHECKOUT;
370
- } else {
371
- $scenario = self::SCENARIO_ONE_PAGE_CHECKOUT;
372
- }
373
- return $scenario;
374
- }
375
-
376
- /**
377
- * Method to validate an item
378
- *
379
- * @param Mage_Sales_Model_Quote_Item $item Item
380
- * @param string $scenario Current Scenario
381
- *
382
- * @throws Exception
383
- *
384
- * @return string
385
- */
386
- private function validateItem($item, $scenario='other')
387
- {
388
- $error = '';
389
- $productId = $item->getProductId();
390
- $newProduct = $item->getProduct();
391
- $oldProduct = Mage::getModel('catalog/product')->load($newProduct->getId());
392
- list($discountToken, $priceWithDiscount) = $this->getDiscountTokenAndValueCookie($this->token, $productId);
393
- $originalPrice = round($oldProduct->getData('price'), 2);
394
- $currentProductPrice = round($newProduct->getData('price'), 2);
395
- $currentItemPrice = round($item->getCustomPrice(), 2);
396
- if ($originalPrice != $currentProductPrice || $originalPrice != $currentItemPrice || $discountToken) {
397
- // Price has changed and cookie was deleted/violated
398
- if (!$discountToken) {
399
- $error = $item->getName();
400
- $this->changeItemPrice($item, $originalPrice, $scenario);
401
- setcookie('discount_'.$this->token.'_'.$productId, null, -1, '/');
402
- } else {
403
- $this->changeItemPrice($item, $priceWithDiscount, $scenario);
404
- }
405
- }
406
- return $error;
407
- }
408
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/local/SWE/Optipricer/etc/.gitignore DELETED
@@ -1 +0,0 @@
1
- config.xml
 
app/design/adminhtml/default/default/template/swe/optipricer/system/config/button.phtml ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script type="text/javascript">
2
+ //<![CDATA[
3
+ function check() {
4
+ var validateEmail = Validation.validate(document.getElementById("swe_swe_group_activation_swe_email"));
5
+ var validateName = Validation.validate(document.getElementById("swe_swe_group_activation_swe_name"));
6
+ var url = '<?php echo $this->getAjaxCheckUrl(); ?>';
7
+ if (validateEmail && validateName && url.indexOf('optipricer') !== -1) {
8
+ var xhr = new XMLHttpRequest();
9
+ var alerted = false;
10
+ var name = document.getElementById("swe_swe_group_activation_swe_name").value;
11
+ var email = document.getElementById("swe_swe_group_activation_swe_email").value;
12
+ var message = document.getElementById("swe_swe_group_activation_swe_message").value;
13
+ var params = '{"name":"'+name+'", "email": "'+email+'", "observation": "'+message+'"}';
14
+ xhr.open('POST', encodeURI(url), true);
15
+ xhr.onreadystatechange = function() {
16
+ if (xhr.status != 200 && xhr.responseText) {
17
+ if (!alerted) {
18
+ var msg;
19
+ try {
20
+ var response = JSON.parse(xhr.responseText);
21
+ if (response.errorCode === 2) {
22
+ msg = 'Name and/or Email are invalid!';
23
+ } else {
24
+ msg = response.message;
25
+ }
26
+ } catch (e) {
27
+ msg = 'Something went wrong. Please, contact admin!';
28
+ }
29
+ alert(msg);
30
+ }
31
+ alerted = true;
32
+ } else if(xhr.responseText) {
33
+ if (!alerted) {
34
+ alert(xhr.responseText);
35
+ }
36
+ alerted = true;
37
+ }
38
+ };
39
+ if (params !== null) {
40
+ xhr.send(params);
41
+ }
42
+ } else {
43
+ alert('Something went wrong. Please, contact admin!');
44
+ }
45
+ }
46
+ //]]>
47
+ </script>
48
+ <?php echo $this->getButtonHtml() ?>
app/etc/modules/SWE_Optipricer.xml CHANGED
@@ -3,11 +3,11 @@
3
  <modules>
4
  <SWE_Optipricer>
5
  <active>true</active>
6
- <codePool>local</codePool>
7
  <depends>
8
  <Mage_Cms />
9
  </depends>
10
- <version>1.0.0</version>
11
  </SWE_Optipricer>
12
  </modules>
13
  </config>
3
  <modules>
4
  <SWE_Optipricer>
5
  <active>true</active>
6
+ <codePool>community</codePool>
7
  <depends>
8
  <Mage_Cms />
9
  </depends>
10
+ <version>0.1.2</version>
11
  </SWE_Optipricer>
12
  </modules>
13
  </config>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
- <version>0.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/LGPL-3.0">GNU Lesser General Public License (LGPL)</license>
7
  <channel>community</channel>
@@ -11,10 +11,10 @@
11
  Customers will receive a discount after a successful product share on Facebook.&#xD;
12
  For more information www.optipricer.com</description>
13
  <notes>Testing candidate</notes>
14
- <authors><author><name>Rui Mendes</name><user>ruidamendes</user><email>rui.mendes@beubi.com</email></author><author><name>beubi</name><user>beubi</user><email>swe@beubi.com</email></author></authors>
15
- <date>2015-02-13</date>
16
- <time>18:58:31</time>
17
- <contents><target name="magelocal"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="8ed206abaf5cb543c1ecdee14edffeb8"/><file name="Discount.php" hash="94eeb6ed1d031ce824b7979ac684e882"/></dir><dir name="Helper"><file name="Data.php" hash="d0fe8d14d8322200cd61852652572f2c"/></dir><dir name="Model"><file name="Config.php" hash="b3b4dbb1fcae77bf0b06cbb51b688269"/><file name="Observer.php" hash="0d822491b36a5a11770232bc688ba1b8"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="a8298c06a29de784667fab43874d7bfa"/></dir></dir><dir name="etc"><file name="config.xml" hash="ac94752d4f796ccd3b9646503d15b25c"/><file name="system.xml" hash="e3c6666a5bea0b84720648ff0f8a0930"/><file name="widget.xml" hash="fbc9c4137471aaffb1d6a39d6bbaca56"/><file name=".gitignore" hash="8bbba098c1565b7ecf097c21a8df20c7"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><file name="widget.phtml" hash="d0b4396cdfc3ac7ec21cd88626935315"/></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="SWE_Optipricer.xml" hash="225863b467db416b4db8b754cf73efa1"/></dir></target><target name="magelocale"><dir name="en_US"><file name="SWE_Optipricer.csv" hash="2c27e6a50f20ae1318274d6fa8b17bfd"/></dir><dir name="pt_PT"><file name="SWE_Optipricer.csv" hash="7ac9dd9cf8e434d982a003789a0cd826"/></dir></target><target name="mage"><dir name="js"><dir name="swe"><file name="optialert.min.js" hash="d819c38f0c3c8ef8a084154da96667eb"/><file name="optipricer.min.js" hash="5fad2fc014c5d54956a4ade5c05c7681"/><file name="optispin.min.js" hash="f2b0a61b3a739d03e88401e2a1163588"/></dir></dir></target><target name="mageskin"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><dir name="system"><dir name="config"><file name="button.phtml" hash=""/></dir></dir></dir></dir></dir></dir></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.3.10</min><max>6.0.0</max></php></required></dependencies>
20
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
+ <version>0.1.1</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/LGPL-3.0">GNU Lesser General Public License (LGPL)</license>
7
  <channel>community</channel>
11
  Customers will receive a discount after a successful product share on Facebook.&#xD;
12
  For more information www.optipricer.com</description>
13
  <notes>Testing candidate</notes>
14
+ <authors><author><name>Rui Mendes</name><user>ruidamendes</user><email>rui.mendes@beubi.com</email></author><author><name>Ant&#xF3;nio Ferreira</name><user>beubi</user><email>swe@beubi.com</email></author></authors>
15
+ <date>2015-02-20</date>
16
+ <time>15:27:27</time>
17
+ <contents><target name="magecommunity"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="2649751a4b02bc385560c43fda74a4c9"/><file name="Discount.php" hash="8a786ae8c8c1f9805fd45390cabad9f4"/></dir><dir name="Helper"><file name="Data.php" hash="e7902afbd2304409bba65f8b62c790ba"/></dir><dir name="Model"><file name="Config.php" hash="0a2d0119793670f963e585ce9ed3dbd6"/><file name="Observer.php" hash="c19c0293802d199e72b7e65bd48d5f32"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="6751a86d6e4e098ef86bc71551395187"/></dir></dir><dir name="etc"><file name="config.xml" hash="ac2a05838628ad7e57b8fe8efed75a58"/><file name="system.xml" hash="3e96d69e7232d980dcc5ad42f011fddf"/><file name="widget.xml" hash="ed2339511b5f926dca59a9dc27a3d623"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><file name="widget.phtml" hash="d0b4396cdfc3ac7ec21cd88626935315"/></dir></dir></dir></dir></dir></dir><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><dir name="system"><dir name="config"><file name="button.phtml" hash="bee11c32cac685652b16474ae6da2da2"/></dir></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="SWE_Optipricer.xml" hash="fef9106f9ed27d9e69a1abcc22f2063e"/></dir></target><target name="magelocale"><dir name="en_US"><file name="SWE_Optipricer.csv" hash="2c27e6a50f20ae1318274d6fa8b17bfd"/></dir><dir name="pt_PT"><file name="SWE_Optipricer.csv" hash="7ac9dd9cf8e434d982a003789a0cd826"/></dir></target><target name="mage"><dir name="js"><dir name="swe"><file name="optialert.min.js" hash="d819c38f0c3c8ef8a084154da96667eb"/><file name="optipricer.min.js" hash="5fad2fc014c5d54956a4ade5c05c7681"/><file name="optispin.min.js" hash="f2b0a61b3a739d03e88401e2a1163588"/></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.3.10</min><max>6.0.0</max></php></required></dependencies>
20
  </package>