1010 SantanderStatusTimeoutError ,
1111)
1212
13- from santander_sdk .api_client .helpers import (retry_one_time_on_request_exception ,)
13+ from santander_sdk .api_client .helpers import (
14+ retry_one_time_on_request_exception ,
15+ )
1416from santander_sdk .types import (
1517 ConfirmOrderStatus ,
1618 CreateOrderStatus ,
2729class SantanderPaymentFlow :
2830 current_step : Literal ["CREATE" , "CONFIRM" ] = "CREATE"
2931
30- def __init__ (self , client : SantanderApiClient , logger : logging .Logger , endpoint : str ):
32+ def __init__ (
33+ self , client : SantanderApiClient , logger : logging .Logger , endpoint : str
34+ ):
3135 self .client = client
3236 self .logger = logger or logging .getLogger (__name__ )
3337 self .endpoint = endpoint
3438
35-
3639 def create_payment (self , data : dict ) -> SantanderPixResponse :
37- response = cast (SantanderPixResponse , self .client .post (self .endpoint , data = data ))
40+ response = cast (
41+ SantanderPixResponse , self .client .post (self .endpoint , data = data )
42+ )
3843 self ._check_for_rejected_error (response )
3944 self .logger .info ("Payment created: " , response .get ("id" ))
4045 return response
41-
46+
4247 def ensure_ready_to_pay (self , confirm_data ) -> None :
43- payment_status = confirm_data .get (' status' )
48+ payment_status = confirm_data .get (" status" )
4449 if payment_status != CreateOrderStatus .READY_TO_PAY :
4550 self .logger .info ("PIX is not ready for payment" , payment_status )
4651 self ._payment_status_polling (
47- payment_id = confirm_data .get ('id' ),
48- until_status = [CreateOrderStatus .READY_TO_PAY ],
49- max_update_attemps = MAX_UPDATE_STATUS_BEFORE_CONFIRM
52+ payment_id = confirm_data .get ("id" ),
53+ until_status = [CreateOrderStatus .READY_TO_PAY ],
54+ max_update_attemps = MAX_UPDATE_STATUS_BEFORE_CONFIRM ,
5055 )
5156
52- def confirm_payment (self , confirm_data : dict , payment_id : str ) -> SantanderPixResponse :
57+ def confirm_payment (
58+ self , confirm_data : dict , payment_id : str
59+ ) -> SantanderPixResponse :
5360 try :
5461 confirm_response = self ._request_confirm_payment (confirm_data , payment_id )
5562 except SantanderRequestError as e :
@@ -58,23 +65,25 @@ def confirm_payment(self, confirm_data: dict, payment_id: str) -> SantanderPixRe
5865
5966 if not confirm_response .get ("status" ) == ConfirmOrderStatus .PAYED :
6067 try :
61- confirm_response = self ._resolve_lazy_status_payed (payment_id , confirm_response .get ("status" ))
68+ confirm_response = self ._resolve_lazy_status_payed (
69+ payment_id , confirm_response .get ("status" )
70+ )
6271 except SantanderStatusTimeoutError as e :
6372 self .logger .info ("Timeout occurred while updating status:" , str (e ))
6473 return confirm_response
6574
66-
6775 @retry_one_time_on_request_exception
68- def _request_payment_status (self , payment_id : str ) -> SantanderPixResponse :
76+ def _request_payment_status (self , payment_id : str ) -> SantanderPixResponse :
6977 if not payment_id :
7078 raise ValueError ("payment_id not provided" )
7179 response = self .client .get (f"{ self .endpoint } /{ payment_id } " )
7280 response = cast (SantanderPixResponse , response )
7381 self ._check_for_rejected_error (response )
7482 return response
7583
76-
77- def _request_confirm_payment (self , confirm_data : dict , payment_id : str ) -> SantanderPixResponse :
84+ def _request_confirm_payment (
85+ self , confirm_data : dict , payment_id : str
86+ ) -> SantanderPixResponse :
7887 self .current_step = "CONFIRM"
7988 if not payment_id :
8089 raise ValueError ("payment_id not provided" )
@@ -83,33 +92,44 @@ def _request_confirm_payment(self, confirm_data: dict, payment_id: str) -> Santa
8392 self ._check_for_rejected_error (response )
8493 return response
8594
86-
8795 def _check_for_rejected_error (self , payment_response : SantanderPixResponse ):
8896 if not payment_response .get ("status" ) == OrderStatus .REJECTED :
89- return
90- reject_reason = payment_response .get ("rejectReason" , "Reason not returned by Santander" )
91- raise SantanderRejectedError (f"Payment rejected by the bank at step { self .current_step } - { reject_reason } " )
92-
97+ return
98+ reject_reason = payment_response .get (
99+ "rejectReason" , "Reason not returned by Santander"
100+ )
101+ raise SantanderRejectedError (
102+ f"Payment rejected by the bank at step { self .current_step } - { reject_reason } "
103+ )
93104
94- def _resolve_lazy_status_payed (self , payment_id : str , current_status : OrderStatusType ) -> None :
105+ def _resolve_lazy_status_payed (
106+ self , payment_id : str , current_status : OrderStatusType
107+ ) -> None :
95108 if not current_status == ConfirmOrderStatus .PENDING_CONFIRMATION :
96- raise SantanderClientError ( f"Unexpected status after confirmation: { current_status } " )
109+ raise SantanderClientError (
110+ f"Unexpected status after confirmation: { current_status } "
111+ )
97112 confirm_response = self ._payment_status_polling (
98- payment_id = payment_id ,
99- until_status = [ConfirmOrderStatus .PAYED ],
100- max_update_attemps = MAX_UPDATE_STATUS_AFTER_CONFIRM
113+ payment_id = payment_id ,
114+ until_status = [ConfirmOrderStatus .PAYED ],
115+ max_update_attemps = MAX_UPDATE_STATUS_AFTER_CONFIRM ,
101116 )
102117 return confirm_response
103-
104118
105- def _payment_status_polling (self , payment_id : str , until_status : List [str ], max_update_attemps : int ) -> SantanderPixResponse :
119+ def _payment_status_polling (
120+ self , payment_id : str , until_status : List [str ], max_update_attemps : int
121+ ) -> SantanderPixResponse :
106122 for attempt in range (1 , max_update_attemps + 1 ):
107123 response = self ._request_payment_status (payment_id )
108- self .logger .info (f"Checking status by polling: { payment_id } - { response .get ("status" )} " )
124+ self .logger .info (
125+ f"Checking status by polling: { payment_id } - { response .get ('status' )} "
126+ )
109127 if response .get ("status" ) in until_status :
110128 break
111129 if attempt == max_update_attemps :
112- raise SantanderStatusTimeoutError ("Status update attempt limit reached" , self .current_step )
130+ raise SantanderStatusTimeoutError (
131+ "Status update attempt limit reached" , self .current_step
132+ )
113133 sleep (UPDATE_STATUS_INTERVAL_TIME )
114134
115- return response
135+ return response
0 commit comments