我一直在关注Stripe的教程,以使用其API创建自己的表单。简而言之,当提交表单时,发生了什么事,一些Javascript将数据发送到其服务器,然后附加了stripeToken
输入字段。然后再次提交表单,我的Flask应用程序应该读取此新表单字段的值。但是,当我尝试运行代码时,它会产生500 Internal Server Error,我认为是因为Flask立即寻找stripeToken
形式,该形式在首次检查时不存在。我可以添加Javascript或Python,直到Stripe添加它后才调用request.form['stripeToken']
吗?
这是我相关的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错误可能是由于我修改数据库引起的吗?
参考方案
NameError:未定义全局名称“条带”
您好像忘记了导入stripe
。
在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…
Python:使用两个列表进行字典 - python如何使用python使用两个列表作为字典list_one_keys = ['key1', 'key2', 'key3', 'key4'] 嵌套列表:list_two_values = [['a1var1', 'a1var2', '…
Python:BeautifulSoup-根据名称属性获取属性值 - python我想根据属性名称打印属性值,例如<META NAME="City" content="Austin"> 我想做这样的事情soup = BeautifulSoup(f) //f is some HTML containing the above meta tag for meta_tag in soup(…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…