|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* |
|
6
|
|
|
* @category Mygento |
|
7
|
|
|
* @package Mygento_Payture |
|
8
|
|
|
* @copyright Copyright © 2016 NKS LLC. (http://www.mygento.ru) |
|
9
|
|
|
*/ |
|
10
|
|
|
class Mygento_Payture_Model_Payture |
|
11
|
|
|
{ |
|
12
|
|
|
public function processOrder($order, $enc_key) |
|
13
|
|
|
{ |
|
14
|
|
|
$collection = Mage::getModel('payture/keys')->getCollection(); |
|
15
|
|
|
$collection->addFieldToFilter('orderid', $order->getId()); |
|
16
|
|
|
$item = $collection->getFirstItem(); |
|
17
|
|
|
if ($item->getSessionid() == null) { |
|
18
|
|
|
$sessionid = $this->initSession($order, $enc_key, $item->getId()); |
|
19
|
|
|
} else { |
|
20
|
|
|
$sessionid = $item->getSessionid(); |
|
21
|
|
|
} |
|
22
|
|
|
if ($sessionid != false) { |
|
23
|
|
|
return Mage::helper('payture')->getHost() . 'Pay?SessionId=' . $sessionid; |
|
24
|
|
|
} |
|
25
|
|
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function requestApiPost($url, $arpost) |
|
29
|
|
|
{ |
|
30
|
|
|
//Create a CURL GET request |
|
31
|
|
|
// @codingStandardsIgnoreStart |
|
32
|
|
|
$ch = curl_init(); |
|
33
|
|
|
$data = ''; |
|
34
|
|
|
foreach ($arpost as $key => $value) { |
|
35
|
|
|
$data .= $key . '=' . $value . ';'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$postFields = [ |
|
39
|
|
|
'Data' => $data, |
|
40
|
|
|
'Key' => Mage::helper('payture')->getKey() |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
Mage::helper('payture')->addLog($url); |
|
44
|
|
|
Mage::helper('payture')->addLog('Post fields:'); |
|
45
|
|
|
Mage::helper('payture')->addLog($postFields); |
|
46
|
|
|
|
|
47
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields)); |
|
48
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
51
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
52
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
|
53
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
54
|
|
|
|
|
55
|
|
|
$result = curl_exec($ch); |
|
56
|
|
|
curl_close($ch); |
|
57
|
|
|
// @codingStandardsIgnoreEnd |
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function initSession($order, $enc_key, $itemid) |
|
62
|
|
|
{ |
|
63
|
|
|
$paytype = Mage::getStoreConfig('payment/payture/paytype'); |
|
64
|
|
|
$request = array( |
|
65
|
|
|
'SessionType' => $paytype, |
|
66
|
|
|
'OrderId' => $order->getId(), |
|
67
|
|
|
'Amount' => $order->getGrandTotal() * 100, |
|
68
|
|
|
'Total' => $order->getGrandTotal(), |
|
69
|
|
|
'IP' => $order->getRemoteIp(), |
|
70
|
|
|
'Url' => Mage::getUrl( |
|
71
|
|
|
'payture/payment/result/', |
|
72
|
|
|
array('_secure' => true, 'order' => $enc_key) |
|
73
|
|
|
), |
|
74
|
|
|
// @codingStandardsIgnoreStart |
|
75
|
|
|
'Cheque' => base64_encode(Mage::helper('payture')->getOrderItemsJson($order)) |
|
76
|
|
|
// @codingStandardsIgnoreEnd |
|
77
|
|
|
); |
|
78
|
|
|
// @codingStandardsIgnoreStart |
|
79
|
|
|
Mage::helper('payture')->addLog('Cheque base64_json_decode: ' . print_r(Mage::helper('core')->jsonDecode(base64_decode($request['Cheque'])),1)); |
|
80
|
|
|
// @codingStandardsIgnoreEnd |
|
81
|
|
|
|
|
82
|
|
|
//add product names |
|
83
|
|
|
$products = ''; |
|
84
|
|
|
$items = $order->getItemsCollection(); |
|
85
|
|
|
foreach ($items as $item) { |
|
86
|
|
|
if ($item->getOriginalPrice() > 0) { |
|
87
|
|
|
$products .= $item->getName() . ', '; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (substr($products, strlen($products) - 2, 2) == ', ') { |
|
92
|
|
|
$products = substr($products, 0, strlen($products) - 2); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$request['Product'] = $products; |
|
96
|
|
|
|
|
97
|
|
|
$result = $this->requestApiPost(Mage::helper('payture')->getHost() . 'Init', $request); |
|
98
|
|
|
Mage::helper('payture')->addLog($result); |
|
99
|
|
|
$xml = simplexml_load_string($result); |
|
100
|
|
|
|
|
101
|
|
|
if ((bool) $xml["Success"][0] == true) { |
|
|
|
|
|
|
102
|
|
|
if ($xml["SessionId"][0]) { |
|
103
|
|
|
$item = Mage::getModel('payture/keys')->load($itemid); |
|
104
|
|
|
$item->setSessionid($xml["SessionId"][0]); |
|
105
|
|
|
$item->setPaytype($paytype); |
|
106
|
|
|
$item->setState('New'); |
|
107
|
|
|
$item->setDate(Mage::getModel('core/date')->date('Y-m-d H:i:s')); |
|
108
|
|
|
$item->save(); |
|
109
|
|
|
return $xml["SessionId"][0]; |
|
110
|
|
|
} |
|
111
|
|
|
} else { |
|
112
|
|
|
$session = Mage::getSingleton('checkout/session'); |
|
113
|
|
|
$session->addError($xml["ErrCode"][0]); |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function processStatus($status, $order_id, $key_id) |
|
119
|
|
|
{ |
|
120
|
|
|
$order = Mage::getModel('sales/order')->load($order_id); |
|
121
|
|
|
if ($order->getId()) { |
|
122
|
|
|
$sess = Mage::getModel('payture/keys')->load($key_id); |
|
123
|
|
|
if ($status == 'Charged') { |
|
124
|
|
|
Mage::helper('payture')->addTransaction($order); |
|
125
|
|
|
$sess->setState('Complete'); |
|
126
|
|
|
} else { |
|
127
|
|
|
$sess->setState($status); |
|
128
|
|
|
} |
|
129
|
|
|
$sess->save(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function checkSign($order_id, $check_result) |
|
134
|
|
|
{ |
|
135
|
|
|
$string = $order_id . '|' . Mage::getStoreConfig('payment/payture/frame_token'); |
|
136
|
|
|
if (hash('sha512', $string) == $check_result) { |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* |
|
144
|
|
|
* @param type $type |
|
145
|
|
|
* @param type $receipt |
|
146
|
|
|
* @param type $order |
|
147
|
|
|
* @return type |
|
148
|
|
|
*/ |
|
149
|
|
|
public function modifyOrder($type, $receipt, $order) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($order->getId()) { |
|
152
|
|
|
$req = array( |
|
153
|
|
|
'Key' => Mage::helper('payture')->getKey(), |
|
154
|
|
|
'OrderId' => $order->getId(), |
|
155
|
|
|
'Password' => Mage::helper('payture')->getPassword(), |
|
156
|
|
|
'Amount' => round($receipt->getGrandTotal() * 100, 0), |
|
157
|
|
|
// @codingStandardsIgnoreStart |
|
158
|
|
|
'Cheque' => base64_encode(Mage::helper('payture')->getOrderItemsJson($order)) |
|
159
|
|
|
// @codingStandardsIgnoreEnd |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
return Mage::helper('payture')->processTransaction($type, $req, $order); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.