Pec24Gateway   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
eloc 140
c 1
b 0
f 0
dl 0
loc 283
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountriesList() 0 5 1
A getParametersForm() 0 15 1
A saveParametersForm() 0 14 4
A getParsianMid() 0 9 1
A __construct() 0 3 1
A formatAmount() 0 3 1
A getDialogURL() 0 3 1
A setGatewayInformation() 0 11 1
A getRedirectURL() 0 5 1
A getCheckoutFormContent() 0 11 1
C gatewayNotify() 0 103 15
A getAuthority() 0 18 2
1
<?php
2
3
namespace XoopsModules\Oledrion\Gateways\Pec24;
4
5
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
6
//require('nusoap.php');
7
8
use XoopsModules\Oledrion;
9
use XoopsModules\Oledrion\Gateways\Gateway;
10
11
/**
12
 * Class Pec24
13
 */
14
class Pec24Gateway extends Gateway
15
{
16
    /**
17
     * Pec24 constructor.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
    }
23
24
    public function setGatewayInformation()
25
    {
26
        $gateway                  = [];
27
        $gateway['name']          = 'Parsian';
28
        $gateway['foldername']    = 'Pec24';
29
        $gateway['version']       = '1.0';
30
        $gateway['description']   = 'سيستم پرداخت الکترونيک بانک پارسیان'; //Parsian Electronic Banking System
31
        $gateway['author']        = 'Hossein Azizabadi';
32
        $gateway['credits']       = 'جسین عزیزآبادی';
33
        $gateway['releaseDate']   = 20121020;
34
        $this->gatewayInformation = $gateway;
35
    }
36
37
    /**
38
     * This method is called to display a form containing the gateways parameters.
39
     * You must return a XoopsThemeForm and this form MUST use the post method.
40
     * The module is in charge to load your defines before to call this method and
41
     * it loads xoopsformloader.php
42
     *
43
     * If your gateway does not requires parameters, then you must return false
44
     *
45
     * @param $postUrl
46
     * @return mixed
47
     * @internal param string $posstUrl The url to use to post data to
48
     */
49
    public function getParametersForm($postUrl)
50
    {
51
        $db                     = \XoopsDatabaseFactory::getDatabaseConnection();
52
        $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db);
53
        $sform                  = new \XoopsThemeForm(_OLEDRION_SAMAN_PARAMETERS . ' - ' . $this->gatewayInformation['name'], 'frmParsian', $postUrl);
54
        $sform->addElement(new \XoopsFormHidden('gateway', $this->gatewayInformation['foldername']));
55
        $pin = new \XoopsFormText(_OLEDRION_SAMAN_MID, 'parsian_mid', 50, 255, $gatewaysOptionsHandler->getGatewayOptionValue($this->gatewayInformation['foldername'], 'parsian_mid'));
56
        $pin->setDescription(_OLEDRION_SAMAN_MIDDSC);
57
        $sform->addElement($pin, true);
58
        $buttonTray = new \XoopsFormElementTray('', '');
59
        $submit_btn = new \XoopsFormButton('', 'post', _AM_OLEDRION_GATEWAYS_UPDATE, 'submit');
60
        $buttonTray->addElement($submit_btn);
61
        $sform->addElement($buttonTray);
62
63
        return $sform;
64
    }
65
66
    /**
67
     * This method is called by the module to save the gateway's parameters
68
     * It's up to you to verify data and eventually to complain about uncomplete or missing data
69
     *
70
     * @param  array $data Receives $_POST
71
     * @return bool True if you succeed to save data else false
72
     */
73
    public function saveParametersForm($data)
74
    {
75
        if ('' !== xoops_trim($this->languageFilename) && file_exists($this->languageFilename)) {
76
            require_once $this->languageFilename;
77
        }
78
        $db                     = \XoopsDatabaseFactory::getDatabaseConnection();
79
        $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db);
80
        $gatewayName            = $this->gatewayInformation['foldername'];
81
        $gatewaysOptionsHandler->deleteGatewayOptions($gatewayName);
82
        if (!$gatewaysOptionsHandler->setGatewayOptionValue($gatewayName, 'parsian_mid', $data['parsian_mid'])) {
83
            return false;
84
        }
85
86
        return true;
87
    }
88
89
    /**
90
     * @param $amount
91
     * @return string
92
     */
93
    private function formatAmount($amount)
94
    {
95
        return number_format($amount, 2, '.', '');
96
    }
97
98
    /**
99
     * @param $cmd_total
100
     * @param $cmd_id
101
     */
102
    public function getAuthority($cmd_total, $cmd_id)
