【问题标题】:Flask view to handle Stripe request can't find stripeToken [duplicate]处理Stripe请求的Flask视图找不到stripeToken [重复]
【发布时间】:2015-10-07 08:06:26
【问题描述】:

我正在尝试将 Stripe 与 Flask 集成,但页面加载后立即出现以下错误。

The browser (or proxy) sent a request that this server could not understand.

为什么会出现此错误,我该如何解决?

@store.route('/payment',methods=['GET', 'POST'])
def payment():
    stripe.api_key = "test_key"
    token = request.form['stripeToken']

    try:
        charge = stripe.Charge.create(
            amount=1000,
            currency="cad",
            source=token,
            description="Example charge"
        )
    except stripe.error.CardError, e:
        pass

    return render_template("payment_form.html")
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
    Stripe.setPublishableKey('test_key');

    jQuery(function($) {
        $('#payment-form').submit(function(event) {
            var $form = $(this);
            $form.find('button').prop('disabled', true);
            Stripe.card.createToken($form, stripeResponseHandler);
            return false;
        });
    });

    function stripeResponseHandler(status, response) {
        var $form = $('#payment-form');

        if (response.error) {
            $form.find('.payment-errors').text(response.error.message);
            $form.find('button').prop('disabled', false);
        } else {
            var token = response.id;
            $form.append($('<input type="hidden" name="stripeToken" />').val(token));
            $form.get(0).submit();
        }
    };
</script>

【问题讨论】:

    标签: python flask stripe-payments


    【解决方案1】:

    页面加载后立即失败的原因是加载页面是GET 请求。 request.form 填写在 POST 请求上,该请求仅在您提交表单时发生。 request.form 是一个特殊的字典,它会引发 400 错误而不是 KeyError

    仅在POST 请求期间处理表单数据。还记得在POST 之后重定向,这样浏览器就不会挂在陈旧的表单数据上。

    from flask import request, redirect, flash, render_template
    
    stripe.api_key = 'test_key'
    
    @store.route('/payment', methods=['GET', 'POST'])
    def payment():
        if request.method == 'POST':
            token = request.form['stripeToken']
    
            try:
                charge = stripe.Charge.create(amount=1000, currency="cad", source=token, description="Example charge")
                return redirect(request.path)
            except stripe.error.CardError, e:
                flash('Error processing payment.', 'error')
    
        return render_template('payment_form.html')
    

    如果您在开发期间启用调试模式,您会发现调试应用程序错误会容易得多:app.run('localhost', debug=True)。这将在发生应用错误时在浏览器中为您提供交互式回溯调试器。

    【讨论】:

      【解决方案2】:

      你想要request.form.get('stripeToken', None)

      【讨论】:

      • 我现在收到The browser (or proxy) sent a request that this server could not understand 错误
      • 您使用的是什么服务器?你用的是什么浏览器?
      • 服务器是什么意思?我正在使用铬
      • 您如何为您的应用程序提供服务?您使用的是 Flask 开发服务器(通过 app.run())还是 uWSGI 或 Apache 或类似服务器?
      • Flask 开发服务器
      猜你喜欢
      • 1970-01-01
      • 2017-09-10
      • 2018-04-12
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      相关资源
      最近更新 更多