Completed
Branch master (741ff8)
by Nikita
02:08
created

Mygento_Payture_Model_Payture::checkSign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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