diff --git a/app/__init__.py b/app/__init__.py index a42cecb..e5e7e35 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -13,13 +13,13 @@ def create_app(): # Load configuration app.config.from_object(Config) + # Setup CORS CORS(app) # Register blueprints app.register_blueprint(auth_bp) app.register_blueprint(autocall_bp, url_prefix="/autocall") - # Error Handlers app.register_error_handler(405, method_not_allowed) app.register_error_handler(415, unsupported_media_type) diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index b279368..392142f 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -2,7 +2,9 @@ import requests from app.config import settings from app.utils.auth import get_headers from app.utils.logger import logger -from flask import jsonify +from flask import jsonify, current_app +from app.models.transactions import Transaction + class SimbrellaClient: @@ -11,9 +13,14 @@ class SimbrellaClient: @staticmethod def disbursement(data): api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement" - logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}") logger.info(f"Calling Disbursement endpoint with data: {data}") + # Check if the transaction exists + logger.info(f"Checking if transaction exists") + with current_app.app_context(): + transaction = Transaction.get_transaction_by_id(transaction_id=data['transactionId']) + logger.info(f"Response from database: {transaction}") + disbursement_data ={ "requestId": data['requestId'], "transactionId": data['transactionId'], @@ -45,7 +52,6 @@ class SimbrellaClient: @staticmethod def collect_loan(data): api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan" - logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}") logger.info(f"Calling CollectLoan endpoint with data: {data}") collect_loan_data = { @@ -76,14 +82,7 @@ class SimbrellaClient: @staticmethod def verify_transaction(): - # api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/TransactionVerify" - # logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}") - # logger.info(f"Calling TransactionVerify endpoint with data: {data}") - try: - # logger.info(f"Here is your TransactionVerify Request data ***** : {data}") - # response = requests.post(api_url, json=data, headers=get_headers()) - # logger.info(f"TransactionVerify response: {response.json()}") return { "status": "00", @@ -96,16 +95,9 @@ class SimbrellaClient: @staticmethod def refresh_disbursement(data): - # api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement" - # logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}") - # logger.info(f"Calling Disbursement endpoint with data: {data}") try: logger.info(f"Here is your Disbursement Request data ***** : {data}") - # response = requests.post(api_url, json=data, headers=get_headers()) - # logger.info(f"Disbursement response: {response.json()}") - - # return response.json() return data @@ -115,19 +107,16 @@ class SimbrellaClient: @staticmethod def payment_callback(data): - # api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Payment" - # logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}") - # logger.info(f"Calling Payment Callback endpoint with data: {data}") try: logger.info(f"Here is your Payment Callback Request data ***** : {data}") - # response = requests.post(api_url, json=data, headers=get_headers()) - # logger.info(f"Payment Callback response: {response.json()}") - - # return response.json() return data except Exception as e: logger.info(f"Failed to call Payment Callback endpoint: {e}") - raise \ No newline at end of file + raise + + @staticmethod + def check_transaction(txn_id): + return run_in_app_context(Transaction.get_transaction_by_id(txn_id)) \ No newline at end of file diff --git a/app/models/__init__.py b/app/models/__init__.py index f386329..0f0d015 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -1 +1,3 @@ -from .transactions import Transaction \ No newline at end of file +from .transactions import Transaction + +__all__ = ['Transaction'] \ No newline at end of file diff --git a/app/utils/auth.py b/app/utils/auth.py index 5daf068..0523774 100644 --- a/app/utils/auth.py +++ b/app/utils/auth.py @@ -2,14 +2,8 @@ from app.config import settings def get_headers(): - # return { - # "Content-Type": "application/json", - # "x-api_key": settings.BANK_CALL_API_KEY, - # "App-Id": settings.BANK_CALL_APP_ID, - # } return { "Content-Type": "application/json", - "x-api-key": "test-api-key-12345", - "App-Id": "app1", + "x-api-key": settings.BANK_CALL_API_KEY, + "App-Id": settings.BANK_CALL_APP_ID, } - diff --git a/wsgi.py b/wsgi.py index f49c773..a5c275c 100644 --- a/wsgi.py +++ b/wsgi.py @@ -7,23 +7,25 @@ from app.utils.logger import logger app = create_app() kafka = KafkaIntegration() -def start_kafka_consumer(): - logger.info("Starting Kafka consumer...") - while True: - try: +def start_kafka_consumer(app): + with app.app_context(): + logger.info("Starting Kafka consumer...") + while True: + try: - message = kafka.receive_disbursement_messages( - topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT - ) + message = kafka.receive_disbursement_messages( + topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT + ) - if message: - logger.info(f"Processed message: {message}") - else: - logger.info("No message received within timeout") + if message: + logger.info(f"Processed message: {message}") + else: + logger.info("No message received within timeout") - except Exception as e: - logger.error(f"Error while receiving message: {e}") + except Exception as e: + logger.error(f"Error while receiving message: {e}") + if __name__ != "__main__": @@ -32,4 +34,4 @@ if __name__ != "__main__": wsgi_app = app # Start kafka in a thread - threading.Thread(target=start_kafka_consumer, daemon=True).start() + threading.Thread(target=start_kafka_consumer, args=(app,), daemon=True).start() \ No newline at end of file