2

APIを使用して独自のフォームを作成するためのStripeのチュートリアルに従っています。簡単に言うと、フォームが送信されると、Javascript の一部がデータをサーバーに送信し、stripeToken入力フィールドを追加します。その後、フォームが再度送信され、Flask アプリはこの新しいフォーム フィールドの値を読み取ることになっています。ただし、コードを実行しようとすると、500 内部サーバー エラーが発生します。これは、Flask がstripeToken最初にチェックしたときに存在しない form をすぐに探しているためだと思います。request.form['stripeToken']Stripeが追加するまで呼び出さないために追加できるJavascriptまたはPythonはありますか?

これが私の関連する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