-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (55 loc) · 1.98 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
from __future__ import print_function
from flask import render_template, request, Flask
import os
import stripe
from pprint import pprint
# set the project root directory as the static folder, you can set others.
app = Flask(__name__)
app.config['SECRET_KEY'] = 'this-is-a-stripe-application'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Provide your Stripe keys here
STRIPE_SECRET_KEY = ''
PUBLISHABLE_SECRET_KEY = ''
CURRENCY = 'usd' # Change it if you need
if not STRIPE_SECRET_KEY:
print("Please provide STRIPE_SECRET_KEY")
exit(-1)
if not PUBLISHABLE_SECRET_KEY:
print("Please provide PUBLISHABLE_SECRET_KEY ")
exit(-1)
stripe.api_key = STRIPE_SECRET_KEY
# Provide your domain name here
YOUR_DOMAIN = 'http://localhost:5000'
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
amount = request.form.get('amount')
email = request.form.get('email')
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
mode='payment',
line_items=[{
'price_data': {
'currency': CURRENCY,
'product_data': {
'name': 'Test Payment',
},
'unit_amount': int(float(amount)*100),
},
'quantity': 1,
}],
success_url=YOUR_DOMAIN + '/success',
cancel_url=YOUR_DOMAIN + '/cancel',
)
return render_template('checkout.html', key=PUBLISHABLE_SECRET_KEY,
amount=amount, currency=CURRENCY.upper(),
checkout_session_id=checkout_session.id)
return render_template("index.html")
@app.route('/cancel', methods=['GET'])
def cancel():
return render_template('cancel.html')
@app.route('/success', methods=['GET'])
def charge():
return render_template('success.html')
if __name__ == "__main__":
app.run(debug=True)