├── README.md ├── app.py └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | A Stripe Demo 2 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from faker import Faker 2 | from flask import Flask, render_template, request, jsonify 3 | import stripe 4 | import requests 5 | 6 | stripe.api_key = '你的sk' 7 | 8 | TURNSTILE_SECRET_KEY = "你的TURNSTILE_SECRET_KEY" 9 | 10 | app = Flask(__name__) 11 | 12 | @app.route('/') 13 | def index(): 14 | return render_template('index.html') 15 | 16 | @app.route('/create-payment-intent', methods=['POST']) 17 | def create_payment(): 18 | try: 19 | # 获取前端发送的 Turnstile token 20 | data = request.json 21 | turnstile_token = data.get("turnstileToken") 22 | 23 | if not turnstile_token: 24 | return jsonify(error="未提供 Turnstile token"), 403 25 | 26 | # 验证 Turnstile token 27 | turnstile_response = requests.post( 28 | "https://challenges.cloudflare.com/turnstile/v0/siteverify", 29 | data={ 30 | "secret": TURNSTILE_SECRET_KEY, 31 | "response": turnstile_token 32 | } 33 | ) 34 | turnstile_result = turnstile_response.json() 35 | 36 | if not turnstile_result.get("success"): 37 | return jsonify(error="Turnstile 验证失败"), 403 38 | 39 | # 如果 Turnstile 验证通过,创建支付意图 40 | amount = 100 41 | fake = Faker('en_US') 42 | intent = stripe.PaymentIntent.create( 43 | amount=amount, 44 | currency='usd', 45 | payment_method_types=['card'], 46 | metadata={ 47 | "first_name": fake.first_name(), 48 | "last_name": fake.last_name() 49 | } 50 | ) 51 | return jsonify({'clientSecret': intent.client_secret}) 52 | except Exception as e: 53 | return jsonify(error=str(e)), 403 54 | 55 | if __name__ == '__main__': 56 | app.run(host="0.0.0.0", port=2000, debug=False) 57 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Stripe Payment 7 | 8 | 9 | 61 | 62 | 63 |
64 |

Stripe Payment

65 |
66 | 73 | 74 |
75 | 76 | 155 | 156 | 157 | --------------------------------------------------------------------------------