11import logging
2- from datetime import datetime , timedelta , timezone
2+ from datetime import UTC , datetime , timedelta
33from decimal import Decimal
44from typing import cast
55
@@ -57,24 +57,30 @@ async def process_message(app: FastAPI, data: bytes) -> bool:
5757 assert wallet_auto_recharge is not None # nosec
5858 assert wallet_auto_recharge .payment_method_id is not None # nosec
5959
60- # Step 3: Get Payment method
61- _payments_repo = PaymentsMethodsRepo (db_engine = app .state .engine )
62- payment_method_db = await _payments_repo .get_payment_method_by_id (
63- payment_method_id = wallet_auto_recharge .payment_method_id
64- )
65-
66- # Step 4: Check spending limits
60+ # Step 3: Check spending limits
6761 _payments_transactions_repo = PaymentsTransactionsRepo (db_engine = app .state .engine )
6862 if await _exceeds_monthly_limit (
6963 _payments_transactions_repo , rabbit_message .wallet_id , wallet_auto_recharge
7064 ):
7165 return True # We do not auto recharge
7266
73- # Step 5: Check last top-up time
74- if await _recently_topped_up (_payments_transactions_repo , rabbit_message .wallet_id ):
67+ # Step 4: Check last top-up time
68+ if await _was_wallet_topped_up_recently (
69+ _payments_transactions_repo , rabbit_message .wallet_id
70+ ):
71+ return True # We do not auto recharge
72+
73+ # Step 5: Check if timestamp when message was created is not too old
74+ if await _is_message_too_old (rabbit_message .created_at ):
7575 return True # We do not auto recharge
7676
77- # Step 6: Perform auto-recharge
77+ # Step 6: Get Payment method
78+ _payments_repo = PaymentsMethodsRepo (db_engine = app .state .engine )
79+ payment_method_db = await _payments_repo .get_payment_method_by_id (
80+ payment_method_id = wallet_auto_recharge .payment_method_id
81+ )
82+
83+ # Step 7: Perform auto-recharge
7884 if settings .PAYMENTS_AUTORECHARGE_ENABLED :
7985 await _perform_auto_recharge (
8086 app , rabbit_message , payment_method_db , wallet_auto_recharge
@@ -114,16 +120,20 @@ async def _exceeds_monthly_limit(
114120 )
115121
116122
117- async def _recently_topped_up (
123+ async def _was_wallet_topped_up_recently (
118124 payments_transactions_repo : PaymentsTransactionsRepo , wallet_id : WalletID
119125):
126+ """
127+ As safety, we check if the last transaction was initiated within the last 5 minutes
128+ in that case we do not auto recharge
129+ """
120130 last_wallet_transaction = (
121131 await payments_transactions_repo .get_last_payment_transaction_for_wallet (
122132 wallet_id = wallet_id
123133 )
124134 )
125135
126- current_timestamp = datetime .now (tz = timezone . utc )
136+ current_timestamp = datetime .now (tz = UTC )
127137 current_timestamp_minus_5_minutes = current_timestamp - timedelta (minutes = 5 )
128138
129139 return (
@@ -132,6 +142,19 @@ async def _recently_topped_up(
132142 )
133143
134144
145+ async def _is_message_too_old (
146+ message_timestamp : datetime ,
147+ ):
148+ """
149+ As safety, we check if the message was created within the last 5 minutes
150+ if not we do not auto recharge
151+ """
152+ current_timestamp = datetime .now (tz = UTC )
153+ current_timestamp_minus_5_minutes = current_timestamp - timedelta (minutes = 5 )
154+
155+ return message_timestamp < current_timestamp_minus_5_minutes
156+
157+
135158async def _perform_auto_recharge (
136159 app : FastAPI ,
137160 rabbit_message : WalletCreditsMessage ,
0 commit comments