103
    {
104
        $url = $this->getDialogURL();
105
        if (extension_loaded('soap')) {
106
            $soapclient = new \Soapclient($url);
0 ignored issues
show
Unused Code introduced by
The assignment to $soapclient is dead and can be removed.
Loading history...
107
        } else {
108
            require_once __DIR__ . '/nusoap.php';
109
            $soapclient = new \Soapclient($url, 'wsdl');
0 ignored issues
show
Bug introduced by
'wsdl' of type string is incompatible with the type array|null expected by parameter $options of Soapclient::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            $soapclient = new \Soapclient($url, /** @scrutinizer ignore-type */ 'wsdl');
Loading history...
110
        }
111
        $params     = [
112
            'pin'         => $this->getParsianMid(),
113
            'amount'      => (int)$this->formatAmount($cmd_total),
114
            'orderId'     => (int)$cmd_id,
115
            'callbackUrl' => OLEDRION_URL . 'gateway-notify.php?cmd_id=' . (int)$cmd_id . '&cmd_total=' . (int)$this->formatAmount($cmd_total),
116
            'authority'   => 0,
117
            'status'      => 1,
118
        ];
119
        $sendParams = [$params];
0 ignored issues
show
Unused Code introduced by
The assignment to $sendParams is dead and can be removed.
Loading history...
120
        //$res = $soapclient->call('PinPaymentRequest', $sendParams);
121
        //return $res['authority'];
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127
    public function getParsianMid()
128
    {
129
        $db                     = \XoopsDatabaseFactory::getDatabaseConnection();
130
        $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db);
131
        global $xoopsConfig;
132
        $gatewayName = $this->gatewayInformation['foldername'];
133
        $parsian_mid = $gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'parsian_mid');
134
135
        return $parsian_mid;
136
    }
137
138
    /**
139
     * Returns the URL to redirect user to (for paying)
140
     * @param $cmd_total
141
     * @param $cmd_id
142
     * @return string
143
     */
144
    public function getRedirectURL($cmd_total, $cmd_id)
