Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25571 - Send queue message for reversal #1883

Merged
merged 32 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1ebddd1
pay-api changes
seeker25 Jan 29, 2025
7b9d6ef
changes for services, need to update refs
seeker25 Jan 29, 2025
4ddd0b0
rename
seeker25 Jan 29, 2025
478bf59
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 25571
seeker25 Feb 11, 2025
2b9772e
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 25571
seeker25 Feb 12, 2025
be597bd
comment update
seeker25 Feb 12, 2025
1a42a51
put back queue message
seeker25 Feb 12, 2025
bcb4872
clean up
seeker25 Feb 12, 2025
38f9347
remove comment
seeker25 Feb 12, 2025
79fb67a
fix jobs
seeker25 Feb 12, 2025
09d453a
fix again
seeker25 Feb 12, 2025
9336cf8
fix
seeker25 Feb 12, 2025
3329e93
lint fix
seeker25 Feb 12, 2025
82af607
comment
seeker25 Feb 12, 2025
381ace5
Add in queue release for refunds.
seeker25 Feb 13, 2025
ff9f6eb
Add in extra condition
seeker25 Feb 13, 2025
c4c70f3
Add in flags that should be toggled by default, small assert
seeker25 Feb 13, 2025
ebf9a33
move out queue message stuff
seeker25 Feb 13, 2025
e798ea8
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 25571
seeker25 Feb 14, 2025
5b95e2f
fixes
seeker25 Feb 14, 2025
9cffba1
update flask-jwt-oidc
seeker25 Feb 14, 2025
0205c30
fix flags
seeker25 Feb 14, 2025
092602f
unit test for pay-queue
seeker25 Feb 14, 2025
bd0b2ad
Unit test for jobs
seeker25 Feb 14, 2025
9c20060
Handle refund invoice release queue message
seeker25 Feb 14, 2025
e338dd9
Add in unit test and mock for bcol
seeker25 Feb 14, 2025
3810f6c
Fix assert_called
seeker25 Feb 14, 2025
e5aa55b
Fix unit test update libraries
seeker25 Feb 14, 2025
52ed09e
lint issue
seeker25 Feb 14, 2025
dfb7025
Fix unit tests
seeker25 Feb 14, 2025
fb6c85e
fixes
seeker25 Feb 14, 2025
5850f78
fix unit test
seeker25 Feb 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 45 additions & 41 deletions jobs/payment-jobs/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jobs/payment-jobs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
pay-api = {git = "https://github.com/ochiu/sbc-pay.git", branch = "25334-EFT-Statement-Payment", subdirectory = "pay-api"}
pay-api = {git = "https://github.com/seeker25/sbc-pay.git", branch = "25571", subdirectory = "pay-api"}
flask = "^3.0.2"
flask-sqlalchemy = "^3.1.1"
sqlalchemy = "^2.0.28"
Expand Down
4 changes: 3 additions & 1 deletion jobs/payment-jobs/tasks/bcol_refund_confirmation_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

from flask import current_app
from pay_api.models import Invoice, InvoiceReference, Payment, db
from pay_api.utils.enums import InvoiceStatus, PaymentSystem
from pay_api.services.bcol_service import BcolService
from pay_api.utils.enums import InvoiceStatus, PaymentSystem, TransactionStatus
from sentry_sdk import capture_message
from sqlalchemy import text

Expand Down Expand Up @@ -121,4 +122,5 @@ def _compare_and_update_records(cls, invoice_refs: List[InvoiceReference], bcol_
invoice.invoice_status_code = InvoiceStatus.REFUNDED.value
invoice.refund_date = datetime.now(tz=timezone.utc)
db.session.add(invoice)
BcolService().release_payment_or_reversal(invoice, TransactionStatus.REVERSED.value)
db.session.commit()
2 changes: 2 additions & 0 deletions jobs/payment-jobs/tasks/direct_pay_automated_refund_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
InvoiceStatus,
PaymentMethod,
PaymentStatus,
TransactionStatus,
)
from sentry_sdk import capture_message

Expand Down Expand Up @@ -239,6 +240,7 @@ def _set_invoice_and_payment_to_refunded(invoice: Invoice):
payment = PaymentModel.find_payment_for_invoice(invoice.id)
payment.payment_status_code = PaymentStatus.REFUNDED.value
payment.save()
DirectPayService().release_payment_or_reversal(invoice, TransactionStatus.REVERSED.value)

