gpt4 book ai didi

javascript - Stripe 支付表格不适用于 Django,无法检索 token

转载 作者:行者123 更新时间:2023-11-30 06:24:56 25 4
gpt4 key购买 nike

我想在我的网站上添加一个支付页面,我从 stripe 文档中复制了代码,但是当我按下支付按钮时没有任何反应。经调试,返回的token为空。问题是,当我在 JsFiddle 中尝试代码时,它起作用了,但是当我将它添加到我的应用程序时,没有任何响应,控制台是空的,所以我迷失了故障排除。这是 html 文件:

{% block content %}
<form method="POST" id="payment-form" >
{% csrf_token %}
<input type="hidden" name="token" />
<div class="group">
<label>
<span>Card</span>
<div id="card-element" class="field"></div>
<div id="card-errors" role="alert"></div>
</label>
</div>
<div class="group">
<label>
<span>Name</span>
<input id="name" name="name" class="field" placeholder="Jane Doe" />
</label>
</div>
<div class="group">
<label>
<span>Address</span>
<input id="address-line1" name="address_line1" class="field" placeholder="77 Winchester Lane" />
</label>
<label>
<span>Address (cont.)</span>
<input id="address-line2" name="address_line2" class="field" placeholder="" />
</label>
<label>
<span>City</span>
<input id="address-city" name="address_city" class="field" placeholder="Coachella" />
</label>
<label>
<span>State</span>
<input id="address-state" name="address_state" class="field" placeholder="CA" />
</label>
<label>
<span>ZIP</span>
<input id="address-zip" name="address_zip" class="field" placeholder="92236" />
</label>
<label>
<span>Country</span>
<input id="address-country" name="address_country" class="field" placeholder="United States" />
</label>
</div>
<button type="submit">Pay $25</button>
<div class="outcome">
<div class="error"></div>
<div class="success">
Success! Your Stripe token is <span class="token"></span>
</div>
</div>
</form>




{% endblock %}

{% block body_scripts %}
<script >
var stripe = Stripe('stripeKeyGoesHere');
var elements = stripe.elements();

var card = elements.create('card', {
hidePostalCode: true,
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
lineHeight: '40px',
fontWeight: 300,
fontFamily: 'Helvetica Neue',
fontSize: '15px',

'::placeholder': {
color: '#CFD7E0',
},
},
}
});
card.mount('#card-element');

function setOutcome(result) {
var successElement = document.querySelector('.success');
var errorElement = document.querySelector('.error');
successElement.classList.remove('visible');
errorElement.classList.remove('visible');
console.log(result);
if (result.token) {
// In this example, we're simply displaying the token
successElement.querySelector('.token').textContent = result.token.id;
successElement.classList.add('visible');

// In a real integration, you'd submit the form with the token to your backend server
var form = document.querySelector('form');
form.querySelector('input[name="token"]').setAttribute('value', result.token.id);
form.submit();
} else if (result.error) {
errorElement.textContent = result.error.message;
errorElement.classList.add('visible');
}
}

card.on('change', function(event) {
setOutcome(event);
});

document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();
var options = {
name: document.getElementById('name').value,
address_line1: document.getElementById('address-line1').value,
address_line2: document.getElementById('address-line2').value,
address_city: document.getElementById('address-city').value,
address_state: document.getElementById('address-state').value,
address_zip: document.getElementById('address-zip').value,
address_country: document.getElementById('address-country').value,
};
stripe.createToken(card, options).then(setOutcome);
});

</script>

这是 View :

def payments(request):
if request.user.profile.is_seller is False:
key = settings.STRIPE_PUBLIC_KEY
if request.method == 'POST':
stripe.api_key = settings.STRIPE_SECRET_KEY
#form = CreateCustomer(request.POST)
token = request.POST.get('token')
print(token)
#if form.is_valid():
#something
return render(request, 'payment_card.html', {'key': key})
else:
print('it is a get')
return render(request, 'payment_card.html', {'key':key})

else:
raise PermissionDenied

最佳答案

当您说单击按钮时没有任何反应时,您能否在调试时(使用浏览器开发工具?)看到表单提交是否真的发生了?

此外,您能否尝试在 if (result.token) { } 之前的 setOutcome 函数中添加一个 console.log(result) ?

当您调试服务器端代码时,您能否看到您的 Controller / View /处理程序,即 payments(request) 在提交表单时被调用?你能查一下request.POST字典的内容吗?

这个想法是检查当您提交表单时是否真的发出了 POST 请求(客户端使用浏览器开发工具),以及您的服务器端 Controller 是否确实接收/处理了请求以及接收到的 POST 请求看起来如何(特别是 request.POST 字典)。

关于javascript - Stripe 支付表格不适用于 Django,无法检索 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073200/

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