/** * Register payment provided in the request. * This is called by JSON_Transaction. * * @param array $params Key-value list of request variables. * @param boolean $userLoggedIn Is user logged in at the time of method call. * * @return array array with keys * - 'success' (boolean) * - 'msg' (string) error message if payment could not be processed. * @access public */ public function processPayment($params, $userLoggedIn = true) { global $interface; global $user; $error = false; $msg = null; $transactionId = $params['transaction']; $tr = new Transaction(); if (!($t = $tr->getTransaction($transactionId))) { error_log("Error processing payment: transaction {$transactionId} not found"); $error = true; } if (!$tr->isTransactionInProgress($transactionId)) { error_log("Error processing payment: transaction {$transactionId} already processed."); $error = true; } if (!$error) { $patron = null; $patronId = $t->cat_username; if (!$userLoggedIn) { // MultiBackend::getConfig expects global user object and user->cat_username to be defined. $user = new User(); $user->cat_username = $patronId; $account = new User_account(); $account->user_id = $t->user_id; $account->cat_username = $t->cat_username; if ($account->find(true)) { $patron = $this->catalog->patronLogin($t->cat_username, $account->cat_password); } if (!$patron) { error_log("Error processing payment: could not perform patron login (transaction {$transactionId})"); $error = true; } } else { $patron = UserAccount::catalogLogin(); } $config = $this->catalog->getConfig('OnlinePayment'); if ($config && $config['enabled']) { $paymentHandler = CatalogConnection::getOnlinePaymentHandler($patronId); $res = $paymentHandler->processResponse($params); if (is_array($res) && isset($res['markFeesAsPaid']) && $res['markFeesAsPaid']) { $finesAmount = $this->catalog->getOnlinePayableAmount($patron); // Check that payable sum has not been updated if ($finesAmount == $res['amount']) { $paidRes = $this->catalog->markFeesAsPaid($patron, $res['amount']); if ($paidRes === true) { $t = new Transaction(); if (!$t->setTransactionRegistered($res['transactionId'])) { error_log("Error updating transaction {$transactionId} status: registered"); } $_SESSION['payment_ok'] = true; } else { $t = new Transaction(); if (!$t->setTransactionRegistrationFailed($res['transactionId'], $paidRes)) { error_log("Error updating transaction {$transactionId} status: registering failed"); } $error = true; $msg = translate($paidRes); } } else { // Payable sum updated. Skip registration and inform user that payment processing has been delayed.. $t = new Transaction(); if (!$t->setTransactionFinesUpdated($res['transactionId'])) { error_log("Error updating transaction {$transactionId} status: payable sum updated"); } $error = true; $msg = translate('online_payment_registration_failed'); } } else { $error = true; $msg = translate($res); } } } $res = array('success' => !$error); if ($msg) { $res['msg'] = $msg; } return $res; }