Osgrow_Copayco - Version 1.0.0

Version Notes

First Stable Release

Download this release

Release Info

Developer Osgrow
Extension Osgrow_Copayco
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/Osgrow/Copayco/Block/Redirect.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copayco Payment extension for Magento by Osgrow
4
+ *
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ * DISCLAIMER
14
+ *
15
+ * Do not edit or add to this file if you wish to upgrade
16
+ * the Osgrow Copayco module to newer versions in the future.
17
+ * If you wish to customize the Osgrow Copayco module for your needs
18
+ * please refer to http://www.magentocommerce.com for more information.
19
+ *
20
+ * @category Osgrow
21
+ * @package Osgrow_Copayco
22
+ * @copyright Copyright (C) 2015 Osgrow (http://osgrow.net/)
23
+ * @author Dmytro Borodulin
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+ class Osgrow_Copayco_Block_Redirect extends Mage_Core_Block_Abstract{
27
+
28
+ /*
29
+ * _toHtml
30
+ * @return string
31
+ */
32
+ protected function _toHtml()
33
+ {
34
+ $oApi = Mage::helper('copayco/api');
35
+ $sFields = str_replace("text", "hidden", $oApi->getFields());
36
+ $sSubmitUrl = $oApi->getSubmitUrl();
37
+
38
+
39
+ ## Html string
40
+ $html = '<html><body>';
41
+ $html.= $this->__('Copayco Payment.');
42
+ $html.="<form action=$sSubmitUrl id='copayco_pay_form' method='POST'>";
43
+ $html.= $sFields;
44
+ $html.= '<input type="submit" value="' . $this->__("If not redirected, click here.") . '">';
45
+ $html.= '</form>';
46
+ $html.= '<script type="text/javascript">document.getElementById("copayco_pay_form").submit();</script>';
47
+ $html.='</body></html>';
48
+ return $html;
49
+ } //function _toHtml
50
+
51
+ }
app/code/community/Osgrow/Copayco/Helper/Api.php ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copayco Payment extension for Magento by Osgrow
4
+ *
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ * DISCLAIMER
14
+ *
15
+ * Do not edit or add to this file if you wish to upgrade
16
+ * the Osgrow Copayco module to newer versions in the future.
17
+ * If you wish to customize the Osgrow Copayco module for your needs
18
+ * please refer to http://www.magentocommerce.com for more information.
19
+ *
20
+ * @category Osgrow
21
+ * @package Osgrow_Copayco
22
+ * @copyright Copyright (C) 2015 Osgrow (http://osgrow.net/)
23
+ * @author Dmytro Borodulin
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ $sApiUrl = Mage::getModuleDir('Helper', 'Osgrow_Copayco') . DS . 'Helper' .DS. 'copayco.php';
28
+ require_once $sApiUrl;
29
+
30
+ class Osgrow_Copayco_Helper_Api extends Mage_Core_Helper_Abstract{
31
+
32
+ /*
33
+ * Construct
34
+ */
35
+ public function __construct()
36
+ {
37
+ $this->oCop = copayco_api::instance();
38
+ }// function __construct
39
+
40
+ /*
41
+ * prepare
42
+ * setting main pay date
43
+ * for transfering to copayco
44
+ */
45
+ public function prepare($aPayData)
46
+ {
47
+ $this->oCop->set_main_data($aPayData['ta_id'], $aPayData['amount'], $aPayData['currency'], $aPayData['shop_id']);
48
+ $this->oCop->set_description($aPayData['description']);
49
+ $this->oCop->set_purpose($aPayData['purpose']);
50
+ }//function prepare
51
+
52
+ /*
53
+ * connfirm
54
+ * curl connection to copayco server
55
+ * 2 steps: check and getting real result
56
+ * @return bool
57
+ */
58
+ public function confirm()
59
+ {
60
+ $oSes = Mage::getSingleton('core/session');
61
+
62
+ try{
63
+ $sTaId = $this->oCop->get_ta_id();
64
+ $oOrder = Mage::getModel('sales/order')->loadByIncrementId($sTaId);
65
+ $nAmount = number_format($oOrder->getGrandTotal(), 2, '.', '');
66
+ $nCurrency = $oOrder->getOrderCurrencyCode();
67
+ //$nCurrency = 'UAH';
68
+
69
+
70
+ ## Setting main pay data ##
71
+ $this->oCop->set_main_data($sTaId, $nAmount, $nCurrency);
72
+ $aReq = $this->oCop->get_request_data();
73
+ $sRequestType = $this->oCop->get_request_type();
74
+
75
+ ## Getting result
76
+ if ($sRequestType == 'check') {
77
+ if ($this->oCop->check_data()) {
78
+ //in case of need may log result: check completed
79
+ $oOrder->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT);
80
+ $oOrder->setStatus('pending_payment');
81
+ $oOrder->addStatusHistoryComment('Customer Started Payment', false);
82
+ $oOrder->save();
83
+ } else {
84
+ //in case of need we log result: check failed
85
+ }
86
+ } else {
87
+ ## payment is in RESERVED status ##
88
+ if ($this->oCop->is_reserved()) {
89
+ //In next version will be the ability of reservation
90
+ $oOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED);
91
+ $oOrder->setStatus('canceled');
92
+ $oOrder->addStatusHistoryComment("Payment is Reserved!!! Ask Copayco team for furher actions", false);
93
+ $oOrder->save();
94
+
95
+ ## payment is in FINISHED status ##
96
+ } elseif ($this->oCop->is_finished()) {
97
+ $oOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING);
98
+ $oOrder->setStatus('processing');
99
+ $oOrder->addStatusHistoryComment('Processing Payment', false);
100
+ $oOrder->save();
101
+ ## payment is in CANCELED status ##
102
+ } else {
103
+ $oOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED);
104
+ $oOrder->setStatus('canceled');
105
+ $oOrder->addStatusHistoryComment('Payment Canceled', false);
106
+ $oOrder->save();
107
+
108
+ }
109
+ }
110
+ } catch (copayco_exception $e) {
111
+ ## error handling
112
+ $nErrType = $e->get_error_type_code();
113
+ $nCode = $e->getCode();
114
+ $this->oCop->set_error_message($e->getMessage() . ' ' . $nCode);
115
+ }
116
+
117
+ ## Showing result to copayco server
118
+ if ($sRequestType == 'perform') {
119
+ $this->oCop->output_perform_answer();
120
+ } else {
121
+ $this->oCop->output_check_answer();
122
+ }
123
+ }// function connfirm
124
+
125
+ /*
126
+ * getFields
127
+ * Getting fields for post form
128
+ * to preceed the payment
129
+ * @return string
130
+ */
131
+ public function getFields()
132
+ {
133
+ $sFormFields = $this->oCop->get_form_fields(
134
+ array(
135
+ 'ta_id' => array(
136
+ 'type' => 'text',
137
+ 'readonly' => 'readonly',
138
+ ),
139
+ 'currency' => array(
140
+ 'id' => 'currency',
141
+ ),
142
+ ),
143
+ "\n"
144
+ );
145
+ return $sFormFields;
146
+ }// function getFields
147
+
148
+
149
+ /*
150
+ * getSubmitUrl
151
+ * getting url for payment form
152
+ */
153
+ public function getSubmitUrl()
154
+ {
155
+ return $this->oCop->get_submit_url();
156
+ }//function getSubmitUrl
157
+
158
+ }
app/code/community/Osgrow/Copayco/Helper/Data.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copayco Payment extension for Magento by Osgrow
4
+ *
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ * DISCLAIMER
14
+ *
15
+ * Do not edit or add to this file if you wish to upgrade
16
+ * the Osgrow Copayco module to newer versions in the future.
17
+ * If you wish to customize the Osgrow Copayco module for your needs
18
+ * please refer to http://www.magentocommerce.com for more information.
19
+ *
20
+ * @category Osgrow
21
+ * @package Osgrow_Copayco
22
+ * @copyright Copyright (C) 2015 Osgrow (http://osgrow.net/)
23
+ * @author Dmytro Borodulin
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Osgrow_Copayco_Helper_Data extends Mage_Core_Helper_Abstract
28
+ {
29
+
30
+ }
app/code/community/Osgrow/Copayco/Helper/config.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * copayco config data
4
+ * the same as in provided API
5
+ */
6
+ $aPayData = Mage::getModel('copayco/standard')->getPayData();
7
+
8
+
9
+ return array (
10
+ 'test_mode' => $aPayData['test_mode'], //At the end of the test change the value to false
11
+ 'shop_id' => $aPayData['shop_id'], // getting from copayco account
12
+ 'sign_key' => $aPayData['sign_key'], // getting from copayco account
13
+ 'notify_mode' => 'curl', // curl only
14
+ //'use_rand' => 2,
15
+ //'charset' => 'windows-1251', // 'windows-1251', 'koi8-r', 'koi8-u'
16
+
17
+ );
18
+ ?>
app/code/community/Osgrow/Copayco/Helper/copayco.php ADDED
@@ -0,0 +1,1087 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * CoPayCo API (CoPayCo Application Programming Interface)
4
+ *
5
+ * This file is part of CoPayCo system
6
+ * Copyright (C) 2010 CoPayCo, http://www.copayco.com/
7
+ *
8
+ * @author: Alexandr Nosov (alex@copayco.com)
9
+ * @version: 2.1.5
10
+ */
11
+ class copayco_api
12
+ {
13
+ /**
14
+ * CoPayCo submit URL
15
+ */
16
+ const SUBMIT_URL = 'https://www.copayco.com/pay.php';
17
+ /**
18
+ * CoPayCo submit test URL
19
+ */
20
+ const SUBMIT_URL_TEST = 'https://www.test.copayco.com/pay.php';
21
+
22
+ /**
23
+ * CoPayCo notification URL
24
+ */
25
+ const NOTIFICATION_URL = 'https://www.copayco.com/notify_delivering.php';
26
+
27
+ /**
28
+ * CoPayCo notification test URL
29
+ */
30
+ const NOTIFICATION_URL_TEST = 'https://www.test.copayco.com/notify_delivering.php';
31
+
32
+ /**
33
+ * Key of transaction id
34
+ */
35
+ const TA_ID_KEY = 'ta_id';
36
+
37
+ /**
38
+ * Key of request type
39
+ */
40
+ const REQUEST_TYPE_KEY = 'request_type';
41
+
42
+ /**
43
+ * Key of signature
44
+ */
45
+ const SIGNATURE_KEY = 'signature';
46
+
47
+ /**
48
+ * Key of signature
49
+ */
50
+ const PAYMENT_MODE_KEY = 'payment_mode';
51
+
52
+ /**
53
+ * Key of status
54
+ */
55
+ const STATUS_KEY = 'status';
56
+
57
+ /**
58
+ * @var array Allowed request fields (From Merchant to CoPAYCo)
59
+ */
60
+ private static $aReqFields = array(
61
+ 'shop_id' => TRUE,
62
+ 'ta_id' => TRUE,
63
+ 'amount' => TRUE,
64
+ 'currency' => TRUE,
65
+ 'description' => FALSE,
66
+ 'custom' => FALSE,
67
+ 'payment_mode' => FALSE,
68
+ 'charset' => FALSE,
69
+ 'lang' => FALSE,
70
+ 'order_no' => FALSE,
71
+ 'purpose' => FALSE,
72
+ 'date_time' => FALSE,
73
+ 'random' => FALSE,
74
+ 'signature' => FALSE,
75
+ );
76
+
77
+ /**
78
+ * @var array Allowed check fields (From CoPAYCo to Merchant)
79
+ */
80
+ private static $aCheckFields = array(
81
+ 'request_type' => TRUE,
82
+ 'ta_id' => TRUE,
83
+ 'amount' => TRUE,
84
+ 'currency' => TRUE,
85
+ 'custom' => FALSE,
86
+ 'payment_mode' => FALSE,
87
+ 'client_ip' => TRUE,
88
+ 'date_time' => FALSE,
89
+ 'random' => FALSE,
90
+ 'signature' => TRUE,
91
+ );
92
+
93
+ /**
94
+ * @var array Allowed perform fields (From CoPAYCo to Merchant)
95
+ */
96
+ private static $aPerformFields = array(
97
+ 'request_type' => TRUE,
98
+ 'status' => TRUE,
99
+ 'ta_id' => TRUE,
100
+ 'cpc_ta_id' => TRUE,
101
+ 'amount' => TRUE,
102
+ 'currency' => TRUE,
103
+ 'custom' => FALSE,
104
+ 'payment_mode' => FALSE,
105
+ 'date_time' => FALSE,
106
+ 'random' => FALSE,
107
+ 'signature' => TRUE,
108
+ );
109
+
110
+ /**
111
+ * @var array Allowed notification fields (From Merchant to CoPAYCo)
112
+ */
113
+ private static $aNotificationFields = array(
114
+ 'shop_id' => TRUE,
115
+ 'ta_id' => TRUE,
116
+ 'amount' => TRUE,
117
+ 'currency' => FALSE,
118
+ 'state' => TRUE,
119
+ 'date_time' => TRUE,
120
+ 'random' => FALSE,
121
+ 'signature' => TRUE,
122
+ 'return_url' => FALSE,
123
+ );
124
+
125
+ /**
126
+ * @var array Correspondence field-data to current property
127
+ */
128
+ private static $aCorrespondence = array(
129
+ 'shop_id' => 'iShopId',
130
+ 'ta_id' => 'sTaId',
131
+ 'amount' => 'nAmount',
132
+ 'currency' => 'sCurrency',
133
+ // 'status' => '',
134
+ 'description' => 'sDescription',
135
+ 'custom' => 'mCustom',
136
+ 'payment_mode' => 'aPaymentMode',
137
+ 'cpc_ta_id' => 'iCPCTaId',
138
+ 'charset' => 'sCharset',
139
+ 'lang' => 'sLanguage',
140
+ 'order_no' => 'sOrderNo',
141
+ 'purpose' => 'sPurpose',
142
+ 'date_time' => 'sDateTime',
143
+ 'random' => 'iRand',
144
+ 'state' => 'sState',
145
+ 'return_url' => 'sReturnUrl',
146
+ );
147
+
148
+ /**
149
+ * @var array Fill property automatically by HTTP-vars
150
+ */
151
+ private static $aAutoFill = array(
152
+ //'aPaymentMode',
153
+ 'iCPCTaId',
154
+ 'sDateTime',
155
+ 'iRand',
156
+ );
157
+
158
+ /**
159
+ * @var array Field number (for error code)
160
+ */
161
+ private static $aFieldNumber = array(
162
+ 'shop_id' => 1,
163
+ 'ta_id' => 2,
164
+ 'amount' => 3,
165
+ 'currency' => 4,
166
+ 'description' => 5,
167
+ 'custom' => 6,
168
+ 'payment_mode' => 7,
169
+ 'request_type' => 9,
170
+ 'status' => 10,
171
+ 'state' => 11,
172
+ 'return_url' => 12,
173
+ 'client_ip' => 20,
174
+ 'cpc_ta_id' => 30,
175
+ 'order_no' => 60,
176
+ 'purpose' => 61,
177
+ 'charset' => 70,
178
+ 'lang' => 71,
179
+ 'date_time' => 80,
180
+ 'random' => 81,
181
+ 'signature' => 99,
182
+ );
183
+
184
+ /**
185
+ * @var array Allowed currencies
186
+ */
187
+ private static $aLegalCurrencies = array(
188
+ 'UAH',
189
+ 'RUB',
190
+ 'USD',
191
+ 'EUR',
192
+ );
193
+
194
+ /**
195
+ * @var array Allowed payment modes
196
+ */
197
+ private static $aLegalPaymentMode = array(
198
+ 'paycard',
199
+ 'account',
200
+ 'ecurrency',
201
+ 'copayco',
202
+ 'terminal',
203
+ 'sms',
204
+ );
205
+
206
+ /**
207
+ * @var array Allowed languages
208
+ */
209
+ private static $aLegalLanguages = array(
210
+ 'ru',
211
+ 'en',
212
+ 'ua',
213
+ );
214
+
215
+ /**
216
+ * @var array Allowed Charsets
217
+ */
218
+ private static $aLegalCharsets = array(
219
+ 'utf-8',
220
+ 'windows-1251',
221
+ 'koi8-r',
222
+ 'koi8-u',
223
+ );
224
+
225
+ /**
226
+ * @var boolean Test mode
227
+ */
228
+ private $bTestMode = FALSE;
229
+
230
+ /**
231
+ * @var boolean Test mode
232
+ */
233
+ private $sNotifyMode = 'curl';
234
+
235
+ /**
236
+ * @var string Error Message
237
+ */
238
+ private $sErrMsg = NULL;
239
+
240
+ /**
241
+ * @var string Signature Key
242
+ */
243
+ private $sSignKey = NULL;
244
+
245
+ /**
246
+ * @var integer Use Random number in The request
247
+ */
248
+ private $iUseRand = 2;
249
+
250
+ /**
251
+ * @var integer Shop ID
252
+ */
253
+ protected $iShopId = NULL;
254
+
255
+ /**
256
+ * @var string Merchant's Transaction ID
257
+ */
258
+ protected $sTaId = NULL;
259
+
260
+ /**
261
+ * @var string CoPAYCo's Transaction ID
262
+ */
263
+ protected $iCPCTaId = NULL;
264
+
265
+ /**
266
+ * @var numeric Amount of payment
267
+ */
268
+ protected $nAmount = NULL;
269
+
270
+ /**
271
+ * @var string Currency of payment
272
+ */
273
+ protected $sCurrency = NULL;
274
+
275
+ /**
276
+ * @var string Description of payment
277
+ */
278
+ protected $sDescription = NULL;
279
+
280
+ /**
281
+ * @var mixed Custom field
282
+ */
283
+ protected $mCustom = NULL;
284
+
285
+ /**
286
+ * @var array Payment Mode (paycard, account, copayco)
287
+ */
288
+ protected $aPaymentMode = array();
289
+
290
+ /**
291
+ * @var string
292
+ */
293
+ protected $sCharset = NULL;
294
+
295
+ /**
296
+ * @var string
297
+ */
298
+ protected $sLanguage = NULL;
299
+
300
+ /**
301
+ * @var string
302
+ */
303
+ protected $sOrderNo = NULL;
304
+
305
+ /**
306
+ * @var string
307
+ */
308
+ protected $sPurpose = NULL;
309
+
310
+ /**
311
+ * @var string Date/time from server which makes request
312
+ */
313
+ protected $sDateTime = NULL;
314
+
315
+ /**
316
+ * @var string Rantom number (0-1024)
317
+ */
318
+ protected $iRand = NULL;
319
+
320
+ /**
321
+ * @var string Transaction State
322
+ */
323
+ protected $sState = NULL;
324
+
325
+ /**
326
+ * @var string Return Url (for HTML-form)
327
+ */
328
+ protected $sReturnUrl = NULL;
329
+
330
+ /**
331
+ * Constructor of copayco
332
+ * @param string $sSignKey
333
+ */
334
+ protected function __construct($sSignKey)
335
+ {
336
+ $aConf = $this->get_config($sSignKey);
337
+
338
+ // Set Signature Key
339
+ if (!empty($aConf['sign_key'])) {
340
+ $this->sSignKey = $aConf['sign_key'];
341
+ }
342
+
343
+ // Set Shop ID
344
+ if (!empty($aConf['shop_id'])) {
345
+ $this->iShopId = $aConf['shop_id'];
346
+ }
347
+
348
+ // Set Notify mode
349
+ if (!empty($aConf['notify_mode'])) {
350
+ if (!in_array($aConf['notify_mode'], array('curl'))) {
351
+ throw new copayco_exception('Incorrect notify mode: "' . $aConf['notify_mode'] . '".', 10001);
352
+ }
353
+ $this->sNotifyMode = $aConf['notify_mode'];
354
+ }
355
+
356
+ // Set Charset
357
+ if (isset($aConf['charset'])) {
358
+ $this->set_charset($aConf['charset']);
359
+ }
360
+
361
+ // Set Random value
362
+ if (isset($aConf['use_rand'])) {
363
+ $this->iUseRand = $aConf['use_rand'];
364
+ }
365
+
366
+ // Set Test mode
367
+ if (isset($aConf['test_mode'])) {
368
+ $this->bTestMode = !empty($aConf['test_mode']);
369
+ }
370
+ } // function __construct
371
+
372
+ /**
373
+ * Get instance of copayco API
374
+ * @param string $sSignKey
375
+ * @return copayco
376
+ */
377
+ public static function instance($sSignKey = NULL)
378
+ {
379
+ return new copayco_api($sSignKey);
380
+ } // function instance
381
+
382
+
383
+ // -------------- Preparing data -------------- \\
384
+
385
+ /**
386
+ * Set main data of copayco payment
387
+ * @param string $sTaId
388
+ * @param numeric $nAmount
389
+ * @param integer $iShopId
390
+ * @param string $sCurrency
391
+ */
392
+ public function set_main_data($sTaId, $nAmount, $sCurrency = 'UAH', $iShopId = NULL)
393
+ {
394
+ // Check ID of TA
395
+ if (empty($sTaId)) {
396
+ throw new copayco_exception('Transaction ID can\'t be empty.', 10020);
397
+ }
398
+
399
+ // Check Amount
400
+ if (empty($nAmount)) {
401
+ throw new copayco_exception('Amount of payment can\'t be equal to 0.', 10030);
402
+ }
403
+ if (!is_numeric($nAmount)) {
404
+ throw new copayco_exception('Amount must be numeric value.', 10031);
405
+ }
406
+ if ($nAmount < 0) {
407
+ throw new copayco_exception('Amount must be greater than 0.', 10032);
408
+ }
409
+
410
+ // Check shop ID
411
+ if (!empty($iShopId)) {
412
+ $this->iShopId = $iShopId;
413
+ } elseif (empty($this->iShopId)) {
414
+ throw new copayco_exception('Shop ID isn\'t set.', 10010);
415
+ }
416
+
417
+ // Check Currency
418
+ if (empty($sCurrency) || !in_array($sCurrency, self::$aLegalCurrencies)) {
419
+ throw new copayco_exception('Currency must be equal to "' . implode('", "', self::$aLegalCurrencies) . '".', 10040);
420
+ }
421
+
422
+ $this->sTaId = $sTaId;
423
+ $this->nAmount = round($nAmount * 100);
424
+ $this->sCurrency = $sCurrency;
425
+ } // function set_main_data
426
+
427
+ /**
428
+ * Set description of copayco payment
429
+ * @param string $sDescription
430
+ */
431
+ public function set_description($sDescription)
432
+ {
433
+ if (!is_scalar($sDescription)) {
434
+ throw new copayco_exception('Description must have a scalar value.', 10050);
435
+ }
436
+ $sDescription = (string)$sDescription;
437
+ if (strlen($sDescription) > 255) {
438
+ throw new copayco_exception('Description can\'t be more than 255 symbols.', 10051);
439
+ }
440
+
441
+ $this->sDescription = $sDescription;
442
+ } // function set_description
443
+
444
+ /**
445
+ * Set custom field of copayco payment
446
+ * @param mixed $mCustom
447
+ */
448
+ public function set_custom_field($mCustom)
449
+ {
450
+ if (!is_scalar($mCustom)) {
451
+ throw new copayco_exception('Custom field must have a scalar value.', 10060);
452
+ }
453
+ $mCustom = (string)$mCustom;
454
+ if (strlen($mCustom) > 255) {
455
+ throw new copayco_exception('Custom field can\'t be more than 255 symbols.', 10061);
456
+ }
457
+
458
+ $this->mCustom = $mCustom;
459
+ } // function set_custom_field
460
+
461
+ /**
462
+ * Set payment mode of copayco payment
463
+ * @param mixed $mPaymentMode
464
+ */
465
+ public function set_payment_mode($mPaymentMode)
466
+ {
467
+ if (empty($mPaymentMode)) {
468
+ throw new copayco_exception('Payment Mode can\'t be empty.', 20070);
469
+ }
470
+
471
+ if (is_array($mPaymentMode)) {
472
+ foreach ($mPaymentMode as $v) {
473
+ $this->set_payment_mode($v);
474
+ }
475
+ } else {
476
+ if (!is_scalar($mPaymentMode) || !in_array($mPaymentMode, self::$aLegalPaymentMode)) {
477
+ throw new copayco_exception('Payment Mode must be equal to "' . implode('", "', self::$aLegalPaymentMode) . '".', 20071);
478
+ }
479
+
480
+ if (!in_array($mPaymentMode, $this->aPaymentMode)) {
481
+ $this->aPaymentMode[] = $mPaymentMode;
482
+ }
483
+ }
484
+ } // function set_payment_mode
485
+
486
+ /**
487
+ * Clear payment mode of copayco payment
488
+ */
489
+ public function clear_payment_mode()
490
+ {
491
+ $this->aPaymentMode = array();
492
+ } // function clear_payment_mode
493
+
494
+ /**
495
+ * Set charset
496
+ * @param string $sCharset
497
+ */
498
+ public function set_charset($sCharset)
499
+ {
500
+ $sCharset = strtolower($sCharset);
501
+ if (!in_array($sCharset, self::$aLegalCharsets)) {
502
+ throw new copayco_exception('Incorrect Charset: "' . $sCharset . '".', 20700);
503
+ }
504
+ $this->sCharset = $sCharset;
505
+ } // function set_charset
506
+
507
+ /**
508
+ * Set language
509
+ * @param string $sLanguage
510
+ */
511
+ public function set_language($sLanguage)
512
+ {
513
+ $sLanguage = strtolower($sLanguage);
514
+ if (!in_array($sLanguage, self::$aLegalLanguages)) {
515
+ throw new copayco_exception('Incorrect Language: "' . $sLanguage . '".', 20710);
516
+ }
517
+ $this->sLanguage = $sLanguage;
518
+ } // function set_language
519
+
520
+ /**
521
+ * Set order_no
522
+ * @param string $sOrderNo
523
+ */
524
+ public function set_order_no($sOrderNo)
525
+ {
526
+ $this->sOrderNo = $sOrderNo;
527
+ } // function set_order_no
528
+
529
+ /**
530
+ * Set purpose
531
+ * @param string $sPurpose
532
+ */
533
+ public function set_purpose($sPurpose)
534
+ {
535
+ $this->sPurpose = $sPurpose;
536
+ } // function set_purpose
537
+
538
+ /**
539
+ * Get form fields
540
+ * @param array $aAttr 2-x array (first key - field name; second key - attribute name; value - attribute value)
541
+ * @param string $sSeparator field separator (for example: "\t", "\n", etc). If $sSeparator is null, method return array of tags, but isn't string
542
+ * @return mixed string OR array
543
+ */
544
+ public function get_form_fields($aAttr = array(), $sSeparator = '')
545
+ {
546
+ $mRet = is_null($sSeparator) ? array() : '';
547
+ $this->set_date_and_rand();
548
+ foreach ($this->prepare_send_data() as $k => $v) {
549
+ $sTag = '<input type="' . (isset($aAttr[$k]['type']) ? $aAttr[$k]['type'] : 'hidden') . '" name="' . $k . '" value="' . htmlspecialchars($v) . '"';
550
+ $sTag .= (isset($aAttr[$k]) ? $this->get_additional_attr($aAttr[$k]) : '') . ' />';
551
+ if (is_null($sSeparator)) {
552
+ $mRet[] = $sTag;
553
+ } else {
554
+ $mRet .= $sTag . $sSeparator;
555
+ }
556
+ }
557
+ return $mRet;
558
+ } // function get_form_fields
559
+
560
+ /**
561
+ * Get request URI
562
+ * @param string $sSeparator -- GET-separator ('&' OR '&amp;')
563
+ * @return string
564
+ */
565
+ public function get_request_uri($sSeparator = '&amp;')
566
+ {
567
+ $sRet = '';
568
+ $this->set_date_and_rand();
569
+ foreach ($this->prepare_send_data() as $k => $v) {
570
+ $sRet .= ($sRet ? $sSeparator : '') . $k . '=' . urlencode($v);
571
+ }
572
+ return $this->get_submit_url() . '?' . $sRet;
573
+ } // function get_request_uri
574
+
575
+ /**
576
+ * Get submit url, taking into account the test mode
577
+ * @return string
578
+ */
579
+ public function get_submit_url()
580
+ {
581
+ return $this->bTestMode ? self::SUBMIT_URL_TEST : self::SUBMIT_URL;
582
+ } // function get_submit_url
583
+
584
+ /**
585
+ * Get notification url, taking into account the test mode
586
+ * @return string
587
+ */
588
+ public function get_notification_url()
589
+ {
590
+ return $this->bTestMode ? self::NOTIFICATION_URL_TEST : self::NOTIFICATION_URL;
591
+ } // function get_submit_url
592
+
593
+ // -------------- Check data (1-st answer) -------------- \\
594
+
595
+ /**
596
+ * Check full data of copayco payment
597
+ * @return boolean
598
+ */
599
+ public function check_data()
600
+ {
601
+ if ($this->get_request_type() != 'check') {
602
+ throw new copayco_exception('It isn\'t check request.', 30091);
603
+ }
604
+ $this->define_http_val();
605
+ return $this->check_http_fields(self::$aCheckFields, TRUE);
606
+ } // function check_data
607
+
608
+ /**
609
+ * Set error message
610
+ * @param string $sMsg
611
+ */
612
+ public function set_error_message($sErrMsg)
613
+ {
614
+ if ($sErrMsg) {
615
+ $this->sErrMsg = $sErrMsg;
616
+ }
617
+ } // function set_error_message
618
+
619
+ /**
620
+ * Output first answer for copayco server
621
+ * @param string $sCharset
622
+ */
623
+ public function output_check_answer($sCharset = NULL)
624
+ {
625
+ $sMsg = $this->get_request_type() == 'check' ? ($this->sErrMsg ? $this->sErrMsg : 'ok') : 'It isn\'t check request.';
626
+ $this->output_header($sMsg, $sCharset);
627
+ echo $sMsg;
628
+ } // function output_first_answer
629
+
630
+ // -------------- Perform data (2-nd answer) -------------- \\
631
+
632
+ /**
633
+ * Get transaction status
634
+ * @return string
635
+ */
636
+ public function get_perform_status()
637
+ {
638
+ if ($this->get_request_type() != 'perform') {
639
+ throw new copayco_exception('It isn\'t perform request.', 30092);
640
+ }
641
+ $this->define_http_val();
642
+ if ($this->check_http_fields(self::$aPerformFields)) {
643
+ return $this->get_http_val(self::STATUS_KEY);
644
+ }
645
+ throw new copayco_exception('Perform status isn\'t set.', 30093);
646
+ } // function get_perform_status
647
+
648
+ /**
649
+ * Get CoPAYCo's transaction id
650
+ * @return string
651
+ */
652
+ public function get_copayco_ta_id()
653
+ {
654
+ return $this->iCPCTaId;
655
+ } // function get_copayco_ta_id
656
+
657
+ /**
658
+ * Check is transaction status reserved?
659
+ * @return boolean
660
+ */
661
+ public function is_reserved()
662
+ {
663
+ return $this->get_perform_status() == 'reserved';
664
+ } // function is_reserved
665
+
666
+ /**
667
+ * Check is transaction status finished?
668
+ * @return boolean
669
+ */
670
+ public function is_finished()
671
+ {
672
+ return $this->get_perform_status() == 'finished';
673
+ } // function is_finished
674
+
675
+ /**
676
+ * Check is transaction status canceled?
677
+ * @return boolean
678
+ */
679
+ public function is_canceled()
680
+ {
681
+ return $this->get_perform_status() == 'canceled';
682
+ } // function is_canceled
683
+
684
+ /**
685
+ * Output first answer for copayco server
686
+ * @param string $sCharset
687
+ */
688
+ public function output_perform_answer($sCharset = NULL)
689
+ {
690
+ $sMsg = $this->get_request_type() == 'perform' ? ($this->sErrMsg ? $this->sErrMsg : 'ok') : 'It isn\'t perform request.';
691
+ $this->output_header($sMsg, $sCharset);
692
+ echo $sMsg;
693
+ } // function output_perform_answer
694
+
695
+ // -------------- Send delivering state: delivered OR canceled -------------- \\
696
+ /**
697
+ * Send "delivered" state about delivering of goods/service
698
+ */
699
+ public function send_delivered_state()
700
+ {
701
+ $this->send_notification('delivered');
702
+
703
+ } // function send_delivered_state
704
+
705
+ /**
706
+ * Send "canceled" state about delivering of goods/service
707
+ */
708
+ public function send_canceled_state()
709
+ {
710
+ $this->send_notification('canceled');
711
+ } // function send_canceled_state
712
+
713
+ // -------------- Auxiliary methods -------------- \\
714
+
715
+ /**
716
+ * Get request type
717
+ * @return string
718
+ */
719
+ final public function get_request_type()
720
+ {
721
+ $sKey = self::REQUEST_TYPE_KEY;
722
+ $sType = $this->get_http_val($sKey);
723
+ if (!in_array($sType, array('check', 'perform'))) {
724
+ throw new copayco_exception('Field "' . $sKey . '" contain incorrect data.', 30002 + self::$aFieldNumber[$sKey] * 10);
725
+ }
726
+ return $sType;
727
+ } // function get_request_type
728
+
729
+ /**
730
+ * Get transaction ID from HTTP-request (for check/perform)
731
+ * @return mixed
732
+ */
733
+ public function get_ta_id()
734
+ {
735
+ return $this->get_http_val(self::TA_ID_KEY);
736
+ } // function get_ta_id
737
+
738
+ /**
739
+ * Get full request data for "check" and "perform" operation
740
+ * @return mixed
741
+ */
742
+ public function get_request_data()
743
+ {
744
+ $aFields = $this->get_request_type() == 'check' ? self::$aCheckFields : self::$aPerformFields;
745
+ $aRet = array();
746
+ foreach ($aFields as $k => $v) {
747
+ $mVal = $this->get_http_val($k);
748
+ if (!is_null($mVal)) {
749
+ $aRet[$k] = $mVal;
750
+ }
751
+ }
752
+ return $aRet;
753
+ } // function get_request_data
754
+
755
+ /**
756
+ * Check signature
757
+ * @return boolean
758
+ */
759
+ public function check_signature($sSignature)
760
+ {
761
+ return $this->sSignKey ? $this->get_signature() == $sSignature : TRUE;
762
+ } // function check_signature
763
+
764
+ /**
765
+ * Get signature
766
+ * @return string
767
+ */
768
+ final public function get_signature()
769
+ {
770
+ if ($this->sSignKey) {
771
+ $sStr = $this->sTaId . $this->nAmount . $this->sCurrency;
772
+ $sStr .= empty($this->mCustom) || !empty($this->sState) ? '' : $this->mCustom;
773
+ $sStr .= empty($this->sState) ? '' : $this->sState;
774
+ $sStr .= empty($this->sDateTime) ? '' : $this->sDateTime;
775
+ $sStr .= empty($this->iRand) ? '' : $this->iRand;
776
+ $sStr .= $this->sSignKey;
777
+ return md5($sStr);
778
+ } else {
779
+ return NULL;
780
+ }
781
+ } // function get_signature
782
+
783
+ /**
784
+ * Get signature key
785
+ * @return string
786
+ */
787
+ final public function get_sign_key()
788
+ {
789
+ return $this->sSignKey;
790
+ } // function get_sign_key
791
+
792
+ /**
793
+ * Get field numbers
794
+ * @return array
795
+ */
796
+ public static function get_field_number($sKey = NULL)
797
+ {
798
+ return empty($sKey) ? self::$aFieldNumber : (isset(self::$aFieldNumber[$sKey]) ? self::$aFieldNumber[$sKey] : NULL);
799
+ } // function get_field_number
800
+
801
+
802
+ // -------------- Private/Protected methods -------------- \\
803
+
804
+ /**
805
+ * Get config
806
+ * @param string $sSignKey
807
+ * @return string
808
+ */
809
+ protected function get_config($sSignKey)
810
+ {
811
+ $sConfPath = $this->get_config_path();
812
+ $aRet = $sConfPath && file_exists($sConfPath) ? include($sConfPath) : array();
813
+
814
+ if (!empty($sSignKey)) {
815
+ $aRet['sign_key'] = $sSignKey;
816
+ }
817
+
818
+ return $aRet;
819
+ } // function get_config
820
+
821
+ /**
822
+ * Get path to config-file
823
+ * @return string
824
+ */
825
+ protected function get_config_path()
826
+ {
827
+ return dirname(__FILE__) . '/config.php';
828
+ } // function get_config_path
829
+
830
+ /**
831
+ * Get additional attributes
832
+ * @return string
833
+ */
834
+ protected function set_date_and_rand()
835
+ {
836
+ $this->sDateTime = date('Y-m-d H:i:s');
837
+ if (!empty($this->iUseRand) && (empty($this->iRand) || $this->iUseRand > 1)) {
838
+ $this->iRand = rand(1, 1024);
839
+ }
840
+ } // function set_date_and_rand
841
+
842
+ /**
843
+ * Get additional attributes
844
+ * @param array $aAttr
845
+ * @return string
846
+ */
847
+ protected function get_additional_attr($aAttr)
848
+ {
849
+ $sRet = '';
850
+ if ($aAttr) {
851
+ foreach ($aAttr as $k => $v) {
852
+ if (!in_array($k, array('type', 'name', 'value'))) {
853
+ $sRet .= ' ' . $k . '="' . $v . '"';
854
+ }
855
+ }
856
+ }
857
+ return $sRet;
858
+ } // function get_additional_attr
859
+
860
+ /**
861
+ * Prepare data for send (For "form" or "link"
862
+ * @return array
863
+ */
864
+ protected function prepare_send_data()
865
+ {
866
+ $aRet = array();
867
+ foreach (self::$aReqFields as $k => $v) {
868
+ $mVal = $this->get_property_val($k, $v);
869
+ if ($mVal) {
870
+ if ($k == self::PAYMENT_MODE_KEY) {
871
+ if (count($mVal) > 1) {
872
+ $i = 0;
873
+ foreach ($mVal as $v1) {
874
+ $aRet[$k . '[' . $i++ . ']'] = $v1;
875
+ }
876
+ } else {
877
+ $aRet[$k] = $mVal[0];
878
+ }
879
+ } else {
880
+ $aRet[$k] = $mVal;
881
+ }
882
+ }
883
+ }
884
+ return $aRet;
885
+ } // function prepare_send_data
886
+
887
+ /**
888
+ * Get value of property
889
+ * @param string $sField
890
+ * @param boolean $bRequired
891
+ * @return mixed
892
+ */
893
+ protected function get_property_val($sField, $bRequired)
894
+ {
895
+ $nNum = self::$aFieldNumber[$sField] * 10;
896
+ if ($sField == self::SIGNATURE_KEY) {
897
+ $mPropVal = $this->get_signature();
898
+ } else {
899
+ $sProp = self::$aCorrespondence[$sField];
900
+ $mPropVal = $this->$sProp;
901
+ }
902
+ if ($bRequired && !$mPropVal) {
903
+ throw new copayco_exception('Required field "' . $sField . '" isn\'t set.', 20001 + $nNum);
904
+ }
905
+ return $mPropVal;
906
+ } // function get_property_val
907
+
908
+ /**
909
+ * Check POST/GET fields of HTTP-request
910
+ * @param array $aCheckFields
911
+ * @return boolean
912
+ */
913
+ protected function check_http_fields($aCheckFields, $bSetErrMsg = FALSE)
914
+ {
915
+ foreach ($aCheckFields as $k => $v) {
916
+ $nNum = self::$aFieldNumber[$k] * 10;
917
+ $mVal = $this->get_http_val($k);
918
+ if ($v && is_null($mVal)) {
919
+ $this->fix_error_message('Undefined required field "' . $k . '".', 30001 + $nNum, $bSetErrMsg);
920
+ }
921
+ $sProp = isset(self::$aCorrespondence[$k]) ? self::$aCorrespondence[$k] : NULL;
922
+ if ($v && $sProp && $mVal != $this->$sProp) {
923
+ $this->fix_error_message('Field "' . $k . '" contain incorrect data.', 30002 + $nNum, $bSetErrMsg);
924
+ }
925
+ }
926
+ if (!$this->check_signature($this->get_http_val(self::SIGNATURE_KEY))) {
927
+ $this->fix_error_message('Request contain incorrect signature.', 30992, $bSetErrMsg);
928
+ }
929
+ return TRUE;
930
+ } // function check_http_fields
931
+
932
+ /**
933
+ * Fix error message for method "check_http_fields"
934
+ * @param string $sErrMsg
935
+ * @param integer $nCode
936
+ * @param boolean $bSetErrMsg
937
+ */
938
+ protected function fix_error_message($sErrMsg, $nCode, $bSetErrMsg)
939
+ {
940
+ if ($bSetErrMsg) {
941
+ $this->set_error_message($sErrMsg);
942
+ }
943
+ throw new copayco_exception($sErrMsg, $nCode);
944
+ } // function fix_error_message
945
+
946
+ /**
947
+ * Define extendet POST/GET value of HTTP-request
948
+ */
949
+ protected function define_http_val()
950
+ {
951
+ $aCor = array_flip(self::$aCorrespondence);
952
+ foreach (self::$aAutoFill as $v) {
953
+ if (empty($this->$v)) {
954
+ $this->$v = $this->get_http_val($aCor[$v]);
955
+ }
956
+ }
957
+ } // function define_http_val
958
+
959
+ /**
960
+ * Get POST/GET value of HTTP-request
961
+ * @param string $sKey
962
+ * @return mixed
963
+ */
964
+ protected function get_http_val($sKey)
965
+ {
966
+ return isset($_POST[$sKey]) ? $_POST[$sKey] : (isset($_GET[$sKey]) ? $_GET[$sKey] : NULL);
967
+ } // function get_http_val
968
+
969
+ /**
970
+ * Send notification about delivering
971
+ * @param string $sState - TA state
972
+ */
973
+ protected function send_notification($sState)
974
+ {
975
+ $this->sState = $sState;
976
+
977
+ $oCurl = curl_init($this->get_notification_url());
978
+ curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
979
+ if ($this->bTestMode) {
980
+ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
981
+ curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
982
+ }
983
+
984
+ $this->set_date_and_rand();
985
+ $aData = array();
986
+ foreach (self::$aNotificationFields as $k => $v) {
987
+ $aData[$k] = $this->get_property_val($k, $v);
988
+ }
989
+
990
+ curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aData);
991
+
992
+
993
+
994
+ $x = curl_exec($oCurl);
995
+ $sErr = curl_error($oCurl);
996
+ curl_close($oCurl);
997
+
998
+ if ($sErr) {
999
+ throw new copayco_exception('There is CURL error ocured:' . $sErr, 50001);
1000
+ }
1001
+ } // function send_notification
1002
+
1003
+ /**
1004
+ * Output HTTP-header
1005
+ * @param string $sMsg
1006
+ * @param string $sCharset
1007
+ */
1008
+ protected function output_header($sMsg, $sCharset = NULL)
1009
+ {
1010
+ if (empty($sCharset)) {
1011
+ $sCharset = empty($this->sCharset) ? 'utf-8' : $this->sCharset;
1012
+ }
1013
+ header('Content-Type: text/plain; charset=' . $sCharset);
1014
+ header('Accept-Ranges: bytes');
1015
+ header('Content-Length: ' . strlen($sMsg));
1016
+ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
1017
+ header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
1018
+ header(
1019
+ $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ?
1020
+ 'Pragma: no-cache' :
1021
+ 'Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0'
1022
+ ); // max-age=0
1023
+ } // function output_header
1024
+
1025
+ } // class copayco_api
1026
+
1027
+
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+ /**
1037
+ * Copayco exception class
1038
+ * Structure of error code
1039
+ * |XX|XX|X|
1040
+ * | | |_ serial number
1041
+ * | |____ field number (See copayco_api::$aFieldNumber)
1042
+ * |_______ error type (10 - source data, 20 - make request, 30 - check data, 40 - perform data, 50 - notify delivering)
1043
+ *
1044
+ */
1045
+ class copayco_exception extends Exception
1046
+ {
1047
+ /**
1048
+ * Get error type code
1049
+ * @return boolean
1050
+ */
1051
+ public function get_error_type_code()
1052
+ {
1053
+ return substr($this->getCode(), 0, 2);
1054
+ } // function get_error_type_code
1055
+
1056
+ /**
1057
+ * Get field code
1058
+ * @return boolean
1059
+ */
1060
+ public function get_field_code()
1061
+ {
1062
+ return substr($this->getCode(), 2, 2);
1063
+ } // function get_field_code
1064
+
1065
+ /**
1066
+ * Get error serial number
1067
+ * @return boolean
1068
+ */
1069
+ public function get_error_serial_number()
1070
+ {
1071
+ return substr($this->getCode(), -1);
1072
+ } // function get_error_serial_number
1073
+
1074
+ /**
1075
+ * Get field name
1076
+ * @return string
1077
+ */
1078
+ public function get_field_name()
1079
+ {
1080
+ $aFields = array_flip(copayco_api::get_field_number());
1081
+ $nCode = (int)$this->get_field_code();
1082
+ return $nCode && isset($aFields[$nCode]) ? $aFields[$nCode] : NULL;
1083
+ } // function get_field_name
1084
+
1085
+ } // class copayco_exception
1086
+
1087
+ ?>
app/code/community/Osgrow/Copayco/Model/Standard.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copayco Payment extension for Magento by Osgrow
4
+ *
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ * DISCLAIMER
14
+ *
15
+ * Do not edit or add to this file if you wish to upgrade
16
+ * the Osgrow Copayco module to newer versions in the future.
17
+ * If you wish to customize the Osgrow Copayco module for your needs
18
+ * please refer to http://www.magentocommerce.com for more information.
19
+ *
20
+ * @category Osgrow
21
+ * @package Osgrow_Copayco
22
+ * @copyright Copyright (C) 2015 Osgrow (http://osgrow.net/)
23
+ * @author Dmytro Borodulin
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Osgrow_Copayco_Model_Standard extends Mage_Payment_Model_Method_Abstract {
28
+
29
+ /*
30
+ * internal payment method identifier
31
+ */
32
+ protected $_code = 'copayco_standard';
33
+
34
+ /*
35
+ * $_canUseForMultishipping
36
+ */
37
+ protected $_canUseForMultishipping = false;
38
+
39
+ /*
40
+ * $_canUseInternal
41
+ */
42
+ protected $_canUseInternal = false;
43
+
44
+ /*
45
+ * $_isInitializeNeeded
46
+ */
47
+ protected $_isInitializeNeeded = true;
48
+
49
+ /*
50
+ * Instantiate state and set it to state object
51
+ */
52
+ public function initialize($paymentAction, $stateObject)
53
+ {
54
+ $stateObject->setState(Mage_Sales_Model_Order::STATE_NEW);
55
+ $stateObject->setStatus('new');
56
+ $stateObject->setIsNotified(false);
57
+ $stateObject->save();
58
+
59
+ }//function initialize
60
+
61
+ /*
62
+ * getOrderPlaceRedirectUrl
63
+ * @return string
64
+ */
65
+ public function getOrderPlaceRedirectUrl()
66
+ {
67
+ return Mage::getUrl('copayco/standard/redirect', array('_secure' => true));
68
+ }//function getOrderPlaceRedirectUrl
69
+
70
+ /*
71
+ * getPayData
72
+ * for pay form
73
+ * @return array
74
+ */
75
+ public function getPayData()
76
+ {
77
+ $oSes = Mage::getSingleton('checkout/session');
78
+ $oOrder = Mage::getModel('sales/order')->loadByIncrementId($oSes->getLastRealOrderId());
79
+ $sStore = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
80
+ $aForm = array(
81
+ 'test_mode' => $this->getConfigData('test_mode'),
82
+ 'shop_id' => $this->getConfigData('shop_id'),
83
+ 'sign_key' => $this->getConfigData('sign_key'),
84
+ 'ta_id' => $oOrder->getIncrementId(),
85
+ 'amount' => number_format($oOrder->getGrandTotal(), 2, '.', ''),
86
+ 'currency' => $oOrder->getOrderCurrencyCode(),
87
+ //'currency' => 'UAH',
88
+ 'description' => $sStore . ': ' . $oOrder->getCustomerFirstname(). ' ' . $oOrder->getCustomerLastname() . ', ' . $oOrder->getIncrementId(),
89
+ 'purpose' => $oOrder->getIncrementId(),
90
+ );
91
+ return $aForm;
92
+ }//function getPayData
93
+
94
+
95
+ }
96
+
app/code/community/Osgrow/Copayco/controllers/StandardController.php ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copayco Payment extension for Magento by Osgrow
4
+ *
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ * DISCLAIMER
14
+ *
15
+ * Do not edit or add to this file if you wish to upgrade
16
+ * the Osgrow Copayco module to newer versions in the future.
17
+ * If you wish to customize the Osgrow Copayco module for your needs
18
+ * please refer to http://www.magentocommerce.com for more information.
19
+ *
20
+ * @category Osgrow
21
+ * @package Osgrow_Copayco
22
+ * @copyright Copyright (C) 2015 Osgrow (http://osgrow.net/)
23
+ * @author Dmytro Borodulin
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Osgrow_Copayco_StandardController extends Mage_Core_Controller_Front_Action
28
+ {
29
+ /*
30
+ * copayco object
31
+ */
32
+ public $oApi;
33
+
34
+ /**
35
+ * refirect
36
+ * first step
37
+ */
38
+ public function redirectAction()
39
+ {
40
+ $aPayData = Mage::getModel('copayco/standard')->getPayData();
41
+ $oSes = Mage::getSingleton('checkout/session');
42
+
43
+ $this->oApi = Mage::helper('copayco/api');
44
+ $this->oApi->prepare($aPayData);
45
+
46
+ $oSes = Mage::getSingleton('checkout/session');
47
+ $oSes->setCopaycoQuoteId($oSes->getQuoteId());
48
+ $this->getResponse()->setBody($this->getLayout()->createBlock('copayco/redirect')->toHtml());
49
+ }//function redirectAction
50
+
51
+ /*
52
+ * confirmAction
53
+ * main copayco api methods
54
+ * Check
55
+ * Payment Result
56
+ */
57
+ public function confirmAction()
58
+ {
59
+ $this->oApi = Mage::helper('copayco/api');
60
+ $this->oApi->confirm();
61
+ }//function confirmAction
62
+
63
+ /*
64
+ * successfulAction
65
+ */
66
+ public function successfulAction()
67
+ {
68
+ $nTaId = Mage::app()->getRequest()->getParam('ta_id');
69
+ $oSes = Mage::getSingleton('checkout/session');
70
+ $oOrder = Mage::getModel('sales/order')->loadByIncrementId($nTaId);
71
+ //$oOrder = Mage::getModel('sales/order')->loadByIncrementId($oSes->getLastRealOrderId());
72
+ $sComment = 'Payment Completed';
73
+
74
+ if ($oOrder->getState() == 'processing' || $oOrder->getStatus() == 'processing'){
75
+ $sTmpState = Mage_Sales_Model_Order::STATE_COMPLETE;
76
+ $oOrder->setData('state', $sTmpState);
77
+ $oOrder->setStatus($oOrder->getConfig()->getStateDefaultStatus($sTmpState));
78
+ $sHistory = $oOrder->addStatusHistoryComment($sComment, false);
79
+ $sHistory->setIsCustomerNotified(true);
80
+ $oOrder->sendOrderUpdateEmail(true, $sComment);
81
+ $oOrder->save();
82
+ $oSes->setQuoteId($oSes->getCopaycoQuoteId());
83
+ Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save();
84
+ $this->_redirect('checkout/onepage/success');
85
+ return;
86
+ } else {
87
+ $this->errorAction();
88
+ return;
89
+ }
90
+
91
+ }//function successfulAction
92
+
93
+ /*
94
+ * errorAction
95
+ * When customer canceled
96
+ * or payment was not proceeded
97
+ */
98
+ public function errorAction()
99
+ {
100
+ $session = Mage::getSingleton('checkout/session');
101
+ $session->setQuoteId($session->getCopaycoQuoteId());
102
+ if ($session->getLastRealOrderId()) {
103
+ $oOrder = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
104
+ if ($oOrder->getId()) {
105
+ $oOrder->cancel();
106
+ $oOrder->addStatusHistoryComment('payment Error', false);
107
+ $oOrder->save();
108
+ }
109
+ }
110
+ $quote = Mage::getModel('sales/quote')->load($session->getCopaycoQuoteId());
111
+ if ($quote->getId()) {
112
+ $quote->setActive(true);
113
+ $quote->save();
114
+ }
115
+ $session->addError(Mage::helper('copayco')->__('Payment canceled! Pleas try again later.'));
116
+ $session->unsQuoteId();
117
+ $session->unsRedirectUrl();
118
+ $this->_redirect('checkout/cart');
119
+
120
+
121
+ }//function errorAction
122
+
123
+
124
+ }
app/code/community/Osgrow/Copayco/etc/config.xml ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Osgrow_Copayco>
5
+ <version>1.0.0</version>
6
+ </Osgrow_Copayco>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <copayco>
11
+ <class>Osgrow_Copayco_Model</class>
12
+ <resourceModel>copayco_resource</resourceModel>
13
+ </copayco>
14
+ </models>
15
+ <blocks>
16
+ <copayco>
17
+ <class>Osgrow_Copayco_Block</class>
18
+ </copayco>
19
+ </blocks>
20
+ <helpers>
21
+ <copayco>
22
+ <class>Osgrow_Copayco_Helper</class>
23
+ </copayco>
24
+ </helpers>
25
+ </global>
26
+ <frontend>
27
+ <routers>
28
+ <copayco_osgrow>
29
+ <use>standard</use>
30
+ <args>
31
+ <module>Osgrow_Copayco</module>
32
+ <frontName>copayco</frontName>
33
+ </args>
34
+ </copayco_osgrow>
35
+ </routers>
36
+
37
+ </frontend>
38
+ <default>
39
+ <payment>
40
+ <copayco_standard>
41
+ <model>copayco/standard</model>
42
+ <active>1</active>
43
+ <title>Copayco</title>
44
+ <validitytime>140</validitytime>
45
+ <sort_order>0</sort_order>
46
+ </copayco_standard>
47
+ </payment>
48
+ </default>
49
+ </config>
app/code/community/Osgrow/Copayco/etc/system.xml ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <payment>
5
+ <groups>
6
+ <copayco_standard translate="label">
7
+ <label>Copayco</label>
8
+ <frontend_type>text</frontend_type>
9
+ <sort_order>2</sort_order>
10
+ <show_in_default>1</show_in_default>
11
+ <show_in_website>1</show_in_website>
12
+ <show_in_store>1</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
+ <title translate="label">
24
+ <label>title</label>
25
+ <frontend_type>text</frontend_type>
26
+ <sort_order>20</sort_order>
27
+ <show_in_default>1</show_in_default>
28
+ <show_in_website>1</show_in_website>
29
+ <show_in_store>1</show_in_store>
30
+ </title>
31
+ <test_mode translate="label">
32
+ <label>Test Mode</label>
33
+ <frontend_type>select</frontend_type>
34
+ <source_model>adminhtml/system_config_source_yesno</source_model>
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
+ </test_mode>
40
+ <shop_id translate="label">
41
+ <label>Shop Id</label>
42
+ <frontend_type>text</frontend_type>
43
+ <sort_order>30</sort_order>
44
+ <show_in_default>1</show_in_default>
45
+ <show_in_website>1</show_in_website>
46
+ <show_in_store>1</show_in_store>
47
+ </shop_id>
48
+ <sign_key translate="label">
49
+ <label>Sign Key</label>
50
+ <frontend_type>text</frontend_type>
51
+ <sort_order>40</sort_order>
52
+ <show_in_default>1</show_in_default>
53
+ <show_in_website>1</show_in_website>
54
+ <show_in_store>0</show_in_store>
55
+ </sign_key>
56
+ <sort_order translate="label">
57
+ <label>Sort Order</label>
58
+ <frontend_type>text</frontend_type>
59
+ <sort_order>80</sort_order>
60
+ <show_in_default>1</show_in_default>
61
+ <show_in_website>1</show_in_website>
62
+ <show_in_store>0</show_in_store>
63
+ </sort_order>
64
+ </fields>
65
+ </copayco_standard>
66
+ </groups>
67
+ </payment>
68
+ </sections>
69
+ </config>
package.xml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Osgrow_Copayco</name>
4
+ <version>1.0.0</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>Payment Module for Magento</summary>
10
+ <description>www.copayco.com payment module allows&#xD;
11
+ recieving payment via internet acquiring(visa &amp; mastercard), internet banking, terminals</description>
12
+ <notes>First Stable Release</notes>
13
+ <authors><author><name>Osgrow</name><user>osgrow</user><email>info@osgrow.net</email></author></authors>
14
+ <date>2015-01-23</date>
15
+ <time>12:59:30</time>
16
+ <contents><target name="magecommunity"><dir name="Osgrow"><dir name="Copayco"><dir name="Block"><file name="Redirect.php" hash="975a5ddf2349a581310764a2f3745c44"/></dir><dir name="Helper"><file name="Api.php" hash="d158e755f2f0494fab29d60c617254f3"/><file name="Data.php" hash="d7f25fb9c67857e162b846067fd4777e"/><file name="config.php" hash="ab356fac7e14430cccab80b0acac0b4d"/><file name="copayco.php" hash="31937b0b511bcadc23718c0c64929b0b"/></dir><dir name="Model"><file name="Standard.php" hash="11d8670f4c2e502eddef9d4e172d2355"/></dir><dir name="controllers"><file name="StandardController.php" hash="92883b5b7c3a52a6129e905f52943431"/></dir><dir name="etc"><file name="config.xml" hash="58f23e2e1e1013a9903eb5beee10463e"/><file name="system.xml" hash="bcbfd92237c1b676d19ccf9bf091215e"/></dir></dir></dir></target><target name="magelocale"><dir/></target></contents>
17
+ <compatible/>
18
+ <dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
19
+ </package>