Paypalnotify   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 0
loc 135
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C preparePage() 0 114 13
1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Shop;
22
23
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class Paypalnotify
28
 * @package HaaseIT\HCSF\Controller\Shop
29
 */
30
class Paypalnotify extends Base
31
{
32
    /**
33
     * @var \Doctrine\DBAL\Connection
34
     */
35
    private $dbal;
36
37
    /**
38
     * Paypalnotify constructor.
39
     * @param ServiceManager $serviceManager
40
     */
41
    public function __construct(ServiceManager $serviceManager)
42
    {
43
        parent::__construct($serviceManager);
44
        $this->dbal = $serviceManager->get('dbal');
45
    }
46
47
    /**
48
     *
49
     */
50
    public function preparePage()
51
    {
52
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
53
        $this->P->cb_pagetype = 'content';
54
55
        $sLogData = '';
56
57
        $iId = \filter_input(INPUT_POST, 'custom', FILTER_SANITIZE_NUMBER_INT);
58
59
        $queryBuilder = $this->dbal->createQueryBuilder();
60
        $queryBuilder
61
            ->select('*')
62
            ->from('orders')
63
            ->where('o_id = ?')
64
            ->andWhere('o_paymentmethod = \'paypal\'')
65
            ->andWhere('o_paymentcompleted = \'n\'')
66
            ->setParameter(0, $iId)
67
        ;
68
        $statement = $queryBuilder->execute();
69
70
        if ($statement->rowCount() == 1) {
71
            $aOrder = $statement->fetch();
72
            $fGesamtbrutto = $this->helperShop->calculateTotalFromDB($aOrder);
73
74
            $postdata = '';
75
76
            foreach ($_POST as $i => $v) {
77
                $postdata .= $i . '=' . urlencode($v) . '&';
78
            }
79
            $postdata .= 'cmd=_notify-validate';
80
            $web = parse_url($this->config->getShop('paypal')['url']);
81
82
            if ($web['scheme'] === 'https') {
83
                $web['port'] = 443;
84
                $ssl = 'ssl://';
85
            } else {
86
                $web['port'] = 80;
87
                $ssl = '';
88
            }
89
            $fp = @fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30);
90
91
            if ($fp) {
92
                fwrite($fp, 'POST ' . $web['path'] . " HTTP/1.1\r\n");
93
                fwrite($fp, 'Host: ' . $web['host'] . "\r\n");
94
                fwrite($fp, "Content-type: application/x-www-form-urlencoded\r\n");
95
                fwrite($fp, 'Content-length: ' . strlen($postdata) . "\r\n");
96
                fwrite($fp, "Connection: close\r\n\r\n");
97
                fwrite($fp, $postdata . "\r\n\r\n");
98
99
                $info = [];
100
                while (!feof($fp)) {
101
                    $info[] = fgets($fp, 1024);
102
                }
103
                fclose($fp);
104
                $info = implode(',', $info);
105
                if (!(strpos($info, 'VERIFIED') === false)) {
106
107
                    $sLogData .= '-- new entry - '.date($this->config->getCore('locale_format_date_time')) . " --\n\n";
108
                    $sLogData .= "W00T!\n\n";
109
                    $sLogData .= \HaaseIT\Toolbox\Tools::debug($_REQUEST, '', true, true) . "\n\n";
110
111
                    // Check if the transaction id has been used before
112
                    $queryBuilder = $this->dbal->createQueryBuilder();
113
                    $queryBuilder
114
                        ->select('o_paypal_tx')
115
                        ->from('orders')
116
                        ->where('o_paypal_tx = ?')
117
                        ->setParameter(0, filter_input(INPUT_POST, 'txn_id'));
118
                    $statement = $queryBuilder->execute();
119
120
                    if ($statement->rowCount() === 0) {
121
                        if (
122
                            filter_input(INPUT_POST, 'payment_status') === 'Completed'
123
                            && filter_input(INPUT_POST, 'mc_gross') == number_format($fGesamtbrutto, 2, '.', '')
124
                            && filter_input(INPUT_POST, 'custom') == $aOrder['o_id']
125
                            && filter_input(INPUT_POST, 'mc_currency') == $this->config->getShop('paypal')['currency_id']
126
                            && filter_input(INPUT_POST, 'business') == $this->config->getShop('paypal')['business']
127
                        ) {
128
                            $queryBuilder = $this->dbal->createQueryBuilder();
129
                            $queryBuilder
130
                                ->update('orders')
131
                                ->set('o_paypal_tx', '?')
132
                                ->set('o_paymentcompleted', 'y')
133
                                ->setParameter(0, filter_input(INPUT_POST, 'txn_id'))
134
                                ->where('o_id = ?')
135
                                ->setParameter(1, $iId);
136
                            $queryBuilder->execute();
137
138
                            $sLogData .= '-- Alles ok. Zahlung erfolgreich. TXNID: ' . $_REQUEST['txn_id'] . " --\n\n";
139
                        } else {
140
                            $sLogData .= "-- In my country we have problem; Problem is evaluation. Throw the data down the log!\n";
141
                            $sLogData .= 'mc_gross: ' . $_REQUEST['mc_gross'] . ' - number_format($fGesamtbrutto, 2, \'.\', \'\'): ' . number_format($fGesamtbrutto,
142
                                    2, '.', '') . "\n";
143
                            $sLogData .= 'custom: ' . $_REQUEST['custom'] . ' - $aOrder[\'o_id\']: ' . $aOrder['o_id'] . "\n";
144
                            $sLogData .= 'payment_status: ' . $_REQUEST['payment_status'] . "\n";
145
                            $sLogData .= 'mc_currency: ' . $_REQUEST['mc_currency'] . ' - HelperConfig::$shop["paypal"]["currency_id"]: ' . $this->config->getShop('paypal')['currency_id'] . "\n";
146
                            $sLogData .= 'business: ' . $_REQUEST['receiver_email'] . ' - HelperConfig::$shop["paypal"]["business"]: ' . $this->config->getShop('paypal')['business'] . "\n\n";
147
                        }
148
                    } else {
149
                        // INVALID LOGGING ERROR
150
                        $sLogData .= '-- new entry - ' . date($this->config->getCore('locale_format_date_time')) . " --\n\nPHAIL\n\n";
151
                        $sLogData .= '!!! JEMAND HAT EINE ALTE TXN_ID BENUTZT: ' . $_REQUEST['txn_id'] . " !!!\n\n";
152
                        $sLogData .= "!!! INVALID !!!\n\n";
153
                    }
154
                } else {
155
                    $sLogData .= '-- new entry - ' . date($this->config->getCore('locale_format_date_time')) . " --\n\nPHAIL - Transaktion fehlgeschlagen. TXNID: " . $_REQUEST['txn_id'] . "\n" . $info . "\n\n";
156
                }
157
158
                file_put_contents(PATH_LOGS . FILE_PAYPALLOG, $sLogData, FILE_APPEND);
159
            }
160
        }
161
162
        $this->helper->terminateScript();
163
    }
164
}
165