Skip to content

Commit

Permalink
23942_p2 - Small fix for other payment methods than DIRECT_PAY (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 authored Oct 24, 2024
1 parent a619db4 commit ed6363b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions pay-api/src/pay_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class TestConfig(_Config): # pylint: disable=too-few-public-methods
PAD_CONFIRMATION_PERIOD_IN_DAYS = 3
# Secret key for encrypting bank account
ACCOUNT_SECRET_KEY = "mysecretkeyforbank"
ALLOW_SKIP_PAYMENT = False


class ProdConfig(_Config): # pylint: disable=too-few-public-methods
Expand Down
47 changes: 29 additions & 18 deletions pay-api/src/pay_api/services/payment_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pay_api.utils.constants import EDIT_ROLE
from pay_api.utils.enums import InvoiceReferenceStatus, InvoiceStatus, LineItemStatus, PaymentMethod, PaymentStatus
from pay_api.utils.errors import Error
from pay_api.utils.util import get_str_by_path
from pay_api.utils.util import generate_transaction_number, get_str_by_path

from .base_payment_system import PaymentSystemService
from .fee_schedule import FeeSchedule
Expand Down Expand Up @@ -133,23 +133,7 @@ def create_invoice(
corp_type_code=invoice.corp_type_code,
)

# Note this flow is for DEV/TEST/SANDBOX ONLY.
if skip_payment and current_app.config.get("ALLOW_SKIP_PAYMENT") is True:
invoice.invoice_status_code = InvoiceStatus.PAID.value
invoice.paid = invoice.total
invoice.commit()
Payment.create(
payment_method=pay_service.get_payment_method_code(),
payment_system=pay_service.get_payment_system_code(),
payment_status=PaymentStatus.COMPLETED.value,
invoice_number=invoice_reference.invoice_number,
invoice_amount=invoice.total,
payment_account_id=invoice.payment_account_id,
).commit()
pay_service._release_payment(invoice) # pylint: disable=protected-access
else:
invoice.commit()
pay_service.complete_post_invoice(invoice, invoice_reference)
cls._handle_invoice(invoice, invoice_reference, pay_service, skip_payment)

invoice = Invoice.find_by_id(invoice.id, skip_auth_check=True)

Expand All @@ -169,6 +153,33 @@ def create_invoice(

return invoice.asdict(include_dynamic_fields=True)

@classmethod
def _handle_invoice(cls, invoice, invoice_reference, pay_service, skip_payment):
"""Handle invoice related operations."""
# Note this flow is for DEV/TEST/SANDBOX ONLY.
if skip_payment and current_app.config.get("ALLOW_SKIP_PAYMENT") is True:
invoice.invoice_status_code = InvoiceStatus.PAID.value
invoice.paid = invoice.total
invoice.commit()
# Invoice references for PAD etc can be created at job, not API.
if not invoice_reference:
invoice_reference = InvoiceReference.create(invoice.id, generate_transaction_number(invoice.id), None)
invoice_reference.status_code = InvoiceReferenceStatus.COMPLETED.value
invoice_reference.save()
Payment.create(
payment_method=pay_service.get_payment_method_code(),
payment_system=pay_service.get_payment_system_code(),
payment_status=PaymentStatus.COMPLETED.value,
invoice_number=invoice_reference.invoice_number,
invoice_amount=invoice.total,
payment_account_id=invoice.payment_account_id,
).commit()
pay_service._release_payment(invoice) # pylint: disable=protected-access
else:
# Normal flow of code here.
invoice.commit()
pay_service.complete_post_invoice(invoice, invoice_reference)

@classmethod
def _find_payment_account(cls, authorization):
# find payment account
Expand Down
11 changes: 11 additions & 0 deletions pay-api/tests/unit/api/test_payment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1397,3 +1397,14 @@ def test_payment_request_skip_payment(session, client, jwt, app):
assert rv.status_code == 201
assert rv.json.get("statusCode") == TransactionStatus.COMPLETED.value
assert rv.json.get("paid") == rv.json.get("total")

# Skip payment EFT invoice references are created later.
data["paymentInfo"] = {"methodOfPayment": PaymentMethod.EFT.value}
rv = client.post(
"/api/v1/payment-requests",
data=json.dumps(data),
headers=headers,
)
assert rv.status_code == 201
assert rv.json.get("statusCode") == TransactionStatus.COMPLETED.value
assert rv.json.get("paid") == rv.json.get("total")

0 comments on commit ed6363b

Please sign in to comment.