-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
127 lines (102 loc) · 3.71 KB
/
app.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
import os
from flask import Flask, render_template, abort, redirect, request
import stripe
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import dotenv
from pyngrok import ngrok
from datetime import datetime
load_dotenv()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Produto(db.Model):
id = db.Column(db.Integer, primary_key=True)
pagador = db.Column(db.String(50), nullable=False)
nome = db.Column(db.String(50), nullable=False)
valor = db.Column(db.Float, nullable=False)
email = db.Column(db.String(100), nullable=False)
data = db.Column(db.DateTime, nullable=False, default=datetime.now())
with app.app_context(): # Criar contexto de aplicação
db.create_all()
stripe.api_key = os.environ['STRIPE_SECRET_KEY']
products = {
'Suporte total': {
'name': 'Pack full - Aprenda Python',
'price': 200000,
},
'Suporte por horas': {
'name': 'Pack horas - Aprenda Python',
'price': 10000,
'por': 'hora',
'adjustable_quantity': {
'enabled': True,
'minimum': 1,
'maximum': 15,
},
},
}
@app.route('/')
def index():
return render_template('index.html', products=products)
@app.route('/order/<product_id>', methods=['POST'])
def order(product_id):
if product_id not in products:
abort(404)
product = products[product_id]
line_item = {
'price_data': {
'product_data': {
'name': product['name'],
},
'unit_amount': product['price'],
'currency': 'BRL',
},
'quantity': 1,
'adjustable_quantity': product.get('adjustable_quantity', {'enabled': False}),
}
checkout_session = stripe.checkout.Session.create(
line_items=[line_item],
payment_method_types=['card'],
mode='payment',
success_url=request.host_url + 'order/success',
cancel_url=request.host_url + 'order/cancel',
)
return redirect(checkout_session.url)
@app.route('/order/success')
def success():
return render_template('success.html')
@app.route('/order/cancel')
def cancel():
return render_template('cancel.html')
@app.route('/event', methods=['POST'])
def new_event():
with app.app_context(): # Criar contexto de aplicação
event = None
payload = request.data
signature = request.headers['STRIPE_SIGNATURE']
try:
event = stripe.Webhook.construct_event(payload, signature, os.environ['STRIPE_WEBHOOK_SECRET'])
except Exception as e:
print(e)
abort(400)
if event['type'] == 'checkout.session.completed':
session = stripe.checkout.Session.retrieve(
event['data']['object'].id, expand=['line_items'])
#print(f'Sale to {session.customer_details.email}:')
for item in session.line_items.data:
#print (item)
print (session.customer_details)
# Criar instância do modelo Produto e salvar no banco de dados
produto = Produto(pagador = session.customer_details.name ,nome=item.description, valor=item.amount_total/100, email=session.customer_details.email)
db.session.add(produto)
db.session.commit()
""" print(f' - {item.quantity} {item.description} '
f'${item.amount_total/100:.02f} {item.currency.upper()}') """
return {'success': True}
public_url = ngrok.connect(5000)
# Acessar a URL pública gerada pelo ngrok
print("URL pública do ngrok: ", public_url)
if __name__ == '__main__':
app.run()