Completed
Push — master ( a7a410...ee8da7 )
by Danila
02:12
created

Mygento_Payture_Model_Payture::requestApiGet()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 2
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
13
    public function processOrder($order, $enc_key)
14
    {
15
        $collection = Mage::getModel('payture/keys')->getCollection();
16
        $collection->addFieldToFilter('orderid', $order->getId());
17
        $item = $collection->getFirstItem();
18
        if ($item->getSessionid() == null) {
19
            $sessionid = $this->initSession($order, $enc_key, $item->getId());
20
        } else {
21
            $sessionid = $item->getSessionid();
22
        }
23
        if ($sessionid != false) {
24
            return Mage::helper('payture')->getHost() . 'Pay?SessionId=' . $sessionid;
25
        } else {
26
            return false;
27
        }
28
    }
29
30
    protected function requestApiGet($url, $arpost)
31
    {
32
33
        //Create a CURL GET request
34
        // @codingStandardsIgnoreStart
35
        $ch = curl_init();
36
        $data = '';
37
        foreach ($arpost as $key => $value) {
38
            $data.= $key . '=' . $value . ';';
39
        }
40
        $full_url = $url . "?Key=" . Mage::helper('payture')->getKey() . '&Data=' . urlencode($data);
41
        Mage::helper('payture')->addLog($full_url);
42
        curl_setopt($ch, CURLOPT_URL, $full_url);
43
        curl_setopt($ch, CURLOPT_POST, false);
44
        curl_setopt($ch, CURLOPT_HEADER, 0);
45
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
46
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
47
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
48
49
        $result = curl_exec($ch);
50
        curl_close($ch);
51
         // @codingStandardsIgnoreEnd   
52
        return $result;
53
    }
54
55
    private function initSession($order, $enc_key, $itemid)
56
    {
57
        $paytype = Mage::getStoreConfig('payment/payture/paytype');
58
        $request = array(
59
            'SessionType' => $paytype,
60
            'OrderId' => $order->getId(),
61
            'Amount' => $order->getGrandTotal() * 100,
62
            'Total' => $order->getGrandTotal(),
63
            'IP' => $order->getRemoteIp(),
64
            'Url' => Mage::getUrl('payture/payment/result/', array('_secure' => true, 'order' => $enc_key)),
65
        );
66
        //add product names
67
        $products = '';
68
        $items = $order->getItemsCollection();
69
        foreach ($items as $item) {
70
            if ($item->getOriginalPrice() > 0) {
71
                $products.=$item->getName() . ', ';
72
            }
73
        }
74
75
        if (substr($products, strlen($products) - 2, 2) == ', ') {
76
            $products = substr($products, 0, strlen($products) - 2);
77
        }
78
        $request['Product'] = $products;
79
        $result = $this->requestApiGet(Mage::helper('payture')->getHost() . 'Init', $request);
80
        Mage::helper('payture')->addLog($result);
81
        $xml = simplexml_load_string($result);
82
        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...
83
            if ($xml["SessionId"][0]) {
84
                $item = Mage::getModel('payture/keys')->load($itemid);
85
                $item->setSessionid($xml["SessionId"][0]);
86
                $item->setPaytype($paytype);
87
                $item->setState('New');
88
                $item->save();
89
                return $xml["SessionId"][0];
90
            }
91
        } else {
92
            $session = Mage::getSingleton('checkout/session');
93
            $session->addError($xml["ErrCode"][0]);
94
            return false;
95
        }
96
    }
97
98
    public function processStatus($status, $order_id, $key_id)
99
    {
100
        $order = Mage::getModel('sales/order')->load($order_id);
101
        if ($order->getId()) {
102
            if ($status == 'Charged') {
103
                Mage::helper('payture')->addTransaction($order);
104
                $sess = Mage::getModel('payture/keys')->load($key_id);
105
                $sess->setState('Complete');
106
                $sess->save();
107
            } else {
108
                $sess = Mage::getModel('payture/keys')->load($key_id);
109
                $sess->setState($status);
110
                $sess->save();
111
            }
112
        }
113
    }
114
115
    public function checkSign($order_id, $check_result)
116
    {
117
        $string = $order_id . '|' . Mage::getStoreConfig('payment/payture/frame_token');
118
        if (hash('sha512', $string) == $check_result) {
119
            return true;
120
        }
121
        return false;
122
    }
123
}
124