-
Notifications
You must be signed in to change notification settings - Fork 0
/
airplane_mode.py
86 lines (69 loc) · 2.59 KB
/
airplane_mode.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
from flask import Flask, request, render_template, url_for, redirect, session, flash
import stripe
import os
from forms import DonateForm
app = Flask(__name__)
STRIPE_KEYS={'sk':os.environ.get('STRIPE_SK'),
'pk':os.environ.get('STRIPE_PK')}
stripe.api_key = STRIPE_KEYS['sk']
app.secret_key = os.environ.get('SECRET_KEY')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/donate', methods = ['POST', 'GET'])
def donate():
form = DonateForm()
if form.validate_on_submit():
amount = form.amount.data
email = form.email.data
amount = str(amount)
if ".0" in amount[-2:]:
amount = int(float(amount))
session['amount_cents'] = amount*100 #int(amount+"00")
session['amount_display'] = "{}.00".format(amount)
else:
a,b = str(amount).split('.')
b = b[:2]
session['amount_cents'] = int(a+b)
session['amount_display'] = "{}.{}".format(a,b)
session['email'] = email
return redirect(url_for('confirm'))
return render_template('donate.html', form = form)
@app.route('/confirm')
def confirm():
key = STRIPE_KEYS['pk']
return render_template('confirm.html', amount_cents = session['amount_cents'],
amount_display=session['amount_display'],
key =key,
email=session['email'])
@app.route('/pay', methods=['POST'])
def pay():
try:
if request.form['stripeTokenType'] == 'bitcoin_receiver':
customer = stripe.Customer.create(
email=session['email'],
source=request.form['stripeToken']
)
else:
customer = stripe.Customer.create(
email=session['email'],
card=request.form['stripeToken'], # having errors getting stripe token
)
charge = stripe.Charge.create(
customer=customer.id,
amount=session['amount_cents'],
currency='usd',
description=customer.email
)
session.pop('email',0)
session.pop('amount_cents',0)
session.pop('amount_display',0)
except stripe.error.CardError as e:
flash("There seems to have been a problem charging your card...")
return render_template('error.html')
except stripe.error.StripeError:
flash("Ooops something went wrong! Not quite sure what...")
return render_template('error.html')
return render_template('thanks.html')
if __name__ == '__main__':
app.run() #debug=True