2

我一直在关注 Stripe 的教程,使用他们的 API 创建我自己的表单。长话短说,当提交表单时,会发生一些 Javascript 将数据发送到他们的服务器,然后附加stripeToken输入字段。然后再次提交表单,我的 Flask 应用程序应该读取这个新表单字段的值。但是,当我尝试运行我的代码时,它会产生 500 Internal Server Error,我认为这是因为 Flask 正在立即查找表单stripeToken,而它在第一次检查时并不存在。是否有 Javascript 或 Python 可以添加,request.form['stripeToken']直到 Stripe 添加后才调用?

这是我的相关 HTML:

<head>
    <title>Settings</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://js.stripe.com/v1/"></script>
    <script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('STRIPE_TEST_KEY');

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

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" id="stripeToken" />').val(token));
        // and re-submit
        $form.get(0).submit();
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });
  </script>
</head>

<body>
    <div id="payments">
        <h1>Add a credit card</h1>
        <form action="{{ url_for('stats') }}" method="POST" id="payment-form">

这是Python:

@app.route('/stats', methods=['GET', 'POST'])
def stats():
#if signed in as user       
if session['signed-in-accttype'] == 'u':
    u=User.query.get(session['signed-in-id'])
    if request.method == 'POST': 
            stripe.api_key = "STRIPE_TEST_KEY"
            token = request.form['stripeToken']

        customer = stripe.Customer.create(
            card=token,
            description=u.uemail
            )
        #update stripe id
        u.ustripe_id = customer.id
        db.session.commit()
        return redirect(url_for('stats'))       
    return render_template('settings.html', u=u)    

和日志:

2013-04-23T01:57:39.590458+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:39] "GET /stats HTTP/1.1" 200 2348 0.014429
2013-04-23T01:57:39.590171+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=3ms service=19ms status=200 bytes=2230
2013-04-23T01:57:39.804162+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=4ms status=404 bytes=238
2013-04-23T01:57:39.808201+00:00 app[web.1]: 10.127.113.186 - - [2013-04-23 01:57:39] "GET /favicon.ico HTTP/1.1" 404 347 0.000610
2013-04-23T01:57:54.934252+00:00 heroku[router]: at=info method=POST path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=24ms status=500 bytes=291
2013-04-23T01:57:54.931497+00:00 app[web.1]: 10.92.74.166 - - [2013-04-23 01:57:54] "POST /stats HTTP/1.1" 500 412 0.020936
2013-04-23T01:57:55.133776+00:00 app[web.1]: 10.44.58.92 - - [2013-04-23 01:57:55] "GET /favicon.ico HTTP/1.1" 404 347 0.000603
2013-04-23T01:57:55.137417+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=24ms service=43ms status=404 bytes=238
2013-04-23T01:57:58.710721+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=15ms status=200 bytes=2230
2013-04-23T01:57:58.714328+00:00 app[web.1]: 10.44.45.210 - - [2013-04-23 01:57:58] "GET /stats HTTP/1.1" 200 2348 0.012623
2013-04-23T01:57:58.826291+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=3ms status=404 bytes=238
2013-04-23T01:57:58.827469+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:58] "GET /favicon.ico HTTP/1.1" 404 347 0.000772

更新:这是在本地运行的回溯)

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Zach/Desktop/t/t.py", line 20, in stats
    stripe.api_key = "STRIPE_TEST_KEY"
NameError: global name 'stripe' is not defined
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -

更新 2:我推断出我的本地测试脚本上的上述错误是因为我忘记了import stripe. 这样做使它运行良好。但是,同样的代码在 Heroku 上运行时会继续产生 500 错误。

更新 3(启示):所以我的 Stripe 仪表板说我确实在创建新用户。因此,我认为可以公平地假设直到(包括)之前的所有代码都customer = stripe.Customer.create有效。那么500错误可能是由我修改数据库引起的吗?

4

2 回答 2

0

NameError:未定义全局名称“条纹”

看起来你忘记了 import stripe

于 2013-04-23T02:29:16.820 回答
0

在检查了我的 Stripe 仪表板后,我意识到客户的创建是正确的。所以错误在于以下代码,我在其中更新数据库中用户的 stripe_id。问题是条带 id 的 Flask 列需要一个整数。然而,Stripe 将 id 分配为字符串。

于 2013-04-24T20:50:56.547 回答