Compare commits
1 Commits
main
...
4da1d1233c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4da1d1233c |
16
.env.example
16
.env.example
@@ -1,16 +0,0 @@
|
|||||||
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
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
|||||||
.env
|
|
||||||
*.pem
|
|
||||||
venv
|
|
||||||
74
bbpay.py
74
bbpay.py
@@ -15,36 +15,40 @@ from eth_utils import to_wei
|
|||||||
|
|
||||||
# Load environment variables from .env file
|
# Load environment variables from .env file
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
ENVIRONMENT = getenv('ENVIRONMENT', 'dev')
|
|
||||||
dotenv_path = f'.env.{ENVIRONMENT}'
|
|
||||||
load_dotenv(dotenv_path)
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app, origins=['https://p2pix.co'])
|
CORS(app)
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
|
|
||||||
class BBPay(Resource):
|
class BBPay(Resource):
|
||||||
def setup_oauth(self):
|
def setup_oauth(self):
|
||||||
client = BackendApplicationClient(client_id=getenv("CLIENT_ID"))
|
client = BackendApplicationClient(client_id=getenv("CLIENT_ID"))
|
||||||
self.oauth = OAuth2Session(client=client)
|
self.oauth = OAuth2Session(client=client)
|
||||||
|
|
||||||
self.cert = 'key.pem'
|
self.cert = 'key.pem'
|
||||||
self.verify_ssl = 'bb.pem'
|
self.verify_ssl = 'bb.pem'
|
||||||
self.baseUrl = getenv("ITP_API_URL")
|
|
||||||
self.token_url = getenv("ITP_TOKEN_URL")
|
# 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.params = {
|
self.params = {
|
||||||
'numeroConvenio': getenv("BBPAY_CONVENIO"),
|
'numeroConvenio': 701,
|
||||||
'gw-dev-app-key': getenv("DEV_APP_KEY")
|
'gw-dev-app-key': getenv("DEV_APP_KEY")
|
||||||
}
|
}
|
||||||
|
|
||||||
self.scope = ['checkout.solicitacoes-requisicao',
|
self.scope = ['checkout.solicitacoes-requisicao',
|
||||||
'checkout.participantes-requisicao',
|
'checkout.participantes-requisicao',
|
||||||
'checkout.solicitacoes-info',
|
'checkout.solicitacoes-info',
|
||||||
'checkout.participantes-info']
|
'checkout.participantes-info']
|
||||||
|
|
||||||
self.oauth.fetch_token(
|
self.oauth.fetch_token(
|
||||||
token_url=self.token_url,
|
token_url='https://oauth.hm.bb.com.br/oauth/token',
|
||||||
client_id=getenv("CLIENT_ID"),
|
client_id=getenv("CLIENT_ID"),
|
||||||
client_secret=getenv("CLIENT_SECRET"), scope=self.scope)
|
client_secret=getenv("CLIENT_SECRET"), scope=self.scope)
|
||||||
|
|
||||||
@@ -55,9 +59,8 @@ class BBPay(Resource):
|
|||||||
class Register(BBPay):
|
class Register(BBPay):
|
||||||
def post(self):
|
def post(self):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
app.logger.debug(data)
|
body = {
|
||||||
body = {
|
'numeroConvenio': 701,
|
||||||
'numeroConvenio': getenv("BBPAY_CONVENIO"),
|
|
||||||
'nomeParticipante': data['chainID'],
|
'nomeParticipante': data['chainID'],
|
||||||
'tipoDocumento': data['tipoDocumento'],
|
'tipoDocumento': data['tipoDocumento'],
|
||||||
'numeroDocumento': data['numeroDocumento'],
|
'numeroDocumento': data['numeroDocumento'],
|
||||||
@@ -66,29 +69,23 @@ class Register(BBPay):
|
|||||||
'tipoConta': data['tipoConta'],
|
'tipoConta': data['tipoConta'],
|
||||||
'codigoIspb': data['codigoIspb'] # Código identificador do Sistema de Pagamentos Brasileiro. Atualmente aceitamos apenas Banco do Brasil, codigoIspb igual a 0
|
'codigoIspb': data['codigoIspb'] # Código identificador do Sistema de Pagamentos Brasileiro. Atualmente aceitamos apenas Banco do Brasil, codigoIspb igual a 0
|
||||||
}
|
}
|
||||||
app.logger.debug(body)
|
|
||||||
response = self.oauth.post(
|
response = self.oauth.post(
|
||||||
self.baseUrl+"/participantes",
|
self.baseUrl+"/participantes",
|
||||||
params=self.params,
|
params=self.params,
|
||||||
json=body,
|
json=body,
|
||||||
verify=self.verify_ssl,
|
verify=self.verify_ssl,
|
||||||
cert=self.cert)
|
cert=self.cert)
|
||||||
if response.status_code != 201:
|
|
||||||
app.logger.debug(response._content)
|
|
||||||
return response.reason, response.status_code
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
class Request(BBPay):
|
class Request(BBPay):
|
||||||
def post(self):
|
def post(self):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
app.logger.debug(data)
|
|
||||||
body = {
|
body = {
|
||||||
"geral": {
|
"geral": {
|
||||||
"numeroConvenio": getenv("BBPAY_CONVENIO"),
|
"numeroConvenio": 701,
|
||||||
"pagamentoUnico": True,
|
"pagamentoUnico": True,
|
||||||
"descricaoSolicitacao": "P2Pix",
|
"descricaoSolicitacao": "P2Pix",
|
||||||
"valorSolicitacao": data['amount'],
|
"valorSolicitacao": data['amount']
|
||||||
#"codigoConciliacaoSolicitacao": data['lockid']
|
|
||||||
},
|
},
|
||||||
# "devedor": {
|
# "devedor": {
|
||||||
# "tipoDocumento": 1,
|
# "tipoDocumento": 1,
|
||||||
@@ -97,35 +94,29 @@ class Request(BBPay):
|
|||||||
"formasPagamento": [{ "codigoTipoPagamento": "PIX", "quantidadeParcelas": 1}],
|
"formasPagamento": [{ "codigoTipoPagamento": "PIX", "quantidadeParcelas": 1}],
|
||||||
"repasse": {
|
"repasse": {
|
||||||
"tipoValorRepasse": "Percentual",
|
"tipoValorRepasse": "Percentual",
|
||||||
"recebedores": [{
|
"recebedores": [{
|
||||||
"identificadorRecebedor": data['pixTarget'],
|
"identificadorRecebedor": data['pixTarget'],
|
||||||
"tipoRecebedor": "Participante",
|
"tipoRecebedor": "Participante",
|
||||||
"valorRepasse": 100 }]
|
"valorRepasse": 100 }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
app.logger.debug(body)
|
|
||||||
response = self.oauth.post(
|
response = self.oauth.post(
|
||||||
self.baseUrl+"/solicitacoes",
|
self.baseUrl+"/solicitacoes",
|
||||||
params=self.params,
|
params=self.params,
|
||||||
json=body,
|
json=body,
|
||||||
verify=self.verify_ssl,
|
verify=self.verify_ssl,
|
||||||
cert=self.cert)
|
cert=self.cert)
|
||||||
app.logger.debug(response._content)
|
|
||||||
if response.status_code != 201:
|
|
||||||
return response.reason, response.status_code
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
class Release(BBPay):
|
class Release(BBPay):
|
||||||
def get(self, numeroSolicitacao):
|
def get(self, numeroSolicitacao):
|
||||||
app.logger.debug(request.get_json())
|
|
||||||
response = self.oauth.get(
|
response = self.oauth.get(
|
||||||
self.baseUrl+f"/solicitacoes/{numeroSolicitacao}",
|
self.baseUrl+f"/solicitacoes/{numeroSolicitacao}",
|
||||||
params=self.params,
|
params=self.params,
|
||||||
verify=self.verify_ssl,
|
verify=self.verify_ssl,
|
||||||
cert=self.cert)
|
cert=self.cert)
|
||||||
app.logger.debug(response._content)
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return response.reason, response.status_code
|
return 'Upstream error', response.status_code
|
||||||
data = response.json()
|
data = response.json()
|
||||||
numeroParticipante = data['repasse']['recebedores'][0]['identificadorRecebedor']
|
numeroParticipante = data['repasse']['recebedores'][0]['identificadorRecebedor']
|
||||||
pixTimestamp = encode_packed(['bytes32'],[b85decode(data['informacoesPix']['txId'])])
|
pixTimestamp = encode_packed(['bytes32'],[b85decode(data['informacoesPix']['txId'])])
|
||||||
@@ -141,16 +132,16 @@ class Release(BBPay):
|
|||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return 'Upstream error', response.status_code
|
return 'Upstream error', response.status_code
|
||||||
chainID = response.json()['nomeParticipante']
|
chainID = response.json()['nomeParticipante']
|
||||||
packed = encode_packed(['bytes32','uint80','bytes32'],
|
packed = encode_packed(['bytes32','uint80','bytes32'],
|
||||||
(f"{chainID}-{numeroParticipante}".encode(), int(valorSolicitacao), pixTimestamp) )
|
(f"{chainID}-{numeroParticipante}".encode(), int(valorSolicitacao), pixTimestamp) )
|
||||||
signable = eth_account.messages.encode_defunct(keccak(packed))
|
signable = eth_account.messages.encode_defunct(keccak(packed))
|
||||||
signature = eth_account.account.Account.sign_message(signable, private_key=getenv('PRIVATE_KEY')).signature.hex()
|
signature = eth_account.account.Account.sign_message(signable, private_key=getenv('PRIVATE_KEY')).signature.hex()
|
||||||
return {
|
return {
|
||||||
'pixTarget': f"{chainID}-{numeroParticipante}",
|
'pixTarget': f"{chainID}-{numeroParticipante}",
|
||||||
'amount': str(valorSolicitacao),
|
'amount': str(valorSolicitacao),
|
||||||
'pixTimestamp': f"0x{pixTimestamp.hex()}",
|
'pixTimestamp': pixTimestamp.hex(),
|
||||||
'signature': f"0x{signature}"
|
'signature': signature }
|
||||||
}
|
|
||||||
|
|
||||||
# (CPF, nome, conta) -> participantID
|
# (CPF, nome, conta) -> participantID
|
||||||
# should be called before deposit
|
# should be called before deposit
|
||||||
@@ -158,16 +149,19 @@ api.add_resource(Register, '/register')
|
|||||||
|
|
||||||
# (amount,pixtarget) -> requestID, QRcodeText
|
# (amount,pixtarget) -> requestID, QRcodeText
|
||||||
# should be called after lock
|
# should be called after lock
|
||||||
api.add_resource(Request, '/request')
|
api.add_resource(Request, '/request')
|
||||||
|
|
||||||
# (requestID) -> sig(pixTarget, amount, pixTimestamp)
|
# (requestID) -> sig(pixTarget, amount, pixTimestamp)
|
||||||
# should be called before release
|
# should be called before release
|
||||||
api.add_resource(Release, '/release/<int:numeroSolicitacao>')
|
api.add_resource(Release, '/release/<int:numeroSolicitacao>')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if getenv("DEBUG"):
|
if getenv("DEBUG"):
|
||||||
disable_warnings()
|
disable_warnings()
|
||||||
app.run(host='::1', debug=True, port=getenv("PORT",5000))
|
app.run(debug=True)
|
||||||
else:
|
else:
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
serve(app, host=getenv("HOST","::1"), port=getenv("PORT",5000))
|
serve(app, host=getenv("HOST","0.0.0.0"), port=getenv("PORT",5000))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user