Completed
Push — master ( 888d0b...6476e7 )
by
unknown
04:54
created

Mygento_Payture_Helper_Data::sendCustomComment()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
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_Helper_Data extends Mage_Core_Helper_Abstract
11
{
12
    public function addLog($text)
13
    {
14
        if (Mage::getStoreConfig('payment/payture/debug')) {
15
            Mage::log($text, null, 'payture.log', true);
16
        }
17
    }
18
19
    public function getKey()
20
    {
21
        return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/key'));
22
    }
23
24
    public function getPassword()
25
    {
26
        return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/password'));
27
    }
28
29
    public function sendEmailByOrder($order)
30
    {
31
        try {
32
            $order->sendNewOrderEmail();
33
        } catch (Exception $e) {
34
            $this->addLog($e->getMessage());
35
        }
36
    }
37
38
    public function getLink($order_id)
39
    {
40
        $collection = Mage::getModel('payture/keys')->getCollection();
41
        $collection->addFieldToFilter('orderid', $order_id);
42
        if (count($collection) == 0) {
43
            $model = Mage::getModel('payture/keys');
44
            $key   = strtr(base64_encode(microtime() . $order_id . rand(1, 1048576)), '+/=', '-_,');
45
            $model->setHkey($key);
46
            $model->setOrderid($order_id);
47
            $model->setSessionid(null);
48
            $model->setDate(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/',
54
                    array('_secure' => true, 'order' => $item->getHkey()));
55
        }
56
    }
57
58 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...
59
    {
60
        $collection = Mage::getModel('payture/keys')->getCollection();
61
        $collection->addFieldToFilter('hkey', $link);
62
        if (count($collection) == 0) {
63
            return false;
64
        }
65
        $item = $collection->getFirstItem();
66
        return $item;
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.',
81
                        false);
82
                    $order->save();
83
                }
84
                $invoice         = Mage::getModel('sales/service_order', $order)->prepareInvoice();
85
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
86
                $invoice->register();
87
                $invoice->getOrder()->setCustomerNoteNotify(false);
88
                $invoice->getOrder()->setIsInProcess(true);
89
                $order->addStatusHistoryComment('Automatically INVOICED by Payture_Invoicer.', false);
90
                $transactionSave = Mage::getModel('core/resource_transaction')
91
                    ->addObject($invoice)
92
                    ->addObject($invoice->getOrder());
93
                $transactionSave->save();
94
                if (Mage::getStoreConfig('payment/payture/send')) {
95
                    $order->sendOrderUpdateEmail($order->getStatus(),
96
                        Mage::getStoreConfig('payment/payture/text'));
97
                }
98
                if (Mage::getStoreConfig('payment/payture/sendinvoice')) {
99
                    $invoice->sendEmail();
100
                }
101
                if (Mage::getStoreConfig('payment/payture/sendadmin') != '') {
102
                    $this->sendCustomComment($order,
103
                        Mage::getStoreConfig('payment/payture/sendadmin'),
104
                        Mage::getStoreConfig('payment/payture/text'));
105
                }
106
            } catch (Exception $e) {
107
                $order->addStatusHistoryComment('Payture_Invoicer: Exception occurred during automaticall transaction action. Exception message: ' . $e->getMessage(),
108
                    false);
109
                $order->save();
110
            }
111
        }
112
    }
113
114
    private function sendCustomComment($order, $toemail, $comment)
115
    {
116
        $storeId = $order->getStore()->getId();
117
118
        $mailer    = Mage::getModel('core/email_template_mailer');
119
        $emailInfo = Mage::getModel('core/email_info');
120
        $emailInfo->addTo($toemail, Mage::getStoreConfig('trans_email/ident_sales/name'));
121
122
        if ($order->getCustomerIsGuest()) {
123
            $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $storeId);
124
        } else {
125
            $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $storeId);
126
        }
127
        $mailer->addEmailInfo($emailInfo);
128
129
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $storeId));
130
        $mailer->setStoreId($storeId);
131
        $mailer->setTemplateId($templateId);
132
        $mailer->setTemplateParams(array(
133
            'order'   => $order,
134
            'comment' => $comment,
135
            'billing' => $order->getBillingAddress()
136
        ));
137
        $mailer->send();
138
    }
139
140
    public function checkTicket($_ticket)
141
    {
142
        $url = $this->getHost() . 'PayStatus?Key=' . Mage::helper('payture')->getKey() . '&OrderId=' . $_ticket->getOrderid();
143
        $xml = simplexml_load_string($this->getData($url));
144
        if ($xml['Success'] == 'True') {
145
            Mage::getModel('payture/payture')->processStatus($xml["State"], $_ticket->getOrderid(),
146
                $_ticket->getId());
147
        }
148
    }
149
150
    public function getHost()
151
    {
152
        if (Mage::getStoreConfig('payment/payture/test')) {
153
            return 'https://sandbox.payture.com/apim/';
154
        }
155
        return 'https://secure.payture.com/apim/';
156
    }
157
158
    public function getData($url)
159
    {
160
        // @codingStandardsIgnoreStart
161
        $ch      = curl_init();
162
        $timeout = 10;
163
        curl_setopt($ch, CURLOPT_URL, $url);
164
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
165
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
166
        $data    = curl_exec($ch);
167
        curl_close($ch);
168
        // @codingStandardsIgnoreEnd
169
        $this->addLog($data);
170
        return $data;
171
    }
172
    
173
    /**
174
     *
175
     * @param type string
176
     * @return mixed
177
     */
178
    public function getConfig($param)
179
    {
180
        return Mage::getStoreConfig('payment/payture/' . $param);
181
    }
182
183
    /**
184
     *
185
     * @param $entity Mage_Sales_Model_Order | Mage_Sales_Model_Order_Invoice | Mage_Sales_Model_Order_Creditmemo
186
     * @return type
187
     */
188
    public function getOrderItemsJson($entity)
189
    {
190
        $shippingTax   = Mage::getStoreConfig('payment/payture/shipping_tax');
191
        $taxValue      = Mage::getStoreConfig('payment/payture/tax_options');
192
        $attributeCode = '';
193
        
194
        if (!Mage::getStoreConfig('payment/payture/tax_all')) {
195
            $attributeCode = Mage::getStoreConfig('payment/payture/product_tax_attr');
196
        }
197
198
        if (!Mage::getStoreConfig('payment/payture/default_shipping_name')) {
199
            $entity->setShippingDescription(Mage::getStoreConfig('payment/payture/custom_shipping_name'));
200
        }
201
202
        $data   = Mage::helper('payture/discount')->getRecalculated($entity, $taxValue,
203
            $attributeCode, $shippingTax);
204
        $result = [];
205
        foreach ($data['items'] as $item) {
206
            $result['Positions'][] = [
207
                'Quantity' => $item['quantity'],
208
                'Price'    => $item['price'],
209
                'Tax'      => $item['tax'],
210
                'Text'     => $item['name'],
211
            ];
212
        }
213
        
214
        $result['CustomerContact'] = $entity->getCustomerEmail();
215
216
        return Mage::helper('core')->jsonEncode($result);
217
    }
218
219
}
220