1313)
1414
1515from santander_sdk .api_client .helpers import (
16- document_type ,
1716 get_pix_key_type ,
1817 retry_one_time_on_request_exception ,
1918 truncate_value ,
2019)
2120from santander_sdk .types import (
22- BeneficiaryDataDict ,
21+ SantanderBeneficiary ,
2322 ConfirmOrderStatus ,
2423 CreateOrderStatus ,
2524 OrderStatus ,
3534MAX_UPDATE_STATUS_ATTEMPTS_TO_CONFIRM = 120
3635PIX_CONFIRM_INTERVAL_TIME = 2
3736
38- TYPE_ACCOUNT_MAP = {
39- "savings" : "CONTA_POUPANCA" ,
40- "payment" : "CONTA_PAGAMENTO" ,
41- "checking" : "CONTA_CORRENTE" ,
42- }
43-
44-
4537def transfer_pix_payment (
4638 client : SantanderApiClient ,
47- pix_info : str | BeneficiaryDataDict ,
39+ pix_key : str | SantanderBeneficiary ,
4840 value : D ,
4941 description : str ,
5042 tags : list [str ] = [],
5143) -> TransferPixResult :
5244 """Realiza uma transferência PIX para uma chave PIX ou para um beneficiário
5345 - Se for informado uma chave PIX, o valor deve ser uma string com a chave CPF, CNPJ, EMAIL, CELULAR ou chave aleatória
5446 - CELULAR deve ser informado no formato +5511912345678 (14 caracteres incluindo o +)
55- - Se for informado um beneficiário, o valor deve ser BeneficiaryDataDict com os dados do beneficiário
47+ - Se for informado um beneficiário, o valor deve ser SantanderBeneficiary com os dados do beneficiário
5648
5749 ### Retorno de sucesso:
5850 - success: True se a transferência foi realizada com sucesso
@@ -69,7 +61,7 @@ def transfer_pix_payment(
6961 )
7062
7163 create_pix_response = _request_create_pix_payment (
72- client , pix_info , value , description , tags
64+ client , pix_key , value , description , tags
7365 )
7466 pix_id = create_pix_response .get ("id" )
7567 logger .info ("Santander - PIX criado com sucesso: {pix_id}" )
@@ -94,6 +86,14 @@ def transfer_pix_payment(
9486 return {"success" : False , "error" : error_message , "data" : None }
9587
9688
89+ def get_transfer (client : SantanderApiClient , pix_payment_id : str ) -> SantanderPixResponse :
90+ if not pix_payment_id :
91+ raise SantanderValueErrorException ("pix_payment_id não informado" )
92+
93+ response = client .get (f"{ PIX_ENDPOINT } /{ pix_payment_id } " )
94+ return cast (SantanderPixResponse , response )
95+
96+
9797def _pix_payment_status_polling (
9898 client : SantanderApiClient ,
9999 pix_id : str ,
@@ -185,48 +185,32 @@ def _confirm_pix_payment(
185185
186186def _request_create_pix_payment (
187187 client : SantanderApiClient ,
188- pix_info : BeneficiaryDataDict | str ,
188+ pix_key : SantanderBeneficiary | str ,
189189 value : D ,
190190 description : str ,
191191 tags : list [str ] = [],
192192) -> SantanderPixResponse :
193- """Cria uma ordem de pagamento. Caso o status seja REJECTED, a exceção SantanderRejectedTransactionException é lançada.
194- Regra de negócio aqui: pagamento por beneficiário na request deve ser informado o bank_code ou ispb, nunca os dois."""
193+ """Cria uma ordem de pagamento. Caso o status seja REJECTED, a exceção SantanderRejectedTransactionException é lançada."""
195194 data = {
196195 "tags" : tags ,
197196 "paymentValue" : truncate_value (value ),
198197 "remittanceInformation" : description ,
199198 }
200- if isinstance (pix_info , str ):
201- pix_type = get_pix_key_type (pix_info )
202- data .update ({"dictCode" : pix_info , "dictCodeType" : pix_type })
203- elif isinstance (pix_info , dict ):
204- try :
205- beneficiary = {
206- "branch" : pix_info ["bank_account" ]["agencia" ],
207- "number" : f"{ pix_info ['bank_account' ]['conta' ]} { pix_info ['bank_account' ]['conta_dv' ]} " ,
208- "type" : TYPE_ACCOUNT_MAP [pix_info ["bank_account" ]["tipo_conta" ]],
209- "documentType" : document_type (
210- pix_info ["bank_account" ]["document_number" ]
211- ),
212- "documentNumber" : pix_info ["bank_account" ]["document_number" ],
213- "name" : pix_info ["recebedor" ]["name" ],
214- }
215- bank_account = pix_info ["bank_account" ]
216- bank_code = bank_account .get ("bank_code_compe" , "" )
217- bank_ispb = bank_account .get ("bank_code_ispb" , "" )
218- if bank_code :
219- beneficiary ["bankCode" ] = bank_code
220- elif bank_ispb :
221- beneficiary ["ispb" ] = bank_ispb
222- else :
223- raise SantanderValueErrorException ("A chave de entrada é inválida" )
224-
225- data .update ({"beneficiary" : beneficiary })
226- except KeyError as e :
227- raise SantanderValueErrorException (
228- f"Campo obrigatório não informado para o beneficiário: { e } "
229- )
199+ if isinstance (pix_key , str ):
200+ pix_type = get_pix_key_type (pix_key )
201+ data .update ({"dictCode" : pix_key , "dictCodeType" : pix_type })
202+ elif isinstance (pix_key , dict ):
203+ beneficiary = cast (dict , pix_key .copy ())
204+ bank_code = pix_key .get ("bankCode" , "" )
205+ ispb = pix_key .get ("ispb" , "" )
206+
207+ if bank_code and ispb :
208+ "Deve ser informado o bankCode ou ispb, nunca os dois."
209+ del beneficiary ["ispb" ]
210+ elif not bank_code and not ispb :
211+ raise SantanderValueErrorException ("Deve ser informado 'bankCode' ou 'ispb'" )
212+
213+ data .update ({"beneficiary" : beneficiary })
230214 else :
231215 raise SantanderValueErrorException ("Chave PIX ou Beneficiário não informado" )
232216
0 commit comments