-
Notifications
You must be signed in to change notification settings - Fork 0
/
iugu_rsa_sample.py
151 lines (131 loc) · 6.69 KB
/
iugu_rsa_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# - Instalar "Crypto" usando o comando
# pip install pycryptodome
# - Instalar "Requests" usando o comando
# pip install requests
# para executar:
# - Altere a linha iuru_rsa.api_token, informando seu token
# - Execute o arquivo com o comando abaixo:
# python ./iugu_rsa_sample.py
######################################################################################################
######################################################################################################
######################################################################################################
#####################################################################################################
#####################################################################################################
#####################################################################################################
#################### ATENÇÃO ##################################
## Para efetuar a Assinatura de Requisições e Pix/TED-Out à partir de uma Subconta, sem uma Chave Criptografada, siga a documentação abaixo:
## https://dev.iugu.com/reference/rsawhitelabel
######################################################################################################
# IUGU_RSA_SAMPLE
import json
class IUGU_RSA_SAMPLE:
print_vars = False
# Link de referência: https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o#criando-chave-api-com-assinatura
api_token = "TOKEN CREATED ON IUGU PANEL"
# Link de referência: https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o#segundo-passo
file_private_key = "/file_path/private_key.pem"
# Link de referência: https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o#quinto-passo
def __get_request_time(self):
from datetime import datetime
current_date = datetime.now().astimezone()
return current_date.isoformat(timespec='seconds')
def __get_private_key(self):
from Crypto.PublicKey import RSA
with open(self.file_private_key) as f:
text_key = f.read()
private_key = RSA.importKey(text_key)
return private_key
# Link de referência: https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o#sexto-passo
def __sign_body(self, method, endpoint, request_time, body, private_key):
import base64
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
ret_sign = ""
pattern = method+"|"+endpoint+"\n"+self.api_token+"|"+request_time+"\n"+body
h = SHA256.new(pattern.encode("utf8"))
signer = PKCS1_v1_5.new(private_key)
signature_bytes = signer.sign(h)
ret_sign = base64.b64encode(signature_bytes).decode('utf-8')
return ret_sign
__last_response = ""
def getLastResponse(self):
return self.__last_response
__last_response_code = 0
def getLastResponseCode(self):
return self.__last_response_code
# Link de referência: https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o#d%C3%A9cimo-primeiro-passo
def __send_data(self, method, endpoint, data, response_code_ok):
self.__last_response = ""
self.__last_response_code = 0
request_time = self.__get_request_time()
body = data
signature = self.__sign_body(
method, endpoint, request_time, body, self.__get_private_key())
if self.print_vars:
print("endpoint: " + method + " - " + endpoint)
print("request_time: " + request_time)
print("api_token: " + self.api_token)
print("body: " + body)
print("signature: " + signature)
import requests
headers = {'Content-type': 'application/json',
'Accept': 'application/json',
'Request-Time': request_time,
'Signature': 'signature='+signature}
if (method.upper() == 'POST'):
r = requests.post('https://api.iugu.com'+endpoint,
data=body, headers=headers)
else:
raise Exception("method "+method+" not implemented")
self.__last_response_code = r.status_code
ret = self.__last_response_code == response_code_ok
self.__last_response = r.text
return ret
# Link de referência: https://dev.iugu.com/reference/validate-signature
def signature_validate(self, data):
method = "POST"
endpoint = "/v1/signature/validate"
return self.__send_data(method, endpoint, data, 200)
def transfer_requests(self, data):
method = "POST"
endpoint = "/v1/transfer_requests"
return self.__send_data(method, endpoint, data, 202)
# ######################################################################################################
######################################################################################################
# Example of use IUGU_RSA_SAMPLE
######################################################################################################
iuru_rsa = IUGU_RSA_SAMPLE()
iuru_rsa.api_token = ""
iuru_rsa.print_vars = True
iuru_rsa.file_private_key = "./private.pem"
# ######################################################################################################
# # signature_validate
# # Link de referência: https://dev.iugu.com/reference/validate-signature
obj = {
"api_token": iuru_rsa.api_token,
"mensagem": "qualquer coisa"
}
if (iuru_rsa.signature_validate(json.dumps(obj))):
print("Response: " + str(iuru_rsa.getLastResponseCode()) + iuru_rsa.getLastResponse())
else:
print("Error: " + str(iuru_rsa.getLastResponseCode()) + iuru_rsa.getLastResponse())
######################################################################################################
######################################################################################################
# transfer_requests
## Link de referência: https://dev.iugu.com/reference/transfer%C3%AAncia-para-terceiros-1 - Consulte esta FAQ caso tenha dúvidas na composição da requisição
obj = {
"api_token": iuru_rsa.api_token,
"transfer_type": "pix",
"amount_cents": 1,
"receiver": {
"pix": {
"key": "00000000000",
"type": "cpf"
}
}
}
if (iuru_rsa.transfer_requests(json.dumps(obj))):
print("Response: " + str(iuru_rsa.getLastResponseCode()) + iuru_rsa.getLastResponse())
else:
print("Error: " + str(iuru_rsa.getLastResponseCode()) + iuru_rsa.getLastResponse())
######################################################################################################