Skip to content

Commit

Permalink
action: implement fee paid
Browse files Browse the repository at this point in the history
This action is used to notify the ACS that a fee has been collected from the patron.

Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d committed Dec 16, 2022
1 parent b96229c commit d9ca44a
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 680 deletions.
48 changes: 43 additions & 5 deletions invenio_sip2/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from invenio_sip2.api import Message
from invenio_sip2.decorators import add_sequence_number, \
check_selfcheck_authentication
from invenio_sip2.errors import SelfcheckCirculationError
from invenio_sip2.errors import SelfcheckCirculationError, SelfcheckError
from invenio_sip2.handlers import authorize_patron_handler, checkin_handler, \
checkout_handler, enable_patron_handler, hold_handler, item_handler, \
patron_handler, patron_status_handler, renew_handler, \
checkout_handler, enable_patron_handler, fee_paid_handler, hold_handler, \
item_handler, patron_handler, patron_status_handler, renew_handler, \
selfcheck_login_handler, system_status_handler, validate_patron_handler
from invenio_sip2.models import SelfcheckSummary
from invenio_sip2.proxies import current_logger
Expand Down Expand Up @@ -530,8 +530,46 @@ def execute(self, message, client, **kwargs):
:param client: the client
:return: message class representing the response of the current action
"""
# TODO: implements action
return
patron_session = client.get_current_patron_session()
if patron_session:
language = patron_session.get('language')
else:
language = client.library_language
patron_id = message.get_field_value('patron_id')
try:
fee_paid = fee_paid_handler(
client.remote_app, client.transaction_user_id, patron_id,
message.get_fixed_field_value('fee_type'),
message.get_fixed_field_value('payment_type'),
message.get_fixed_field_value('currency_type'),
message.get_field_value('fee_amount'),
institution_id=client.institution_id,
terminal=client.terminal,
language=language
)
except SelfcheckError as error:
fee_paid = error.data
current_app.logger.error('{message}'.format(
message=error), exc_info=True)

current_logger.debug(f'[Fee paid]: handler response: {fee_paid}')

# prepare message based on required fields
response_message = self.prepare_message_response(
payment_accepted=fee_paid.is_accepted,
transaction_date=acs_system.sip2_current_date,
institution_id=client.institution_id,
patron_id=patron_id
)

# add optional fields
for optional_field in self.optional_fields:
response_message.add_field(
field=optional_field,
field_value=fee_paid.get(optional_field.name)
)

return response_message


class Hold(Action):
Expand Down
6 changes: 4 additions & 2 deletions invenio_sip2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
checkin="...",
hold="...",
renew="...",
)
),
fee_paid_handler="...",
)
)
Expand Down Expand Up @@ -1006,7 +1007,8 @@
fee_type=dict(length=2, label=_('fee type')),
payment_type=dict(length=2, label=_('payment type')),
currency_type=dict(length=3, label=_('currency type')),
payment_accepted=dict(length=1, label=_('payment accepted')),
payment_accepted=dict(length=1, label=_('payment accepted'),
callback=convert_bool_to_char),
circulation_status=dict(length=2, label=_('circulation status')),
security_marker=dict(length=2, label=_('security marker')),
language=dict(length=3, label=_('language')),
Expand Down
6 changes: 5 additions & 1 deletion invenio_sip2/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""Invenio-SIP2 exceptions."""


class SelfcheckCirculationError(Exception):
class SelfcheckError(Exception):
"""Selfcheck Circulation error."""

def __init__(self, error, data, **kwargs):
Expand All @@ -27,6 +27,10 @@ def __init__(self, error, data, **kwargs):
super().__init__(error, **kwargs)


class SelfcheckCirculationError(SelfcheckError):
"""Selfcheck Circulation error."""


class UnknownFieldIdMessageError(Exception):
"""Unknown SIP2 field id."""

Expand Down
8 changes: 8 additions & 0 deletions invenio_sip2/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def __init__(self, app):
self.patron_handlers = {}
self.item_handlers = {}
self.circulation_handlers = {}
self.fee_paid_handler = {}
self.supported_messages = {}

# register api handlers
Expand Down Expand Up @@ -383,4 +384,11 @@ def __init__(self, app):
if circulation_handlers:
self.circulation_handlers[remote] = circulation_handlers

if conf.get('fee_paid_handler'):
self.fee_paid_handler[remote] = handlers.make_api_handler(
conf.get('fee_paid_handler'),
with_data=True
)
supported_messages.add_supported_message('fee_paid')

self.supported_messages[remote] = supported_messages
8 changes: 5 additions & 3 deletions invenio_sip2/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"""Handlers for customizing sip2 api."""

from invenio_sip2.handlers.api_handlers import authorize_patron_handler, \
checkin_handler, checkout_handler, enable_patron_handler, hold_handler, \
item_handler, patron_handler, patron_status_handler, renew_handler, \
selfcheck_login_handler, system_status_handler, validate_patron_handler
checkin_handler, checkout_handler, enable_patron_handler, \
fee_paid_handler, hold_handler, item_handler, patron_handler, \
patron_status_handler, renew_handler, selfcheck_login_handler, \
system_status_handler, validate_patron_handler
from invenio_sip2.handlers.utils import make_api_handler

