Sparo_CharityProcessor - Version 1.0.2

Version Notes

Removal of curl requirement. Preventing double-notification to Sparo's servers

Download this release

Release Info

Developer Kyril Revels
Extension Sparo_CharityProcessor
Version 1.0.2
Comparing to
See all releases


Code changes from version 1.0.1 to 1.0.2

app/code/community/Sparo/CharityProcessor/Model/Environments.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Sparo_CharityProcessor_Model_Environments {
4
+ public function toOptionArray(){
5
+ return array(
6
+ array('value' => 'http://scripts.demo.sparo.com', 'label' => 'No'),
7
+ array('value' => 'https://scripts.sparo.com', 'label' => 'Yes')
8
+ );
9
+ }
10
+ }
app/code/community/Sparo/CharityProcessor/Model/Observer.php DELETED
@@ -1,62 +0,0 @@
1
- <?php
2
-
3
- class Sparo_CharityProcessor_Model_Observer {
4
-
5
- public function sendConfirmation($observer){
6
- $transaction_id = Mage::getSingleton('core/session')->getTransactionId();
7
-
8
- if(!empty($transaction_id)){
9
- // fetch the order
10
- $order = $observer->getEvent()->getOrder();
11
-
12
- // retrieve sparo api connection info
13
- $merchant_id = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_merchantid');
14
- $apikey = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_apikey');
15
- $production = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_productionmode');
16
-
17
- // send confirmation with charity and transaction id
18
- $url = ($production) ?
19
- 'https://tx.sparo.com/confirm.php' :
20
- 'http://tx.demo.sparo.com/confirm.php';
21
-
22
- $postdata = array(
23
- 'merchant' => $merchant_id,
24
- 'apikey' => $apikey,
25
- 'txid' => $transaction_id,
26
- 'txorderid' => $order->getRealOrderId(),
27
- 'txcost' => $order->getSubtotal()
28
- );
29
-
30
-
31
- $ch = curl_init();
32
- if(!empty($ch)){
33
- curl_setopt($ch, CURLOPT_URL, $url);
34
- curl_setopt($ch, CURLOPT_POST, true);
35
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
36
-
37
- curl_exec($ch);
38
-
39
- if(!curl_errno($ch)){
40
- // successful connection
41
- $res = curl_getinfo($ch);
42
- $this->logMessage(sprintf("%s [order id: %s] [transaction id: %s] Server responded with status code: %d", date('c'), $order->getRealOrderId(), $transaction_id, $res['http_code']));
43
- }
44
- else{
45
- // connection failure
46
- $this->logMessage(sprintf("%s [order id: %s] [transaction id: %s] Error initiating connection with server: %s", date('c'), $order->getRealOrderId(), $transaction_id, curl_error($ch)));
47
- }
48
-
49
- curl_close($ch);
50
- }
51
-
52
- // unset session variables
53
- Mage::getSingleton('core/session')->unsCharityId();
54
- Mage::getSingleton('core/session')->unsTransactionId();
55
- }
56
- }
57
-
58
- protected function logMessage($msg){
59
- Mage::log($msg, null, 'sparo.log');
60
- }
61
-
62
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Sparo/CharityProcessor/Model/Resource/Script/Setup.php CHANGED
@@ -21,6 +21,24 @@ HTML;
21
 
22
  }
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  public function addContainerBlock(){
25
  Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
26
 
@@ -35,4 +53,25 @@ HTML;
35
 
36
  Mage::getModel('cms/block')->setData($containerBlock)->save();
37
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
21
 
22
  }
23
 
