Mage_Mainpay - Version 1.0.0

Version Notes

Stable version.

Download this release

Release Info

Developer Magento Core Team
Extension Mage_Mainpay
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/local/Mage/Mainpay/Block/Mainpaymodel/Form.php ADDED
@@ -0,0 +1,9 @@
1
+ <?php
2
+ class Mage_Mainpay_Block_Mainpaymodel_Form extends Mage_Payment_Block_Form {
3
+
4
+ protected function _construct() {
5
+ $this->setTemplate('mainpay/mainpaymodel/form.phtml');
6
+ parent::_construct();
7
+ }
8
+
9
+ }
app/code/local/Mage/Mainpay/Block/Mainpaymodel/Redirect.php ADDED
@@ -0,0 +1,27 @@
1
+ <?php
2
+ class Mage_Mainpay_Block_Mainpaymodel_Redirect extends Mage_Core_Block_Abstract {
3
+
4
+ protected function _toHtml() {
5
+ $mainpay = Mage::getModel('mainpay/mainpaymodel');
6
+
7
+ $form = new Varien_Data_Form();
8
+ $form ->setAction($mainpay->getMainpayUrl())
9
+ ->setId('mainpay_mainpaymodel_checkout')
10
+ ->setName('mainpay_mainpaymodel_checkout')
11
+ ->setMethod('POST')
12
+ ->setUseContainer(true);
13
+
14
+ foreach ($mainpay->getMainpayCheckoutFormFields() as $field=>$value) {
15
+ $form->addField($field, 'hidden', array('name'=>$field, 'value'=>$value));
16
+ }
17
+
18
+ $html = '<html><body>';
19
+ $html.= iconv('UTF-8', 'windows-1251', $this->__('You will be redirected to MainPay payment interface in a few seconds.'));
20
+ $html.= $form->toHtml();
21
+ $html.= '<script type="text/javascript">document.getElementById("mainpay_mainpaymodel_checkout").submit();</script>';
22
+ $html.= '</body></html>';
23
+
24
+ return $html;
25
+ }
26
+
27
+ }
app/code/local/Mage/Mainpay/Helper/Data.php ADDED
@@ -0,0 +1,5 @@
1
+ <?php
2
+ class Mage_Mainpay_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+
5
+ }
app/code/local/Mage/Mainpay/Model/Mainpaymodel.php ADDED
@@ -0,0 +1,149 @@
1
+ <?php
2
+
3
+ class Mage_Mainpay_Model_Mainpaymodel extends Mage_Payment_Model_Method_Abstract
4
+ {
5
+ protected $_code = 'mainpay_mainpaymodel';
6
+ protected $_formBlockType = 'mainpay/mainpaymodel_form';
7
+
8
+ public function getCheckout() {
9
+ return Mage::getSingleton('checkout/session');
10
+ }
11
+
12
+ public function getQuote() {
13
+ return $this->getCheckout()->getQuote();
14
+ }
15
+
16
+ public function getOrder()
17
+ {
18
+ $order_id = $this->getCheckout()->getLastRealOrderId();
19
+ if (!$order_id){
20
+ echo 'Session expired';
21
+ exit;
22
+ } else {
23
+ return Mage::getModel('sales/order')->loadByIncrementId($order_id);
24
+ }
25
+ }
26
+
27
+ public function getOrderPlaceRedirectUrl() {
28
+ return Mage::getUrl('mainpay/mainpaymodel/redirect', array('_secure' => true));
29
+ }
30
+
31
+ public function getMainpayUrl() {
32
+ $url = 'https://partner.mainpay.ru/a1lite/input';
33
+ return $url;
34
+ }
35
+
36
+ public function createFormBlock($name) {
37
+ $block = $this->getLayout()->createBlock('mainpay/mainpaymodel_form', $name)
38
+ ->setMethod('mainpay_mainpaymodel')
39
+ ->setPayment($this->getPayment())
40
+ ->setTemplate('mainpay/mainpaymodel/form.phtml');
41
+
42
+ return $block;
43
+ }
44
+
45
+ public function getMainpayCheckoutFormFields(){
46
+ $order_id = $this->getCheckout()->getLastOrderId();
47
+ /** Amount retriving */
48
+ $currency_code = $this->getOrder()->getOrderCurrencyCode();
49
+ $amount = $this->getOrder()->getGrandTotal();
50
+ if ($currency_code != 'RUB'){
51
+ $amount = Mage::helper('directory')->currencyConvert($amount, $currency_code, 'RUB');
52
+ }
53
+
54
+ $name = Mage::helper('mainpay')->__('Payment for order ').$order_id.Mage::helper('mainpay')->__(' at online store ').$_SERVER["SERVER_NAME"];
55
+
56
+ $fields = array (
57
+ 'key' => Mage::getStoreConfig('payment/mainpay_mainpaymodel/key'),
58
+ 'cost' => sprintf('%.2f', $amount),
59
+ 'name' => $name,
60
+ 'default_email' => '',
61
+ 'order_id' => 0,
62
+ 'comment' => $order_id,
63
+ );
64
+
65
+ return $fields;
66
+ }
67
+
68
+ protected function _getEncriptedConfig($path)
69
+ {
70
+ return Mage::helper('core')->decrypt(Mage::getStoreConfig($path));
71
+ }
72
+
73
+ public function processNotification(){
74
+ $parameters = array (
75
+ $_POST["tid"],
76
+ urldecode($_POST["name"]),
77
+ $_POST["comment"],
78
+ $_POST["partner_id"],
79
+ $_POST["service_id"],
80
+ $_POST["order_id"],
81
+ $_POST["type"],
82
+ $_POST["partner_income"],
83
+ $_POST["system_income"],
84
+ $_POST["test"],
85
+ $this->_getEncriptedConfig('payment/mainpay_mainpaymodel/secret_key'),
86
+ // Mage::getStoreConfig('payment/mainpay_mainpaymodel/secret_key'),
87
+ );
88
+
89
+ $given_check = $_POST["check"];
90
+ $generated_check = md5(join('',$parameters));
91
+ $order_id = $parameters[2];
92
+ $order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
93
+ $amount = sprintf('%.2f', $order->getGrandTotal());
94
+ /** Check verification */
95
+ if ($given_check != $generated_check){
96
+ $order->addStatusToHistory(
97
+ $order->getStatus(),
98
+ Mage::helper('mainpay')->__('Security check failed!')
99
+ )->save();
100
+ /** Verify amount */
101
+ } elseif ($amount != $parameters[8]){
102
+ $order->addStatusToHistory(
103
+ $order->getStatus(),
104
+ Mage::helper('mainpay')->__('Order total amount does not match mainpay gross total amount')
105
+ );
106
+ $order->save();
107
+ /** Payment succesful*/
108
+ } else {
109
+ $newOrderStatus = $this->getConfigData('order_status', $order->getStoreId());
110
+ if (empty($newOrderStatus)) {
111
+ $newOrderStatus = $order->getStatus();
112
+ }
113
+ if (!$order->canInvoice()) {
114
+ $order->addStatusToHistory(
115
+ $order->getStatus(),
116
+ Mage::helper('mainpay')->__('Error in creating an invoice', true),
117
+ $notified = true
118
+ );
119
+ } else {
120
+ $order->getPayment()->setTransactionId($parameters[0]);
121
+ $invoice = $order->prepareInvoice();
122
+ $invoice->register()->pay();
123
+ Mage::getModel('core/resource_transaction')
124
+ ->addObject($invoice)
125
+ ->addObject($invoice->getOrder())
126
+ ->save();
127
+ $order->setState(
128
+ Mage_Sales_Model_Order::STATE_COMPLETE, $newOrderStatus,
129
+ Mage::helper('mainpay')->__('Invoice #%s created', $invoice->getIncrementId()),
130
+ $notified = true
131
+ );
132
+ }
133
+ $order->save();
134
+ $order->sendNewOrderEmail();
135
+ }
136
+ }
137
+
138
+ public function isInitializeNeeded() {
139
+ return true;
140
+ }
141
+
142
+ public function initialize($paymentAction, $stateObject) {
143
+ $state = Mage_Sales_Model_Order::STATE_PENDING_PAYMENT;
144
+ $stateObject->setState($state);
145
+ $stateObject->setStatus(Mage::getSingleton('sales/order_config')->getStateDefaultStatus($state));
146
+ $stateObject->setIsNotified(false);
147
+ return $this;
148
+ }
149
+ }
app/code/local/Mage/Mainpay/controllers/MainpaymodelController.php ADDED
@@ -0,0 +1,82 @@
1
+ <?php
2
+
3
+ class Mage_Mainpay_MainpaymodelController extends Mage_Core_Controller_Front_Action {
4
+
5
+ protected $_order;
6
+
7
+ public function getSession() {
8
+ return Mage::getSingleton('checkout/session');
9
+ }
10
+
11
+ public function getMainpay() {
12
+ return Mage::getSingleton('mainpay/mainpaymodel');
13
+ }
14
+
15
+ protected function _expireAjax()
16
+ {
17
+ if (!Mage::getSingleton('checkout/session')->getQuote()->hasItems()) {
18
+ $this->getResponse()->setHeader('HTTP/1.1','403 Session Expired');
19
+ exit;
20
+ }
21
+ }
22
+
23
+ public function getOrder() {
24
+ if ($this->_order == null) {
25
+ $session = $this->getSession();
26
+ $this->_order = Mage::getModel('sales/order');
27
+ $this->_order->loadByIncrementId($session->getLastRealOrderId());
28
+ }
29
+ return $this->_order;
30
+ }
31
+
32
+ /**
33
+ * Redirect customer to Mainpay payment interface
34
+ */
35
+ public function redirectAction() {
36
+ $this->getResponse()->setHeader('Content-type', 'text/html; charset=utf8')
37
+ ->setBody($this->getLayout()->createBlock('mainpay/mainpaymodel_redirect')->toHtml());
38
+ }
39
+
40
+ /**
41
+ * Validate data from Mainpay server and update the database
42
+ */
43
+ public function notificationAction() {
44
+ if (!$this->getRequest()->isPost()) {
45
+ $this->norouteAction();
46
+ return;
47
+ }
48
+ $this->getMainpay()->processNotification($this->getRequest()->getPost());
49
+ }
50
+
51
+ /**
52
+ * What to do when customer succesfully returns (order is payed)
53
+ */
54
+ /*
55
+ public function successAction()
56
+ {
57
+ $session = $this->getSession();
58
+ $session->setQuoteId($_GET['comment']);
59
+ $session->getQuote()->setIsActive(false)->save();
60
+ $session->setLastRealOrderId($order_id);
61
+
62
+ $this->_redirect('checkout/onepage/success', array('_secure'=>true));
63
+ }
64
+
65
+ public function cancelAction()
66
+ {
67
+ $session = $this->getSession();
68
+ $session->setLastRealOrderId($_GET['comment']);
69
+
70
+ // cancel order
71
+ if ($session->getLastRealOrderId()) {
72
+ $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
73
+ if ($order->getId()) {
74
+ $order->cancel()->save();
75
+ }
76
+ }
77
+
78
+ $this->_redirect('checkout/cart');
79
+ }
80
+ *
81
+ */
82
+ }
app/code/local/Mage/Mainpay/etc/config.xml ADDED
@@ -0,0 +1,101 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Mage_Mainpay>
5
+ <version>0.1.0</version>
6
+ </Mage_Mainpay>
7
+ </modules>
8
+
9
+ <global>
10
+ <models>
11
+ <mainpay>
12
+ <class>Mage_Mainpay_Model</class>
13
+ <resourceModel>mainpay_mysql4</resourceModel>
14
+ </mainpay>
15
+ <mainpay_mysql4>
16
+ <class>Mage_Mainpay_Model_Mysql4</class>
17
+ </mainpay_mysql4>
18
+ </models>
19
+ <helpers>
20
+ <mainpay>
21
+ <class>Mage_Mainpay_Helper</class>
22
+ </mainpay>
23
+ </helpers>
24
+ <resources>
25
+ <mainpay_setup>
26
+ <setup>
27
+ <module>Mage_Mainpay</module>
28
+ </setup>
29
+ <connection>
30
+ <use>core_setup</use>
31
+ </connection>
32
+ </mainpay_setup>
33
+ <mainpay_write>
34
+ <connection>
35
+ <use>core_write</use>
36
+ </connection>
37
+ </mainpay_write>
38
+ <mainpay_read>
39
+ <connection>
40
+ <use>core_read</use>
41
+ </connection>
42
+ </mainpay_read>
43
+ </resources>
44
+ <blocks>
45
+ <mainpay>
46
+ <class>Mage_Mainpay_Block</class>
47
+ </mainpay>
48
+ </blocks>
49
+ </global>
50
+
51
+ <frontend>
52
+ <secure_url>
53
+ <mainpay_mainpaymodel>/mainpay/mainpaymodel</mainpay_mainpaymodel>
54
+ </secure_url>
55
+ <routers>
56
+ <mainpay>
57
+ <use>standard</use>
58
+ <args>
59
+ <module>Mage_Mainpay</module>
60
+ <frontName>mainpay</frontName>
61
+ </args>
62
+ </mainpay>
63
+ </routers>
64
+ <translate>
65
+ <modules>
66
+ <Mage_Mainpay>
67
+ <files>
68
+ <default>Mage_Mainpay.csv</default>
69
+ </files>
70
+ </Mage_Mainpay>
71
+ </modules>
72
+ </translate>
73
+ </frontend>
74
+
75
+ <adminhtml>
76
+ <translate>
77
+ <modules>
78
+ <Mage_Mainpay>
79
+ <files>
80
+ <default>Mage_Mainpay.csv</default>
81
+ </files>
82
+ </Mage_Mainpay>
83
+ </modules>
84
+ </translate>
85
+ </adminhtml>
86
+
87
+ <default>
88
+ <payment>
89
+ <mainpay_mainpaymodel>
90
+ <active>0</active>
91
+ <model>mainpay/mainpaymodel</model>
92
+ <order_status>processing</order_status>
93
+ <title>MainPay</title>
94
+
95
+ <key />
96
+ <secret_key backend_model="adminhtml/system_config_backend_encrypted" />
97
+ <sort_order />
98
+ </mainpay_mainpaymodel>
99
+ </payment>
100
+ </default>
101
+ </config>
app/code/local/Mage/Mainpay/etc/system.xml ADDED
@@ -0,0 +1,70 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <payment>
5
+ <groups>
6
+ <mainpay_mainpaymodel translate="label" module="mainpay">
7
+ <label>Mainpay</label>
8
+ <frontend_type>text</frontend_type>
9
+ <sort_order>231</sort_order>
10
+ <show_in_default>1</show_in_default>
11
+ <show_in_website>1</show_in_website>
12
+ <show_in_store>0</show_in_store>
13
+ <fields>
14
+ <active translate="label">
15
+ <label>Enabled</label>
16
+ <frontend_type>select</frontend_type>
17
+ <source_model>adminhtml/system_config_source_yesno</source_model>
18
+ <sort_order>10</sort_order>
19
+ <show_in_default>1</show_in_default>
20
+ <show_in_website>1</show_in_website>
21
+ <show_in_store>0</show_in_store>
22
+ </active>
23
+ <order_status translate="label">
24
+ <label>New order status</label>
25
+ <frontend_type>select</frontend_type>
26
+ <source_model>adminhtml/system_config_source_order_status</source_model>
27
+ <sort_order>30</sort_order>
28
+ <show_in_default>1</show_in_default>
29
+ <show_in_website>1</show_in_website>
30
+ <show_in_store>0</show_in_store>
31
+ </order_status>
32
+ <title translate="label">
33
+ <label>Title</label>
34
+ <frontend_type>text</frontend_type>
35
+ <sort_order>20</sort_order>
36
+ <show_in_default>1</show_in_default>
37
+ <show_in_website>1</show_in_website>
38
+ <show_in_store>0</show_in_store>
39
+ </title>
40
+ <key translate="label">
41
+ <label>Key</label>
42
+ <frontend_type>text</frontend_type>
43
+ <sort_order>40</sort_order>
44
+ <show_in_default>1</show_in_default>
45
+ <show_in_website>1</show_in_website>
46
+ <show_in_store>0</show_in_store>
47
+ </key>
48
+ <secret_key translate="label">
49
+ <label>Secret key</label>
50
+ <frontend_type>text</frontend_type>
51
+ <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
52
+ <sort_order>50</sort_order>
53
+ <show_in_default>1</show_in_default>
54
+ <show_in_website>1</show_in_website>
55
+ <show_in_store>0</show_in_store>
56
+ </secret_key>
57
+ <sort_order translate="label">
58
+ <label>Sort order</label>
59
+ <frontend_type>text</frontend_type>
60
+ <sort_order>60</sort_order>
61
+ <show_in_default>1</show_in_default>
62
+ <show_in_website>1</show_in_website>
63
+ <show_in_store>1</show_in_store>
64
+ </sort_order>
65
+ </fields>
66
+ </mainpay_mainpaymodel>
67
+ </groups>
68
+ </payment>
69
+ </sections>
70
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Mage_Mainpay</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Payment module for Mainpay</summary>
10
+ <description>Payment module for Mainpay</description>
11
+ <notes>Stable version.</notes>
12
+ <authors><author><name>Stanislav</name><user>auto-converted</user><email>s.basenko@mainpay.ru</email></author></authors>
13
+ <date>2011-11-22</date>
14
+ <time>11:47:18</time>
15
+ <contents><target name="magelocal"><dir name="Mage"><dir name="Mainpay"><dir name="Block"><dir name="Mainpaymodel"><file name="Form.php" hash="78a5865d5e69b54db8582525b429c3cd"/><file name="Redirect.php" hash="231e6c491c913895ca32229fac33a16e"/></dir></dir><dir name="Helper"><file name="Data.php" hash="8be5a2337ea6a7a32b37ea56c9425496"/></dir><dir name="Model"><file name="Mainpaymodel.php" hash="7b7341f3f5813b1ddbf5597ae74e8d88"/></dir><dir name="controllers"><file name="MainpaymodelController.php" hash="93f5ff71b2565962b4187791973b8a40"/></dir><dir name="etc"><file name="config.xml" hash="acc149051d1305cf0c8beb6d6c5e8309"/><file name="system.xml" hash="7c69cc11c09d5ed1b259850bc8d99c38"/></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies/>
18
+ </package>