-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (35 loc) · 1.18 KB
/
server.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
#! /usr/bin/env python3.6
"""
Python 3.6 or newer required.
"""
import json
import stripe
# This is your test secret API key.
stripe.api_key = 'sk_test_uaxz8O6EqdRdgSy5uC5FcRFb'
from flask import Flask, jsonify, request
app = Flask(__name__, static_folder='public',
static_url_path='', template_folder='public')
def calculate_order_amount(items):
# Replace this constant with a calculation of the order's amount
# Calculate the order total on the server to prevent
# people from directly manipulating the amount on the client
return 1400
@app.route('/create-payment-intent', methods=['POST'])
def create_payment():
try:
data = json.loads(request.data)
# Create a PaymentIntent with the order amount and currency
intent = stripe.PaymentIntent.create(
amount=calculate_order_amount(data['items']),
currency='eur',
automatic_payment_methods={
'enabled': True,
},
)
return jsonify({
'clientSecret': intent['client_secret']
})
except Exception as e:
return jsonify(error=str(e)), 403
if __name__ == '__main__':
app.run(port=4242)