2121from ppr_api .callback .document_storage .storage_service import DocumentTypes , GoogleStorageService
2222from ppr_api .callback .utils .exceptions import ReportDataException , ReportException , StorageException
2323from ppr_api .exceptions import DatabaseException
24- from ppr_api .models import Draft , FinancingStatement , MailReport , Registration
24+ from ppr_api .models import Draft , EventTracking , FinancingStatement , MailReport , Registration , SearchResult
2525from ppr_api .models import utils as model_utils
2626from ppr_api .reports import get_callback_pdf
2727from ppr_api .reports .v2 .report_utils import ReportTypes
3939PAY_CANCELLED_MSG = "Payment status cancelled/deleted: draft reverted."
4040PAY_NO_REG_MSG = "Change no financing statement found for base reg num={reg_num}."
4141PAY_REG_SUCCESS_MSG = "Registration created fs id={fs_id} reg_id={reg_id} reg num={reg_num}."
42+ PAY_SEARCH_SUCCESS_MSG = "Search selection allowed for search id={search_id}."
4243ALLOWED_PAY_STATUS = [
4344 StatusCodes .CANCELLED .value ,
4445 StatusCodes .COMPLETED .value ,
@@ -151,6 +152,11 @@ def post_payment_callback(invoice_id: str): # pylint: disable=too-many-return-s
151152 elif pay_status not in ALLOWED_PAY_STATUS :
152153 error_msg : str = PAY_STATUS_INVALID_MSG .format (pay_status = pay_status )
153154 return pay_callback_error ("02" , invoice_id , HTTPStatus .BAD_REQUEST , error_msg )
155+
156+ search_id : int = get_search_id (invoice_id )
157+ if search_id and search_id > 0 :
158+ return pay_callback_search (search_id , invoice_id )
159+
154160 draft : Draft = Draft .find_by_invoice_id (invoice_id )
155161 if not draft :
156162 return pay_callback_error ("03" , invoice_id , HTTPStatus .NOT_FOUND )
@@ -171,6 +177,40 @@ def post_payment_callback(invoice_id: str): # pylint: disable=too-many-return-s
171177 return pay_callback_error ("00" , invoice_id , HTTPStatus .INTERNAL_SERVER_ERROR , str (default_err ))
172178
173179
180+ def get_search_id (invoice_id : str ):
181+ """Try to get the search id if the payment is for a search request."""
182+ events = EventTracking .find_by_key_id (int (invoice_id ))
183+ if not events :
184+ return None
185+ for event in events :
186+ if event .event_tracking_type == EventTracking .EventTrackingTypes .PPR_PAYMENT .value and event .message :
187+ message : str = str (event .message )
188+ if message .startswith ("11" ):
189+ tokens = message .split ("*" )
190+ if tokens and len (tokens ) > 1 :
191+ search_id = int (tokens [1 ])
192+ logger .info (f"get_search_id found id={ search_id } " )
193+ return search_id
194+ return None
195+
196+
197+ def pay_callback_search (search_id : int , invoice_id : str ):
198+ """Handle a payment complete notification for a search request."""
199+ search_result : SearchResult = SearchResult .find_by_search_id (search_id )
200+ if not search_result :
201+ return pay_callback_error ("13" , invoice_id , HTTPStatus .NOT_FOUND )
202+ if not search_result .is_payment_pending ():
203+ return pay_callback_error ("14" , invoice_id , HTTPStatus .BAD_REQUEST , f"Search id={ search_id } " )
204+ logger .info (f"Request valid for invoice id={ invoice_id } search id={ search_id } , marking search as completed." )
205+ cc_payment_utils .track_event ("09" , invoice_id , HTTPStatus .OK , f"Search id={ search_id } " )
206+ search_result .score = None
207+ search_result .save ()
208+ logger .info (f"Search pending payment status removed for { search_id } ." )
209+ msg = PAY_SEARCH_SUCCESS_MSG .format (search_id = search_id )
210+ cc_payment_utils .track_event ("15" , invoice_id , HTTPStatus .OK , msg )
211+ return {}, HTTPStatus .OK
212+
213+
174214def pay_callback_error (code : str , key_id : int , status_code , message : str = None ):
175215 """Return the payment event listener callback error response based on the code."""
176216 error : str = cc_payment_utils .track_event (code , key_id , status_code , message )
0 commit comments