问题描述
我正在构建带条纹集成的Django应用程序。我想使用收费API,以使个人通过该网站进行捐款。目前,我已经从文档和条带元素页面复制/粘贴了大多数charge api代码。但是,python选项卡下包含的javascript不会发送创建客户并向卡收费所需的stripetoken。我已将信息打印在特定的URL上,但似乎未生成stripetoken密钥,因为它甚至没有返回空的表单部分。其余的默认javascript似乎工作正常,但Submit函数或stripetokenHandler似乎根本不起作用。我尝试为该字段添加隐藏的输入并将处理程序切换为ajax,而这两种方法均未成功。有什么我想念的吗?
Django HTML:
<form action="{% url 'Charge' %}" method="post" id="payment-form">
{% csrf_token %}
<div class="input-section">
<label>Name on Card:</label>
<input type="text" name="name" placeholder="enter name..." />
</div>
<div class="donation-input">
<div class="input-section">
<label>Donation Amount:</label>
<div style="display: flex;">
$<input id="amount" type="text" name="amount" />
</div>
</div>
</div>
<div class="form-row">
<label for="card-element" style="font-family: 'Oswald',sans-serif;">
Credit or debit card
</label>
<div id="card-element">
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div>
</div>
<img style="display: block; margin: 10px 0;" width="300" height="94" src="{% static 'securetransaction.png' %}" alt="Stripe secure transaction">
<button type="submit" class="form-button">Submit Payment</button>
</form>
条纹JS:
var stripe = Stripe('/*public test key here*/');
// Create an instance of Elements.
var elements = stripe.elements();
var style = {
base: {
color: '#32325d',fontFamily: '"Helvetica Neue",Helvetica,sans-serif',fontSmoothing: 'antialiased',fontSize: '16px','::placeholder': {
color: '#aab7c4'
}
},invalid: {
color: '#fa755a',iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
var card = elements.create('card',{style: style});
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
// Handle real-time validation errors from the card Element.
card.on('change',function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit',function(event) {
event.preventDefault();
stripe.createtoken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
console.log(result.token)
// Send the token to your server.
stripetokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripetokenHandler(token) {
console.log('Submit Form')
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
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();
}
和Django视图:
def charge(request):
if request.method == 'POST':
print(request.POST) // Does not print any stripetoken
customer = stripe.Customer.create(
name = request.POST['name'],source = request.POST['stripetoken'] // Causes error due to missing token
)
charge = stripe.Charge.create(
customer = customer,amount = (request.POST['amount'] * 100),currency= 'usd',description = 'Donation'
)
return render(request,'thankyou.html')
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)