I have a registration form that collects credit card info. The workflow is as follows:
Here's a the form submission code:
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(
[...]
)
db.session.add(user)
#Charge
amount = 10000
customer = stripe.Customer.create(
email=job.company_email,
card=request.form['stripeToken']
)
try:
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Registration payment'
)
except StripeError as e:
***I want to raise a form validation error here if possible.***
db.session.commit()
return redirect(url_for('home'))
return render_template('register.html', form=form)
I solved it by manually appending errors to the field i wanted.
It looks like that
try:
[...]
except StripeError as e:
form.payment.errors.append('the error message')
else:
db.session.commit()
return redirect(url_for('home'))