Completed
Push — master ( f0d2bc...c8cdc0 )
by
unknown
02:18
created

Mygento_Payture_Model_Payture::modifyOrder()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 22

Duplication

Lines 9
Ratio 29.03 %

Importance

Changes 0
Metric Value
dl 9
loc 31
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 3
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 requestApiGet($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
        $full_url = $url . "?Key=" . Mage::helper('payture')->getKey() . '&Data=' . urlencode($data);
38
        Mage::helper('payture')->addLog($full_url);
39
        curl_setopt($ch, CURLOPT_URL, $full_url);
40
        curl_setopt($ch, CURLOPT_POST, false);
41
        curl_setopt($ch, CURLOPT_HEADER, 0);
42
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
43
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
44
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
45
46
        $result = curl_exec($ch);
47
        curl_close($ch);
48
        // @codingStandardsIgnoreEnd   
49
        return $result;
50
    }
51
52
    private function initSession($order, $enc_key, $itemid)
53
    {
54
        $paytype  = Mage::getStoreConfig('payment/payture/paytype');
55
        $request  = array(
56
            'SessionType'     => $paytype,
57
            'OrderId'         => $order->getId(),
58
            'Amount'          => $order->getGrandTotal() * 100,
59
            'Total'           => $order->getGrandTotal(),
60
            'IP'              => $order->getRemoteIp(),
61
            'Url'             => Mage::getUrl(
62
                'payture/payment/result/',
63
                array('_secure' => true, 'order' => $enc_key)
64
            ),
65
            // @codingStandardsIgnoreStart
66
            'ChequePositions' => base64_encode(Mage::helper('payture')->getOrderItemsJson($order))
67
            // @codingStandardsIgnoreEnd
68
        );
69
        // @codingStandardsIgnoreStart
70
        Mage::helper('payture')->addLog('ChequePositions base64_json_decode: ' . print_r(Mage::helper('core')->jsonDecode(base64_decode($request['ChequePositions'])),1));
71
        // @codingStandardsIgnoreEnd
72
        
73
        //add product names
74
        $products = '';
75
        $items    = $order->getItemsCollection();
76
        foreach ($items as $item) {
77
            if ($item->getOriginalPrice() > 0) {
78
                $products .= $item->getName() . ', ';
79
            }
80
        }
81
82
        if (substr($products, strlen($products) - 2, 2) == ', ') {
83
            $products = substr($products, 0, strlen($products) - 2);
84
        }
85
86
        $request['Product'] = $products;
87
88
        $result = $this->requestApiGet(Mage::helper('payture')->getHost() . 'Init', $request);
89
        Mage::helper('payture')->addLog($result);
90
        $xml    = simplexml_load_string($result);
91
92
        if ((bool) $xml["Success"][0] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
93
            if ($xml["SessionId"][0]) {
94
                $item = Mage::getModel('payture/keys')->load($itemid);
95
                $item->setSessionid($xml["SessionId"][0]);
96
                $item->setPaytype($paytype);
97
                $item->setState('New');
98
                $item->setDate(Mage::getModel('core/date')->date('Y-m-d H:i:s'));
99
                $item->save();
100
                return $xml["SessionId"][0];
101
            }
102
        } else {
103
            $session = Mage::getSingleton('checkout/session');
104
            $session->addError($xml["ErrCode"][0]);
105
            return false;
106
        }
107
    }
108
109
    public function processStatus($status, $order_id, $key_id)
110
    {
111
        $order = Mage::getModel('sales/order')->load($order_id);
112
        if ($order->getId()) {
113
            $sess = Mage::getModel('payture/keys')->load($key_id);
114
            if ($status == 'Charged') {
115
                Mage::helper('payture')->addTransaction($order);
116
                $sess->setState('Complete');
117
            } else {
118
                $sess->setState($status);
119
            }
120
            $sess->save();
121
        }
122
    }
123
124
    public function checkSign($order_id, $check_result)
125
    {
126
        $string = $order_id . '|' . Mage::getStoreConfig('payment/payture/frame_token');
127
        if (hash('sha512', $string) == $check_result) {
128
            return true;
129
        }
130
        return false;
131
    }
132
    
133
    /**
134
     *
135
     * @param type $type
136
     * @param type $receipt
137
     * @param type $order
138
     * @return type
139
     */
140
    public function modifyOrder($type, $receipt, $order)
141
    {
142
        if ($order->getId()) {
143
            $req = array(
144
                'Key' => Mage::helper('payture')->getKey(),
145
                'OrderId' => $order->getId(),
146
                'Password' => Mage::helper('payture')->getPassword(),
147
                'Amount' => round($receipt->getGrandTotal() * 100, 0),
148
                // @codingStandardsIgnoreStart
149
                'ChequePositions' => base64_encode(Mage::helper('payture')->getOrderItemsJson($order))
150
                // @codingStandardsIgnoreEnd
151
            );
152
            // @codingStandardsIgnoreStart
153
            Mage::helper('payture')->addLog('ChequePositions ' . $type . ' base64_json_decode: ' . print_r(Mage::helper('core')->jsonDecode(base64_decode($req['ChequePositions'])),1));
154
            // @codingStandardsIgnoreEnd
155
            $url = Mage::helper('payture')->getHost() . $type . '?' . http_build_query($req);
156
            Mage::helper('payture')->addLog($url);
157
            $xml = Mage::helper('payture')->getData($url);
158
            Mage::helper('payture')->addLog($xml);
159 View Code Duplication
            if ($xml["Success"] == 'True') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
                $collection = Mage::getModel('payture/keys')->getCollection();
161
                $collection->addFieldToFilter('orderid', $order->getId());
162
                $item = $collection->getFirstItem();
163
                $sess = Mage::getModel('payture/keys')->load($item->getId());
164
                $sess->setState($type . 'ed');
165
                $sess->save();
166
                Mage::helper('payture')->addTransaction($order);
167
            }
168
            return $xml;
169
        }
170
    }
171
}
172