Conditions | 25 |
Paths | 243 |
Total Lines | 195 |
Code Lines | 120 |
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 |
||
263 | public function gatewayNotify($gatewaysLogPath) |
||
264 | { |
||
265 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
266 | $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db); |
||
267 | $commandsHandler = new Oledrion\CommandsHandler($db); |
||
268 | $executionStartTime = microtime(true); |
||
269 | error_reporting(0); |
||
270 | @$xoopsLogger->activated = false; |
||
|
|||
271 | |||
272 | $log = ''; |
||
273 | $req = 'cmd=_notify-validate'; |
||
274 | $slashes = get_magic_quotes_gpc(); |
||
275 | foreach ($_POST as $key => $value) { |
||
276 | if ($slashes) { |
||
277 | $log .= "$key=" . stripslashes($value) . "\n"; |
||
278 | $value = urlencode(stripslashes($value)); |
||
279 | } else { |
||
280 | $log .= "$key=" . $value . "\n"; |
||
281 | $value = urlencode($value); |
||
282 | } |
||
283 | $req .= "&$key=$value"; |
||
284 | } |
||
285 | $url = $this->getDialogURL(); |
||
286 | $gatewayName = $this->gatewayInformation['foldername']; |
||
287 | $paypal_email = $gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'paypal_email'); |
||
288 | $paypal_money = $gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'paypal_money'); |
||
289 | $header = ''; |
||
290 | $header .= "POST /cgi-bin/webscr HTTP/1.1\r\n"; |
||
291 | $header .= "Host: $url\r\n"; |
||
292 | $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
||
293 | $header .= 'Content-Length: ' . mb_strlen($req) . "\r\n\r\n"; |
||
294 | $errno = 0; |
||
295 | $errstr = ''; |
||
296 | $fp = fsockopen("ssl://$url", 443, $errno, $errstr, 30); |
||
297 | if ($fp) { |
||
298 | fwrite($fp, "$header$req"); |
||
299 | while (!feof($fp)) { |
||
300 | $res = fgets($fp, 1024); |
||
301 | if (0 === strcmp(trim($res), 'VERIFIED')) { |
||
302 | $log .= "PAYPAL VERIFIED\n"; |
||
303 | $paypalok = true; |
||
304 | if ('COMPLETED' !== mb_strtoupper($_POST['payment_status'])) { |
||
305 | $paypalok = false; |
||
306 | } |
||
307 | if (mb_strtoupper($_POST['receiver_email']) != mb_strtoupper($paypal_email)) { |
||
308 | $paypalok = false; |
||
309 | } |
||
310 | if (mb_strtoupper($_POST['mc_currency']) != mb_strtoupper($paypal_money)) { |
||
311 | $paypalok = false; |
||
312 | } |
||
313 | if (!$_POST['custom']) { |
||
314 | $paypalok = false; |
||
315 | } |
||
316 | $montant = $_POST['mc_gross']; |
||
317 | |||
318 | //R.B. start |
||
319 | $ref = (int)$_POST['custom']; // Order number |
||
320 | $commande = null; |
||
321 | $commande = $commandsHandler->get($ref); |
||
322 | |||
323 | if (!is_object($commande)) { |
||
324 | // TODO: Why is this failing? |
||
325 | // TODO: Is there a more appropriate response code? |
||
326 | //header("HTTP/1.1 500 Internal Server Error"); |
||
327 | http_response_code(500); |
||
328 | $log .= sprintf("not_object: %d\n", $ref); |
||
329 | file_put_contents($gatewaysLogPath, $log, FILE_APPEND | LOCK_EX); |
||
330 | |||
331 | return; |
||
332 | } |
||
333 | //R.B. end |
||
334 | $pid = pcntl_fork(); |
||
335 | switch ($pid) { |
||
336 | case -1: |
||
337 | die('could not fork'); |
||
338 | break; |
||
339 | case 0: |
||
340 | // In the new (child) process |
||
341 | |||
342 | // At this point, all PayPal session variables collected, done Paypal session |
||
343 | // Rest of transaction can be processed offline to decouple site load from Paypal transaction time |
||
344 | // PayPal requires this session to return within 30 seconds, or will retry |
||
345 | $PayPalEndTime = microtime(true); |
||
346 | if ($paypalok) { |
||
347 | /* R.B. start |
||
348 | $ref = \Xmf\Request::getInt('custom', 0, 'POST'); // Numéro de la commande |
||
349 | $commande = null; |
||
350 | $commande = $commandsHandler->get($ref); |
||
351 | if (is_object($commande)) { |
||
352 | */ //R.B. end |
||
353 | |||
354 | if ($montant == $commande->getVar('cmd_total')) { |
||
355 | // Verified order |
||
356 | $email_name = sprintf('%s/%d%s', OLEDRION_UPLOAD_PATH, $commande->getVar('cmd_id'), OLEDRION_CONFIRMATION_EMAIL_FILENAME_SUFFIX); |
||
357 | if (file_exists($email_name)) { |
||
358 | $commandsHandler->validateOrder($commande); // Validation of the order and inventory update |
||
359 | $msg = []; |
||
360 | $msg = unserialize(file_get_contents($email_name)); |
||
361 | // Add Transaction ID variable to email variables for templates |
||
362 | $msg['TRANSACTION_ID'] = $_POST['txn_id']; |
||
363 | // Send confirmation email to user |
||
364 | $email_address = $commande->getVar('cmd_email'); |
||
365 | Oledrion\Utility::sendEmailFromTpl('command_client.tpl', $email_address, sprintf(_OLEDRION_THANKYOU_CMD, $xoopsConfig['sitename']), $msg); |
||
366 | // Send mail to admin |
||
367 | Oledrion\Utility::sendEmailFromTpl('command_shop.tpl', Oledrion\Utility::getEmailsFromGroup(Oledrion\Utility::getModuleOption('grp_sold')), _OLEDRION_NEW_COMMAND, $msg); |
||
368 | |||
369 | //R.B. start |
||
370 | // TODO: add transaction ID to SMS and online user invoice |
||
371 | // Update user database |
||
372 | if (file_exists(OLEDRION_DB_UPDATE_SCRIPT)) { |
||
373 | include OLEDRION_DB_UPDATE_SCRIPT; |
||
374 | $product_ids = $_POST['item_name']; |
||
375 | $products = []; |
||
376 | $products = explode(',', $product_ids); |
||
377 | foreach ($products as $item) { |
||
378 | $product_id = $item; |
||
379 | // updateDB($product_id, $user_id, $transaction_id); |
||
380 | $log .= updateDB($product_id, $_POST['receiver_email'], $_POST['txn_id']); |
||
381 | } |
||
382 | } |
||
383 | //R.B. end |
||
384 | |||
385 | if (false === @unlink($email_name)) { |
||
386 | throw new \RuntimeException('The file ' . $email_name . ' could not be deleted.'); |
||
387 | } |
||
388 | } else { |
||
389 | $duplicate_ipn = 1; |
||
390 | } |
||
391 | } else { |
||
392 | $commandsHandler->setFraudulentOrder($commande); |
||
393 | } |
||
394 | } else { |
||
395 | //R.B. start |
||
396 | // $log .= "not_object\n"; |
||
397 | // } |
||
398 | // } else { |
||
399 | //R.B. end |
||
400 | $log .= "paypal not OK\n"; |
||
401 | if (\Xmf\Request::hasVar('custom', 'POST')) { |
||
402 | // R.B. start |
||
403 | // $ref = \Xmf\Request::getInt('custom', 0, 'POST'); |
||
404 | // $commande = null; |
||
405 | // $commande = $commandsHandler->get($ref); |
||
406 | // if (is_object($commande)) { |
||
407 | //R.B. end |
||
408 | switch (mb_strtoupper($_POST['payment_status'])) { |
||
409 | case 'PENDING': |
||
410 | $commandsHandler->setOrderPending($commande); |
||
411 | break; |
||
412 | case 'FAILED': |
||
413 | $commandsHandler->setOrderFailed($commande); |
||
414 | break; |
||
415 | // R.B. } |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 | // Write to the log file |
||
420 | $logfp = fopen($gatewaysLogPath, 'ab'); |
||
421 | if ($logfp) { |
||
422 | if ($duplicate_ipn) { |
||
423 | fwrite($logfp, sprintf("Duplicate paypal IPN, order: %d\n", $commande->getVar('cmd_id'))); |
||
424 | } else { |
||
425 | fwrite($logfp, str_repeat('-', 120) . "\n"); |
||
426 | fwrite($logfp, date('d/m/Y H:i:s') . "\n"); |
||
427 | if (\Xmf\Request::hasVar('txn_id', 'POST')) { |
||
428 | fwrite($logfp, 'Transaction : ' . $_POST['txn_id'] . "\n"); |
||
429 | } |
||
430 | fwrite($logfp, 'Result : ' . $log . "\n"); |
||
431 | } |
||
432 | $executionEndTime = microtime(true); |
||
433 | $PayPalSeconds = $PayPalEndTime - $executionStartTime; |
||
434 | $TotalSeconds = $executionEndTime - $executionStartTime; |
||
435 | fwrite($logfp, "Paypal session took $PayPalSeconds, Total transaction took $TotalSeconds seconds.\n"); |
||
436 | fclose($logfp); |
||
437 | } |
||
438 | |||
439 | break; |
||
440 | default: |
||
441 | // In the main (parent) process in which the script is running |
||
442 | |||
443 | // At this point, all PayPal session variables collected, done Paypal session |
||
444 | // Rest of transaction can be proccessed offline to decouple Paypal transaction time from site load |
||
445 | // PayPal requires this session to return within 30 seconds, or will retry |
||
446 | |||
447 | return; |
||
448 | break; |
||
449 | } |
||
450 | } else { |
||
451 | $log .= "$res\n"; |
||
452 | } |
||
453 | } |
||
454 | fclose($fp); |
||
455 | } else { |
||
456 | $errtext = "Error with the fsockopen function, unable to open communication ' : ($errno) $errstr\n"; |
||
457 | file_put_contents($gatewaysLogPath, $errtext, FILE_APPEND | LOCK_EX); |
||
458 | } |
||
461 |