【发布时间】:2018-09-24 13:37:51
【问题描述】:
我的 Stripe 付款视图出现以下错误:
InvalidRequestError at /advertise/post/
Request req_rbwdcpSrc9U8AD: Must provide source or customer.
我只是从Stripe (Python) tutorial 复制了代码,所以我不确定为什么它不起作用。:
def pay(request, context):
ad = get_object_or_404(AdvertisePost, hash=context['hash'])
amount = ad.total_price * 100
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_exhH9odKfkT4mxzbtVxuJOBZ"
# Token is created using Checkout or Elements!
# Get the payment token ID submitted by the form:
if request.method == "POST":
token = request.POST.get('stripeToken')
print('Token:', token) #prints None
charge = stripe.Charge.create(
amount=amount,
currency='aud',
description='Boosted Post',
source=token,
)
context = {
'amount': amount,
'ad': ad
}
return render(request, 'advertising/pay.html', context)
这是我的 html 中的表单:
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_8rSK99eA02ntvemImQCeV6su"
data-amount="{{ amount }}"
data-name="My name"
data-description="Example charge"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="aud">
</script>
</form>
知道问题出在哪里吗?
PS:我正在使用它进行一次性付款。没有拯救客户或类似的东西。
【问题讨论】:
-
如果没有token,则验证失败,需要在post数据中查找错误。检查点4; stripe.com/docs/checkout#tokens
-
问题是,这个错误只是来自页面加载。我实际上并没有提交结帐表格
-
这可能是在我的本地开发服务器上,不是 HTTPS 吗?
-
啊,对,所以它在
GET上。脚本的data-keyattr 应该是您的公钥。但是您的示例与您在视图中放置的键不匹配。 -
data-key是我的公钥吗? (我的 django 视图有我的密钥)。我更改了几个字母以防它们敏感,但我肯定在 html 表单中使用 pk 并在 django 视图中使用 sk - 所有复制都从条带粘贴,自动将相关键嵌入到它们的位置。
标签: python django stripe-payments