[英]Can't create credit card token with stripe (django)
I'm using django for a web app and I'm trying to process payments with stripe, I'm having trouble creating a token (I think that's a problem). 我正在将django用于Web应用程序,并且试图处理带区卷的付款,无法创建令牌(我认为这是一个问题)。 Here's my html/javascript code for the checkout page
这是我结帐页面的html / javascript代码
<head>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
</head>
<body>
<form action="{% url 'checkout:p' %}" id="payment-form" method="post">
{% csrf_token %}
number:<input class="card-number" type='text'>
cvc:<input class="card-cvc" type='text'>
month:<input class="card-expiry-month" type='text'>
year:<input class="card-expiry-year" type='text'>
<script type="text/javascript">
$('form').submit((fucnction(){
var $form = $('#payment-form');
Stripe.setPublishableKey('pk_test_KaWOobBc2ELxFoSoqmS1gtz2');
Stripe.card.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
function stripeResponseHandler(status, response) {
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="text" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
}
});
</script>
<input type="text" name="life" value=42>
<button type="submit">Submit</button>
</form>
</body>
Then here's the view I'm using. 这就是我正在使用的视图。
class CheckoutView(View):
template_name = "checkout/checkout.html"
credit_card_form = CreditCardInfo
def get(self, request, **kwargs):
return render(request, self.template_name, {})
def post(self, request, *args, **kwargs):
stripe.api_key = "sk_test_bb2N1MRHoMGhvnc4ZCvrMRMk"
life = request.POST.get('life', '')
card = request.POST.get('stripeToken', '')
stripe.Charge.create(
amount= 10.00,
currency="USD",
card=card,
desciription="Test charge")
return HttpResponseRedirect(reverse('home'))
The error message that I get is "You have passed a blank string for 'card'. You should remove the 'card' parameter from your request or supply a non-blank value." 我收到的错误消息是“您为'card'传递了一个空白字符串。您应该从请求中删除'card'参数或提供一个非空白值。” I'm assuming this just means that I'm not properly creating the credit card token, but I could be wrong.
我假设这只是意味着我没有正确创建信用卡令牌,但是我可能错了。 Any help would be great, thanks.
任何帮助将是巨大的,谢谢。
The card
parameter shouldn't be a stripeToken
, it should be something like this card
参数不应该是stripeToken
,应该像这样
"card": {
"id": "card_15EGuS2eZvKYlo2CgNp97XYZ",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 1,
"exp_year": 2050,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"country": "US",
"name": null,
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"cvc_check": "pass",
"address_line1_check": null,
"address_zip_check": null,
"dynamic_last4": null,
"customer": null
},
You can checkout the rest of the paramters in the stripe documentation . 您可以在条纹文档中签出其余的参数。
You should be passing card={'id': stripeToken}
in your view. 您应该在视图中传递
card={'id': stripeToken}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.