Skip to content

Commit

Permalink
Add a transaction event log for online payment.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Sep 1, 2023
1 parent 97374ee commit 4514acf
Show file tree
Hide file tree
Showing 19 changed files with 401 additions and 65 deletions.
2 changes: 2 additions & 0 deletions module/Finna/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@
'Finna\Db\Row\Search' => 'VuFind\Db\Row\RowGatewayFactory',
'Finna\Db\Row\Session' => 'Finna\Db\Row\SessionFactory',
'Finna\Db\Row\Transaction' => 'VuFind\Db\Row\RowGatewayFactory',
'Finna\Db\Row\TransactionEventLog' => 'VuFind\Db\Row\RowGatewayFactory',
'Finna\Db\Row\User' => 'Finna\Db\Row\UserFactory',
'Finna\Db\Row\UserList' => 'VuFind\Db\Row\UserListFactory',
],
Expand Down Expand Up @@ -646,6 +647,7 @@
'Finna\Db\Table\Search' => 'VuFind\Db\Table\GatewayFactory',
'Finna\Db\Table\Session' => 'VuFind\Db\Table\GatewayFactory',
'Finna\Db\Table\Transaction' => 'VuFind\Db\Table\GatewayFactory',
'Finna\Db\Table\TransactionEventLog' => 'VuFind\Db\Table\GatewayFactory',
'Finna\Db\Table\User' => 'VuFind\Db\Table\UserFactory',
'Finna\Db\Table\UserList' => 'VuFind\Db\Table\GatewayFactory',
'Finna\Db\Table\UserResource' => 'VuFind\Db\Table\GatewayFactory',
Expand Down
16 changes: 16 additions & 0 deletions module/Finna/sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ CREATE TABLE `finna_transaction` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 collate utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `finna_transaction_event_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`transaction_id` int(11) NOT NULL,
`date` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
`server_ip` varchar(255) DEFAULT '',
`server_name` varchar(255) DEFAULT '',
`request_uri` varchar(1024) DEFAULT '',
`message` varchar(255) DEFAULT '',
`data` mediumtext DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `finna_transaction_event_log_ibfk1` FOREIGN KEY (`transaction_id`) REFERENCES `finna_transaction` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 collate utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `finna_fee` (
Expand Down
41 changes: 32 additions & 9 deletions module/Finna/src/Finna/AjaxHandler/AbstractOnlinePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

use Finna\Db\Row\Transaction as TransactionRow;
use Finna\Db\Table\Transaction as TransactionTable;
use Finna\Db\Table\TransactionEventLog;
use Finna\OnlinePayment\OnlinePayment;
use Finna\OnlinePayment\Receipt;
use Laminas\Session\Container as SessionContainer;
Expand All @@ -53,6 +54,7 @@ abstract class AbstractOnlinePaymentAction extends \VuFind\AjaxHandler\AbstractB
\Laminas\Log\LoggerAwareInterface
{
use \VuFind\Log\LoggerAwareTrait;
use \Finna\OnlinePayment\OnlinePaymentEventLogTrait;

/**
* ILS connection
Expand Down Expand Up @@ -103,17 +105,25 @@ abstract class AbstractOnlinePaymentAction extends \VuFind\AjaxHandler\AbstractB
*/
protected $receipt;

/**
* Transaction event log table
*
* @var TransactionEventLog
*/
protected $eventLogTable;

/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param Connection $ils ILS connection
* @param TransactionTable $tt Transaction table
* @param UserTable $ut User table
* @param OnlinePayment $op Online payment manager
* @param SessionContainer $os Online payment session
* @param array $ds Data source configuration
* @param Receipt $rcp Receipt
* @param SessionSettings $ss Session settings
* @param Connection $ils ILS connection
* @param TransactionTable $tt Transaction table
* @param UserTable $ut User table
* @param OnlinePayment $op Online payment manager
* @param SessionContainer $os Online payment session
* @param array $ds Data source configuration
* @param Receipt $rcp Receipt
* @param TransactionEventLog $elt Transaction event log table
*/
public function __construct(
SessionSettings $ss,
Expand All @@ -123,7 +133,8 @@ public function __construct(
OnlinePayment $op,
SessionContainer $os,
array $ds,
Receipt $rcp
Receipt $rcp,
TransactionEventLog $elt
) {
$this->sessionSettings = $ss;
$this->ils = $ils;
Expand All @@ -133,6 +144,7 @@ public function __construct(
$this->onlinePaymentSession = $os;
$this->dataSourceConfig = $ds;
$this->receipt = $rcp;
$this->eventLogTable = $elt;
}

/**
Expand All @@ -153,6 +165,7 @@ protected function markFeesAsPaidForTransaction(TransactionRow $t): array
);

$t->setRegistrationFailed('patron login error');
$this->addTransactionEvent($t->id, 'Patron login failed');
return ['success' => false];
}

Expand Down Expand Up @@ -193,6 +206,7 @@ protected function markFeesAsPaidForTransaction(TransactionRow $t): array
. print_r($finesAmount, true)
);
$t->setFinesUpdated();
$this->addTransactionEvent($t->id, 'Registration with the ILS failed: fines updated');
return [
'success' => false,
'msg' => 'online_payment_registration_failed',
Expand All @@ -202,6 +216,7 @@ protected function markFeesAsPaidForTransaction(TransactionRow $t): array

try {
$this->logWarning('Transaction ' . $t->transaction_id . ': start marking fees as paid.');
$this->addTransactionEvent($t->id, 'Started registration with the ILS');
$res = $this->ils->markFeesAsPaid(
$patron,
$t->amount,
Expand All @@ -220,12 +235,19 @@ protected function markFeesAsPaidForTransaction(TransactionRow $t): array
);
if ('fines_updated' === $res) {
$t->setFinesUpdated();
$this->addTransactionEvent($t->id, 'Registration with the ILS failed: fines updated');
} else {
$t->setRegistrationFailed('Failed to mark fees paid: ' . ($res ?: 'no error information'));
$this->addTransactionEvent(
$t->id,
'Registration with the ILS failed: ' . ($res ?: 'no error information')
);
}
return ['success' => false, 'msg' => 'markFeesAsPaid failed'];
}
$t->setRegistered();
$this->addTransactionEvent($t->id, 'Successfully registered with the ILS');

$this->onlinePaymentSession->paymentOk = true;
} catch (\Exception $e) {
$this->logError(
Expand All @@ -235,6 +257,7 @@ protected function markFeesAsPaidForTransaction(TransactionRow $t): array
$this->logException($e);

$t->setRegistrationFailed($e->getMessage());
$this->addTransactionEvent($t->id, 'Registration with the ILS failed', ['error' => $e->getMessage()]);
return ['success' => false, 'msg' => $e->getMessage()];
}
return ['success' => true];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function __invoke(
$container->get(\Finna\OnlinePayment\OnlinePayment::class),
$container->get('Finna\OnlinePayment\Session'),
$container->get(\VuFind\Config\PluginManager::class)->get('datasources')->toArray(),
$container->get(\Finna\OnlinePayment\Receipt::class)
$container->get(\Finna\OnlinePayment\Receipt::class),
$tablePluginManager->get(\Finna\Db\Table\TransactionEventLog::class),
);
$result->setLogger($container->get(\VuFind\Log\Logger::class));
return $result;
Expand Down
9 changes: 8 additions & 1 deletion module/Finna/src/Finna/AjaxHandler/OnlinePaymentNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public function handleRequest(Params $params)
return $this->formatResponse('', self::STATUS_HTTP_BAD_REQUEST);
}

$this->addTransactionEvent($t->id, 'Notify handler called');

if ($t->isRegistered()) {
$this->addTransactionEvent($t->id, 'Transaction already registered');
// Already registered, treat as success:
return $this->formatResponse('');
}
Expand Down Expand Up @@ -125,7 +128,11 @@ public function handleRequest(Params $params)
$this->ils->getMyProfile($patron)
);
try {
$this->receipt->sendEmail($user, $patronProfile, $t);
$res = $this->receipt->sendEmail($user, $patronProfile, $t);
$this->addTransactionEvent(
$t->id,
$res ? 'Receipt sent' : 'Receipt not sent (no email address)'
);
} catch (\Exception $e) {
$this->logger->err("Failed to send email receipt for $transactionId: " . (string)$e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@

namespace Finna\Controller;

use Laminas\Stdlib\Parameters;

use function count;
use function is_callable;

/**
* Online payment controller trait.
Expand Down Expand Up @@ -340,6 +337,7 @@ protected function handleOnlinePayment($patron, $fines, $view)
'Online payment response handler called. Request: '
. (string)$request
);
$this->addTransactionEvent($transaction->id, 'Response handler called');

if ($transaction->isRegistered()) {
// Already registered, treat as success:
Expand All @@ -366,7 +364,11 @@ protected function handleOnlinePayment($patron, $fines, $view)
$catalog->getMyProfile($patron)
);
$receipt = $this->serviceLocator->get(\Finna\OnlinePayment\Receipt::class);
$receipt->sendEmail($user, $patronProfile, $transaction);
$res = $receipt->sendEmail($user, $patronProfile, $transaction);
$this->addTransactionEvent(
$transaction->id,
$res ? 'Receipt sent' : 'Receipt not sent (no email address)'
);
}
// Reload transaction and check if registration is still pending:
$transaction = $trTable->getTransaction($transactionId);
Expand All @@ -376,6 +378,7 @@ protected function handleOnlinePayment($patron, $fines, $view)
$view->registerPaymentParams = [
'transactionId' => $transaction->transaction_id,
];
$this->addTransactionEvent($transaction->id, 'Registration requested');
}
} elseif ($paymentHandler::PAYMENT_CANCEL === $result) {
$this->flashMessenger()
Expand Down Expand Up @@ -484,21 +487,18 @@ protected function handleDebugMsg($msg)
}

/**
* Log exception.
* Add an event log entry for a transaction
*
* @param Exception $e Exception
* @param int $id Transaction ID
* @param string $status Status message
* @param array $data Additional data
*
* @return void
*/
protected function handleException($e)
protected function addTransactionEvent(int $id, string $status, array $data = []): void
{
$this->ensureLogger();
if (PHP_SAPI !== 'cli') {
if ($this->logger instanceof \VuFind\Log\Logger) {
$this->logger->logException($e, new Parameters());
}
} elseif (is_callable([$this, 'logException'])) {
$this->logException($e);
}
$eventTable = $this->getTable(\Finna\Db\Table\TransactionEventLog::class);
$data += ['source' => static::class];
$eventTable->addEvent($id, $status, $data);
}
}
60 changes: 60 additions & 0 deletions module/Finna/src/Finna/Db/Row/TransactionEventLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Row definition for online payment transaction event log
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Db_Table
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/

namespace Finna\Db\Row;

/**
* Row definition for online payment transaction event log
*
* @category VuFind
* @package Db_Table
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*
* @property int $id
* @property int $finna_transaction_id
* @property string $date
* @property string $status
* @property string $data
*/
class TransactionEventLog extends \VuFind\Db\Row\RowGateway implements \VuFind\Db\Table\DbTableAwareInterface
{
use \VuFind\Db\Table\DbTableAwareTrait;

/**
* Constructor
*
* @param \Laminas\Db\Adapter\Adapter $adapter Database adapter
*/
public function __construct($adapter)
{
parent::__construct('id', 'finna_transaction_event_log', $adapter);
}
}
Loading

0 comments on commit 4514acf

Please sign in to comment.