146 lines
5.4 KiB
Python
146 lines
5.4 KiB
Python
"""Wave 0 Deterministic Payment and Bank Account Mock Adapter for Iteration 05.
|
|
|
|
Pre-seeds bank accounts, simulates UTR/instrument verification,
|
|
and provides deterministic triggers for clearance and cheque bounce events.
|
|
Fulfills UP-AI v1.5 Wave 0 requirements for Financial Settlement (UC-011, UC-012, UC-013).
|
|
"""
|
|
from datetime import datetime, timezone
|
|
from decimal import Decimal, ROUND_HALF_UP
|
|
from typing import Dict, Any, List, Optional
|
|
import uuid
|
|
|
|
|
|
BANK_ACCOUNTS = [
|
|
{
|
|
"id": "ba-sbi-001",
|
|
"account_code": "BANK-SBI-001",
|
|
"account_name": "State Bank of India — Neemuch Mandi Branch",
|
|
"account_type": "BANK",
|
|
"bank_name": "State Bank of India",
|
|
"account_number": "30918274619",
|
|
"current_balance_inr": Decimal("15000000.00"),
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "ba-hdfc-001",
|
|
"account_code": "BANK-HDFC-001",
|
|
"account_name": "HDFC Bank — Indore Operations Branch",
|
|
"account_type": "BANK",
|
|
"bank_name": "HDFC Bank",
|
|
"account_number": "50200081726354",
|
|
"current_balance_inr": Decimal("8500000.00"),
|
|
"is_active": True,
|
|
},
|
|
{
|
|
"id": "ca-cash-001",
|
|
"account_code": "CASH-MAIN-001",
|
|
"account_name": "Mandi Yard Physical Cash Chest",
|
|
"account_type": "CASH",
|
|
"bank_name": None,
|
|
"account_number": None,
|
|
"current_balance_inr": Decimal("500000.00"),
|
|
"is_active": True,
|
|
},
|
|
]
|
|
|
|
|
|
class PaymentMockAdapter:
|
|
"""In-memory deterministic payment gateway and bank account mock."""
|
|
|
|
def __init__(self, accounts: Optional[List[Dict[str, Any]]] = None):
|
|
self._accounts: Dict[str, Dict[str, Any]] = {}
|
|
source = accounts or BANK_ACCOUNTS
|
|
for acc in source:
|
|
self._accounts[acc["id"]] = dict(acc)
|
|
# Index by account code as well
|
|
self._accounts[acc["account_code"]] = self._accounts[acc["id"]]
|
|
|
|
def get_account(self, account_identifier: str) -> Optional[Dict[str, Any]]:
|
|
"""Fetch bank/cash account by ID or account code."""
|
|
return self._accounts.get(account_identifier)
|
|
|
|
def list_accounts(self) -> List[Dict[str, Any]]:
|
|
"""List unique bank/cash accounts."""
|
|
seen = set()
|
|
result = []
|
|
for acc in self._accounts.values():
|
|
if acc["id"] not in seen:
|
|
seen.add(acc["id"])
|
|
result.append(dict(acc))
|
|
return result
|
|
|
|
def credit_account(self, account_identifier: str, amount_inr: Decimal) -> Dict[str, Any]:
|
|
"""Credit funds into account (e.g. from customer receipt)."""
|
|
acc = self.get_account(account_identifier)
|
|
if not acc:
|
|
raise ValueError(f"Account {account_identifier} not found")
|
|
acc["current_balance_inr"] = (acc["current_balance_inr"] + amount_inr).quantize(
|
|
Decimal("0.01"), rounding=ROUND_HALF_UP
|
|
)
|
|
return dict(acc)
|
|
|
|
def debit_account(self, account_identifier: str, amount_inr: Decimal) -> Dict[str, Any]:
|
|
"""Debit funds from account (e.g. for supplier disbursement or expense)."""
|
|
acc = self.get_account(account_identifier)
|
|
if not acc:
|
|
raise ValueError(f"Account {account_identifier} not found")
|
|
if acc["current_balance_inr"] < amount_inr:
|
|
raise ValueError(
|
|
f"Insufficient funds in account {account_identifier}. "
|
|
f"Available: INR {acc['current_balance_inr']}, Required: INR {amount_inr}"
|
|
)
|
|
acc["current_balance_inr"] = (acc["current_balance_inr"] - amount_inr).quantize(
|
|
Decimal("0.01"), rounding=ROUND_HALF_UP
|
|
)
|
|
return dict(acc)
|
|
|
|
def verify_instrument(self, payment_mode: str, instrument_ref: str) -> Dict[str, Any]:
|
|
"""Validates payment instrument format deterministically."""
|
|
mode = payment_mode.upper()
|
|
if mode in ("NEFT", "RTGS"):
|
|
# Standard Indian UTR is alphanumeric, usually 16 or 22 characters
|
|
is_valid = len(instrument_ref.strip()) >= 8
|
|
return {
|
|
"valid": is_valid,
|
|
"mode": mode,
|
|
"instrument_ref": instrument_ref,
|
|
"status": "VERIFIED" if is_valid else "INVALID_UTR_FORMAT",
|
|
}
|
|
elif mode == "CHEQUE":
|
|
# 6-digit cheque number
|
|
is_valid = len(instrument_ref.strip()) >= 6
|
|
return {
|
|
"valid": is_valid,
|
|
"mode": mode,
|
|
"instrument_ref": instrument_ref,
|
|
"status": "VERIFIED" if is_valid else "INVALID_CHEQUE_FORMAT",
|
|
}
|
|
elif mode in ("UPI", "CASH", "BANK_TRANSFER"):
|
|
return {
|
|
"valid": True,
|
|
"mode": mode,
|
|
"instrument_ref": instrument_ref,
|
|
"status": "VERIFIED",
|
|
}
|
|
return {
|
|
"valid": False,
|
|
"mode": mode,
|
|
"instrument_ref": instrument_ref,
|
|
"status": "UNSUPPORTED_PAYMENT_MODE",
|
|
}
|
|
|
|
def simulate_cheque_bounce(
|
|
self, cheque_no: str, reason: str = "Insufficient Funds"
|
|
) -> Dict[str, Any]:
|
|
"""Simulates cheque return/bounce notification from clearing house."""
|
|
return {
|
|
"instrument_ref": cheque_no,
|
|
"clearance_status": "BOUNCED",
|
|
"bounce_reason": reason,
|
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
}
|
|
|
|
|
|
payment_mock = PaymentMockAdapter()
|
|
|