__all__ = (
Expand All @@ -37,4 +38,5 @@
'checkin_handler',
'hold_handler',
'renew_handler',
'fee_paid_handler'
)
17 changes: 13 additions & 4 deletions invenio_sip2/handlers/api_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"""Handlers for customizing SIP2 APIs."""

from invenio_sip2.handlers.base import base_authorize_patron_handler, \
base_circulation_handlers, base_enable_patron_handler, base_item_handler, \
base_patron_handler, base_patron_status_handler, \
base_selfcheck_login_handler, base_system_status_handler, \
base_validate_patron_handler
base_circulation_handlers, base_enable_patron_handler, \
base_fee_paid_handler, base_item_handler, base_patron_handler, \
base_patron_status_handler, base_selfcheck_login_handler, \
base_system_status_handler, base_validate_patron_handler


def selfcheck_login_handler(remote, login, password, **kwargs):
Expand Down Expand Up @@ -91,3 +91,12 @@ def renew_handler(remote, user_id, item_identifier,
"""Handle renew an item functionality."""
return base_circulation_handlers(remote, 'renew', user_id,
item_identifier, *args, **kwargs)


def fee_paid_handler(remote, user_id, patron_identifier, fee_type,
payment_type, currency_type, fee_amount, *args, **kwargs):
"""Handle fee paid functionality."""
return base_fee_paid_handler(
remote, user_id, patron_identifier, fee_type, payment_type,
currency_type, fee_amount, *args, **kwargs
)
33 changes: 28 additions & 5 deletions invenio_sip2/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,39 @@ def base_item_handler(remote, item_identifier, **kwargs):
return handlers['item'](item_identifier, **kwargs)


def base_circulation_handlers(remote, handler, user_id, item_identifier,
*args, **kwargs):
def base_circulation_handlers(remote, handler, user_id,
item_or_patron_identifier, *args, **kwargs):
"""Handle checkout functionality.
:param remote: remote ils
:param handler: circulation handler (e.g.: checkin, checkout,...)
:param user_id: Identifier of selfcheck client user
:param item_identifier: Identifier of the item (e.g. id, barcode,...)
:param patron_identifier: Identifier of the patron (e.g. id, barcode,...)
:param item_or_patron_identifier: Identifier of the item or the patron
(e.g. id, barcode,...) regarding circulation operation
returns: Circulation handler
"""
handlers = acs_system.sip2_handlers.circulation_handlers[remote]
return handlers[handler](user_id, item_identifier, *args, **kwargs)
return handlers[handler](
user_id, item_or_patron_identifier, *args, **kwargs
)


def base_fee_paid_handler(remote, user_id, patron_identifier, fee_type,
payment_type, currency_type, fee_amount, *args,
**kwargs):
"""Handle checkout functionality.
:param remote: remote ils
:param user_id: Identifier of selfcheck client user
:param patron_identifier: Identifier of the patron (e.g. id, barcode,...)
:param fee_type: Type of the fee
:param payment_type: Type of the payment
:param currency_type: Type of the currency
:param fee_amount: Amount of the fee
returns: Circulation handler
"""
handler = acs_system.sip2_handlers.fee_paid_handler[remote]
return handler(
user_id, patron_identifier, fee_type, payment_type, currency_type,
fee_amount, *args, **kwargs
)
2 changes: 1 addition & 1 deletion invenio_sip2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get(cls, name):
try:
return getattr(cls, name)
except AttributeError:
raise UnknownFieldIdMessageError
raise UnknownFieldIdMessageError(message=name)


class MessageTypeVariableField(object):
Expand Down
34 changes: 34 additions & 0 deletions invenio_sip2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,32 @@ def has_magnetic_media(self):
return self.get('magnetic_media')


class SelfcheckFeePaid(dict):
"""Class representing fee paid handler response."""

def __init__(self, accepted=False, **kwargs):
"""Constructor.
:param accepted: payment operation is accepted or not
should be set to True if the ACS accept the payment.
should be set to 0 if the ACS did not accept the payment.
:param kwargs: optional fields
"""
# required properties
self['payment_accepted'] = accepted
self['screen_messages'] = []

# optional properties
for key, value in kwargs.items():
if value:
self[key] = value

@property
def is_accepted(self):
"""Shortcut for payment_accepted operation."""
return self.get('payment_accepted', False)


class SelfcheckLanguage(Enum):
"""Enum class to list all available language."""

Expand Down Expand Up @@ -547,6 +573,14 @@ class SelfcheckFeeType(object):
HOLD_FEE = '09'


class SelfcheckPaymentType(object):
"""Class to handle all available payment type."""

CASH = '00'
VISA = '01'
CREDIT_CARD = '02'


class SelfcheckMediaType(object):
"""Class to handle all available media type."""

Expand Down
Loading

0 comments on commit d9ca44a

Please sign in to comment.