24
+ public function addScriptBlock_1_0_2(){
25
+ Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
26
+
27
+ $content = <<<HTML
28
+ <script type="text/javascript" id="SPARO-lib" data-merchant="{{config path="sparoconfig/sparoconfig_merchant/sparo_merchantid"}}" src="{{config path="sparoconfig/sparoconfig_merchant/sparo_productionmode"}}/sparo.min.js"></script>
29
+ HTML;
30
+
31
+ $scriptBlock = array(
32
+ 'title' => 'Sparo Charity Selection Tags',
33
+ 'identifier' => 'sparo_charity_selection_tags',
34
+ 'content' => $content,
35
+ 'is_active' => 1,
36
+ 'stores' => array(0),
37
+ );
38
+
39
+ Mage::getModel('cms/block')->setData($scriptBlock)->save();
40
+ }
41
+
42
  public function addContainerBlock(){
43
  Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
44
 
53
 
54
  Mage::getModel('cms/block')->setData($containerBlock)->save();
55
  }
56
+
57
+ public function updateScriptBlock_1_0_2() {
58
+ Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
59
+
60
+ $blocks = Mage::getModel('cms/block')
61
+ ->getCollection()
62
+ ->addFieldToFilter('identifier', 'sparo_charity_selection_tags');
63
+
64
+ $new_content = <<<HTML
65
+ <script type="text/javascript" id="SPARO-lib" data-merchant="{{config path="sparoconfig/sparoconfig_merchant/sparo_merchantid"}}" src="{{config path="sparoconfig/sparoconfig_merchant/sparo_productionmode"}}/sparo.min.js"></script>
66
+ HTML;
67
+
68
+ $updated_data = array('content' => $new_content);
69
+
70
+ foreach($blocks as $b){
71
+ $model = Mage::getModel('cms/block')->load($b->getId())->addData($updated_data);
72
+ $model->save();
73
+ }
74
+
75
+ }
76
+
77
  }
app/code/community/Sparo/CharityProcessor/Model/SaleObserver.php ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Sparo_CharityProcessor_Model_SaleObserver {
4
+
5
+ public function sendConfirmation($observer){
6
+ $transaction_id = Mage::getModel('core/cookie')->get('sparo_txid');
7
+
8
+ if(!empty($transaction_id)){
9
+ // fetch the order
10
+ $order = $observer->getEvent()->getOrder();
11
+ if($order->getAlreadyProcessedBySparo()){
12
+ return;
13
+ }
14
+
15
+ // retrieve sparo api connection info
16
+ $merchant_id = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_merchantid');
17
+ $apikey = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_apikey');
18
+ $environment = Mage::getStoreConfig('sparoconfig/sparoconfig_merchant/sparo_productionmode');
19
+
20
+ // send confirmation with charity and transaction id
21
+ $url = (strstr($environment, 'demo.sparo.com') !== false) ? 'http://tx2.demo.sparo.com/confirm.php' : 'https://tx2.sparo.com/confirm.php';
22
+
23
+ $postdata = array(
24
+ 'merchant' => $merchant_id,
25
+ 'apikey' => $apikey,
26
+ 'txid' => $transaction_id,
27
+ 'txorderid' => $order->getRealOrderId(),
28
+ 'txcost' => $order->getSubtotal()
29
+ );
30
+
31
+ $opts = array(
32
+ 'http' => array(
33
+ 'method'=>'POST',
34
+ 'content'=>http_build_query($postdata)
35
+ )
36
+ );
37
+
38
+ $context = stream_context_create($opts);
39
+ $fp = fopen($url, 'rb', false, $context);
40
+
41
+ if(!$fp){
42
+ $this->logMessage(sprintf("Problem connecting to url: %s", $url));
43
+ }
44
+ else{
45
+ $res = stream_get_contents($fp);
46
+ fclose($fp);
47
+ if($res === false){
48
+ $this->logMessage(sprintf("%s [order id: %s] [transaction id: %s] Error initiating connection with server: %s", date('c'), $order->getRealOrderId(), $transaction_id, curl_error($ch)));
49
+ }
50
+ else{
51
+ $this->logMessage(sprintf("%s [order id: %s] [transaction id: %s] Server responded with status code: %d", date('c'), $order->getRealOrderId(), $transaction_id, $res['http_code']));
52
+ }
53
+ }
54
+
55
+ // unset session variables
56
+ Mage::getModel('core/cookie')->delete('sparo_txid');
57
+ Mage::getModel('core/cookie')->delete('sparo_charity');
58
+ $order->setAlreadyProcessedBySparo(true);
59
+ }
60
+ }
61
+
62
+ protected function logMessage($msg){
63
+ Mage::log($msg, null, 'sparo.log');
64
+ }
65
+
66
+ }
app/code/community/Sparo/CharityProcessor/controllers/IndexController.php CHANGED
@@ -3,23 +3,28 @@ class Sparo_CharityProcessor_IndexController extends Mage_Core_Controller_Front_
3
 
