Completed
Push — master ( f7b82b...d24bb5 )
by Danila
02:12
created

Mygento_Payture_Helper_Data::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
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_Helper_Data extends Mage_Core_Helper_Abstract
11
{
12
13
    public function addLog($text)
14
    {
15
        if (Mage::getStoreConfig('payment/payture/debug')) {
16
            Mage::log($text, null, 'payture.log', true);
17
        }
18
    }
19
20
    public function getKey()
21
    {
22
        return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/key'));
23
    }
24
25
    public function getPassword()
26
    {
27
        return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/password'));
28
    }
29
30
    public function sendEmailByOrder($order)
31
    {
32
        try {
33
            $order->sendNewOrderEmail();
34
        } catch (Exception $e) {
35
            $this->addLog($e->getMessage());
36
        }
37
    }
38
39
    public function getLink($order_id)
40
    {
41
        $collection = Mage::getModel('payture/keys')->getCollection();
42
        $collection->addFieldToFilter('orderid', $order_id);
43
        if (count($collection) == 0) {
44
            $model = Mage::getModel('payture/keys');
45
            $key = strtr(base64_encode(microtime() . $order_id . rand(1, 1048576)), '+/=', '-_,');
46
            $model->setHkey($key);
47
            $model->setOrderid($order_id);
48
            $model->setSessionid(null);
49
            $model->save();
50
            return Mage::getUrl('payture/payment/paynow/', array('_secure' => true, 'order' => $key));
51
        } else {
52
            $item = $collection->getFirstItem();
53
            return Mage::getUrl('payture/payment/paynow/', array('_secure' => true, 'order' => $item->getHkey()));
54
        }
55
    }
56
57 View Code Duplication
    public function decodeid($link)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        $collection = Mage::getModel('payture/keys')->getCollection();
60
        $collection->addFieldToFilter('hkey', $link);
61
        if (count($collection) == 0) {
62
            return false;
63
        } else {
64
            $item = $collection->getFirstItem();
65
            return $item;
66
        }
67
    }
68
69
    public function addtransaction($order)
70
    {
71
        $orders = Mage::getModel('sales/order_invoice')->getCollection()
72
                ->addAttributeToFilter('order_id', array('eq' => $order->getId()));
73
        $orders->getSelect()->limit(1);
74
        if ((int) $orders->count() !== 0) {
75
            return $this;
76
        }
77
        if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {
78
            try {
79
                if (!$order->canInvoice()) {
80
                    $order->addStatusHistoryComment('Payture_Invoicer: Order cannot be invoiced.', false);
81
                    $order->save();
82
                }
83
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
84
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
85
                $invoice->register();
86
                $invoice->getOrder()->setCustomerNoteNotify(false);
87
                $invoice->getOrder()->setIsInProcess(true);
88
                $order->addStatusHistoryComment('Automatically INVOICED by Payture_Invoicer.', false);
89
                $transactionSave = Mage::getModel('core/resource_transaction')
90
                        ->addObject($invoice)
91
                        ->addObject($invoice->getOrder());
92
                $transactionSave->save();
93
                if (Mage::getStoreConfig('payment/payture/send')) {
94
                    $order->sendOrderUpdateEmail($order->getStatus(), Mage::getStoreConfig('payment/payture/text'));
95
                }
96
                if (Mage::getStoreConfig('payment/payture/sendinvoice')) {
97
                    $invoice->sendEmail();
98
                }
99
                if (Mage::getStoreConfig('payment/payture/sendadmin') != '') {
100
                    $this->sendCustomComment($order, Mage::getStoreConfig('payment/payture/sendadmin'), Mage::getStoreConfig('payment/payture/text'));
101
                }
102
            } catch (Exception $e) {
103
                $order->addStatusHistoryComment('Payture_Invoicer: Exception occurred during automaticall transaction action. Exception message: ' . $e->getMessage(), false);
104
                $order->save();
105
            }
106
        }
107
    }
108
109
    private function sendCustomComment($order, $toemail, $comment)
110
    {
111
        $storeId = $order->getStore()->getId();
112
113
        $mailer = Mage::getModel('core/email_template_mailer');
114
        $emailInfo = Mage::getModel('core/email_info');
115
        $emailInfo->addTo($toemail, Mage::getStoreConfig('trans_email/ident_sales/name'));
116
117
        if ($order->getCustomerIsGuest()) {
118
            $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $storeId);
119
        } else {
120
            $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $storeId);
121
        }
122
        $mailer->addEmailInfo($emailInfo);
123
124
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $storeId));
125
        $mailer->setStoreId($storeId);
126
        $mailer->setTemplateId($templateId);
127
        $mailer->setTemplateParams(array(
128
            'order' => $order,
129
            'comment' => $comment,
130
            'billing' => $order->getBillingAddress()
131
        ));
132
        $mailer->send();
133
    }
134
135
    public function checkTicket($_ticket)
136
    {
137
        $url = $this->getHost() . 'PayStatus?Key=' . Mage::helper('payture')->getKey() . '&OrderId=' . $_ticket->getOrderid();
138
        $xml = $this->getData($url);
139
        if ($xml["Success"] == 'True') {
140
            Mage::getModel('payture/payture')->processStatus($xml["State"], $_ticket->getOrderid(), $_ticket->getId());
141
        }
142
    }
143
144
    public function getHost()
145
    {
146
        if (Mage::getStoreConfig('payment/payture/test')) {
147
            return 'https://sandbox.payture.com/apim/';
148
        }
149
        return 'https://secure.payture.com/apim/';
150
    }
151
152
    public function getData($url)
153
    {
154
        $ch = curl_init();
155
        $timeout = 10;
156
        curl_setopt($ch, CURLOPT_URL, $url);
157
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
158
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
159
        $data = curl_exec($ch);
160
        curl_close($ch);
161
        return $data;
162
    }
163
}
164