クレジット カード情報を収集する登録フォームがあります。ワークフローは次のとおりです。
- ユーザーは、ストライプを通じて登録データとカード データを入力します。
- フォームの登録データが検証されます。
- フォームが有効な場合、支払いが処理されます。
- 決済が通ればOK、ユーザー登録完了です。
- 支払いが失敗した場合、フォームの非表示フィールドで検証エラーを発生できるようにしたいと考えています。それは可能ですか?
フォーム送信コードは次のとおりです。
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)