4
 
5
  public function indexAction(){
6
- $charity_id = $this->getRequest()->getParam('charity');
7
- $transaction_id = $this->getRequest()->getParam('txid');
8
 
9
- if(!empty($charity_id)){
10
- Mage::getSingleton('core/session')->setCharityId($charity_id);
11
- }
12
- else {
13
- Mage::getSingleton('core/session')->unsCharityId();
14
- }
15
 
 
 
 
 
 
 
16
 
17
- if(!empty($transaction_id)){
18
- Mage::getSingleton('core/session')->setTransactionId($transaction_id);
19
- }
20
- else {
21
- Mage::getSingleton('core/session')->unsTransactionId();
22
- }
 
23
  }
24
  }
25
 
3
 
4
 
5
  public function indexAction(){
6
+ $model = new Sparo_CharityProcessor_Model_SaleObserver();
7
+ var_dump(get_class($model));
8
 
9
+ $model = Mage::getModel('sparo_charityprocessor/saleobserver');
10
+ var_dump(get_class($model));
11
+ //$charity_id = $this->getRequest()->getParam('charity');
12
+ //$transaction_id = $this->getRequest()->getParam('txid');
 
 
13
 
14
+ //if(!empty($charity_id)){
15
+ //Mage::getSingleton('core/session')->setCharityId($charity_id);
16
+ //}
17
+ //else {
18
+ //Mage::getSingleton('core/session')->unsCharityId();
19
+ //}
20
 
21
+
22
+ //if(!empty($transaction_id)){
23
+ //Mage::getSingleton('core/session')->setTransactionId($transaction_id);
24
+ //}
25
+ //else {
26
+ //Mage::getSingleton('core/session')->unsTransactionId();
27
+ //}
28
  }
29
  }
30
 
app/code/community/Sparo/CharityProcessor/etc/config.xml CHANGED
@@ -10,7 +10,7 @@
10
  -->
11
  <Sparo_CharityProcessor>
12
  <!-- The version of our module, starting at 0.0.1 -->
13
- <version>1.0.1</version>
14
  </Sparo_CharityProcessor>
15
  </modules>
16
  <global>
@@ -31,19 +31,20 @@
31
  </sparo_charityprocessor>
32
  </models>
33
  <events>
34
- <checkout_type_onepage_save_order_after>
35
  <observers>
36
- <sparo_charityprocessor>
37
- <class>sparo_charityprocessor/observer</class>
38
  <method>sendConfirmation</method>
39
  <type>singleton</type>
40
- </sparo_charityprocessor>
41
  </observers>
42
- </checkout_type_onepage_save_order_after>
43
  </events>
44
  </global>
45
  <frontend>
46
  <routers>
 
47
  <charityprocessor>
48
  <use>standard</use>
49
  <args>
@@ -51,6 +52,7 @@
51
  <frontName>charityprocessor</frontName>
52
  </args>
53
  </charityprocessor>
 
54
  </routers>
55
  </frontend>
56
  </config>
10
  -->
11
  <Sparo_CharityProcessor>
12
  <!-- The version of our module, starting at 0.0.1 -->
13
+ <version>1.0.2</version>
14
  </Sparo_CharityProcessor>
15
  </modules>
16
  <global>
31
  </sparo_charityprocessor>
32
  </models>
33
  <events>
34
+ <sales_order_save_after>
35
  <observers>