@classmethod
def _set_refund_partials_posted(cls, invoice: Invoice):
Expand Down
2 changes: 2 additions & 0 deletions jobs/payment-jobs/tasks/eft_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
PaymentStatus,
PaymentSystem,
ReverseOperation,
TransactionStatus,
)
from sentry_sdk import capture_message
from sqlalchemy import func
Expand Down Expand Up @@ -449,3 +450,4 @@ def _handle_invoice_refund(cls, invoice: InvoiceModel, invoice_reference: Invoic
invoice.refund_date = datetime.now(tz=timezone.utc)
invoice.refund = invoice.total
invoice.flush()
EftService().release_payment_or_reversal(invoice, TransactionStatus.REVERSED.value)
seeker25 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions pay-api/src/pay_api/resources/v1/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_eft_accounts():
return jsonify(response), status


# NOTE: STRR and BAR use this route to check the PAD status and/or payment method, be careful when changing.
@bp.route("/<string:account_number>", methods=["GET", "OPTIONS"])
@cross_origin(origins="*", methods=["GET", "PUT", "DELETE"])
@_jwt.requires_auth
Expand Down
14 changes: 10 additions & 4 deletions pay-api/src/pay_api/services/base_payment_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pay_api.models.refunds_partial import RefundPartialLine
from pay_api.services import gcp_queue_publisher
from pay_api.services.cfs_service import CFSService
from pay_api.services.flags import flags
from pay_api.services.invoice import Invoice
from pay_api.services.invoice_reference import InvoiceReference
from pay_api.services.payment import Payment
Expand Down Expand Up @@ -195,7 +196,7 @@ def ensure_no_payment_blockers(self, payment_account: PaymentAccount) -> None:
raise BusinessException(Error.EFT_INVOICES_OVERDUE)

@staticmethod
def _release_payment(invoice: Invoice):
def release_payment_or_reversal(invoice: Invoice, transaction_status=TransactionStatus.COMPLETED.value):
"""Release record."""
from .payment_transaction import PaymentTransaction # pylint:disable=import-outside-toplevel,cyclic-import

Expand All @@ -207,9 +208,14 @@ def _release_payment(invoice: Invoice):
]:
return

payload = PaymentTransaction.create_event_payload(invoice, TransactionStatus.COMPLETED.value)
if transaction_status == TransactionStatus.REVERSED.value and not flags.is_on(
"queue-message-for-reversals", default=False
):
return

payload = PaymentTransaction.create_event_payload(invoice, transaction_status)
try:
current_app.logger.info(f"Releasing record for invoice {invoice.id}")
current_app.logger.info(f"Releasing record for invoice {invoice.id} with status {transaction_status}")
gcp_queue_publisher.publish_to_queue(
gcp_queue_publisher.QueueMessage(
source=QueueSources.PAY_API.value,
Expand Down Expand Up @@ -370,7 +376,7 @@ def wrapper(*func_args, **func_kwargs):
current_app.logger.info("Completing the payment as sandbox token is detected.")
instance: PaymentSystemService = func_args[0]
instance.complete_payment(func_args[1], func_args[2]) # invoice and invoice ref
instance._release_payment(func_args[1]) # pylint: disable=protected-access
instance.release_payment_or_reversal(func_args[1])
return None
return function(*func_args, **func_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/bcol_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ def complete_post_invoice(
"""Complete any post payment activities if needed."""
self.complete_payment(invoice, invoice_reference)
# Publish message to the queue with payment token, so that they can release records on their side.
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/eft_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def create_invoice(
def complete_post_invoice(self, invoice: Invoice, invoice_reference: InvoiceReference) -> None:
"""Complete any post invoice activities if needed."""
# Publish message to the queue with payment token, so that they can release records on their side.
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)

def get_default_invoice_status(self) -> str:
"""Return the default status for invoice when created."""
Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/ejv_pay_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def complete_post_invoice(self, invoice: Invoice, invoice_reference: InvoiceRefe
self.complete_payment(invoice, invoice_reference)

# Publish message to the queue with payment token, so that they can release records on their side.
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)

def process_cfs_refund(
self,
Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/internal_pay_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def complete_post_invoice(self, invoice: Invoice, invoice_reference: InvoiceRefe
if invoice.invoice_status_code != InvoiceStatus.APPROVED.value:
self.complete_payment(invoice, invoice_reference)
# Publish message to the queue with payment token, so that they can release records on their side.
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)

def get_default_invoice_status(self) -> str:
"""Return the default status for invoice when created."""
Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/online_banking_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_receipt(

def apply_credit(self, invoice: Invoice) -> None:
"""Apply credit to the invoice."""
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)

def cancel_invoice(self, payment_account: PaymentAccount, inv_number: str):
"""Adjust the invoice to zero."""
Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/pad_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def complete_post_invoice(
) -> None:
"""Complete any post invoice activities if needed."""
# Publish message to the queue with payment token, so that they can release records on their side.
self._release_payment(invoice=invoice)
self.release_payment_or_reversal(invoice=invoice)

def process_cfs_refund(
self,
Expand Down
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/services/payment_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _handle_invoice(cls, invoice, invoice_reference, pay_service, skip_payment):
invoice_amount=invoice.total,
payment_account_id=invoice.payment_account_id,
).commit()
pay_service._release_payment(invoice) # pylint: disable=protected-access
pay_service.release_payment_or_reversal(invoice) # pylint: disable=protected-access
else:
# Normal flow of code here.
invoice.commit()
Expand Down
Loading
Loading