-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
93 lines (71 loc) · 2.54 KB
/
run.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
import os
import stripe
import pprint
from flask import Flask, render_template, jsonify, request, Response
STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY")
STRIPE_PUBLISHABLE_KEY = os.environ.get("STRIPE_PUBLISHABLE_KEY")
VERIFIED = False
app = Flask(__name__)
stripe.api_key = STRIPE_SECRET_KEY
class LoggingMiddleware(object):
def __init__(self, app):
self._app = app
def __call__(self, environ, resp):
errorlog = environ['wsgi.errors']
pprint.pprint(('REQUEST', environ), stream=errorlog)
def log_response(status, headers, *args):
pprint.pprint(('RESPONSE', status, headers), stream=errorlog)
return resp(status, headers, *args)
return self._app(environ, log_response)
@app.route("/")
def index():
# register_domain(request.host.split(':')[0])
return render_template("index.html", **{
"STRIPE_PUBLISHABLE_KEY": STRIPE_PUBLISHABLE_KEY
})
@app.route("/.well-known/apple-developer-merchantid-domain-association")
def verify():
with open('static/apple-developer-merchantid-domain-association') as fo:
return Response(fo.read(), mimetype="text/plain")
return ""
@app.route("/charge", methods=["POST"])
def charge():
token = request.form.get("token")
amount = int(float(request.form.get("amount")) * 100) # convert to int for Stripe
if not token or not amount:
return "ERROR", 400
# Create Customer
cus = stripe.Customer.create(
description="Apple Pay for the Web Test",
source=token,
email="test@test.test",
metadata={
"token": token,
}
)
# Create Charge
ch = stripe.Charge.create(
amount=amount,
customer=cus.id,
currency="usd"
)
return jsonify({'charge': ch, 'customer': cus})
# DOMAIN REGISTRATION #
# def register_domain(domain):
# global VERIFIED
# if not VERIFIED:
# print "Attempting to register <{}>...".format(domain)
# try:
# stripe.ApplePayDomain.create(domain_name=domain)
# print "Successful registration!"
# except Exception as e:
# pass
# VERIFIED = True
# END DOMAIN REGISTRATION #
if __name__ == "__main__":
if not STRIPE_SECRET_KEY:
raise ValueError("Please pass the `STRIPE_SECRET_KEY` envionment variable.")
if not STRIPE_PUBLISHABLE_KEY:
raise ValueError("Please pass the `STRIPE_PUBLISHABLE_KEY` envionment variable.")
app.wsgi_app = LoggingMiddleware(app.wsgi_app)
app.run(host='0.0.0.0', port=5000, debug=True)