Files
digifi-flaskA001/api/controllers/penal.py
T
2025-03-20 13:35:44 +01:00

67 lines
2.2 KiB
Python

"""
Controller for penal charge endpoints.
"""
from flask import Blueprint, request, jsonify
from api.middleware import api_key_required
from api.models import PenalChargeRequest, PenalChargeResponse
import logging
# Configure logger
logger = logging.getLogger(__name__)
# Create blueprint
penal_bp = Blueprint('penal', __name__)
@penal_bp.route('/PenalCharge', methods=['POST'])
@api_key_required
def penal_charge():
"""
Endpoint to process penalty charge requests.
This method handles requests to charge customers for penalties
as per existing debt. Results of these requests will be received
from the NotificationCallback endpoint.
Returns:
JSON response acknowledging the penalty charge request
"""
try:
# Parse and validate request
data = request.get_json()
if not data:
return jsonify({
'resultCode': '400',
'resultDescription': 'Invalid JSON payload'
}), 400
# Validate required fields
required_fields = ['transactionId', 'fbnTransactionId', 'debtId', 'customerId',
'accountId', 'penalCharge', 'lienAmount', 'countryId']
for field in required_fields:
if field not in data:
return jsonify({
'resultCode': '422',
'resultDescription': f'Missing required field: {field}'
}), 422
# Create request model
req = PenalChargeRequest.from_dict(data)
# Process penal charge request (this would connect to the business logic)
# For demonstration, we'll return a mock response
# Create response
response = PenalChargeResponse(
resultCode="00",
resultDescription="Penal charge debited successfully"
)
logger.info(f"Processed penal charge for customer {req.customerId}, amount {req.penalCharge}")
return jsonify(response.to_dict())
except Exception as e:
logger.error(f"Error processing penal charge: {str(e)}")
return jsonify({
'resultCode': '500',
'resultDescription': 'Internal server error'
}), 500