StarSMP_Customers_Tracking_Integration - Version 1.0.1

Version Notes

First stable version

Download this release

Release Info

Developer KremsaDigital
Extension StarSMP_Customers_Tracking_Integration
Version 1.0.1
Comparing to
See all releases


Version 1.0.1

app/code/community/KremsaDigital/StarSocial/Helper/Data.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class KremsaDigital_StarSocial_Helper_Data extends Mage_Core_Helper_Abstract
4
+ {
5
+
6
+ }
app/code/community/KremsaDigital/StarSocial/Model/Observer.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class KremsaDigital_StarSocial_Model_Observer {
4
+ public function optinAfterRegister($observer) {
5
+ $customer = $observer->getCustomer();
6
+ $track = Mage::getModel('starsocial/track');
7
+ $data = array(
8
+ 'email'=>$customer->getData('email'),
9
+ "first_name" => $customer->getData('firstname'),
10
+ "last_name" => $customer->getData('lastname'),
11
+ );
12
+ $track->optin($data);
13
+ }
14
+
15
+ public function optinAfterLogin($observer) {
16
+ $customer = $observer->getCustomer();
17
+ $track = Mage::getModel('starsocial/track');
18
+ $data = array(
19
+ 'email'=>$customer->getData('email'),
20
+ "first_name" => $customer->getData('firstname'),
21
+ "last_name" => $customer->getData('lastname'),
22
+ );
23
+ $track->optin($data);
24
+ }
25
+
26
+ public function optinAfterLogout($observer) {
27
+ $track = Mage::getModel('starsocial/track');
28
+ $track->logout();
29
+ }
30
+
31
+ public function trackAddToCart($observer) {
32
+ $track = Mage::getModel('starsocial/track');
33
+ $product = Mage::getModel('catalog/product')
34
+ ->load(Mage::app()->getRequest()->getParam('product', 0));
35
+ $product_qty = Mage::app()->getRequest()->getParam('qty', 0);
36
+ $sum = $product->getFinalPrice()*$product_qty;
37
+ $track->track('add_to_cart', $sum);
38
+ }
39
+
40
+ public function trackCheckoutComplete($observer) {
41
+ $order_ids = $observer->getData('order_ids');
42
+ $sum = 0;
43
+ foreach ($order_ids as $order_id) {
44
+ $order = Mage::getModel('sales/order')->load($order_id);
45
+ $sum += $order->getSubtotalInclTax();
46
+ }
47
+
48
+ $track = Mage::getModel('starsocial/track');
49
+ $track->track('checkout_complete', $sum);
50
+ }
51
+ }
app/code/community/KremsaDigital/StarSocial/Model/Track.php ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ require_once(Mage::getBaseDir('lib') . '/KremsaDigital/StarSocial/starsmp.php');
3
+
4
+ class KremsaDigital_StarSocial_Model_Track {
5
+ private $smpInstance = null;
6
+ private $siteUrl = null;
7
+ const VIPFAN_KEY = 'starsocial_vipfan_id';
8
+
9
+ private function isAuthorized() {
10
+ return !($this->smpInstance == null);
11
+ }
12
+
13
+ private function getProtocol() {
14
+ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
15
+ return $protocol;
16
+ }
17
+
18
+ private function authorize() {
19
+ if (Mage::getStoreConfig('starsocial/conf/active') == '0') {
20
+ return;
21
+ }
22
+ $smpSettings = new StarSMPSettings();
23
+ $smpSettings->loyaltyId = Mage::getStoreConfig('starsocial/conf/loyalty_id');
24
+ $smpSettings->clientId = Mage::getStoreConfig('starsocial/conf/client_id');
25
+ $smpSettings->clientSecret = Mage::getStoreConfig('starsocial/conf/client_secret');
26
+
27
+ try {
28
+ $smp = new StarSMP($smpSettings);
29
+ if (Mage::getStoreConfig('starsocial/conf/is_beta') == '1') {
30
+ $smp->setApiUrl('https://api.starsmp.com-beta.com/api/');
31
+ }
32
+ $smp->authorize();
33
+ } catch (Exception $e) {
34
+ throw $e;
35
+ }
36
+ $this->smpInstance = $smp;
37
+ $this->siteUrl = $this->getProtocol() . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
38
+ }
39
+
40
+ public function track($action, $value) {
41
+ if (Mage::getStoreConfig('starsocial/conf/active') == '0') {
42
+ return;
43
+ }
44
+ if (!$this->isAuthorized()) {
45
+ $this->authorize();
46
+ }
47
+
48
+ try {
49
+ $vipfanId = Mage::getSingleton('core/session')->getVipfanId();
50
+ if ($vipfanId) {
51
+ $this->smpInstance->setVipfanID($vipfanId);
52
+ }
53
+ $this->smpInstance->track($action, $this->siteUrl, $value); // tracked under vipfan opted in above
54
+ } catch (Exception $e) {
55
+ throw $e;
56
+ }
57
+ }
58
+
59
+ public function optin($data) {
60
+ if (Mage::getStoreConfig('starsocial/conf/active') == '0') {
61
+ return;
62
+ }
63
+ if (!$this->isAuthorized()) {
64
+ $this->authorize();
65
+ }
66
+ try {
67
+ $vipfan = $this->smpInstance->optin($data, $this->siteUrl);
68
+ Mage::register($this::VIPFAN_KEY, $vipfan['id']);
69
+ Mage::getSingleton('core/session')->setVipfanId($vipfan['id']);
70
+ } catch (Exception $e) {
71
+ throw $e;
72
+ }
73
+ }
74
+
75
+ public function logout() {
76
+ if (Mage::getStoreConfig('starsocial/conf/active') == '0') {
77
+ return;
78
+ }
79
+ if (!$this->isAuthorized()) {
80
+ $this->authorize();
81
+ }
82
+ try {
83
+ $this->smpInstance->logout();
84
+ } catch (Exception $e) {
85
+ throw $e;
86
+ }
87
+ Mage::getSingleton('core/session')->unsVipfanId();
88
+ Mage::register($this::VIPFAN_KEY, null);
89
+ }
90
+ }
app/code/community/KremsaDigital/StarSocial/etc/adminhtml.xml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <admin>
6
+ <children>
7
+ <system>
8
+ <children>
9
+ <config>
10
+ <children>
11
+ <starsocial translate="title" module="starsocial">
12
+ <title>StarSocial</title>
13
+ </starsocial>
14
+ </children>
15
+ </config>
16
+ </children>
17
+ </system>
18
+ </children>
19
+ </admin>
20
+ </resources>
21
+ </acl>
22
+ </config>
app/code/community/KremsaDigital/StarSocial/etc/config.xml ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <KremsaDigital_StarSocial>
5
+ <version>1.0</version>
6
+ </KremsaDigital_StarSocial>
7
+ </modules>
8
+
9
+ <frontend>
10
+ <layout>
11
+ <updates>
12
+ <StarSocial>
13
+ <file>StarSocial.xml</file>
14
+ </StarSocial>
15
+ </updates>
16
+ </layout>
17
+ <events>
18
+ <customer_register_success>
19
+ <observers>
20
+ <optinAfterRegister>
21
+ <class>KremsaDigital_StarSocial_Model_Observer</class>
22
+ <method>optinAfterRegister</method>
23
+ </optinAfterRegister>
24
+ </observers>
25
+ </customer_register_success>
26
+ <customer_login>
27
+ <observers>
28
+ <optinAfterRegister>
29
+ <class>KremsaDigital_StarSocial_Model_Observer</class>
30
+ <method>optinAfterLogin</method>
31
+ </optinAfterRegister>
32
+ </observers>
33
+ </customer_login>
34
+ <customer_logout>
35
+ <observers>
36
+ <optinAfterRegister>
37
+ <class>KremsaDigital_StarSocial_Model_Observer</class>
38
+ <method>optinAfterLogout</method>
39
+ </optinAfterRegister>
40
+ </observers>
41
+ </customer_logout>
42
+ <controller_action_predispatch_checkout_cart_add>
43
+ <observers>
44
+ <trackAfterCartAdd>
45
+ <class>KremsaDigital_StarSocial_Model_Observer</class>
46
+ <method>trackAddToCart</method>
47
+ </trackAfterCartAdd>
48
+ </observers>
49
+ </controller_action_predispatch_checkout_cart_add>
50
+ <checkout_onepage_controller_success_action>
51
+ <observers>
52
+ <trackCheckoutComplete>
53
+ <class>KremsaDigital_StarSocial_Model_Observer</class>
54
+ <method>trackCheckoutComplete</method>
55
+ </trackCheckoutComplete>
56
+ </observers>
57
+ </checkout_onepage_controller_success_action>
58
+ </events>
59
+ </frontend>
60
+
61
+ <adminhtml>
62
+ <acl>
63
+ <resources>
64
+ <admin>
65
+ <children>
66
+ <system>
67
+ <children>
68
+ <config>
69
+ <children>
70
+ <starsocial translate="title" module="starsocial">
71
+ <title>StarSocial</title>
72
+ </starsocial>
73
+ </children>
74
+ </config>
75
+ </children>
76
+ </system>
77
+ </children>
78
+ </admin>
79
+ </resources>
80
+ </acl>
81
+ </adminhtml>
82
+
83
+ <global>
84
+ <resources>
85
+ <StarSocial_setup>
86
+ <setup>
87
+ <module>KremsaDigital_StarSocial</module>
88
+ </setup>
89
+ <connection>
90
+ <use>core_setup</use>
91
+ </connection>
92
+ </StarSocial_setup>
93
+ <StarSocial_write>
94
+ <connection>
95
+ <use>core_write</use>
96
+ </connection>
97
+ </StarSocial_write>
98
+ <StarSocial_read>
99
+ <connection>
100
+ <use>core_read</use>
101
+ </connection>
102
+ </StarSocial_read>
103
+ </resources>
104
+ <helpers>
105
+ <starsocial>
106
+ <class>KremsaDigital_StarSocial_Helper</class>
107
+ </starsocial>
108
+ </helpers>
109
+ <models>
110
+ <starsocial>
111
+ <class>KremsaDigital_StarSocial_Model</class>
112
+ </starsocial>
113
+ </models>
114
+ </global>
115
+ </config>
app/code/community/KremsaDigital/StarSocial/etc/system.xml ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <tabs>
4
+ <kremsadigital>
5
+ <label>Kremsa Digital</label>
6
+ <sort_order>999</sort_order>
7
+ </kremsadigital>
8
+ </tabs>
9
+ <sections>
10
+ <starsocial translate="label" module="starsocial">
11
+ <label>Star Social</label>
12
+ <tab>kremsadigital</tab>
13
+ <frontend_type>text</frontend_type>
14
+ <sort_order>10</sort_order>
15
+ <show_in_default>1</show_in_default>
16
+ <show_in_website>1</show_in_website>
17
+ <show_in_store>1</show_in_store>
18
+ <groups>
19
+ <conf translate="label">
20
+ <label>General Configurations</label>
21
+ <sort_order>10</sort_order>
22
+ <show_in_default>1</show_in_default>
23
+ <show_in_website>1</show_in_website>
24
+ <show_in_store>1</show_in_store>
25
+ <fields>
26
+ <active translate="label">
27
+ <label>Extentions Active</label>
28
+ <frontend_type>select</frontend_type>
29
+ <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>3</sort_order>
30
+ <show_in_default>1</show_in_default>
31
+ <show_in_website>1</show_in_website>
32
+ <show_in_store>1</show_in_store>
33
+ </active>
34
+ <is_beta translate="label">
35
+ <label>Beta env.?</label>
36
+ <frontend_type>select</frontend_type>
37
+ <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>5</sort_order>
38
+ <show_in_default>1</show_in_default>
39
+ <show_in_website>1</show_in_website>
40
+ <show_in_store>1</show_in_store>
41
+ </is_beta>
42
+ <loyalty_id translate="label">
43
+ <label>Loyalty Program</label>
44
+ <frontend_type>text</frontend_type>
45
+ <sort_order>10</sort_order>
46
+ <show_in_default>1</show_in_default>
47
+ <show_in_website>1</show_in_website>
48
+ <show_in_store>1</show_in_store>
49
+ </loyalty_id>
50
+ <client_id translate="label">
51
+ <label>Client ID</label>
52
+ <frontend_type>text</frontend_type>
53
+ <sort_order>20</sort_order>
54
+ <show_in_default>1</show_in_default>
55
+ <show_in_website>1</show_in_website>
56
+ <show_in_store>1</show_in_store>
57
+ </client_id>
58
+ <client_secret translate="label">
59
+ <label>Client Secret</label>
60
+ <frontend_type>text</frontend_type>
61
+ <sort_order>30</sort_order>
62
+ <show_in_default>1</show_in_default>
63
+ <show_in_website>1</show_in_website>
64
+ <show_in_store>1</show_in_store>
65
+ </client_secret>
66
+ <tag_starsmp translate="label">
67
+ <label>Tag StarSMP</label>
68
+ <frontend_type>textarea</frontend_type>
69
+ <sort_order>40</sort_order>
70
+ <show_in_default>1</show_in_default>
71
+ <show_in_website>1</show_in_website>
72
+ <show_in_store>1</show_in_store>
73
+ </tag_starsmp>
74
+ <tag_starsmp_checkout translate="label">
75
+ <label>Tag StarSMP - checkout</label>
76
+ <frontend_type>textarea</frontend_type>
77
+ <sort_order>50</sort_order>
78
+ <show_in_default>1</show_in_default>
79
+ <show_in_website>1</show_in_website>
80
+ <show_in_store>1</show_in_store>
81
+ </tag_starsmp_checkout>
82
+ </fields>
83
+ </conf>
84
+ </groups>
85
+ </starsocial>
86
+ </sections>
87
+ </config>
app/design/frontend/default/default/layout/StarSocial.xml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <layout version="0.1.0">
3
+ <default>
4
+ <reference name="before_body_end">
5
+ <block type="core/template" name="starsocial.tags" template="starsocial/tags.phtml"/>
6
+ </reference>
7
+ </default>
8
+
9
+ <checkout_onepage_success translate="label">
10
+ <reference name="before_body_end">
11
+ <block type="core/template" name="starsocial.tags.checkout" template="starsocial/tags-checkout.phtml"/>
12
+ </reference>
13
+ </checkout_onepage_success>
14
+
15
+ </layout>
app/design/frontend/default/default/template/starsocial/tags-checkout.phtml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
3
+ $order = Mage::getSingleton('sales/order')->load($orderId);
4
+ $subtotal = $order->getSubtotalInclTax();
5
+ $total = $order->getGrandTotal();
6
+ ?>
7
+ <script type="text/javascript">
8
+ var SUBTOTAL_INCL_TAX = <?php echo json_encode($subtotal);?>;
9
+ var GRANDTOTAL_INCL_TAX = <?php echo json_encode($total);?>;
10
+ </script>
11
+ <?
12
+ echo Mage::getStoreConfig('starsocial/conf/tag_starsmp_checkout');
app/design/frontend/default/default/template/starsocial/tags.phtml ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+
3
+ echo Mage::getStoreConfig('starsocial/conf/tag_starsmp');
app/etc/modules/KremsaDesign_StarSocial.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <KremsaDigital_StarSocial>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </KremsaDigital_StarSocial>
8
+ </modules>
9
+ </config>
lib/KremsaDigital/StarSocial/starsmp.php ADDED
@@ -0,0 +1,439 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class StarSMPSettings
4
+ {
5
+ public $clientSecret;
6
+ public $clientId;
7
+ public $loyaltyId;
8
+ public $debugLog;
9
+ }
10
+
11
+ class StarSMPAsync
12
+ {
13
+ protected $sdk = null;
14
+ protected $clientSecret = null;
15
+ protected $queue = array();
16
+ protected $connectTimeout = 3;
17
+ protected $startupUs = 125000;
18
+
19
+ public function __construct(StarSMPSettings $settings) {
20
+ $this->sdk = new StarSMP($settings);
21
+ $this->clientSecret = $settings->clientSecret;
22
+
23
+ if (isset($_GET[$this->getAsyncKey()])) {
24
+ $this->sdk->debug("async restore", array("url" => $this->getAsyncUrlInfo(), "data" =>file_get_contents("php://input")));
25
+
26
+ ignore_user_abort(true);
27
+ $this->wakeup(file_get_contents("php://input"));
28
+ $this->flush();
29
+ exit;
30
+ }
31
+ else {
32
+ }
33
+ }
34
+
35
+ protected function getAsyncUrlInfo() {
36
+ $scheme = "http://";
37
+ $host = $_SERVER["HTTP_HOST"];
38
+ $port = 80;
39
+ $uri = $_SERVER["PHP_SELF"] . "?" . $this->getAsyncKey() . "=1";
40
+ return compact("scheme", "host", "port", "uri");
41
+ }
42
+
43
+ public function execute() {
44
+ $this->sdk->debug("async init", array("url" => $this->getAsyncUrlInfo(), "data" => $this->queue));
45
+
46
+ // php-fpm makes this easy.
47
+ if (function_exists("fastcgi_finish_request")) {
48
+ fastcgi_finish_request();
49
+ $this->flush();
50
+ return;
51
+ }
52
+
53
+ $info = $this->getAsyncUrlInfo();
54
+
55
+ $sock = @fsockopen($info["host"], $info["port"], $errno, $error, $this->connectTimeout);
56
+ if (!$sock)
57
+ throw new Exception("Unable to initiate connection: " . $error, $errno);
58
+
59
+ $data = $this->sleep();
60
+ fwrite($sock,
61
+ "POST " . $info["uri"] . " HTTP/1.1\r\n" .
62
+ "Host: " . $info["host"] . ":" . $info["port"] . "\r\n" .
63
+ "Connection: close\r\n" .
64
+ "Content-Type: application/x-php-serialized\r\n" .
65
+ "Content-Length: " . strlen($data) . "\r\n" .
66
+ "\r\n");
67
+ fwrite($sock, $data);
68
+
69
+ // Give the socket a bit of time for startup.
70
+ usleep($this->startupUs);
71
+
72
+ fclose($sock);
73
+ }
74
+
75
+ protected function getAsyncKey() {
76
+ return "async_" . sha1(sha1_file(__FILE__) . $this->sdk->getClientId() . $this->clientSecret);
77
+ }
78
+
79
+ public function sleep() {
80
+ $response = serialize($this->queue);
81
+ $this->queue = array();
82
+ return $response;
83
+ }
84
+
85
+ public function wakeup($data) {
86
+ $this->queue = unserialize($data);
87
+ }
88
+
89
+ public function flush() {
90
+ foreach ($this->queue as $item) {
91
+ $response = call_user_func_array(array($this->sdk, $item["method"]), $item["args"]);
92
+ }
93
+ $this->queue = array();
94
+ }
95
+
96
+ public function __call($method, $args) {
97
+ $this->queue[] = compact("method", "args");
98
+ }
99
+ }
100
+
101
+ class StarSMP
102
+ {
103
+ const PHP_SDK_VERSION = 1.1;
104
+
105
+ // @var int|float API call timeout in seconds.
106
+ protected $timeout = 10;
107
+ // @var string API endpoint (normally not changed.)
108
+ protected $apiUrl = "https://api.starsmp.com/api/";
109
+ // @var string OAuth2 client id.
110
+ protected $clientId;
111
+ // @var string OAuth2 client secret.
112
+ protected $clientSecret;
113
+ // @var string OAuth2 access token.
114
+ private $accessToken;
115
+ // @var string proxy uri, use tcp://hostname:port.
116
+ private $proxy;
117
+ // @var string loyalty program id, treat as opaque.
118
+ private $loyaltyId;
119
+ // @var string vipfan id, treat as opaque.
120
+ private $vipfanId;
121
+ // @var debug file location
122
+ private $debugLog;
123
+ // @var cookie name prefix
124
+ private $cookiePrefix = "__star_";
125
+
126
+ public function getClientId() {
127
+ return $this->clientId;
128
+ }
129
+
130
+ public function setApiUrl($url) {
131
+ return $this->apiUrl = $url;;
132
+ }
133
+
134
+ public function debug($message, $object = null) {
135
+ if ($this->debugLog != null)
136
+ {
137
+ $date = date(DATE_ISO8601);
138
+ $object = json_encode($object);
139
+ $debug = json_encode(debug_backtrace());
140
+
141
+ $data = "[{$date}] {$message}\n -> {$object}\n -> {$debug}\n\n";
142
+ file_put_contents($this->debugLog, $data, FILE_APPEND);
143
+ }
144
+ }
145
+
146
+ public function setProxy($proxy) {
147
+ $this->proxy = $proxy;
148
+ }
149
+
150
+ public function __construct(StarSMPSettings $settings) {
151
+ if (empty($settings->loyaltyId))
152
+ throw new Exception("Loyalty ID missing");
153
+
154
+ $this->loyaltyId = $settings->loyaltyId;
155
+ $this->clientId = $settings->clientId;
156
+ $this->clientSecret = $settings->clientSecret;
157
+ $this->debugLog = $settings->debugLog;
158
+
159
+ $this->debug("construct", $settings);
160
+ }
161
+
162
+ public function getAccessToken() {
163
+ return $this->accessToken;
164
+ }
165
+
166
+ public function setAccessToken($token) {
167
+ $this->accessToken = $token;
168
+ }
169
+
170
+ public function authorize() {
171
+ $response = $this->api("auth.token", array(
172
+ "grant_type" => "none",
173
+ "client_id" => $this->clientId,
174
+ "client_secret" => $this->clientSecret,
175
+ "return" => 1,
176
+ ));
177
+
178
+ if (!isset($response["response"]["access_token"]))
179
+ throw new Exception("Invalid client id or secret provided");
180
+
181
+ $this->accessToken = $response["response"]["access_token"];
182
+ return true;
183
+ }
184
+
185
+ public function optin($params, $instance = null) {
186
+ if ($this->accessToken == null)
187
+ $this->authorize();
188
+
189
+ if (!isset($params["email"]) && !isset($params["fb_user_id"])) {
190
+ throw new Exception("email or fb_user_id is required for optin");
191
+ }
192
+
193
+ if (!is_null($instance))
194
+ $params += array("instance" => $instance);
195
+
196
+ $response = $this->api("loyalty.optin", $params);
197
+ if (!isset($response["response"]["id"]))
198
+ throw new Exception("Failed to optin vipfan");
199
+
200
+ $this->vipfanId = $response["response"]["id"];
201
+ return $response["response"];
202
+ }
203
+
204
+ public function logout() {
205
+ $this->vipfanId = null;
206
+ }
207
+
208
+ /**
209
+ * Generate a Cookie: header line for the API call.
210
+ *
211
+ * This can be overridden to get them from somewhere else.
212
+ * Default functionality is prefixed local cookies.
213
+ * See also saveRecievedCookies().
214
+ *
215
+ * @return string|null
216
+ */
217
+ protected function generateCookieHeader() {
218
+
219
+ $cookie_data = null;
220
+
221
+ if (!empty($_COOKIE)) {
222
+ $cookie_parts = array();
223
+ foreach ($_COOKIE as $key => $val) {
224
+ // send only cookies with our prefix
225
+ if (strpos($key, $this->cookiePrefix) === 0) {
226
+ $key = str_replace($this->cookiePrefix, '', $key);
227
+ $cookie_parts[] = $key . "=" . $val;
228
+ }
229
+ }
230
+ if (!empty($cookie_parts)) {
231
+ $cookie_data = "Cookie: " . implode("; ", $cookie_parts);
232
+ }
233
+ }
234
+
235
+ return $cookie_data;
236
+ }
237
+
238
+ /**
239
+ * Do the post request
240
+ * @param $url
241
+ * @param array $postData
242
+ * @return string
243
+ */
244
+ protected function webRequest($url, $postData = array()) {
245
+ $this->debug("request: {$url}", $postData);
246
+
247
+ // get cookie data and send them together with request
248
+ $cookie_data = $this->generateCookieHeader();
249
+
250
+ $content = http_build_query($postData, null, "&");
251
+ $context = array(
252
+ "http" => array(
253
+ "method" => "POST",
254
+ "header" =>
255
+ "Connection: close" . "\r\n" .
256
+ "Content-Length: " . strlen($content) . "\r\n" .
257
+ "Content-Type: application/x-www-form-urlencoded" . "\r\n" .
258
+ (isset($cookie_data) ? $cookie_data . "\r\n" : ""),
259
+ "user_agent" => "smp-php-sdk-" . self::PHP_SDK_VERSION . ". php/" . PHP_VERSION,
260
+ "content" => $content,
261
+ "protocol_version" => "1.0",
262
+ "ignore_errors" => true,
263
+ "timeout" => $this->timeout,
264
+ ),
265
+ "ssl" => array(
266
+ ),
267
+ );
268
+
269
+ if (!is_null($this->proxy))
270
+ {
271
+ $context["http"]["request_fulluri"] = true;
272
+ $context["http"]["proxy"] = $this->proxy;
273
+ }
274
+
275
+ $result = null;
276
+
277
+ try {
278
+ $context = stream_context_create($context);
279
+ $result = file_get_contents($url, false, $context);
280
+ }
281
+ catch (Exception $e) {
282
+ $this->debug("error", $e);
283
+ }
284
+
285
+ // parse recieved cookies
286
+ $this->saveRecievedCookies($http_response_header);
287
+
288
+ return $result;
289
+ }
290
+
291
+ /**
292
+ * Parse recieved cookies and save them with our own prefix
293
+ * @param $headers - response headers
294
+ */
295
+ protected function saveRecievedCookies($headers) {
296
+ foreach ($headers as $hdr) {
297
+ if (!preg_match('/^Set-Cookie:\s*([^;]+);{0,1}\s*(.+)/', $hdr, $matches)) {
298
+ continue;
299
+ }
300
+
301
+ // cookie name=value
302
+ list($cookie_name, $cookie_value) = explode("=", $matches[1]);
303
+ $cookie_name = $this->cookiePrefix . $cookie_name;
304
+
305
+ // get allowed cookie attributes
306
+ $cookie_attr = array(
307
+ "expires" => 0,
308
+ "path" => "",
309
+ "domain" => "",
310
+ "secure" => false,
311
+ "httponly" => false
312
+ );
313
+
314
+ // overrride default attribute values
315
+ if (isset($matches[2])) {
316
+ $attr = explode(";", $matches[2]);
317
+ foreach ($attr as $name_value) {
318
+ $attr_parts = explode("=", $name_value);
319
+ $attr_name = trim($attr_parts[0]);
320
+ if (isset($attr_parts[1])) {
321
+ $attr_val = trim($attr_parts[1]);
322
+ if ($attr_name=="expires") {
323
+ $attr_val = strtotime($attr_val);
324
+ }
325
+ }
326
+ else {
327
+ // for secure and httponly
328
+ $attr_val = true;
329
+ }
330
+ // set only allowed cookie attributes
331
+ if ($this->isAllowedCookieAttribute($attr_name)) {
332
+ $cookie_attr[$attr_name] = $attr_val;
333
+ }
334
+ }
335
+ }
336
+
337
+ // set cookie
338
+ if ($cookie_name!="") {
339
+ $_COOKIE[$cookie_name] = $cookie_value;
340
+ setcookie($cookie_name, $cookie_value, $cookie_attr['expires'], $cookie_attr['path'], $cookie_attr['domain'], $cookie_attr['secure'], $cookie_attr['httponly']);
341
+ }
342
+ } // end foreach
343
+ }
344
+
345
+ /**
346
+ * Check if given cookie attribute is allowed to be set at our side
347
+ * @param $attr_name - cookie attribute name
348
+ * @return bool - true if it's allowed, otherwise false
349
+ */
350
+ private function isAllowedCookieAttribute($attr_name) {
351
+ $allowed_attributes = array("httponly", "expires");
352
+ return in_array($attr_name, $allowed_attributes);
353
+ }
354
+
355
+ public function exception_handler($errno, $errstr, $errfile, $errline) {
356
+ throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
357
+ }
358
+
359
+ /**
360
+ * Calls API
361
+ * @param string $method
362
+ * @param array $parameters
363
+ * @return mixed
364
+ * @throws Exception
365
+ */
366
+ public function api($method, $parameters) {
367
+ $this->debug("api call: {$method}", $parameters);
368
+
369
+ $params = $parameters + array(
370
+ "method" => $method,
371
+ "id_loyalty" => $this->loyaltyId,
372
+ "access_token" => $this->accessToken,
373
+ "id_vipfan" => $this->vipfanId
374
+ );
375
+
376
+ $return = $this->webRequest($this->apiUrl, $params);
377
+ $return = json_decode($return, true);
378
+
379
+ if (!isset($return["result"]) || $return["result"] != "success") {
380
+ $error = isset($return["error"]) ? $return["error"] : "";
381
+ $error = (empty($error) && isset($return["response"]) ? print_r($return["response"], true) : $error);
382
+ $error = (empty($error) ? print_r($return, true) : $error);
383
+
384
+ throw new Exception("SMP API Error: " . $error);
385
+ }
386
+
387
+ return $return;
388
+ }
389
+
390
+ public function setVipfanID($vipfanId) {
391
+ $this->debug("set vipfan", $vipfanId);
392
+ $this->vipfanId = $vipfanId;
393
+ }
394
+
395
+ /**
396
+ * Creates a new vip.me link from given url
397
+ * @param string $url
398
+ * @param string $label Label of the link (campaign)
399
+ * @param array $og_tags
400
+ * @return mixed
401
+ */
402
+ public function createLink($url, $label = "", $og_tags = array()) {
403
+ return $this->api("link.simple", array(
404
+ "opengraph_data" => is_array($og_tags) ? json_encode($og_tags) : $og_tags,
405
+ "label" => $label,
406
+ "url" => $url
407
+ ));
408
+ }
409
+
410
+ /**
411
+ * Tracks action on given instance (url)
412
+ * @param string $action
413
+ * @param string $instance Page url
414
+ * @param float $value Optional value of the tracked action
415
+ * @return mixed
416
+ */
417
+ public function track($action, $instance, $value = 0) {
418
+ if ($this->accessToken == null)
419
+ $this->authorize();
420
+
421
+ return $this->api("action.track", array(
422
+ "action" => $action,
423
+ "instance" => $instance,
424
+ "value" => $value
425
+ ));
426
+ }
427
+
428
+ static function create_signed_request($data, $secret) {
429
+ $data['algorithm'] = 'HMAC-SHA256';
430
+ $payload = rtrim(strtr(base64_encode(json_encode($data)), '+/', '-_'), '=');
431
+
432
+ $signature = hash_hmac('sha256', $payload, $secret, $raw = true);
433
+ $signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');
434
+
435
+ return $signature . '.' . $payload;
436
+ }
437
+ }
438
+
439
+ ?>
package.xml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>StarSMP_Customers_Tracking_Integration</name>
4
+ <version>1.0.1</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Integrates Magento with StarSMP - digital campaigns tracking and automation tool.</summary>
10
+ <description>StarSMP shows which marketing effort earns the most - ROI. StarSMP enables you to monitor all your digital campaigns and see what actions they bring. Further, StarSMP enables you to automate marketing campaigns.&#xD;
11
+ &#xD;
12
+ StarSMP main features:&#xD;
13
+ - track actions (anything that visitor do on your website)&#xD;
14
+ - automate rewards (automated emails, webhooks, repsonses)&#xD;
15
+ - analyze results (grouped and detail analytics of marketing campaigns) &#xD;
16
+ &#xD;
17
+ StarSMP Universal Tag - supports Google Analytics, Google Remarketing. Facebook Retargeting and any other custom tracking. &#xD;
18
+ &#xD;
19
+ StarSMP Universal Tag Features: &#xD;
20
+ - managing tracking tags in one place&#xD;
21
+ - add custom tracking codes&#xD;
22
+ - automatically deploys to your Magento e-shop&#xD;
23
+ - control access to your tracking codes</description>
24
+ <notes>First stable version</notes>
25
+ <authors><author><name>KremsaDigital</name><user>kremsa_design</user><email>kremsa.office.sk@gmail.com</email></author></authors>
26
+ <date>2014-10-07</date>
27
+ <time>19:33:16</time>
28
+ <contents><target name="magecommunity"><dir name="KremsaDigital"><dir name="StarSocial"><dir name="Helper"><file name="Data.php" hash="f2178de63bddfcaffe71bef7b8fcf829"/></dir><dir name="Model"><file name="Observer.php" hash="6cc3b6d784af3b900d61cf4cce27c83d"/><file name="Track.php" hash="77e647afddb19e3add21069c39965f46"/></dir><dir name="etc"><file name="adminhtml.xml" hash="735e6dfffa1e18830ec0f9a061eaa480"/><file name="config.xml" hash="5ba3521633493ef0b9ef9b9d4943fe47"/><file name="system.xml" hash="559342dbfacd62b62e8f05b15828bffd"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="default"><dir name="default"><dir name="layout"><file name="StarSocial.xml" hash="44aa76355559f1ab659e76fa5767dbd2"/></dir><dir name="template"><dir name="starsocial"><file name="tags-checkout.phtml" hash="259497eae83a0aa571b83f3de7713ea4"/><file name="tags.phtml" hash="0b2867be89168097b89c1368038e376e"/></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="KremsaDesign_StarSocial.xml" hash="f07ad022b4e7aaa1c67f0bd9d2458f99"/></dir></target><target name="magelib"><dir name="KremsaDigital"><dir name="StarSocial"><file name="starsmp.php" hash="d6c0f4ca17813c9ce955fab7b10cb40a"/></dir></dir></target></contents>
29
+ <compatible/>
30
+ <dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php><package><name>Mage_Core_Modules</name><channel>community</channel><min>1.6.0.0</min><max>1.9.0.1</max></package></required></dependencies>
31
+ </package>