145
    {
146
        $authority = $this->getAuthority($cmd_total, $cmd_id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $authority is correct as $this->getAuthority($cmd_total, $cmd_id) targeting XoopsModules\Oledrion\Ga...Gateway::getAuthority() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
147
148
        return 'https://www.pecco24.com:27635/pecpaymentgateway/?au=' . $authority;
149
    }
150
151
    /**
152
     * Returns the form to use before to redirect user to the gateway
153
     *
154
     * @param  Oledrion\Commands $order Objects of type Commands
155
     * @return array  Key = element's name, Value = Element's value
156
     */
157
    public function getCheckoutFormContent($order)
158
    {
159
        $ret                = [];
160
        $ret['pin']         = $this->getParsianMid();
161
        $ret['amount']      = (int)$this->formatAmount($order->getVar('cmd_total'));
162
        $ret['orderId']     = $order->getVar('cmd_id');
163
        $ret['callbackUrl'] = OLEDRION_URL . 'gateway-notify.php?cmd_id=' . $order->getVar('cmd_id') . '&cmd_total=' . (int)$this->formatAmount($order->getVar('cmd_total'));
164
        $ret['authority']   = 0;
165
        $ret['status']      = 1;
166
167
        return $ret;
168
    }
169
170
    /**
171
     * Returns the list of countries codes used by the gateways
172
     */
173
    public function getCountriesList()
174
    {
175
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
176
177
        return \XoopsLists::getCountryList();
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    private function getDialogURL()
184
    {
185
        return 'https://www.pecco24.com:27635/pecpaymentgateway/eshopservice.asmx?wsdl';
186
    }
187
188
    /**
189
     * This method is in charge to dialog with the gateway to verify the payment's statuts
190
     *
191
     * @param  string $gatewaysLogPath The full path (and name) to the log file
192
     * @return string
193
     */
194
    public function gatewayNotify($gatewaysLogPath)
195
    {
196
        $db              = \XoopsDatabaseFactory::getDatabaseConnection();
197
        $commandsHandler = new Oledrion\CommandsHandler($db);
198
        // Get from bank
199
        $authority = $_GET['au'];
200
        $status    = $_GET['rs'];
201
        $cmd_id    = \Xmf\Request::getInt('cmd_id', 0, 'GET');
202
        $cmd_total = \Xmf\Request::getInt('cmd_total', 0, 'GET');
203
        // Set soap
204
        $url = $this->getDialogURL();
205
        if (extension_loaded('soap')) {
206
            $soapclient = new \SoapClient($url);
207
        } else {
208
            require_once __DIR__ . '/nusoap.php';
209
            $soapclient = new \SoapClient($url, 'wsdl');
0 ignored issues
show
Bug introduced by
'wsdl' of type string is incompatible with the type array|null expected by parameter $options of SoapClient::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
            $soapclient = new \SoapClient($url, /** @scrutinizer ignore-type */ 'wsdl');
Loading history...
210
        }
211
        // here we update our database
212
        $save_ok = 0;
213
        if ($authority) {
214
            $save_ok = 1;
215
        }
216
        // doing
217
        if ((0 == $status) && $save_ok) {
218
            if ((!$soapclient) || ($err = $soapclient->getError())) {
0 ignored issues
show
Unused Code introduced by
The assignment to $err is dead and can be removed.
Loading history...
introduced by
$soapclient is of type SoapClient, thus it always evaluated to true.
Loading history...
219
                // this is unsucccessfull connection
220
                $commande = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $commande is dead and can be removed.
Loading history...
221
                $commande = $commandsHandler->get($cmd_id);
222
                if (is_object($commande)) {
223
                    $commandsHandler->setOrderFailed($commande);
224
                    $user_log = 'خطا در پرداخت - خطا در ارتباط با بانک';
225
                } else {
226
                    $commandsHandler->setFraudulentOrder($commande);
227
                    $user_log = 'خطا در ارتباط با بانک - اطلاعات پرداخت شما نا معتبر است';
228
                }
229
            } else {
230
                //$status = 1;
231
                $params     = [
232
                    'pin'       => $this->getParsianMid(),
233
                    'authority' => $authority,
234
                    'status'    => $status,
235
                ];
236
                $sendParams = [$params];
237
                $res        = $soapclient->call('PinPaymentEnquiry', $sendParams);
238
                $status     = $res['status'];
239
                if (0 == $status) {
240
                    // this is a succcessfull payment
241
                    // we update our DataBase
242
                    $commande = null;
243
                    $commande = $commandsHandler->get($cmd_id);
244
                    if (is_object($commande)) {
245
                        if ($cmd_total == (int)$commande->getVar('cmd_total')) {
246
                            $commandsHandler->validateOrder($commande);
247
                            $user_log = 'پرداخت شما با موفقیت انجام شد. محصول برای شما ارسال می شود';
248
                        } else {
249
                            $commandsHandler->setFraudulentOrder($commande);
250
                            $user_log = 'اطلاعات پرداخت شما نا معتبر است';
251
                        }
252
                    }
253
                    $log .= "VERIFIED\t";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $log seems to be never defined.
Loading history...
254
                } else {
255
                    // this is a UNsucccessfull payment
256
                    // we update our DataBase
257
                    $commande = null;
258
                    $commande = $commandsHandler->get($cmd_id);
259
                    if (is_object($commande)) {
260
                        $commandsHandler->setOrderFailed($commande);
261
                        $user_log = 'خطا در پرداخت - وضعیت این پرداخت صحیح نیست';
262
                    } else {
263
                        $commandsHandler->setFraudulentOrder($commande);
264
                        $user_log = 'وضعیت این پرداخت صحیح نیست - اطلاعات پرداخت شما نا معتبر است';
265
                    }
266
                    $log .= "$status\n";
267
                }
268
            }
269
        } else {
270
            // this is a UNsucccessfull payment
271
            $commande = null;
272
            $commande = $commandsHandler->get($cmd_id);
273
            if (is_object($commande)) {
274
                $commandsHandler->setOrderFailed($commande);
275
                $user_log = 'خطا در پرداخت - این پرداخت نا معتبر است';
276
            } else {
277
                $commandsHandler->setFraudulentOrder($commande);
278
                $user_log = 'این پرداخت نا معتبر است - اطلاعات پرداخت شما نا معتبر است';
279
            }
280
            $log .= "$status\n";
281
        }
282
283
        // Ecriture dans le fichier log
284
        $fp = fopen($gatewaysLogPath, 'ab');
285
        if ($fp) {
0 ignored issues
show
introduced by
$fp is of type resource, thus it always evaluated to false.
Loading history...
286
            fwrite($fp, str_repeat('-', 120) . "\n");
287
            fwrite($fp, date('d/m/Y H:i:s') . "\n");
288
            if (isset($status)) {
289
                fwrite($fp, 'Transaction : ' . $status . "\n");
290
            }
291
            fwrite($fp, 'Result : ' . $log . "\n");
292
            fwrite($fp, 'Peyment note : ' . $user_log . "\n");
293
            fclose($fp);
294
        }
295
296
        return $user_log;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_log does not seem to be defined for all execution paths leading up to this point.
Loading history...
297
    }
298
}
299