Files
Azeez Muibi ba4d878daf Major update
2025-03-27 08:21:20 +01:00

77 lines
3.0 KiB
Python

from flask import request, jsonify
from marshmallow import ValidationError
from app.utils.logger import logger
from app.api.schemas.transaction_verify import TransactionVerifySchema, TransactionVerifyResponseSchema
class TransactionVerifyService:
@staticmethod
def process_request(data):
"""
Process the TransactionVerify request.
Args:
data (dict): The request data.
Returns:
dict: A standardized response.
"""
try:
logger.info("Processing TransactionVerify request")
# Validate input data using TransactionVerifySchema
schema = TransactionVerifySchema()
validated_data = schema.load(data) # Raises ValidationError if invalid
# Simulated processing logic
# In a real implementation, this would interact with your business logic
# to verify the transaction status
# For demonstration, we'll simulate different responses based on transaction type
transaction_type = validated_data.get('transactionType')
if transaction_type == "Disbursement":
provided_amount = 100.0
collected_amount = 0.0
result_description = "Disbursement Status retrieved successfully."
elif transaction_type == "Collection":
provided_amount = 0.0
collected_amount = 75.0
result_description = "Collection Status retrieved successfully."
else: # Penalty
provided_amount = 0.0
collected_amount = 7.50
result_description = "Penalty Status retrieved successfully."
response_data = {
"requestId": validated_data.get('requestId'),
"countryCode": validated_data.get('countryCode'),
"transactionId": validated_data.get('transactionId'),
"transactionType": transaction_type,
"customerId": validated_data.get('customerId'),
"accountId": validated_data.get('accountId'),
"providedAmount": provided_amount,
"collectedAmount": collected_amount,
"resultCode": "00",
"resultDescription": result_description
}
# Validate the response using the response schema
response_schema = TransactionVerifyResponseSchema()
validated_response = response_schema.dump(response_data)
return jsonify(validated_response)
except ValidationError as err:
logger.error(f"Validation Error: {err.messages}")
return jsonify({
"resultCode": "01",
"resultDescription": f"Validation error: {err.messages}"
}), 422
except Exception as e:
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify({
"resultCode": "08",
"resultDescription": f"Error occurred: {str(e)}"
}), 500