I am trying to integrate Stripe Checkout in a flutter web app. Every time I send the POST request, I keep getting the CORS blocked error. I have been dealing with this problem for days now. I have tried every solution I could find online. I have tried adding Flask_Cors, cross_origin. I have tried adding the headers. Nothing seems to work. I am pretty new to backend stuff so I am not sure whats going on. Here is my Flutter code:
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);
});
}
Here is the server code(hosted on Heroku):
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)
Any help is greatly appreciated.