From 49f57eb19d37f154ea3bab57d6ea9c5d687e3922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 27 Feb 2018 22:09:15 +0100 Subject: [PATCH] MDL-61741 enrol_paypal: Prevent some edge-case false errors While testing the plugin in various scenarios, I've noticed that there are two situations which the current code does not handle quite well: * Detecting for existing txn_id record was likely to throw "found more than one record" debugging message. Fixed by explicitly ignoring multiple records. * The incoming IPN verification message does not always contain the recipient's email in the "business" field. When a payment has to be claimed manually at PayPal (e.g. as a result of currencies mismatch and other settings), the email is coming only in the "receiver_email" field. This led to PHP notice and caused the script to fail falsely. --- enrol/paypal/ipn.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/enrol/paypal/ipn.php b/enrol/paypal/ipn.php index 4b6117e0c12..a686f138fa2 100644 --- a/enrol/paypal/ipn.php +++ b/enrol/paypal/ipn.php @@ -180,19 +180,25 @@ if (strlen($result) > 0) { // At this point we only proceed with a status of completed or pending with a reason of echeck - - - if ($existing = $DB->get_record("enrol_paypal", array("txn_id"=>$data->txn_id))) { // Make sure this transaction doesn't exist already + // Make sure this transaction doesn't exist already. + if ($existing = $DB->get_record("enrol_paypal", array("txn_id" => $data->txn_id), "*", IGNORE_MULTIPLE)) { \enrol_paypal\util::message_paypal_error_to_admin("Transaction $data->txn_id is being repeated!", $data); die; - } - if (core_text::strtolower($data->business) !== core_text::strtolower($plugin->get_config('paypalbusiness'))) { // Check that the email is the one we want it to be - \enrol_paypal\util::message_paypal_error_to_admin("Business email is {$data->business} (not ". + // Check that the receiver email is the one we want it to be. + if (isset($data->business)) { + $recipient = $data->business; + } else if (isset($data->receiver_email)) { + $recipient = $data->receiver_email; + } else { + $recipient = 'empty'; + } + + if (core_text::strtolower($recipient) !== core_text::strtolower($plugin->get_config('paypalbusiness'))) { + \enrol_paypal\util::message_paypal_error_to_admin("Business email is {$recipient} (not ". $plugin->get_config('paypalbusiness').")", $data); die; - } if (!$user = $DB->get_record('user', array('id'=>$data->userid))) { // Check that user exists