Compare commits
2 Commits
2729be8d2c
...
60a22e1b0b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60a22e1b0b | ||
|
|
68f33a3249 |
16
.env.example
Normal file
16
.env.example
Normal file
@ -0,0 +1,16 @@
|
||||
PRIVATE_KEY=0x....
|
||||
CLIENT_ID=
|
||||
CLIENT_SECRET=
|
||||
DEV_APP_KEY=
|
||||
DEBUG=false
|
||||
ITP_TOKEN_URL = https://oauth.hm.bb.com.br/oauth/token
|
||||
# Url de produção com autenticação mTLS.
|
||||
#ITP_API_URL="https://api-bbpay.bb.com.br/checkout/v2"
|
||||
# Url de homologação com autenticação mTLS.
|
||||
ITP_API_URL = "https://api-bbpay.hm.bb.com.br/checkout/v2"
|
||||
# Url de homologação sem autenticação mTLS.
|
||||
#ITP_API_URL = "https://api.extranet.hm.bb.com.br/checkout/v2"
|
||||
|
||||
# 05/08/2025
|
||||
#ITP_API_URL=https://checkout.mtls.api.bb.com.br
|
||||
#ITP_API_URL=https://checkout.mtls.api.hm.bb.com.br
|
||||
46
bbpay.py
46
bbpay.py
@ -20,35 +20,28 @@ app = Flask(__name__)
|
||||
CORS(app)
|
||||
api = Api(app)
|
||||
|
||||
class BBPay(Resource):
|
||||
class BBPay(Resource):
|
||||
def setup_oauth(self):
|
||||
client = BackendApplicationClient(client_id=getenv("CLIENT_ID"))
|
||||
self.oauth = OAuth2Session(client=client)
|
||||
|
||||
self.cert = 'key.pem'
|
||||
self.verify_ssl = 'bb.pem'
|
||||
|
||||
# Url de homologação com autenticação mTLS.
|
||||
self.baseUrl = "https://api-bbpay.hm.bb.com.br/checkout/v2"
|
||||
|
||||
# Url de homologação sem autenticação mTLS.
|
||||
#self.baseUrl = "https://api.extranet.hm.bb.com.br/checkout/v2"
|
||||
|
||||
# Url de produção com autenticação mTLS.
|
||||
#self.baseUrl = "https://api-bbpay.bb.com.br/checkout/v2"
|
||||
self.baseUrl = getenv("ITP_API_URL")
|
||||
self.token_url = getenv("ITP_TOKEN_URL")
|
||||
|
||||
self.params = {
|
||||
'numeroConvenio': 701,
|
||||
'gw-dev-app-key': getenv("DEV_APP_KEY")
|
||||
}
|
||||
|
||||
self.scope = ['checkout.solicitacoes-requisicao',
|
||||
'checkout.participantes-requisicao',
|
||||
'checkout.solicitacoes-info',
|
||||
self.scope = ['checkout.solicitacoes-requisicao',
|
||||
'checkout.participantes-requisicao',
|
||||
'checkout.solicitacoes-info',
|
||||
'checkout.participantes-info']
|
||||
|
||||
self.oauth.fetch_token(
|
||||
token_url='https://oauth.hm.bb.com.br/oauth/token',
|
||||
token_url=self.token_url,
|
||||
client_id=getenv("CLIENT_ID"),
|
||||
client_secret=getenv("CLIENT_SECRET"), scope=self.scope)
|
||||
|
||||
@ -59,7 +52,7 @@ class BBPay(Resource):
|
||||
class Register(BBPay):
|
||||
def post(self):
|
||||
data = request.get_json()
|
||||
body = {
|
||||
body = {
|
||||
'numeroConvenio': 701,
|
||||
'nomeParticipante': data['chainID'],
|
||||
'tipoDocumento': data['tipoDocumento'],
|
||||
@ -70,11 +63,13 @@ class Register(BBPay):
|
||||
'codigoIspb': data['codigoIspb'] # Código identificador do Sistema de Pagamentos Brasileiro. Atualmente aceitamos apenas Banco do Brasil, codigoIspb igual a 0
|
||||
}
|
||||
response = self.oauth.post(
|
||||
self.baseUrl+"/participantes",
|
||||
params=self.params,
|
||||
self.baseUrl+"/participantes",
|
||||
params=self.params,
|
||||
json=body,
|
||||
verify=self.verify_ssl,
|
||||
cert=self.cert)
|
||||
if response.status_code != 201:
|
||||
return 'Upstream error', response.status_code
|
||||
return response.json()
|
||||
|
||||
class Request(BBPay):
|
||||
@ -85,7 +80,8 @@ class Request(BBPay):
|
||||
"numeroConvenio": 701,
|
||||
"pagamentoUnico": True,
|
||||
"descricaoSolicitacao": "P2Pix",
|
||||
"valorSolicitacao": data['amount']
|
||||
"valorSolicitacao": data['amount'],
|
||||
"codigoConciliacaoSolicitacao": data['lockid']
|
||||
},
|
||||
# "devedor": {
|
||||
# "tipoDocumento": 1,
|
||||
@ -94,7 +90,7 @@ class Request(BBPay):
|
||||
"formasPagamento": [{ "codigoTipoPagamento": "PIX", "quantidadeParcelas": 1}],
|
||||
"repasse": {
|
||||
"tipoValorRepasse": "Percentual",
|
||||
"recebedores": [{
|
||||
"recebedores": [{
|
||||
"identificadorRecebedor": data['pixTarget'],
|
||||
"tipoRecebedor": "Participante",
|
||||
"valorRepasse": 100 }]
|
||||
@ -106,6 +102,8 @@ class Request(BBPay):
|
||||
json=body,
|
||||
verify=self.verify_ssl,
|
||||
cert=self.cert)
|
||||
if response.status_code != 201:
|
||||
return 'Upstream error', response.status_code
|
||||
return response.json()
|
||||
|
||||
class Release(BBPay):
|
||||
@ -132,7 +130,7 @@ class Release(BBPay):
|
||||
if response.status_code != 200:
|
||||
return 'Upstream error', response.status_code
|
||||
chainID = response.json()['nomeParticipante']
|
||||
packed = encode_packed(['bytes32','uint80','bytes32'],
|
||||
packed = encode_packed(['bytes32','uint80','bytes32'],
|
||||
(f"{chainID}-{numeroParticipante}".encode(), int(valorSolicitacao), pixTimestamp) )
|
||||
signable = eth_account.messages.encode_defunct(keccak(packed))
|
||||
signature = eth_account.account.Account.sign_message(signable, private_key=getenv('PRIVATE_KEY')).signature.hex()
|
||||
@ -143,26 +141,22 @@ class Release(BBPay):
|
||||
'signature': f"0x{signature}"
|
||||
}
|
||||
|
||||
|
||||
# (CPF, nome, conta) -> participantID
|
||||
# should be called before deposit
|
||||
api.add_resource(Register, '/register')
|
||||
|
||||
# (amount,pixtarget) -> requestID, QRcodeText
|
||||
# should be called after lock
|
||||
api.add_resource(Request, '/request')
|
||||
api.add_resource(Request, '/request')
|
||||
|
||||
# (requestID) -> sig(pixTarget, amount, pixTimestamp)
|
||||
# should be called before release
|
||||
api.add_resource(Release, '/release/<int:numeroSolicitacao>')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if getenv("DEBUG"):
|
||||
disable_warnings()
|
||||
app.run(debug=True)
|
||||
else:
|
||||
from waitress import serve
|
||||
serve(app, host=getenv("HOST","0.0.0.0"), port=getenv("PORT",5000))
|
||||
|
||||
|
||||
serve(app, host=getenv("HOST","0.0.0.0"), port=getenv("PORT",5000))
|
||||
Loading…
x
Reference in New Issue
Block a user