11from decimal import Decimal as D
22import logging
33from time import sleep
4- from typing import List , Literal
4+ from typing import List , Literal , cast
55
66from santander_client .api_client import get_client
77from santander_client .api_client .exceptions import (
2222 BeneficiaryDataDict ,
2323 ConfirmOrderStatus ,
2424 CreateOrderStatus ,
25- SantanderAPIErrorResponse ,
26- SantanderCreatePixResponse ,
25+ OrderStatus ,
26+ OrderStatusType ,
27+ SantanderPixResponse ,
2728 TransferPixResult ,
2829)
2930
4041 "checking" : "CONTA_CORRENTE" ,
4142}
4243
43- SantanderPixResponse = SantanderCreatePixResponse | SantanderAPIErrorResponse
44-
4544
4645def transfer_pix_payment (
4746 pix_info : str | BeneficiaryDataDict , value : D , description : str , tags = []
@@ -74,16 +73,21 @@ def transfer_pix_payment(
7473
7574 if not pix_id :
7675 raise SantanderClientException (
77- "ID do pagamento PIX não foi retornada pela criação do pagamento"
76+ "ID do pagamento não foi retornada na criação"
77+ )
78+
79+ if payment_status is None :
80+ raise SantanderClientException (
81+ "Status do pagamento não retornado na criação"
7882 )
7983
8084 confirm_response = _confirm_pix_payment (pix_id , value , payment_status )
8185
82- return {"success" : True , "data" : confirm_response }
86+ return {"success" : True , "data" : confirm_response , "error" : "" }
8387 except Exception as e :
8488 error_message = str (e )
8589 logger .error (error_message )
86- return {"success" : False , "error" : error_message }
90+ return {"success" : False , "error" : error_message , "data" : None }
8791
8892
8993def _pix_payment_status_polling (
@@ -92,7 +96,11 @@ def _pix_payment_status_polling(
9296 context : Literal ["CREATE" , "CONFIRM" ],
9397 max_attempts : int ,
9498) -> SantanderPixResponse :
95- for attempt in range (max_attempts ):
99+ response = _request_pix_payment_status (pix_id , context )
100+ if response .get ("status" ) in until_status :
101+ return response
102+
103+ for attempt in range (max_attempts - 1 ):
96104 response = _request_pix_payment_status (pix_id , context )
97105 payment_status = response .get ("status" )
98106 logger .info (
@@ -102,7 +110,7 @@ def _pix_payment_status_polling(
102110 if payment_status in until_status :
103111 break
104112
105- if attempt == max_attempts - 1 :
113+ if attempt == max_attempts - 2 :
106114 raise SantanderTimeoutToChangeStatusException (
107115 "Limite de tentativas de atualização do status do pagamento PIX atingido" ,
108116 context ,
@@ -114,7 +122,7 @@ def _pix_payment_status_polling(
114122
115123
116124def _confirm_pix_payment (
117- pix_id : str , value : D , payment_status : CreateOrderStatus
125+ pix_id : str , value : D , payment_status : OrderStatusType
118126) -> SantanderPixResponse :
119127 """Confirma o pagamento PIX, realizando polling até que o status seja PAYED ou permaneça PENDING_CONFIRMATION.
120128
@@ -169,7 +177,7 @@ def _confirm_pix_payment(
169177
170178
171179def _request_create_pix_payment (
172- pix_info : BeneficiaryDataDict , value : D , description : str , tags = []
180+ pix_info : BeneficiaryDataDict | str , value : D , description : str , tags = []
173181) -> SantanderPixResponse :
174182 """Cria uma ordem de pagamento. Caso o status seja REJECTED, a exceção SantanderRejectedTransactionException é lançada.
175183 Regra de negócio aqui: pagamento por beneficiário na request deve ser informado o bank_code ou ispb, nunca os dois."""
@@ -193,13 +201,16 @@ def _request_create_pix_payment(
193201 "documentNumber" : pix_info ["bank_account" ]["document_number" ],
194202 "name" : pix_info ["recebedor" ]["name" ],
195203 }
196- if pix_info .get ("bank_account" ):
197- if pix_info ["bank_account" ].get ("bank_code_compe" ):
198- beneficiary ["bankCode" ] = pix_info ["bank_account" ][
199- "bank_code_compe"
200- ]
201- else :
202- beneficiary ["ispb" ] = pix_info ["bank_account" ]["bank_code_ispb" ]
204+ bank_account = pix_info ["bank_account" ]
205+ bank_code = bank_account .get ("bank_code_compe" , "" )
206+ bank_ispb = bank_account .get ("bank_code_ispb" , "" )
207+ if bank_code :
208+ beneficiary ["bankCode" ] = bank_code
209+ elif bank_ispb :
210+ beneficiary ["ispb" ] = bank_ispb
211+ else :
212+ raise SantanderValueErrorException ("A chave de entrada é inválida" )
213+
203214 data .update ({"beneficiary" : beneficiary })
204215 except KeyError as e :
205216 raise SantanderValueErrorException (
@@ -209,7 +220,7 @@ def _request_create_pix_payment(
209220 raise SantanderValueErrorException ("Chave PIX ou Beneficiário não informado" )
210221
211222 client = get_client ()
212- response = client .post (PIX_ENDPOINT , data = data )
223+ response = cast ( SantanderPixResponse , client .post (PIX_ENDPOINT , data = data ) )
213224 _check_for_rejected_exception (response , "Criação do pagamento PIX" )
214225 return response
215226
@@ -231,6 +242,7 @@ def _request_confirm_pix_payment(pix_payment_id: str, value: D) -> SantanderPixR
231242 "paymentValue" : truncate_value (value ),
232243 }
233244 response = client .patch (f"{ PIX_ENDPOINT } /{ pix_payment_id } " , data = data )
245+ response = cast (SantanderPixResponse , response )
234246 _check_for_rejected_exception (response , "Confirmação do pagamento PIX" )
235247 return response
236248
@@ -249,13 +261,14 @@ def _request_pix_payment_status(
249261
250262 client = get_client ()
251263 response = client .get (f"{ PIX_ENDPOINT } /{ pix_payment_id } " )
264+ response = cast (SantanderPixResponse , response )
252265 _check_for_rejected_exception (response , step_description )
253266 return response
254267
255268
256269def _check_for_rejected_exception (pix_response : SantanderPixResponse , step : str ):
257270 """Uma transação quando rejeitada com status REJECTED, não deve ser continuada"""
258- if pix_response .get ("status" ) == " REJECTED" :
271+ if pix_response .get ("status" ) == OrderStatus . REJECTED :
259272 reject_reason = pix_response .get (
260273 "rejectReason" , "Motivo não retornado pelo Santander"
261274 )
0 commit comments