33from time import sleep
44from typing import List , Literal , cast
55
6- from santander_client .api_client import get_client
6+ from santander_client .api_client . client import SantanderApiClient
77from santander_client .api_client .exceptions import (
88 SantanderClientException ,
99 SantanderRejectedTransactionException ,
4343
4444
4545def transfer_pix_payment (
46- pix_info : str | BeneficiaryDataDict , value : D , description : str , tags = []
46+ client : SantanderApiClient ,
47+ pix_info : str | BeneficiaryDataDict ,
48+ value : D ,
49+ description : str ,
50+ tags : list [str ] = [],
4751) -> TransferPixResult :
4852 """Realiza uma transferência PIX para uma chave PIX ou para um beneficiário
4953 - Se for informado uma chave PIX, o valor deve ser uma string com a chave CPF, CNPJ, EMAIL, CELULAR ou chave aleatória
@@ -65,7 +69,7 @@ def transfer_pix_payment(
6569 )
6670
6771 create_pix_response = _request_create_pix_payment (
68- pix_info , value , description , tags
72+ client , pix_info , value , description , tags
6973 )
7074 pix_id = create_pix_response .get ("id" )
7175 logger .info ("Santander - PIX criado com sucesso: {pix_id}" )
@@ -81,7 +85,7 @@ def transfer_pix_payment(
8185 "Status do pagamento não retornado na criação"
8286 )
8387
84- confirm_response = _confirm_pix_payment (pix_id , value , payment_status )
88+ confirm_response = _confirm_pix_payment (client , pix_id , value , payment_status )
8589
8690 return {"success" : True , "data" : confirm_response , "error" : "" }
8791 except Exception as e :
@@ -91,17 +95,18 @@ def transfer_pix_payment(
9195
9296
9397def _pix_payment_status_polling (
98+ client : SantanderApiClient ,
9499 pix_id : str ,
95100 until_status : List [str ],
96101 context : Literal ["CREATE" , "CONFIRM" ],
97102 max_attempts : int ,
98103) -> SantanderPixResponse :
99- response = _request_pix_payment_status (pix_id , context )
104+ response = _request_pix_payment_status (client , pix_id , context )
100105 if response .get ("status" ) in until_status :
101106 return response
102107
103108 for attempt in range (max_attempts - 1 ):
104- response = _request_pix_payment_status (pix_id , context )
109+ response = _request_pix_payment_status (client , pix_id , context )
105110 payment_status = response .get ("status" )
106111 logger .info (
107112 f"Santander - Verificando status do pagamento PIX por polling: { pix_id } - { payment_status } "
@@ -122,7 +127,7 @@ def _pix_payment_status_polling(
122127
123128
124129def _confirm_pix_payment (
125- pix_id : str , value : D , payment_status : OrderStatusType
130+ client : SantanderApiClient , pix_id : str , value : D , payment_status : OrderStatusType
126131) -> SantanderPixResponse :
127132 """Confirma o pagamento PIX, realizando polling até que o status seja PAYED ou permaneça PENDING_CONFIRMATION.
128133
@@ -134,19 +139,20 @@ def _confirm_pix_payment(
134139 if payment_status != CreateOrderStatus .READY_TO_PAY :
135140 logger .info (f"Santander - PIX não está pronto para pagamento: { pix_id } " )
136141 _pix_payment_status_polling (
142+ client ,
137143 pix_id = pix_id ,
138144 until_status = [CreateOrderStatus .READY_TO_PAY ],
139145 context = "CREATE" ,
140146 max_attempts = MAX_UPDATE_STATUS_ATTEMPTS ,
141147 )
142148
143149 try :
144- confirm_response = _request_confirm_pix_payment (pix_id , value )
150+ confirm_response = _request_confirm_pix_payment (client , pix_id , value )
145151 except SantanderRequestException as e :
146152 logger .error (
147153 f"Santander - Erro ao confirmar pagamento PIX: { str (e )} , { pix_id } , verificando status atual"
148154 )
149- confirm_response = _request_pix_payment_status (pix_id , "CONFIRM" )
155+ confirm_response = _request_pix_payment_status (client , pix_id , "CONFIRM" )
150156
151157 if confirm_response .get ("status" ) == ConfirmOrderStatus .PAYED :
152158 return confirm_response
@@ -158,6 +164,7 @@ def _confirm_pix_payment(
158164
159165 try :
160166 confirm_response = _pix_payment_status_polling (
167+ client ,
161168 pix_id = pix_id ,
162169 until_status = [ConfirmOrderStatus .PAYED ],
163170 context = "CONFIRM" ,
@@ -177,7 +184,11 @@ def _confirm_pix_payment(
177184
178185
179186def _request_create_pix_payment (
180- pix_info : BeneficiaryDataDict | str , value : D , description : str , tags = []
187+ client : SantanderApiClient ,
188+ pix_info : BeneficiaryDataDict | str ,
189+ value : D ,
190+ description : str ,
191+ tags : list [str ] = [],
181192) -> SantanderPixResponse :
182193 """Cria uma ordem de pagamento. Caso o status seja REJECTED, a exceção SantanderRejectedTransactionException é lançada.
183194 Regra de negócio aqui: pagamento por beneficiário na request deve ser informado o bank_code ou ispb, nunca os dois."""
@@ -219,13 +230,14 @@ def _request_create_pix_payment(
219230 else :
220231 raise SantanderValueErrorException ("Chave PIX ou Beneficiário não informado" )
221232
222- client = get_client ()
223233 response = cast (SantanderPixResponse , client .post (PIX_ENDPOINT , data = data ))
224234 _check_for_rejected_exception (response , "Criação do pagamento PIX" )
225235 return response
226236
227237
228- def _request_confirm_pix_payment (pix_payment_id : str , value : D ) -> SantanderPixResponse :
238+ def _request_confirm_pix_payment (
239+ client : SantanderApiClient , pix_payment_id : str , value : D
240+ ) -> SantanderPixResponse :
229241 """Confirma o pagamento PIX através do PATCH e status AUTHORIZED
230242 O HTTP code de sucesso é 200, mesmo que o status seja REJECTED
231243 Caso o status seja REJECTED, a exceção SantanderRejectedTransactionException é lançada
@@ -236,7 +248,6 @@ def _request_confirm_pix_payment(pix_payment_id: str, value: D) -> SantanderPixR
236248 if not value :
237249 raise SantanderValueErrorException ("Valor não informado" )
238250
239- client = get_client ()
240251 data = {
241252 "status" : "AUTHORIZED" ,
242253 "paymentValue" : truncate_value (value ),
@@ -249,7 +260,7 @@ def _request_confirm_pix_payment(pix_payment_id: str, value: D) -> SantanderPixR
249260
250261@retry_one_time_on_request_exception
251262def _request_pix_payment_status (
252- pix_payment_id : str , step_description : str
263+ client : SantanderApiClient , pix_payment_id : str , step_description : str
253264) -> SantanderPixResponse :
254265 """
255266 Retorna o estado atual do processamento de um pagamento PIX
@@ -259,7 +270,6 @@ def _request_pix_payment_status(
259270 if not pix_payment_id :
260271 raise SantanderValueErrorException ("pix_payment_id não informado" )
261272
262- client = get_client ()
263273 response = client .get (f"{ PIX_ENDPOINT } /{ pix_payment_id } " )
264274 response = cast (SantanderPixResponse , response )
265275 _check_for_rejected_exception (response , step_description )
0 commit comments