| Conditions | 23 |
| Paths | 1833 |
| Total Lines | 149 |
| Code Lines | 112 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 233 | public function gatewayNotify($gatewaysLogPath) |
||
| 234 | { |
||
| 235 | global $xoopsConfig; |
||
| 236 | $executionStartTime = microtime(true); |
||
| 237 | error_reporting(0); |
||
| 238 | @$xoopsLogger->activated = false; |
||
| 239 | |||
| 240 | $log = ''; |
||
| 241 | $req = 'cmd=_notify-validate'; |
||
| 242 | $slashes = get_magic_quotes_gpc(); |
||
| 243 | foreach ($_POST as $key => $value) { |
||
| 244 | if ($slashes) { |
||
| 245 | $log .= "$key=" . stripslashes($value) . "\n"; |
||
| 246 | $value = urlencode(stripslashes($value)); |
||
| 247 | } else { |
||
| 248 | $log .= "$key=" . $value . "\n"; |
||
| 249 | $value = urlencode($value); |
||
| 250 | } |
||
| 251 | $req .= "&$key=$value"; |
||
| 252 | } |
||
| 253 | $url = $this->getdialogURL(); |
||
| 254 | $gatewayName = $this->gatewayInformation['foldername']; |
||
| 255 | $paypal_email = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_email'); |
||
| 256 | $paypal_money = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_money'); |
||
| 257 | $header = ''; |
||
| 258 | $header .= "POST /cgi-bin/webscr HTTP/1.1\r\n"; |
||
| 259 | $header .= "Host: $url\r\n"; |
||
| 260 | $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
||
| 261 | $header .= 'Content-Length: ' . strlen($req) . "\r\n\r\n"; |
||
| 262 | $errno = 0; |
||
| 263 | $errstr = ''; |
||
| 264 | $fp = fsockopen("ssl://$url", 443, $errno, $errstr, 30); |
||
| 265 | if ($fp) { |
||
| 266 | fwrite($fp, "$header$req"); |
||
| 267 | while (!feof($fp)) { |
||
| 268 | $res = fgets($fp, 1024); |
||
| 269 | if (strcmp(trim($res), 'VERIFIED') == 0) { |
||
| 270 | $log .= "PAYPAL VERIFIED\n"; |
||
| 271 | $paypalok = true; |
||
| 272 | if (strtoupper($_POST['payment_status']) !== 'COMPLETED') { |
||
| 273 | $paypalok = false; |
||
| 274 | } |
||
| 275 | if (strtoupper($_POST['receiver_email']) != strtoupper($paypal_email)) { |
||
| 276 | $paypalok = false; |
||
| 277 | } |
||
| 278 | if (strtoupper($_POST['mc_currency']) != strtoupper($paypal_money)) { |
||
| 279 | $paypalok = false; |
||
| 280 | } |
||
| 281 | if (!$_POST['custom']) { |
||
| 282 | $paypalok = false; |
||
| 283 | } |
||
| 284 | $montant = $_POST['mc_gross']; |
||
| 285 | $pid = pcntl_fork(); |
||
| 286 | switch ($pid) { |
||
| 287 | case -1: // pcntl_fork() failed |
||
| 288 | die('could not fork'); |
||
| 289 | break; |
||
| 290 | case 0: |
||
| 291 | // In the new (child) process |
||
| 292 | // At this point, all PayPal session variables collected, done Paypal session |
||
| 293 | // Rest of transaction can be processed offline to decouple site load from Paypal transaction time |
||
| 294 | // PayPal requires this session to return within 30 seconds, or will retry |
||
| 295 | $PayPalEndTime = microtime(true); |
||
| 296 | if ($paypalok) { |
||
| 297 | $ref = (int)$_POST['custom']; // Numéro de la commande |
||
| 298 | $commande = null; |
||
| 299 | $commande = $this->handlers->h_oledrion_commands->get($ref); |
||
| 300 | if (is_object($commande)) { |
||
| 301 | if ($montant == $commande->getVar('cmd_total')) { // Commande vérifiée |
||
| 302 | $email_name = sprintf('%s/%d%s', OLEDRION_UPLOAD_PATH, $commande->getVar('cmd_id'), OLEDRION_CONFIRMATION_EMAIL_FILENAME_SUFFIX); |
||
| 303 | if (file_exists($email_name)) { |
||
| 304 | $this->handlers->h_oledrion_commands->validateOrder($commande); // Validation de la commande et mise à jour des stocks |
||
| 305 | $msg = array(); |
||
| 306 | $msg = unserialize(file_get_contents($email_name)); |
||
| 307 | // Add Transaction ID variable to email variables for templates |
||
| 308 | $msg['TRANSACTION_ID'] = $_POST['txn_id']; |
||
| 309 | // Send confirmation email to user |
||
| 310 | $email_address = $commande->getVar('cmd_email'); |
||
| 311 | OledrionUtility::sendEmailFromTpl('command_client.tpl', $email_address, sprintf(_OLEDRION_THANKYOU_CMD, $xoopsConfig['sitename']), $msg); |
||
| 312 | // Send mail to admin |
||
| 313 | OledrionUtility::sendEmailFromTpl('command_shop.tpl', OledrionUtility::getEmailsFromGroup(OledrionUtility::getModuleOption('grp_sold')), _OLEDRION_NEW_COMMAND, $msg); |
||
| 314 | unlink($email_name); |
||
| 315 | // TODO: add transaction ID to online user invoice |
||
| 316 | // TODO: update user database |
||
| 317 | } else { |
||
| 318 | $duplicate_ipn = 1; |
||
| 319 | } |
||
| 320 | } else { |
||
| 321 | $this->handlers->h_oledrion_commands->setFraudulentOrder($commande); |
||
| 322 | } |
||
| 323 | } else { |
||
| 324 | $log .= "not_object\n"; |
||
| 325 | } |
||
| 326 | } else { |
||
| 327 | $log .= "paypal not OK\n"; |
||
| 328 | if (isset($_POST['custom'])) { |
||
| 329 | $ref = (int)$_POST['custom']; |
||
| 330 | $commande = null; |
||
| 331 | $commande = $this->handlers->h_oledrion_commands->get($ref); |
||
| 332 | if (is_object($commande)) { |
||
| 333 | switch (strtoupper($_POST['payment_status'])) { |
||
| 334 | case 'PENDING': |
||
| 335 | $this->handlers->h_oledrion_commands->setOrderPending($commande); |
||
| 336 | break; |
||
| 337 | case 'FAILED': |
||
| 338 | $this->handlers->h_oledrion_commands->setOrderFailed($commande); |
||
| 339 | break; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | // Ecriture dans le fichier log |
||
| 345 | $logfp = fopen($gatewaysLogPath, 'a'); |
||
| 346 | if ($logfp) { |
||
| 347 | if ($duplicate_ipn) { |
||
| 348 | fwrite($logfp, sprintf("Duplicate paypal IPN, order: %d\n", $commande->getVar('cmd_id'))); |
||
| 349 | } else { |
||
| 350 | fwrite($logfp, str_repeat('-', 120) . "\n"); |
||
| 351 | fwrite($logfp, date('d/m/Y H:i:s') . "\n"); |
||
| 352 | if (isset($_POST['txn_id'])) { |
||
| 353 | fwrite($logfp, 'Transaction : ' . $_POST['txn_id'] . "\n"); |
||
| 354 | } |
||
| 355 | fwrite($logfp, 'Result : ' . $log . "\n"); |
||
| 356 | } |
||
| 357 | $executionEndTime = microtime(true); |
||
| 358 | $PayPalSeconds = $PayPalEndTime - $executionStartTime; |
||
| 359 | $TotalSeconds = $executionEndTime - $executionStartTime; |
||
| 360 | fwrite($logfp, "Paypal session took $PayPalSeconds, Total transaction took $TotalSeconds seconds.\n"); |
||
| 361 | fclose($logfp); |
||
| 362 | } |
||
| 363 | break; |
||
| 364 | default: |
||
| 365 | // In the main (parent) process in which the script is running |
||
| 366 | // At this point, all PayPal session variables collected, done Paypal session |
||
| 367 | // Rest of transaction can be proccessed offline to decouple Paypal transaction time from site load |
||
| 368 | // PayPal requires this session to return within 30 seconds, or will retry |
||
| 369 | return; |
||
| 370 | break; |
||
| 371 | } |
||
| 372 | } else { |
||
| 373 | $log .= "$res\n"; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | fclose($fp); |
||
| 377 | } else { |
||
| 378 | $errtext = "Error with the fsockopen function, unable to open communication ' : ($errno) $errstr\n"; |
||
| 379 | file_put_contents($gatewaysLogPath, $errtext, FILE_APPEND | LOCK_EX); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.