-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·85 lines (68 loc) · 2.46 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
from flask import Flask, request, url_for, redirect, json, abort
import urllib
import requests
import mailchimp
import stripe
app = Flask(__name__)
app.config.from_pyfile('settings.cfg')
stripe.api_key = app.config.get('STRIPE_SECRET_KEY')
if app.config.get('MAILCHIMP_APIKEY'):
mailchimp = mailchimp.Mailchimp(app.config['MAILCHIMP_APIKEY'])
@app.route('/')
def index():
return 'Kaiwa App'
@app.route('/oauth/start')
def start():
qs = urllib.urlencode({
'client_id': app.config['DO_CLIENT_ID'],
'redirect_uri': url_for('callback', _external=True, _scheme='https'),
'response_type': 'code',
'scope': 'read write'
})
return redirect('https://cloud.digitalocean.com/v1/oauth/authorize?' + qs)
@app.route('/oauth/callback')
def callback():
code = request.values['code']
r = requests.post('https://cloud.digitalocean.com/v1/oauth/token', data={
'grant_type': 'authorization_code',
'code': code,
'client_id': app.config['DO_CLIENT_ID'],
'client_secret': app.config['DO_CLIENT_SECRET'],
'redirect_uri': url_for('callback', _external=True)
})
data = r.json()
return redirect(app.config['CALLBACK_REDIRECT'] + '?token=' + data['access_token'])
@app.route('/charge', methods=['POST'])
def charge():
success_resp = (json.dumps({"success": True}), 200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "POST"})
if not app.config.get('STRIPE_SECRET_KEY'):
return success_resp
amount = int(float(request.form['amount']) * 100)
if amount < 200:
abort(400)
if app.config.get('MAILCHIMP_LIST'):
try:
mailchimp.lists.subscribe(app.config['MAILCHIMP_LIST'],
{'email': request.form['email']},
double_optin=False, update_existing=True, send_welcome=False)
except Exception as e:
app.logger.error(e)
try:
charge = stripe.Charge.create(
source=request.form['token'],
amount=amount,
currency='usd',
description="Kaiwa contribution",
metadata={"email": request.form['email']},
receipt_email=request.form['email']
)
except stripe.error.StripeError as e:
app.logger.error(e)
abort(400)
return success_resp
if __name__ == '__main__':
app.run(debug=True)