SweOptipricer - Version 0.1.2

Version Notes

Testing candidate

Download this release

Release Info

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


Code changes from version 0.1.1 to 0.1.2

app/code/community/SWE/Optipricer/Block/Button.php CHANGED
@@ -6,7 +6,7 @@
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
  {
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.2
10
  */
11
  class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
12
  {
app/code/community/SWE/Optipricer/Block/Discount.php CHANGED
@@ -6,7 +6,7 @@
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
  {
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.2
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
app/code/community/SWE/Optipricer/Helper/Data.php CHANGED
@@ -6,7 +6,7 @@
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
  {
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.2
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
app/code/community/SWE/Optipricer/Model/Config.php CHANGED
@@ -6,7 +6,7 @@
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
  {
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.2
10
  */
11
  class SWE_Optipricer_Model_Config
12
  {
app/code/community/SWE/Optipricer/Model/Observer.php CHANGED
@@ -7,7 +7,7 @@
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
  {
@@ -17,6 +17,7 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
17
  const URL_SEPARATOR = '/';
18
  const SWE_CUSTOM_OPTION_KEY = 'SWE_Optipricer_Coupon';
19
  const LOG_KEY = 'OPTIPRICER# ';
 
20
 
21
  /**
22
  * @var String Token
@@ -191,21 +192,21 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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
  }
@@ -213,7 +214,6 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
213
  } catch (Exception $e) {
214
  $this->logOptipricer(self::LOG_KEY.'_validateQuoteItem_'.$e);
215
  }
216
-
217
  return $error;
218
  }
219
 
@@ -226,7 +226,6 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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
 
@@ -366,7 +365,7 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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
  }
@@ -385,7 +384,7 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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));
@@ -412,9 +411,9 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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 {
@@ -475,15 +474,20 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
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
  }
@@ -498,7 +502,7 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
498
  */
499
  private function hasCookie($token, $productId)
500
  {
501
- return isset($_COOKIE['discount_'.$token.'_'.$productId]);
502
  }
503
 
504
  /**
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.2
11
  */
12
  class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
  {
17
  const URL_SEPARATOR = '/';
18
  const SWE_CUSTOM_OPTION_KEY = 'SWE_Optipricer_Coupon';
19
  const LOG_KEY = 'OPTIPRICER# ';
20
+ const COOKIE_PREFIX = 'swe_';
21
 
22
  /**
23
  * @var String Token
192
  $error = null;
193
  try{
194
  $product = $item->getProduct();
195
+ $productId = $product->getId();
196
  //Check if ProductId is in Session Checkout (SWE_Optipricer_Coupon_ID)
197
  $sessionKey = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
198
  if ($prodSessionItems) {
199
  //Validate Session Parameters matching with the Cookie
200
+ list($item, $error) = $this->validateSessionParamsWithCookie($item, $prodSessionItems, $sessionKey);
201
  } else {
202
  if ($cart && $this->hasCookie($this->token, $productId)) {
203
  // Optipricer discount was activated and this is the first time adding to the cart
204
  $cookieParams = $this->getCookieParams($this->token, $productId);
205
+ if (count($cookieParams) && $cookieParams['productId'] == $productId) {
206
  $item = $this->changeItemPrice($item, $product->getFinalPrice(), $cookieParams['finalPrice']);
207
  $item = $this->createSessionParameter($item);
208
  } else {
209
+ $this->removeCookie(self::COOKIE_PREFIX.$this->token.'_'.$productId);
210
  }
211
  }
212
  }
214
  } catch (Exception $e) {
215
  $this->logOptipricer(self::LOG_KEY.'_validateQuoteItem_'.$e);
216
  }
 
217
  return $error;
218
  }
219
 
226
  */
227
  private function redeemCoupon($discountToken)
228
  {
 
229
  $url = $this->endPoint.self::URL_ENDPOINT_COUPON.
230
  self::URL_SEPARATOR.$discountToken.self::URL_SEPARATOR.self::URL_ENDPOINT_REDEEM;
231
 
365
 
366
  $item = $this->unvalidateItemSession($item, $this->getOriginalPriceBySession($optiKeySession), $optiKeySession);
367
  $item->save();
368
+ $this->removeCookie(self::COOKIE_PREFIX.$this->token.'_'.$productId);
369
 
370
  return $error;
371
  }
384
  //Check if ProductId is in Session Checkout (SWE_Optipricer_Coupon_ID)
385
  $sessionKey = self::SWE_CUSTOM_OPTION_KEY.'_'.$productId;
386
  $parms = array(
387
+ 'cookieName' => self::COOKIE_PREFIX.$this->token.'_'.$productId,
388
  'originalPrice' => $product->getFinalPrice()
389
  );
390
  Mage::getSingleton('checkout/session')->setData($sessionKey, serialize($parms));
411
  //Validate Cookie
412
  $cookieParams = $this->getCookieParams($this->token, $productId);
413
  //Product doesn't have a valid Cookie
414
+ if (!$cookieParams || $cookieParams['productId'] != $productId) {
415
  $item = $this->unvalidateItemSession($item, $originalPrice, $optiKeySession);
416
+ $this->removeCookie(self::COOKIE_PREFIX.$this->token.'_'.$productId);
417
  $error = $item->getName();
418
  }
419
  } else {
474
  private function getCookieParams($token, $productId)
475
  {
476
  $result = array();
477
+ $cookie = $_COOKIE[self::COOKIE_PREFIX.$token.'_'.$productId];
478
  $dataAux = explode(':', $cookie);
479
  $data = $this->secureContent(json_encode($dataAux[0]), 'decrypt');
480
  $coupon = json_decode($data, true);
481
  if (is_array($coupon)) {
482
+ $result['productId'] = null;
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
+ //Get ProductId
488
+ if (array_key_exists('product', $coupon)) {
489
+ $result['productId'] = array_key_exists('id', $coupon['product']) ? $coupon['product']['id'] : null;
490
+ }
491
  }
492
  return $result;
493
  }
502
  */
503
  private function hasCookie($token, $productId)
504
  {
505
+ return isset($_COOKIE[self::COOKIE_PREFIX.$token.'_'.$productId]);
506
  }
507
 
508
  /**
app/code/community/SWE/Optipricer/controllers/Adminhtml/SweoptipricerController.php CHANGED
@@ -6,7 +6,7 @@
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
  {
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.2
10
  */
11
  class SWE_Optipricer_Adminhtml_SweoptipricerController extends Mage_Adminhtml_Controller_Action
12
  {
app/code/community/SWE/Optipricer/etc/config.xml CHANGED
@@ -7,7 +7,7 @@
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>
@@ -76,7 +76,7 @@
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>
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.2
11
  */
12
  -->
13
  <config>
76
  <default>
77
  <swe>
78
  <swe_group_activation>
79
+ <swe_endpoint>http://lh.optipricer.com:4437/api/</swe_endpoint>
80
  <swe_enable>0</swe_enable>
81
  </swe_group_activation>
82
  <swe_group_parameters>
app/code/community/SWE/Optipricer/etc/system.xml CHANGED
@@ -7,13 +7,13 @@
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">
16
- <label>SWE - Optipricer</label>
17
  <sort_order>100</sort_order>
18
  </swe>
19
  </tabs>
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.2
11
  */
12
  -->
13
  <config>
14
  <tabs>
15
  <swe translate="label" module="optipricer">
16
+ <label><![CDATA[<img height="16" width="16" style="vertical-align: top;" src="http://optipricer.com/favicon.ico" />&nbsp;SWE - Optipricer]]></label>
17
  <sort_order>100</sort_order>
18
  </swe>
19
  </tabs>
app/code/community/SWE/Optipricer/etc/widget.xml CHANGED
@@ -7,7 +7,7 @@
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>
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.2
11
  */
12
  -->
13
  <widgets>
app/design/adminhtml/default/default/template/swe/optipricer/system/config/button.phtml CHANGED
@@ -10,6 +10,8 @@
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() {
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 ip = "<?php echo $_SERVER['SERVER_ADDR']; ?>";
14
+ message = '<MAGENTO CONTACT> \n IP:' + ip +'\n' + message;
15
  var params = '{"name":"'+name+'", "email": "'+email+'", "observation": "'+message+'"}';
16
  xhr.open('POST', encodeURI(url), true);
17
  xhr.onreadystatechange = function() {
app/design/frontend/base/default/template/swe/optipricer/widget.phtml CHANGED
@@ -321,7 +321,6 @@
321
  }
322
 
323
  </style>
324
-
325
  <div id="main-optipricer">
326
  <div class="fade"></div>
327
  <div id="background1">
@@ -335,37 +334,38 @@
335
  <script>
336
  var cp = new Coupon(<?php echo $offer['name'];?>, "<?php echo $offer['data'];?>", "<?php echo $offer['store_token'];?>", "<?php echo $offer['product_id']; ?>", <?php echo $offer['o_price']; ?>, "<?php echo $offer['formatted_price']?>", "<?php echo $offer['url_api'];?>", "<?php echo $offer['currency'];?>");
337
 
338
- function statusChangeCallback(response) {
339
- cp.readCookie();
340
- var logged = cp.getLoginStatus(response);
341
- if (cp.cookie && logged) {
342
- cp.handleRequest(null);
343
- } else {
344
- cp.removeSpin();
 
 
 
345
  }
346
- }
347
- window.fbAsyncInit = function () {
348
- FB.init({
349
- appId: '263200773711733',
350
- xfbml: true,
351
- version: 'v2.2'
352
- });
353
 
354
- FB.getLoginStatus(function (response) {
355
- statusChangeCallback(response);
356
- });
357
- };
358
 
359
- (function (d, s, id) {
360
- var js, fjs = d.getElementsByTagName(s)[0];
361
- if (d.getElementById(id)) {
362
- return;
363
- }
364
- js = d.createElement(s);
365
- js.id = id;
366
- js.src = "//connect.facebook.net/<?php echo $offer['locale'];?>/all.js";
367
- fjs.parentNode.insertBefore(js, fjs);
368
- }(document, 'script', 'facebook-jssdk'));
 
369
  </script>
370
-
371
-
321
  }
322
 
323
  </style>
 
324
  <div id="main-optipricer">
325
  <div class="fade"></div>
326
  <div id="background1">
334
  <script>
335
  var cp = new Coupon(<?php echo $offer['name'];?>, "<?php echo $offer['data'];?>", "<?php echo $offer['store_token'];?>", "<?php echo $offer['product_id']; ?>", <?php echo $offer['o_price']; ?>, "<?php echo $offer['formatted_price']?>", "<?php echo $offer['url_api'];?>", "<?php echo $offer['currency'];?>");
336
 
337
+ window.addEventListener("load", downloadJSOpti, false);
338
+ function downloadJSOpti(){
339
+ function statusChangeCallback(response) {
340
+ cp.readCookie();
341
+ var logged = cp.getLoginStatus(response);
342
+ if (cp.cookie && logged) {
343
+ cp.handleRequest(null);
344
+ } else {
345
+ cp.removeSpin();
346
+ }
347
  }
348
+ window.fbAsyncInit = function () {
349
+ FB.init({
350
+ appId: '263200773711733',
351
+ xfbml: true,
352
+ version: 'v2.2'
353
+ });
 
354
 
355
+ FB.getLoginStatus(function (response) {
356
+ statusChangeCallback(response);
357
+ });
358
+ };
359
 
360
+ (function (d, s, id) {
361
+ var js, fjs = d.getElementsByTagName(s)[0];
362
+ if (d.getElementById(id)) {
363
+ return;
364
+ }
365
+ js = d.createElement(s);
366
+ js.id = id;
367
+ js.src = "//connect.facebook.net/<?php echo $offer['locale'];?>/all.js";
368
+ fjs.parentNode.insertBefore(js, fjs);
369
+ }(document, 'script', 'facebook-jssdk'));
370
+ }
371
  </script>
 
 
js/swe/optipricer.min.js CHANGED
@@ -1 +1 @@
1
- function Coupon(e,t,i,s,n,o,r,a){var c={n:e,d:t,st:i,p:s,o_p:n,o_pf:o,url:r,ps:a};this._initialize(c),this.setSpin();var h=this;document.getElementById("optibutton").addEventListener("click",function(e){e.preventDefault(),e.stopPropagation(),h.socialCredentials&&h.socialCredentials.facebookToken?(h.sendForm(),h.lockPermissions=!0):(h.checkLogin(h.perms,!0),h.lockPermissions=!1)})}function treatError(e,t,i){var s='{"msg":"'+e+'", "url":"'+t+'", "line":'+i+"}";sendRequest("logs/create","post",s,500,1,function(){})}function sendRequest(e,t,i,s,n,o){var r=cp.url+""+e,a="Token "+cp.st,c=null,h=new XMLHttpRequest;h.open(t.toUpperCase(),encodeURI(r),!0),h.setRequestHeader("Authorization",a),h.onreadystatechange=function l(){200!=h.status&&201!=h.status&&204!=h.status&&304!=h.status?(n++,s=4*s,6>n&&(c=window.setTimeout(function(){h.open(t.toUpperCase(),encodeURI(r),!0),h.onreadystatechange=l(),h.send(i)},s))):(c&&clearTimeout(c),o(h))},null!==i&&h.send(i)}var coupon={_initialize:function(e){this.d=e.d,this.n=e.n,this.st=e.st,this.p=e.p,this.ck_n="discount_"+this.st+"_"+this.p,this.url=e.url,this.o_p=e.o_p,this.o_pf=e.o_pf,this.p_s=e.ps,this.v=null,this.html=null,this.opts={lines:13,length:10,width:5,radius:10,corners:1,rotate:0,direction:1,color:"#010046",speed:1,trail:60,shadow:!0,hwaccel:!1,className:"spinner",zIndex:2e9,top:"43%",left:"54%"},this.spinner=new Spinner(this.opts),this.perms=["email","read_stream","user_interests","user_relationships","user_birthday","user_likes","user_work_history","user_education_history","publish_actions"]},_findRiskPrice:function(e){var t=this._htmlTree(document.getElementById("main-optipricer"),this.n);t&&this._riskPrice(t,e)},_htmlTree:function(e,t){var i=null;if(e.parentNode){var s=e.parentNode;s.childNodes.length>1&&(i=this._searchParent(s,t)),null===i&&this._htmlTree(s.parentNode,t)}return i},_replaceDiv:function(e,t,i){var s=document.getElementById(e);if(i)s.innerHTML="",s.innerHTML=t;else{var n=document.getElementById("opti-fbmessage");n?n.innerText=t:(n=document.createElement("h4"),n.id="opti-fbmessage",n.innerText=t,s.appendChild(n))}},_riskPrice:function(e,t){var i,s,n,o,r=e.childNodes,a=this.o_p.toString().replace(".",",");for(i=0;i<r.length;i++)r[i].hasChildNodes()&&r[i].innerHTML?this._riskPrice(r[i],t):(o=void 0!==r[i].innerHTML?r[i].innerHTML:r[i].data,n=e.parentNode,-1===o.indexOf(this.p_s)||-1===o.indexOf(this.o_pf)&&-1===o.indexOf(a)||-1!==n.className.indexOf("opti-discount")||(this.p_symbol=o.indexOf(this.p_s)>3?!0:!1,s=e.cloneNode(!0),s.innerHTML=this.getPriceToPrint(-1!==o.indexOf(".")?!0:!1),s.style.display="block",n.className=n.className+" opti-discount",n.appendChild(s),e.style.textDecoration="line-through"))},_searchParent:function(e,t){var i,s=e.childNodes;for(i=0;i<s.length;i++)if(void 0!==s[i].tagName&&"main-optipricer"!==s[i].id){if(-1!==s[i].innerHTML.indexOf(t))return e;s[i].hasChildNodes()&&this._searchParent(s[i],t)}return null},_toHtml:function(){var e=document.createElement("h2"),t=document.createElement("div");return t.id="optipricerdiscount"," "+e.outerHTML+" "+t.outerHTML},_missingPermissions:function(e){var t,i,s=[];if(e){for(i=0;i<this.perms.length;i++){var n=!1;for(t=0;t<e.length;t++)e[t].permission===this.perms[i]&&"granted"===e[t].status&&(n=!0,t=e.length);n||s.push(this.perms[i])}return s}return void 0},checkLogin:function(e,t){var i=this;FB.login(function(e){i.getLoginStatus(e)&&(i.cookie?i.handleRequest(null):FB.api("/v2.2/"+i.socialCredentials.facebookId+"/permissions","GET",function(e){var s=i._missingPermissions(e.data);0===s.length?i.sendForm():t&&i.sendForm()}))},{scope:e.join(",")})},checkPermissions:function(e){if(this.lockPermissions){var t=this,i=[];FB.api("/v2.2/"+t.socialCredentials.facebookId+"/permissions","GET",function(s){i=t._missingPermissions(s.data),void 0===i?t.checkLogin(t.perms,!0):i.length>0&&(t._replaceDiv("background1",e,!1),t.checkLogin(i,!1))}),this.removeSpin()}else this._replaceDiv("background1",e,!1),this.removeSpin()},createCookie:function(){document.cookie=this.ck_n+"="+this.c+":"+this.n+":"+this.v+":"+this.socialCredentials.facebookId+"; expires="+this.expires+"; path=/"},getLoginStatus:function(e){if("connected"===e.status){var t={facebookId:e.authResponse.userID,facebookToken:e.authResponse.accessToken};return this.setSocialCredentials(t),!0}return!1},getPriceToPrint:function(e){var t=e?this.v.toString():this.v.toString().replace(".",",");return this.p_symbol?t+" "+this.p_s:this.p_s+" "+t},handleErrors:function(e){var t=JSON.parse(e.responseText);switch(t.errorCode){case 1:case 2:case 3:case 4:case 6:this.removeSpin(),this._replaceDiv("main-optipricer",t.html,!0);break;case 5:this.checkPermissions(t.message)}},handleRequest:function(e){var t;e&&(t=JSON.parse(e.response),this.c=t.coupon,this.v=t.value,this.expires=new Date(1e3*t.expiryTime).toString(),this.createCookie()),this._findRiskPrice(this.v),this.removeSpin(),this.html=t&&t.html?t.html:this.html?this.html:this._toHtml(),this._replaceDiv("background1",this.html,!0);var i=(this.o_p-this.v)/this.o_p*100;document.getElementById("optipricerdiscount").innerHTML="<h5>You got a <span class='discount-opti'>"+i.toFixed(0)+"%</span> discount</h5>"},readCookie:function(){this.setSpin();var e="; "+document.cookie,t=e.split("; "+this.ck_n+"=");if(2===t.length){var i=t[1].split(":");this.cookie=!0,this.c=i[0],this.n=i[1],this.v=i[2],this.socialCredentials={},this.socialCredentials.facebookId=i[3].split(";")[0]}else this.cookie=!1},removeSpin:function(){var e=document.getElementsByClassName("fade")[0];e.style.display="none",this.spinner.stop()},sendForm:function(){this.setComment(document.getElementById("form_comment").value);var e=this.toJson();if(this.comment.length>2){document.getElementById("form_comment").setCustomValidity("");var t=this.url+"coupon/",i=[["Authorization","Token "+this.st]];i.push(["Accept","application/json"]),this.setSpin(),this.sendRequest(t,"POST",i,e,this)}else alertify.custom=alertify.extend("custom"),alertify.custom("You must comment something about the product..")},sendRequest:function(e,t,i,s,n){var o,r=new XMLHttpRequest,a=!0;for(r.open(t.toUpperCase(),encodeURI(e),!0),o=0;o<i.length;o++)2===i[o].length?r.setRequestHeader(i[o][0],i[o][1]):a=!1;if(a){var c=0;r.onreadystatechange=function(){if(200!=r.status&&201!=r.status&&204!=r.status&&304!=r.status)try{JSON.parse(r.responseText),""!==r.responseText&&0===c&&(c++,n.handleErrors(r))}catch(t){n.removeSpin(),treatError("[widget-magento]"+t.message,e,0)}else n.handleRequest(r)},null!==s&&r.send(new Blob([s],{type:"application/json"}))}else console.log("The input headers are defined wrong, must be a bi dimensional array, header[x][2]")},setComment:function(e){this.comment=e},setSocialCredentials:function(e){this.socialCredentials?this.socialCredentials.facebookId===e.facebookId?this.socialCredentials.facebookToken=e.facebookToken:(this.cookie=!1,this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken):(this.socialCredentials={},this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken)},setSpin:function(){var e=document.getElementsByClassName("fade")[0],t=document.getElementById("main-optipricer");e.style.display="block",e.style.width=t.offsetWidth+"px",e.style.height=t.offsetHeight+"px",this.spinner.spin(e)},toJson:function(){var e={data:this.d,social_credentials:this.socialCredentials,comment:this.comment};return JSON.stringify(e)}};Coupon.prototype=coupon,window.onerror=function(e,t,i){treatError(e,t,i)};
1
+ function Coupon(e,t,i,s,n,o,r,a){var c={n:e,d:t,st:i,p:s,o_p:n,o_pf:o,url:r,ps:a};this._initialize(c),this.setSpin();var h=this;document.getElementById("optibutton").addEventListener("click",function(e){e.preventDefault(),e.stopPropagation(),h.socialCredentials&&h.socialCredentials.facebookToken?(h.sendForm(),h.lockPermissions=!0):(h.checkLogin(h.perms,!0),h.lockPermissions=!1)})}function treatError(e,t,i){var s='{"msg":"'+e+'", "url":"'+t+'", "line":'+i+"}";sendRequest("logs/create","post",s,500,1,function(){})}function sendRequest(e,t,i,s,n,o){var r=cp.url+""+e,a="Token "+cp.st,c=null,h=new XMLHttpRequest;h.open(t.toUpperCase(),encodeURI(r),!0),h.setRequestHeader("Authorization",a),h.onreadystatechange=function l(){200!=h.status&&201!=h.status&&204!=h.status&&304!=h.status?(n++,s=4*s,6>n&&(c=window.setTimeout(function(){h.open(t.toUpperCase(),encodeURI(r),!0),h.onreadystatechange=l(),h.send(i)},s))):(c&&clearTimeout(c),o(h))},null!==i&&h.send(i)}var coupon={_initialize:function(e){this.d=e.d,this.n=e.n,this.st=e.st,this.p=e.p,this.ck_n="swe_"+this.st+"_"+this.p,this.url=e.url,this.o_p=e.o_p,this.o_pf=e.o_pf,this.p_s=e.ps,this.v=null,this.html=null,this.opts={lines:13,length:10,width:5,radius:10,corners:1,rotate:0,direction:1,color:"#010046",speed:1,trail:60,shadow:!0,hwaccel:!1,className:"spinner",zIndex:2e9,top:"43%",left:"54%"},this.spinner=new Spinner(this.opts),this.perms=["email","read_stream","user_interests","user_relationships","user_birthday","user_likes","user_work_history","user_education_history","publish_actions"]},_findRiskPrice:function(e){var t=this._htmlTree(document.getElementById("main-optipricer"),this.n);t&&this._riskPrice(t,e)},_htmlTree:function(e,t){var i=null;if(e.parentNode){var s=e.parentNode;s.childNodes.length>1&&(i=this._searchParent(s,t)),null===i&&this._htmlTree(s.parentNode,t)}return i},_replaceDiv:function(e,t,i){var s=document.getElementById(e);if(i)s.innerHTML="",s.innerHTML=t;else{var n=document.getElementById("opti-fbmessage");n?n.innerText=t:(n=document.createElement("h4"),n.id="opti-fbmessage",n.innerText=t,s.appendChild(n))}},_riskPrice:function(e,t){var i,s,n,o,r=e.childNodes,a=this.o_p.toString().replace(".",",");for(i=0;i<r.length;i++)r[i].hasChildNodes()&&r[i].innerHTML?this._riskPrice(r[i],t):(o=void 0!==r[i].innerHTML?r[i].innerHTML:r[i].data,n=e.parentNode,-1===o.indexOf(this.p_s)||-1===o.indexOf(this.o_pf)&&-1===o.indexOf(a)||-1!==n.className.indexOf("opti-discount")||(this.p_symbol=o.indexOf(this.p_s)>3?!0:!1,s=e.cloneNode(!0),s.innerHTML=this.getPriceToPrint(-1!==o.indexOf(".")?!0:!1),s.style.display="block",n.className=n.className+" opti-discount",n.appendChild(s),e.style.textDecoration="line-through"))},_searchParent:function(e,t){var i,s=e.childNodes;for(i=0;i<s.length;i++)if(void 0!==s[i].tagName&&"main-optipricer"!==s[i].id){if(-1!==s[i].innerHTML.indexOf(t))return e;s[i].hasChildNodes()&&this._searchParent(s[i],t)}return null},_toHtml:function(){var e=document.createElement("h2"),t=document.createElement("div");return t.id="optipricerdiscount"," "+e.outerHTML+" "+t.outerHTML},_missingPermissions:function(e){var t,i,s=[];if(e){for(i=0;i<this.perms.length;i++){var n=!1;for(t=0;t<e.length;t++)e[t].permission===this.perms[i]&&"granted"===e[t].status&&(n=!0,t=e.length);n||s.push(this.perms[i])}return s}return void 0},checkLogin:function(e,t){var i=this;FB.login(function(e){i.getLoginStatus(e)&&(i.cookie?i.handleRequest(null):FB.api("/v2.2/"+i.socialCredentials.facebookId+"/permissions","GET",function(e){var s=i._missingPermissions(e.data);0===s.length?i.sendForm():t&&i.sendForm()}))},{scope:e.join(",")})},checkPermissions:function(e){if(this.lockPermissions){var t=this,i=[];FB.api("/v2.2/"+t.socialCredentials.facebookId+"/permissions","GET",function(s){i=t._missingPermissions(s.data),void 0===i?t.checkLogin(t.perms,!0):i.length>0&&(t._replaceDiv("background1",e,!1),t.checkLogin(i,!1))}),this.removeSpin()}else this._replaceDiv("background1",e,!1),this.removeSpin()},createCookie:function(){document.cookie=this.ck_n+"="+this.c+":"+this.n+":"+this.v+":"+this.socialCredentials.facebookId+"; expires="+this.expires+"; path=/"},getLoginStatus:function(e){if("connected"===e.status){var t={facebookId:e.authResponse.userID,facebookToken:e.authResponse.accessToken};return this.setSocialCredentials(t),!0}return!1},getPriceToPrint:function(e){var t=e?this.v.toString():this.v.toString().replace(".",",");return this.p_symbol?t+" "+this.p_s:this.p_s+" "+t},handleErrors:function(e){var t=JSON.parse(e.responseText);switch(t.errorCode){case 1:case 2:case 3:case 4:case 6:this.removeSpin(),this._replaceDiv("main-optipricer",t.html,!0);break;case 5:this.checkPermissions(t.message)}},handleRequest:function(e){var t;e&&(t=JSON.parse(e.response),this.c=t.coupon,this.v=t.value,this.expires=new Date(1e3*t.expiryTime).toString(),this.createCookie()),this._findRiskPrice(this.v),this.removeSpin(),this.html=t&&t.html?t.html:this.html?this.html:this._toHtml(),this._replaceDiv("background1",this.html,!0);var i=(this.o_p-this.v)/this.o_p*100;document.getElementById("optipricerdiscount").innerHTML="<h5>You got a <span class='discount-opti'>"+i.toFixed(0)+"%</span> discount</h5>"},readCookie:function(){this.setSpin();var e="; "+document.cookie,t=e.split("; "+this.ck_n+"=");if(2===t.length){var i=t[1].split(":");this.cookie=!0,this.c=i[0],this.n=i[1],this.v=i[2],this.socialCredentials={},this.socialCredentials.facebookId=i[3].split(";")[0]}else this.cookie=!1},removeSpin:function(){var e=document.getElementsByClassName("fade")[0];e.style.display="none",this.spinner.stop()},sendForm:function(){this.setComment(document.getElementById("form_comment").value);var e=this.toJson();if(this.comment.length>2){document.getElementById("form_comment").setCustomValidity("");var t=this.url+"coupon/",i=[["Authorization","Token "+this.st]];i.push(["Accept","application/json"]),this.setSpin(),this.sendRequest(t,"POST",i,e,this)}else alertify.custom=alertify.extend("custom"),alertify.custom("You must comment something about the product..")},sendRequest:function(e,t,i,s,n){var o,r=new XMLHttpRequest,a=!0;for(r.open(t.toUpperCase(),encodeURI(e),!0),o=0;o<i.length;o++)2===i[o].length?r.setRequestHeader(i[o][0],i[o][1]):a=!1;if(a){var c=0;r.onreadystatechange=function(){if(200!=r.status&&201!=r.status&&204!=r.status&&304!=r.status)try{JSON.parse(r.responseText),""!==r.responseText&&0===c&&(c++,n.handleErrors(r))}catch(t){n.removeSpin(),treatError("[widget-magento]"+t.message,e,0)}else n.handleRequest(r)},null!==s&&r.send(new Blob([s],{type:"application/json"}))}else console.log("The input headers are defined wrong, must be a bi dimensional array, header[x][2]")},setComment:function(e){this.comment=e},setSocialCredentials:function(e){this.socialCredentials?this.socialCredentials.facebookId===e.facebookId?this.socialCredentials.facebookToken=e.facebookToken:(this.cookie=!1,this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken):(this.socialCredentials={},this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken)},setSpin:function(){var e=document.getElementsByClassName("fade")[0],t=document.getElementById("main-optipricer");e.style.display="block",e.style.width=t.offsetWidth+"px",e.style.height=t.offsetHeight+"px",this.spinner.spin(e)},toJson:function(){var e={data:this.d,social_credentials:this.socialCredentials,comment:this.comment};return JSON.stringify(e)}};Coupon.prototype=coupon,window.onerror=function(e,t,i){treatError(e,t,i)};
package.xml CHANGED
@@ -1,7 +1,7 @@
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>
@@ -12,9 +12,9 @@ Customers will receive a discount after a successful product share on Facebook.&
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>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
+ <version>0.1.2</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>
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-03-24</date>
16
+ <time>17:07:05</time>
17
+ <contents><target name="magecommunity"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="a396da802f3218b846983e0edc1612c2"/><file name="Discount.php" hash="ea7fb0043c06352c5a230677ffa58174"/></dir><dir name="Helper"><file name="Data.php" hash="382b06d5bdb2ccca8b2eca14dfbcd423"/></dir><dir name="Model"><file name="Config.php" hash="51220275c7f817d6e34b756679e949a1"/><file name="Observer.php" hash="5f65cf218c21409e799782d5f6860824"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="632b100eba3ab7344429ed7b31952bc6"/></dir></dir><dir name="etc"><file name="config.xml" hash="9c02bfd523b1341bc1211dfbd086cf60"/><file name="system.xml" hash="a48519da9f2529de9dff369061ba2953"/><file name="widget.xml" hash="113da72e38ce3a230a1ea8e4cf0a1340"/></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="2cb14a351ce2ec686afe228c326caf9e"/></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="24a7d6503d556b5a068b03fd21b2aba5"/></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="d1dc47bba0b6a8d1bb4171196944c051"/><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>