(20250102) Print statement removed.

This commit is contained in:
2025-01-02 10:12:05 +00:00
parent 2f563b2a0e
commit 7f98f1f662
5 changed files with 108 additions and 49 deletions
@@ -36,6 +36,7 @@ sys.path.append(".")
sys.path.append("..")
# My async utils:
from utils_v2.string import json
from utils_v2.database.async_mysql_v2 import AsyncMySQL
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
@@ -310,6 +311,51 @@ class PaymentsController(CoreAuthTokenController, ABC):
# We don't support updating payments themselves,
# but we will allow updating fields like tags, adding events, etc.
async def add_receipt(
self,
sql_conn: AsyncMySQL,
mongo_data_conn: AsyncMongo,
payment: CorePaymentModel,
session_token: str = None
) -> bool:
"""
The API endpoint that initiates the payment request will send details for calling this procedure in the
'metadata' field. We take those values and invoke the procedure to add a receipt in the database's records.
:param sql_conn: The database connection to use to perform this activity.
:param mongo_data_conn: The database connection to use to perform this activity.
:param payment: The record of the payment whose details need to be added as a receipt.
:param session_token: The session token of the user to identify whose request caused this receipt update.
:return: True is the receipt was added, else False.
"""
# Call the database's procedure:
sql_json = await self.call_procedure(
sql_conn = sql_conn,
proc_name = "accounting_receipt_add",
proc_args = (
payment.metadata["idClient"],
payment.metadata["date"],
payment.metadata["source"],
payment.metadata["amount"],
payment.metadata["reference"],
payment.metadata["inAccount"],
payment.metadata["idUser"],
payment.metadata["documentUrl"],
payment.metadata["notes"]
),
retry_count = 1,
backoff_seconds = 0.5,
backoff_multiplier = 1.1,
session_token = session_token
)
print("RECEIPT ADD SQL JSON:", json.to_string(sql_json, default=str))
# Return the response:
if sql_json["status"] == 1: return True
else: return False
async def add_event_by_payment_id(
self,
mongo_data_conn: AsyncMongo,
@@ -238,7 +238,6 @@ class SafaricomMPesaExpressPaymentsController(PaymentsController):
) if isinstance(payment_request.payerNo, str) else payment_request.payerNo,
party_b = auth_token.auth["businessShortCode"]
)
print(client_response.to_markdown())
# Add this event to the payment's document:
event_note_success = await self.add_event_by_payment_id(
@@ -345,7 +344,7 @@ class SafaricomMPesaExpressPaymentsController(PaymentsController):
}
)
# For now, we just insert the event into the record:
# We just insert the event into the record:
event_note_success = await self.add_event_by_client_reference_id(
mongo_data_conn = mongo_data_conn,
event = PaymentEvent(
@@ -360,23 +359,20 @@ class SafaricomMPesaExpressPaymentsController(PaymentsController):
client_reference_id = pg_reference_id
)
# In case the payment collector's credentials have changed,
# we mark his account as disabled:
# if pg_result_code in [2001]:
# payment = await self.get_payment_by_client_reference_id(
# mongo_data_conn = mongo_data_conn,
# client_reference_id = pg_reference_id
# )
# auth_token = await self.get_token_from_id(
# mongo_data_conn = mongo_data_conn,
# token_id = payment.tokenId
# )
# auth_token.status = "disabled"
# await self.set_token(
# sql_conn = sql_conn,
# mongo_data_conn = mongo_data_conn,
# auth_token = auth_token
# )
# We now fetch the payment record and use that for cases like adding the receipt or disabling an account whose
# credentials are invalid:
payment = await self.get_payment_by_client_reference_id(
mongo_data_conn = mongo_data_conn,
client_reference_id = pg_reference_id
)
# For when the payment is successful:
if pg_result_code in []:
await self.add_receipt(
sql_conn = sql_conn,
mongo_data_conn = mongo_data_conn,
payment = payment
)
# Done here:
if event_note_success: