All code is provided below!
I am building a website which has a flask_wtf form that the user fills in, upon validation it sends a GET request to an endpoint which in turn creates a stripe checkout session (POST-Redirect-GET pattern). The first time I fill in and submit the form it works perfectly and redirects to Stripe checkout, however subsequent attempts will not redirect to stripe but just to the beginning of the form page again (with everything filled in).
I have already tried different things to try to get it working:
- Use sessions and update the csrf_token after each validated submit.
session['csrf_token'] = generate_csrf()
- Clear form data after each validated submit:
form = RegistrationFormNutrition(formdata=None)
However, both of these methods did not work. Now I am lost and don’t know what to try next to resolve this problem. So I have come here to seek the answers, hopefully one of you can point me in the right direction. The code for the endpoints is provided below.
Endpoint that validates form:
schedule.route('/schedule/fitness', methods=['POST', 'GET'])
def schedule_fitness():
form = RegistrationFormFitness()
if form.validate_on_submit():
schedule_type = 'fitness'
schedule_item_id = create_general_schedule_order_entry(form.data, schedule_type)
session['csrf_token'] = generate_csrf() # tried this, did not work
form = RegistrationFormNutrition(formdata=None) # tried this, did not work
return redirect(
url_for('schedule.initiate_checkout',
email=form.data["email"],
order_id=schedule_item_id,
schedule_type=schedule_type
)
)
return render_template('schedule_fitness.html', form=form)
Endpoint that initiates stripe checkout:
schedule.route('/initiate_checkout', methods=['GET'])
def initiate_checkout():
email = request.args.get('email')
order_id = request.args.get('order_id')
schedule_type = request.args.get('schedule_type')
return create_checkout_session(email, order_id, schedule_type)
Function that creates stripe checkout session:
def create_checkout_session(email, order_id, schedule_type):
stripe.api_key = stripe_keys["secret_key"]
line_items = TEST_PRODUCT
domain_url = "http://localhost/"
try:
checkout_session = stripe.checkout.Session.create(
success_url=domain_url + f"schedule/order?email={email}&order_id={order_id} &schedule_type={schedule_type}",
cancel_url=domain_url + "cancelled",
payment_method_types=["card", "ideal", "paypal"],
mode="payment",
metadata={
"order_id": str(order_id),
"schedule_type": schedule_type
},
line_items=line_items,
)
return redirect(checkout_session.url, code=303)
except Exception as e:
print(e)
return jsonify(error=str(e)), 403
I have already tried different things to try to get it working:
- Use sessions and update the csrf_token after each validated submit. session[‘csrf_token’] = generate_csrf()
- Clear form data after each validated submit: form = RegistrationFormNutrition(formdata=None)
mh_dss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.