36
+ <sparo_charityprocessor_order_save_after>
37
+ <class>Sparo_CharityProcessor_Model_SaleObserver</class>
38
  <method>sendConfirmation</method>
39
  <type>singleton</type>
40
+ </sparo_charityprocessor_order_save_after>
41
  </observers>
42
+ </sales_order_save_after>
43
  </events>
44
  </global>
45
  <frontend>
46
  <routers>
47
+ <!--
48
  <charityprocessor>
49
  <use>standard</use>
50
  <args>
52
  <frontName>charityprocessor</frontName>
53
  </args>
54
  </charityprocessor>
55
+ -->
56
  </routers>
57
  </frontend>
58
  </config>
app/code/community/Sparo/CharityProcessor/etc/system.xml CHANGED
@@ -20,11 +20,13 @@
20
  <show_in_store>1</show_in_store>
21
  <fields>
22
  <sparo_productionmode translate="label comment">
23
- <label>Run in Production</label>
24
  <comment>
25
  <![CDATA[Must be enabled when ready to send live transactions to the Sparo servers. Otherwise, all transactions will be sent to Sparo's testing environment]]>
26
  </comment>
27
- <frontend_type>checkbox</frontend_type>
 
 
28
  <sort_order>1000</sort_order>
29
  <show_in_default>1</show_in_default>
30
  <show_in_website>1</show_in_website>
20
  <show_in_store>1</show_in_store>
21
  <fields>
22
  <sparo_productionmode translate="label comment">
23
+ <label>Run in Production?</label>
24
  <comment>
25
  <![CDATA[Must be enabled when ready to send live transactions to the Sparo servers. Otherwise, all transactions will be sent to Sparo's testing environment]]>
26
  </comment>
27
+ <frontend_type>select</frontend_type>
28
+ <!-- <source_model>adminhtml/system_config_source_yesno</source_model> -->
29
+ <source_model>sparo_charityprocessor/environments</source_model>
30
  <sort_order>1000</sort_order>
31
  <show_in_default>1</show_in_default>
32
  <show_in_website>1</show_in_website>
app/code/community/Sparo/CharityProcessor/sql/sparo_charityprocessor_setup/install-1.0.2.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $installer = $this;
3
+ $installer->startSetup();
4
+
5
+ //$installer->unregisterOldObserver();
6
+ $installer->addScriptBlock_1_0_2();
7
+ $installer->addContainerBlock();
8
+
9
+ $installer->endSetup();
app/code/community/Sparo/CharityProcessor/sql/sparo_charityprocessor_setup/upgrade-1.0.1-1.0.2.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $installer = $this;
4
+ $installer->startSetup();
5
+
6
+ //$installer->unregisterOldObserver();
7
+ $installer->updateScriptBlock_1_0_2();
8
+
9
+ $installer->endSetup();
package.xml CHANGED
@@ -1,22 +1,2 @@
1
  <?xml version="1.0"?>
2
- <package>
3
- <name>Sparo_CharityProcessor</name>
4
- <version>1.0.1</version>
5
- <stability>stable</stability>
6
- <license uri="http://opensource.org/licenses/MIT">MITL</license>
7
- <channel>community</channel>
8
- <extends/>
9
- <summary>Allows registered Sparo merchants to make charitable donations on behalf of their customers</summary>
10
- <description>Sparo's purpose is threefold. It allows everyone to do more good - more often - through their everyday purchases, provides merchants a way in which they can contribute to the causes that their customers care about, all while advancing the good work that charities do for their community everyday.</description>
11
- <notes>Use of this module requires a merchant account with Sparo, in addition to an active API Key.&#xD;
12
- &#xD;
13
- Find out more information:&#xD;
14
- &#xD;
15
- https://sparo.com/pages/merchant-about</notes>
16
- <authors><author><name>Kyril Revels</name><user>krevels</user><email>kyril@developdc.com</email></author></authors>
17
- <date>2015-04-07</date>
18
- <time>18:11:44</time>
19
- <contents><target name="magecommunity"><dir name="Sparo"><dir name="CharityProcessor"><dir name="Model"><file name="Observer.php" hash="bdd542e661bcf033c5917f1e9642e37e"/><dir name="Resource"><dir name="Script"><file name="Setup.php" hash="6fa8e98464aeaf4f385cf44eaa0af401"/></dir></dir></dir><dir name="controllers"><file name="IndexController.php" hash="8ecb0daa40ada2842beaf83ecdf47a64"/></dir><dir name="etc"><file name="adminhtml.xml" hash="d9d7d2e16fc75a3ed21fb37d799e2d4a"/><file name="config.xml" hash="f49431810fef7bcf5087afca0e6a18f3"/><file name="system.xml" hash="4632c60b3f154d8fbf4a0d34b7b1f347"/></dir><dir name="sql"><dir name="sparo_charityprocessor_setup"><file name="install-1.0.1.php" hash="5ecb0bfa51900ecbc56a2173cf048a97"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Sparo_CharityProcessor.xml" hash="3b6207ba3e2657bad87b2a6ea2ae6343"/></dir></target></contents>
20
- <compatible/>
21
- <dependencies><required><php><min>5.3.0</min><max>5.7.0</max></php><package><name>Mage_Core_Modules</name><channel>community</channel><min>1.5.0.0</min><max>1.9</max></package><extension><name>curl</name><min>5.3.0</min><max>5.7.0</max></extension></required></dependencies>
22
- </package>
1
  <?xml version="1.0"?>
2
+ <package><name>Sparo_CharityProcessor</name><version>1.0.2</version><stability>stable</stability><license>MIT</license><channel>community</channel><extends></extends><summary>Allows registered Sparo merchants to make charitable donations on behalf of their customers</summary><description>Sparo's purpose is threefold. It allows everyone to do more good - more often - through their everyday purchases, provides merchants a way in which they can contribute to the causes that their customers care about, all while advancing the good work that charities do for their community everyday.</description><notes>Removal of curl requirement. Preventing double-notification to Sparo's servers</notes><authors><author><name>Kyril Revels</name><user>krevels</user><email>kyril@developdc.com</email></author></authors><date>2015-04-16</date><time>9:28:59</time><compatible></compatible><dependencies><required><php><min>5.3.0</min><max>6.0.0</max></php></required></dependencies><contents><target name="mage"><dir name="app"><dir name="etc"><dir name="modules"><file name="Sparo_CharityProcessor.xml" hash="3b6207ba3e2657bad87b2a6ea2ae6343"/></dir></dir><dir name="code"><dir name="community"><dir name="Sparo"><dir name="CharityProcessor"><dir name="etc"><file name="adminhtml.xml" hash="d9d7d2e16fc75a3ed21fb37d799e2d4a"/><file name="config.xml" hash="d0ef0cc70a4a49381a7446a8bb36dafc"/><file name="system.xml" hash="432f2375966dd01f5317013a2fa705a2"/></dir><dir name="controllers"><file name="IndexController.php" hash="ad0d9deb21fd29504258288765426691"/></dir><dir name="Model"><file name="Environments.php" hash="9e84c98ca7e2b798db172b050611329c"/><file name="SaleObserver.php" hash="68124748cf0ebc3417f7271682dc3666"/><dir name="Resource"><dir name="Script"><file name="Setup.php" hash="f00dc26fea5e19f8939a2d696cef07bd"/></dir></dir></dir><dir name="sql"><dir name="sparo_charityprocessor_setup"><file name="install-1.0.1.php" hash="5ecb0bfa51900ecbc56a2173cf048a97"/><file name="install-1.0.2.php" hash="38478931517fcd45fff695fb2bafadc8"/><file name="upgrade-1.0.1-1.0.2.php" hash="156d4107844816e516d7e1f89878c810"/></dir></dir></dir></dir></dir></dir></dir></target></contents></package>