gpt4 book ai didi

django - 无效请求错误 : This customer has no attached payment source

转载 作者:行者123 更新时间:2023-12-04 01:58:40 25 4
gpt4 key购买 nike

我一直在关注 Stripe 的快速入门指南。

https://stripe.com/docs/quickstart

https://stripe.com/docs/subscriptions/quickstart

这是我的表格的样子:

<form id="register-form" method="post">
{% csrf_token %}
{% for field in registration_form %}
<p>{{ field }}</p>
{% if field.errors %}
<p class="text-danger">{{ field.errors.as_text|cut:"* "|escape }}</p>
{% endif %}
{% endfor %}
{% if registration_form.non_field_errors %}
<p class="text-danger">{{ registration_form.non_field_errors.as_text|cut:"* "|escape }}</p>
{% endif %}
<div id="stripe-wrapper">
<p id="card-element"></p>
</div>
<p id="card-errors" role="alert"></p>
<button id="register-btn" class="btn btn-primary" type="submit">Register</button>

<script type="text/javascript">
var displayError= document.getElementById('card-errors');
var stripe= Stripe("pk_test_BjhejGz5DZNcSHUVaqoipMtF");
var elements= stripe.elements();

var style= {
base: {
fontSize: "1.1875rem",
fontSmoothing: "always",
fontWeight: "600"
}
};

var card= elements.create("card",{style:style});
card.mount("#card-element");
card.addEventListener('change', function(event) {

if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});

var formID= "register-form";
var form= document.getElementById(formID);
form.addEventListener("submit",function(event){
event.preventDefault();

stripe.createToken(card).then(function(result){
if(result.error) {
displayError.textContent= result.error.message;
} else {
stripeTokenHandler(result.token, formID);
}
})
});
// tut https://stripe.com/docs/stripe-js/elements/quickstart#create-form
function stripeTokenHandler(token, formID) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById(formID);
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
</script>
</form>

然后我的 views.py有这个:
if registration_form.is_valid():
stripe.api_key= "sk_test_8rdFokhVsbsJJysHeKgyrMTc"
stripeCustomer= stripe.Customer.create(
email=request.POST["username"],
)
subscription= stripe.Subscription.create(
customer=stripeCustomer["id"],
items=[{"plan":"plan_CLFfBrRAKl7TRt"}],
)

这给了我一个错误:
Internal Server Error: /login-register/
Traceback (most recent call last):
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in _get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/mezzanine/pages/middleware.py", line 98, in process_view
return view_func(request, *view_args, **view_kwargs)
File "/home/myUserName/myDjangoProjectWithStripe/product_blog/theme/views.py", line 154, in login_register
items=[{"plan":"plan_CLFfBrRAKl7TRt"}],
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/stripe/api_resources/subscription.py", line 33, in create
return super(Subscription, cls).create(**params)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/stripe/api_resources/abstract/createable_api_resource.py", line 17, in create
response, api_key = requestor.request('post', url, params, headers)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/stripe/api_requestor.py", line 153, in request
resp = self.interpret_response(rbody, rcode, rheaders)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/stripe/api_requestor.py", line 365, in interpret_response
self.handle_error_response(rbody, rcode, resp.data, rheaders)
File "/home/myUserName/myDjangoProjectWithStripe/local/lib/python2.7/site-packages/stripe/api_requestor.py", line 178, in handle_error_response
raise err
InvalidRequestError: Request req_o39ceLlhtpJmmr: This customer has no attached payment source

如何将付款来源附加到客户?因为我在测试,所以我使用测试信用卡,它的号码是 4242 4242 4242 4242 .

最佳答案

错误意味着它所说的,客户没有附加的付款方式/来源。您正在添加 stripeToken到您的 html 表单,但您没有对它的服务器端做任何事情。您需要将 token 添加到您的客户(在创建客户时将 source 设置为 token ),然后您就可以向客户收费、创建订阅等。

关于django - 无效请求错误 : This customer has no attached payment source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com