-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (66 loc) · 2.45 KB
/
main.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
import names
import starkbank
from flask import Flask
from flask.globals import request
from random import randint
from cpf_generator import CPF
from datetime import datetime, timedelta
from flask_apscheduler import APScheduler
ambiente = ""
idProjeto = ""
chavePrivada = ""
contaPrincipal = ""
# Criacao da variavel project para uso da API
projeto = starkbank.Project(environment=ambiente,
id=idProjeto,
private_key=chavePrivada)
starkbank.user = projeto
app = Flask(__name__)
# Scheduler para chamar gerador de invoice a cada 3 horas
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
# Efetuar a transferencia de um valor determinado para conta principal stark
def transferir(valor, nome):
transferencia = starkbank.Transaction(
amount=valor,
receiver_id=contaPrincipal,
description=f"{nome} | Carteira Intermediaria -> Carteira Principal",
external_id=str(randint(1000000, 100000000)))
starkbank.transaction.create([transferencia])
# Webhook para verificar e atualizar a transferencia a cada invoice
@app.route('/hook', methods=['POST'])
def hook():
if request.method == 'POST':
log = request.json['event']['log']
valor = log['invoice']['amount']
nome = log['invoice']['name']
### Debug ###
print("---------------------------")
print(log['type'])
print(valor)
print(nome)
print("---------------------------")
#############
if log['type'] == 'credited':
transferir(valor - 50, nome)
return 'OK', 200
# A cada 3 horas, enviar de 8 a 12 invoices
@scheduler.task("interval", id='invoices', hours=3)
def gerar_invoices():
invoice_list = []
# Loop para criar invoice com cpf valido, nome e valores aleatorios para cada
for i in range(randint(8, 12)):
invoice_list.append(
starkbank.Invoice(amount=randint(10000, 100000),
name=names.get_full_name(),
tax_id=CPF.format(CPF.generate()),
due=datetime.utcnow() + timedelta(hours=24),
expiration=timedelta(hours=48).total_seconds(),
fine=randint(1, 20),
interest=1.5))
starkbank.invoice.create(invoice_list)
return None
if __name__ == '__main__':
gerar_invoices()
app.run(host='0.0.0.0', port=80)