ECommSolutionHook - Version 1.0.0

Version Notes

Initial release of ECommSolution Hooks

Download this release

Release Info

Developer ECommSolution
Extension ECommSolutionHook
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/local/Ecommsolutions/Maghook/Helper/Data.php ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Generic helper for module
5
+ *
6
+ * @author ECommsolution Dev-ID-25
7
+ */
8
+ class Ecommsolutions_Maghook_Helper_Data extends Mage_Core_Helper_Abstract
9
+ {
10
+
11
+ }
app/code/local/Ecommsolutions/Maghook/Model/Observer.php ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Observer to handle event
5
+ * Sends JSON data to URL on cart save and purchase
6
+ *
7
+ * @author ECommSolution Dev-id: 26
8
+ * @copyright Copyright (c) 2015 ECommSolution
9
+ *
10
+ */
11
+ class Ecommsolutions_Maghook_Model_Observer {
12
+
13
+ private $_is_enabled ;
14
+ private $_cart_change_hook ;
15
+ private $_sales_hook;
16
+ private $_shipment_hook ;
17
+ private $_customer_add_hook ;
18
+
19
+
20
+ const IS_ENABLED = 'ecommsolutions/ecommsolutions_group/aa_is_enabled';
21
+ const CART_CHANGE_URL = 'ecommsolutions/ecommsolutions_group/aa_cart_change';
22
+ const ORDER_URL = 'ecommsolutions/ecommsolutions_group/aa_sales_change';
23
+ const SHIPMENT_URL = 'ecommsolutions/ecommsolutions_group/aa_shipping_hook';
24
+ const CUSTOMER_URL = 'ecommsolutions/ecommsolutions_group/aa_customer_signup';
25
+
26
+
27
+ private function initConfig($store_id){
28
+ //$order->getStoreId();
29
+ if (!isset($store_id) or $store_id == "")
30
+ $st_id = Mage::app()->getStore()->getStoreId();
31
+ else
32
+ $st_id = $store_id;
33
+
34
+ $this->_is_enabled = $this->getValueFromConfigByName(self::IS_ENABLED,$st_id);
35
+ $this->_cart_change_hook = $this->getValueFromConfigByName(self::CART_CHANGE_URL,$st_id);
36
+ $this->_sales_hook = $this->getValueFromConfigByName(self::ORDER_URL,$st_id);
37
+ $this->_shipment_hook = $this->getValueFromConfigByName(self::SHIPMENT_URL,$st_id);
38
+ $this->_customer_add_hook = $this->getValueFromConfigByName(self::CUSTOMER_URL,$st_id);
39
+ }
40
+
41
+ private function getValueFromConfigByName($name,$store_id){
42
+ return Mage::getStoreConfig($name,$store_id);
43
+ }
44
+
45
+ public function postOrder($observer) {
46
+
47
+ $order = $observer->getEvent()->getOrder();
48
+ $this->initConfig($order->getStoreId());
49
+ if($this->_is_enabled == 0) return;
50
+ $orderStatus = $order->getStatus();
51
+ Mage::log('in post order : ' . $orderStatus);
52
+
53
+ try{
54
+ if (!is_null($orderStatus)){
55
+ $txn_ord["order_data"] = $this->transformOrder($order);
56
+ $txn_ord['order_status'] = $orderStatus;
57
+ $response = $this->proxy($txn_ord,$this->_sales_hook);
58
+ }
59
+ }
60
+ catch (Exception $e) {
61
+ echo 'Exception in order hook : ' . $e->getMessage(). "\n";
62
+ }
63
+ return $this;
64
+ }
65
+
66
+
67
+ public function cartSave($observer) {
68
+ $this->initConfig('');
69
+ try{
70
+ if($this->_is_enabled == 0) return;
71
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
72
+ $email = $customer->getEmail();
73
+
74
+ $result = array('email' => $email ,
75
+ 'full_name' => $customer->getName(),
76
+ 'first_name' => $customer->getFirstname(),
77
+ 'middle_name' => $customer->getMiddlename(),
78
+ 'last_name' => $customer->getLastname(),
79
+ 'dob' => $customer->getDob(),
80
+ 'gender' => $customer->getGender(),
81
+ );
82
+
83
+ if(Mage::getSingleton('customer/session')->isLoggedIn())
84
+ $result['is_logged_in'] = true;
85
+ else
86
+ $result['is_logged_in'] = false;
87
+
88
+ $allitems = $observer["cart"]->getQuote()->getAllItems();
89
+ $productMediaConfig = Mage::getModel('catalog/product_media_config');
90
+ foreach ($allitems as $item) {
91
+ $prodcut = $item->getProduct();
92
+ $prodcut = Mage::getModel('catalog/product')->load($prodcut->getId());
93
+ $productPrice = $prodcut->getPrice();
94
+ $itemqty = $item->getQty() ;
95
+ $sub_total = $productPrice * $itemqty ;
96
+ $prd_counter = array(
97
+ 'name' => $prodcut->getName() ,
98
+ 'quantity' => $itemqty ,
99
+ 'price' => Mage::helper('core')->currency($productPrice, true, false),
100
+ 'sub_total' => Mage::helper('core')->currency($sub_total, true, false),
101
+ 'thumbnail_image' => $productMediaConfig->getMediaUrl($prodcut->getThumbnail()) ,
102
+ 'product_url' => $prodcut->getProductUrl(true),
103
+ 'product_sku' => $item->getSku(),
104
+ 'prodcut_description' => $prodcut->getDescription(),
105
+ 'prodcut_short_description' => $prodcut->getShortDescription()
106
+ ) ;
107
+ $result['cartDetail']['cart'][] = $prd_counter ;
108
+ }
109
+ if(count($allitems) == 0)
110
+ $result['cartDetail']['cart'] = '';
111
+
112
+ $result['cartDetail']['total_price'] = $observer["cart"]->getQuote()->getGrandTotal();
113
+ $this->proxy($result,$this->_cart_change_hook);
114
+ }
115
+ catch (Exception $e) {
116
+ echo 'Exception in cart hook : ' . $e->getMessage(). "\n";
117
+ }
118
+ return $this;
119
+ }
120
+
121
+
122
+ public function postUserCreation($observer) {
123
+ $this->initConfig('');
124
+ if($this->_is_enabled == 0) return;
125
+ $customer = $observer["customer"];
126
+ $customer = $customer->getData();
127
+
128
+ try{
129
+ $result = array('email' => $customer["email"] ,
130
+ 'first_name' => $customer["firstname"] ,
131
+ 'last_name' => $customer["lastname"] ,
132
+ 'store_name' => $customer["created_in"],
133
+ 'store_id' => $customer["store_id"],
134
+ 'signup_date' => $customer["created_at"]
135
+ );
136
+ $this->proxy($result,$this->_customer_add_hook);
137
+ }
138
+ catch (Exception $e) {
139
+ echo 'Exception in subscriber hook : ' . $e->getMessage(). "\n";
140
+ }
141
+ return $this;
142
+ }
143
+
144
+
145
+ public function saveShipping($observer){
146
+ try {
147
+ $shipment = $observer->getEvent()->getShipment();
148
+ $order = $shipment->getOrder();
149
+ $this->initConfig($order->getStoreId());
150
+ if($this->_is_enabled == 0) return;
151
+ $order_data = $order->getData();
152
+ $result["order_data"] = $this->transformOrder($order);
153
+ $tracks = $shipment->getAllTracks();
154
+
155
+ foreach ($tracks as $track) {
156
+ $trk_data = $track->getData();
157
+ $track_id = $trk_data["entity_id"];
158
+ $track_cntr = array(
159
+ 'tracking_number' => $trk_data["track_number"] ,
160
+ 'carrier_code' => $trk_data["carrier_code"] ,
161
+ 'tracking_title' => $trk_data["title"],
162
+ 'tracking_created_at' => $trk_data["created_at"],
163
+ 'tracking_entity_id' => $trk_data["entity_id"] ,
164
+ 'tracking_url' => Mage::helper('shipping')->getTrackingPopUpUrlByTrackId($track_id)
165
+ ) ;
166
+ $result['order_data']['shipping_details'][] = $track_cntr ;
167
+
168
+ }
169
+ $this->proxy($result,$this->_shipment_hook);
170
+ }
171
+ catch (Exception $e) {
172
+ echo 'Exception in shipment hook : ' . $e->getMessage(). "\n";
173
+ }
174
+ }
175
+ /**
176
+ * Curl data and return body
177
+ *
178
+ * @param $data
179
+ * @param $url
180
+ * @return stdClass $output
181
+ */
182
+ private function proxy($data, $url) {
183
+
184
+ $output = new stdClass();
185
+ $ch = curl_init();
186
+ $body = json_encode($data);
187
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
188
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
189
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
190
+ //'Accept: application/json',
191
+ 'Content-Type: application/json',
192
+ 'Content-Length: ' . strlen($body),
193
+ // http://stackoverflow.com/questions/11359276/php-curl-exec-returns-both-http-1-1-100-continue-and-http-1-1-200-ok-separated-b
194
+ 'Expect:' // Remove "HTTP/1.1 100 Continue" from response
195
+ ));
196
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60 * 2); // 2 minutes to connect
197
+ curl_setopt($ch, CURLOPT_TIMEOUT, 60 * 4); // 8 minutes to fetch the response
198
+ curl_setopt($ch, CURLOPT_URL, $url);
199
+ curl_setopt($ch, CURLOPT_HEADER, 1);
200
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
201
+
202
+ // ignore cert issues
203
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
204
+
205
+ // execute
206
+ $response = curl_exec($ch);
207
+ $output->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
208
+ curl_close($ch);
209
+
210
+ // handle response
211
+ $arr = explode("\r\n\r\n", $response, 2);
212
+ if (count($arr) == 2) {
213
+ $output->header = $arr[0];
214
+ $output->body = $arr[1];
215
+ } else {
216
+ $output->body = "Unexpected response";
217
+ }
218
+ return $output;
219
+ }
220
+
221
+ /**
222
+ * Transform order into one data object for posting
223
+ */
224
+ /**
225
+ * @param $orderIn Mage_Sales_Model_Order
226
+ * @return mixed
227
+ */
228
+ private function transformOrder($orderIn) {
229
+ Mage::Log(' in transformOrder');
230
+
231
+ $orderOut = $orderIn->getData();
232
+ $orderOut['line_items'] = array();
233
+ foreach ($orderIn->getAllItems() as $item) {
234
+ $orderOut['line_items'][] = $item->getData();
235
+ }
236
+
237
+ /** @var $customer Mage_Customer_Model_Customer */
238
+ $customer = Mage::getModel('customer/customer')->load($orderIn->getCustomerId());
239
+ $orderOut['customer'] = $customer->getData();
240
+ $orderOut['customer']['customer_id'] = $orderIn->getCustomerId();
241
+
242
+ /** @var $shipping_address Mage_Sales_Model_Order_Address*/
243
+ $shipping_address = $orderIn->getShippingAddress();
244
+ $orderOut['shipping_address'] = $shipping_address->getData();
245
+
246
+ /** @var $shipping_address Mage_Sales_Model_Order_Address*/
247
+ $billing_address = $orderIn->getBillingAddress();
248
+ $orderOut['billing_address'] = $billing_address->getData();
249
+
250
+ /** @var $shipping_address Mage_Sales_Model_Order_Payment*/
251
+ $payment = $orderIn->getPayment()->getData();
252
+
253
+ // remove cc fields
254
+ foreach ($payment as $key => $value) {
255
+ if (strpos($key, 'cc_') !== 0) {
256
+ $orderOut['payment'][$key] = $value;
257
+ }
258
+ }
259
+
260
+ /** @var $orderOut Mage_Core_Model_Session */
261
+ $session = Mage::getModel('core/session');
262
+ $orderOut['visitor'] = $session->getValidatorData();
263
+ return $orderOut;
264
+ }
265
+ }
app/code/local/Ecommsolutions/Maghook/etc/config.xml ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Ecommsolutions_Maghook>
5
+ <version>1.0.0</version>
6
+ </Ecommsolutions_Maghook>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <maghook>
11
+ <class>Ecommsolutions_Maghook_Model</class>
12
+ </maghook>
13
+ </models>
14
+ <helpers>
15
+ <maghook>
16
+ <class>Ecommsolutions_Maghook_Helper</class>
17
+ </maghook>
18
+ </helpers>
19
+ <events>
20
+ <sales_order_save_after>
21
+ <observers>
22
+ <post_order>
23
+ <class>maghook/observer</class>
24
+ <method>postOrder</method>
25
+ </post_order>
26
+ </observers>
27
+ </sales_order_save_after>
28
+ <customer_register_success>
29
+ <observers>
30
+ <post_user_creation>
31
+ <class>maghook/observer</class>
32
+ <method>postUserCreation</method>
33
+ </post_user_creation>
34
+ </observers>
35
+ </customer_register_success>
36
+ <checkout_cart_save_after>
37
+ <observers>
38
+ <cart_save>
39
+ <class>maghook/observer</class>
40
+ <method>cartSave</method>
41
+ </cart_save>
42
+ </observers>
43
+ </checkout_cart_save_after>
44
+ <sales_order_shipment_save_after>
45
+ <observers>
46
+ <save_shipping>
47
+ <class>maghook/observer</class>
48
+ <method>saveShipping</method>
49
+ </save_shipping>
50
+ </observers>
51
+ </sales_order_shipment_save_after>
52
+ </events>
53
+ </global>
54
+ <adminhtml>
55
+ <acl>
56
+ <resources>
57
+ <all>
58
+ <title>Allow Everything</title>
59
+ </all>
60
+ <admin>
61
+ <children>
62
+ <system>
63
+ <children>
64
+ <config>
65
+ <children>
66
+ <ecommsolutions>
67
+ <title>ECommSolutions</title>
68
+ </ecommsolutions>
69
+ </children>
70
+ </config>
71
+ </children>
72
+ </system>
73
+ </children>
74
+ </admin>
75
+ </resources>
76
+ </acl>
77
+ </adminhtml>
78
+ </config>
app/code/local/Ecommsolutions/Maghook/etc/system.xml ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <tabs>
4
+ <ecommsolutions translate="label" module="maghook">
5
+ <label>ECommSolutions Hooks</label>
6
+ <sort_order>101</sort_order>
7
+ </ecommsolutions>
8
+ </tabs>
9
+ <sections>
10
+ <ecommsolutions translate="label" module="maghook">
11
+ <label>Setting</label>
12
+ <tab>ecommsolutions</tab>
13
+ <sort_order>1000</sort_order>
14
+ <show_in_default>1</show_in_default>
15
+ <show_in_website>1</show_in_website>
16
+ <show_in_store>1</show_in_store>
17
+
18
+ <groups>
19
+ <ecommsolutions_group translate="label" module="maghook">
20
+ <label>Webhook setting</label>
21
+ <frontend_type>text</frontend_type>
22
+ <sort_order>1000</sort_order>
23
+ <show_in_default>1</show_in_default>
24
+ <show_in_website>1</show_in_website>
25
+ <show_in_store>1</show_in_store>
26
+ <comment>
27
+ </comment>
28
+ <expanded>1</expanded>
29
+ <fields>
30
+ <aa_is_enabled translate="label">
31
+ <label>Enable hook event: </label>
32
+ <comment>Select Yes/No</comment>
33
+ <frontend_type>select</frontend_type>
34
+ <sort_order>1</sort_order>
35
+ <show_in_default>1</show_in_default>
36
+ <show_in_website>1</show_in_website>
37
+ <show_in_store>1</show_in_store>
38
+ <source_model>adminhtml/system_config_source_yesno</source_model>
39
+ </aa_is_enabled>
40
+ <aa_cart_change translate="label">
41
+ <label>Cart change hook: </label>
42
+ <comment>This will send request on cart change(product/qty change in cart)</comment>
43
+ <frontend_type>text</frontend_type>
44
+ <sort_order>2</sort_order>
45
+ <show_in_default>1</show_in_default>
46
+ <show_in_website>1</show_in_website>
47
+ <show_in_store>1</show_in_store>
48
+ <validate>required-entry</validate>
49
+ <depends><aa_is_enabled>1</aa_is_enabled></depends>
50
+ </aa_cart_change>
51
+ <aa_sales_change translate="label">
52
+ <label>Order hook: </label>
53
+ <comment>This will send request on order </comment>
54
+ <frontend_type>text</frontend_type>
55
+ <sort_order>3</sort_order>
56
+ <show_in_default>1</show_in_default>
57
+ <show_in_website>1</show_in_website>
58
+ <show_in_store>1</show_in_store>
59
+ <validate>required-entry</validate>
60
+ <depends><aa_is_enabled>1</aa_is_enabled></depends>
61
+ </aa_sales_change>
62
+ <aa_shipping_hook translate="label">
63
+ <label>Order shipment hook: </label>
64
+ <comment>This will send request on every shipment</comment>
65
+ <frontend_type>text</frontend_type>
66
+ <sort_order>4</sort_order>
67
+ <show_in_default>1</show_in_default>
68
+ <show_in_website>1</show_in_website>
69
+ <show_in_store>1</show_in_store>
70
+ <validate>required-entry</validate>
71
+ <depends><aa_is_enabled>1</aa_is_enabled></depends>
72
+ </aa_shipping_hook>
73
+ <aa_customer_signup translate="label">
74
+ <label>Customer signup hook: </label>
75
+ <comment>This will send request on every customer signup</comment>
76
+ <frontend_type>text</frontend_type>
77
+ <sort_order>4</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
+ <validate>required-entry</validate>
82
+ <depends><aa_is_enabled>1</aa_is_enabled></depends>
83
+ </aa_customer_signup>
84
+ </fields>
85
+ </ecommsolutions_group>
86
+ </groups>
87
+ </ecommsolutions>
88
+ </sections>
89
+ </config>
app/etc/modules/Ecommsolutions_Maghook.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Ecommsolutions_Maghook>
5
+ <active>true</active>
6
+ <codePool>local</codePool>
7
+ </Ecommsolutions_Maghook>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>ECommSolutionHook</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>Apache - ASL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Correct your app with Magento via EcommSolution Hooks</summary>
10
+ <description>This extension provide you to specify webhook for cart change, purchase, shipment and new user signup. &#xD;
11
+ &#xD;
12
+ You can setup webhook for them and capture request in realtime , that can connect/notify your App.</description>
13
+ <notes>Initial release of ECommSolution Hooks</notes>
14
+ <authors><author><name>ECommSolution</name><user>ECommSolution</user><email>AA.Systems@outlook.com</email></author></authors>
15
+ <date>2015-07-01</date>
16
+ <time>18:38:40</time>
17
+ <contents><target name="magelocal"><dir name="Ecommsolutions"><dir name="Maghook"><dir name="etc"><file name="config.xml" hash="0949995a66d27bea14370e709f061ff6"/><file name="system.xml" hash="64321d0a30af190f149d08c881a8b380"/></dir><dir name="Helper"><file name="Data.php" hash="beb2dcf81f3bfe397551e650e1af63a2"/></dir><dir name="Model"><file name="Observer.php" hash="f360735e643816c6b47ad49740e1e37c"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Ecommsolutions_Maghook.xml" hash="38f30e07964db6075fb64597e43ea883"/></dir></target></contents>
18
+ <compatible/>
19
+ <dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
20
+ </package>