From d1a7fea2edff3099c2424e1f3ba70b1a27a33483 Mon Sep 17 00:00:00 2001 From: Travis Semple Date: Wed, 8 Nov 2023 13:56:15 -0800 Subject: [PATCH] Add extra step to set transactions to REFUNDED if billable = 'f' (#1321) --- pay-api/src/pay_api/services/base_payment_system.py | 3 ++- pay-api/src/pay_api/services/bcol_service.py | 3 ++- pay-api/src/pay_api/services/direct_pay_service.py | 3 ++- pay-api/src/pay_api/services/ejv_pay_service.py | 5 ++++- pay-api/src/pay_api/services/internal_pay_service.py | 3 ++- pay-api/src/pay_api/services/online_banking_service.py | 3 ++- pay-api/src/pay_api/services/pad_service.py | 3 ++- pay-api/src/pay_api/services/paybc_service.py | 3 ++- pay-api/src/pay_api/services/refund.py | 4 +++- pay-api/tests/unit/services/test_direct_pay_service.py | 8 ++++---- pay-api/tests/unit/services/test_payment_service.py | 5 ++++- 11 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pay-api/src/pay_api/services/base_payment_system.py b/pay-api/src/pay_api/services/base_payment_system.py index b8d9a43b0..1ad861ff7 100644 --- a/pay-api/src/pay_api/services/base_payment_system.py +++ b/pay-api/src/pay_api/services/base_payment_system.py @@ -102,7 +102,8 @@ def get_payment_system_url_for_payment(self, payment: Payment, # pylint:disable """Return the payment system portal URL for payment.""" return None - def process_cfs_refund(self, invoice: InvoiceModel): # pylint:disable=unused-argument + def process_cfs_refund(self, invoice: InvoiceModel, # pylint:disable=unused-argument + payment_account: PaymentAccount): # pylint:disable=unused-argument """Process Refund if any.""" return None diff --git a/pay-api/src/pay_api/services/bcol_service.py b/pay-api/src/pay_api/services/bcol_service.py index 6ead40d6c..c4739125f 100644 --- a/pay-api/src/pay_api/services/bcol_service.py +++ b/pay-api/src/pay_api/services/bcol_service.py @@ -147,7 +147,8 @@ def get_payment_method_code(self): """Return CC as the method code.""" return PaymentMethod.DRAWDOWN.value - def process_cfs_refund(self, invoice: InvoiceModel): + def process_cfs_refund(self, invoice: InvoiceModel, + payment_account: PaymentAccount): # pylint:disable=unused-argument """Process refund in CFS.""" self._publish_refund_to_mailer(invoice) payment: PaymentModel = PaymentModel.find_payment_for_invoice(invoice.id) diff --git a/pay-api/src/pay_api/services/direct_pay_service.py b/pay-api/src/pay_api/services/direct_pay_service.py index 66ac13473..88a2301f4 100644 --- a/pay-api/src/pay_api/services/direct_pay_service.py +++ b/pay-api/src/pay_api/services/direct_pay_service.py @@ -140,7 +140,8 @@ def get_pay_system_reason_code(self, pay_response_url: str) -> str: # pylint:di return pay_system_reason_code return None - def process_cfs_refund(self, invoice: InvoiceModel): + def process_cfs_refund(self, invoice: InvoiceModel, + payment_account: PaymentAccount): # pylint:disable=unused-argument """Process refund in CFS.""" current_app.logger.debug(' str: # pylint:disable=unused-argument + def process_cfs_refund(self, invoice: InvoiceModel, + payment_account: PaymentAccount) -> str: # pylint:disable=unused-argument """Do nothing to process refund; as the refund is handled by CRON job. Return the status after checking invoice status. @@ -76,6 +77,8 @@ def process_cfs_refund(self, invoice: InvoiceModel) -> str: # pylint:disable=un 2.1 Return REFUND_REQUESTED """ current_app.logger.info(f'Received JV refund for invoice {invoice.id}, {invoice.invoice_status_code}') + if not payment_account.billable: + return InvoiceStatus.REFUNDED.value if invoice.invoice_status_code == InvoiceStatus.APPROVED.value: if InvoiceReference.find_active_reference_by_invoice_id(invoice.id): return InvoiceStatus.REFUND_REQUESTED.value diff --git a/pay-api/src/pay_api/services/internal_pay_service.py b/pay-api/src/pay_api/services/internal_pay_service.py index 4e581d973..8bd9fd7b9 100644 --- a/pay-api/src/pay_api/services/internal_pay_service.py +++ b/pay-api/src/pay_api/services/internal_pay_service.py @@ -90,7 +90,8 @@ def get_default_invoice_status(self) -> str: """Return the default status for invoice when created.""" return InvoiceStatus.APPROVED.value - def process_cfs_refund(self, invoice: InvoiceModel): + def process_cfs_refund(self, invoice: InvoiceModel, + payment_account: PaymentAccount): # pylint:disable=unused-argument """Process refund in CFS.""" if invoice.total == 0: raise BusinessException(Error.NO_FEE_REFUND) diff --git a/pay-api/src/pay_api/services/online_banking_service.py b/pay-api/src/pay_api/services/online_banking_service.py index ca9dbf09f..85bbec726 100644 --- a/pay-api/src/pay_api/services/online_banking_service.py +++ b/pay-api/src/pay_api/services/online_banking_service.py @@ -66,6 +66,7 @@ def cancel_invoice(self, payment_account: PaymentAccount, inv_number: str): current_app.logger.debug(' Di pay_system_service: PaymentSystemService = PaymentSystemFactory.create_from_payment_method( payment_method=invoice.payment_method_code ) - invoice_status = pay_system_service.process_cfs_refund(invoice) + payment_account = PaymentAccount.find_by_id(invoice.payment_account_id) + invoice_status = pay_system_service.process_cfs_refund(invoice, payment_account=payment_account) refund.flush() message = REFUND_SUCCESS_MESSAGES.get(f'{invoice.payment_method_code}.{invoice.invoice_status_code}') # set invoice status diff --git a/pay-api/tests/unit/services/test_direct_pay_service.py b/pay-api/tests/unit/services/test_direct_pay_service.py index c1e591078..f2e57687c 100644 --- a/pay-api/tests/unit/services/test_direct_pay_service.py +++ b/pay-api/tests/unit/services/test_direct_pay_service.py @@ -192,7 +192,7 @@ def test_process_cfs_refund_success(monkeypatch): direct_pay_service = DirectPayService() - direct_pay_service.process_cfs_refund(invoice) + direct_pay_service.process_cfs_refund(invoice, payment_account) assert True @@ -208,7 +208,7 @@ def test_process_cfs_refund_bad_request(): invoice.save() direct_pay_service = DirectPayService() with pytest.raises(BusinessException) as excinfo: - direct_pay_service.process_cfs_refund(invoice) + direct_pay_service.process_cfs_refund(invoice, payment_account) assert excinfo.value.code == Error.INVALID_REQUEST.name @@ -241,7 +241,7 @@ def test_process_cfs_refund_duplicate_refund(monkeypatch): ] } with pytest.raises(HTTPError) as excinfo: - direct_pay_service.process_cfs_refund(invoice) + direct_pay_service.process_cfs_refund(invoice, payment_account) assert invoice.invoice_status_code == InvoiceStatus.PAID.value with patch('pay_api.services.oauth_service.requests.post') as mock_post: @@ -257,5 +257,5 @@ def test_process_cfs_refund_duplicate_refund(monkeypatch): 'txnNumber': 'REGT00005433' } with pytest.raises(BusinessException) as excinfo: - direct_pay_service.process_cfs_refund(invoice) + direct_pay_service.process_cfs_refund(invoice, payment_account) assert excinfo.value.code == Error.DIRECT_PAY_INVALID_RESPONSE.name diff --git a/pay-api/tests/unit/services/test_payment_service.py b/pay-api/tests/unit/services/test_payment_service.py index 90e4a0667..87d2121b6 100644 --- a/pay-api/tests/unit/services/test_payment_service.py +++ b/pay-api/tests/unit/services/test_payment_service.py @@ -24,6 +24,7 @@ from pay_api.models import RoutingSlip as RoutingSlipModel from pay_api.services import CFSService from pay_api.services.internal_pay_service import InternalPayService +from pay_api.services.payment_account import PaymentAccount as PaymentAccountService from pay_api.services.payment_service import PaymentService from pay_api.utils.enums import InvoiceStatus, PaymentMethod, PaymentStatus, RoutingSlipStatus from requests.exceptions import ConnectionError, ConnectTimeout, HTTPError @@ -388,6 +389,8 @@ def test_internal_rs_back_active(session, public_user_mock): assert rs.status == RoutingSlipStatus.COMPLETE.name invoice = Invoice.find_by_id(invoice['id']) - InternalPayService().process_cfs_refund(invoice) + payment_account = PaymentAccountService() + payment_account._dao = account_model # pylint: disable=protected-access + InternalPayService().process_cfs_refund(invoice, payment_account) assert rs.status == RoutingSlipStatus.ACTIVE.name