-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.py
53 lines (38 loc) · 1.35 KB
/
backend.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
import os
import stripe
from flask import Flask, render_template, request
PUBLISHABLE_KEY = os.getenv('STRIPE_PUBLISHABLE_KEY', None)
SECRET_KEY = os.getenv('STRIPE_SECRET_KEY', None)
DEBUG = False
# Configure Flask
application = Flask(__name__)
# Configure Stripe
stripe.api_key = SECRET_KEY
def render_response(kind, message):
return '["{0}","{1}"]'.format(kind, message)
@application.route('/')
def index():
return render_template('frontend.html', **{'pk': PUBLISHABLE_KEY})
@application.route('/create_and_charge_customer', methods=['POST'])
def create_and_charge_customer():
token = request.form['token']
email = request.form['email']
amount_in_dollars = float(request.form['amount'])
amount_in_cents = int(amount_in_dollars)
try:
customer = stripe.Customer.create(email=email, source=token)
customer_id = customer['id']
charge = stripe.Charge.create(
amount=amount_in_cents,
customer=customer_id,
currency='aud'
)
except stripe.error.StripeError as e:
#body = e.json_body
return render_response("error", e)
except Exception as e:
return render_response("error", "backend error")
return render_response("success", "You made a successful payment!")
if __name__ == '__main__':
application.debug = DEBUG
application.run()