Flutter Web集成Stripe Checkout时遇Flask CORS拦截问题求助
Flutter Web集成Stripe Checkout遭遇CORS拦截问题求助
我正在Flutter Web应用中集成Stripe Checkout,但每次发送POST请求都会触发CORS拦截错误,已经卡了好几天。试过网上能找到的各种方案,包括添加Flask_Cors、cross_origin装饰器、设置请求头,都没用。我是后端新手,不知道问题出在哪。
Flutter前端代码
Future checkout(double amount) async { var url = Uri.parse('https://spaceshuttleparking-checkout.herokuapp.com/create-checkout-session'); final response = await http.post( headers:{ "Accept": "application/json", "Access-Control-Allow-Origin": "*", "Access-Origin-Allow-Methods": "GET, DELETE, HEAD, OPTIONS, POST" }, url, body: json.encode( { 'parking':{ 'name':'Parking Space', 'amount':amount, } } )).then((value) { print(value.body); print(value.statusCode); print(value.request); }); }
Heroku部署的Flask后端代码
import json import os import stripe from flask import Flask, render_template, jsonify, request, redirect from flask_cors import CORS, cross_origin # This is your test secret API key. stripe.api_key = 'sk_test_51LfcOSB1SRO7vxqKhIxQE6vTmXKkYTBJoqXGNHYCwI0nCosmQpVG8MJb7TJala9hjOUZIDh8A9XPgJ1FYTETJBO700uWTUBSbG' app = Flask(__name__, static_folder='public',static_url_path='', template_folder='public') app.debug = True CORS(app, resources={r"*": {"origins": "*"}}) app.config['CORS_HEADERS'] = 'Content-Type' 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-checkout-session', methods=['POST', 'GET']) @cross_origin(supports_credentials=True) def create_checkout_session(): request_data = request.data request_data = json.loads(request_data.decode('utf-8')) try: parking = request_data['parking'] session = stripe.checkout.Session.create( line_items = [ { 'price_data': { 'currency':'aud', 'product_data': { 'name': parking['name'], }, 'unit_amount': parking['amount'], }, 'quantity':1, } ], mode='payment', success_url='https://spaceshuttleparking-checkout.herokuapp.com/success', cancel_url='https://spaceshuttleparking-checkout.herokuapp.com/cancel' ) return redirect(session.url, code=303) except Exception as e: return jsonify(error=str(e)), 403 if __name__ == '__main__': app.run(port=4242)
内容的提问来源于stack exchange,提问作者Danish Ajaib
相关产品推荐
相关产品推荐

