使用Django 1.9&条纹1.35.0。我正在项目中尝试Stripe(stripe.js,not'checkout'),但是Stripe不会返回令牌。我查看了我的STRIPE_SECRET_KEY& STRIPE_PUBLISHABLE_KEY以确保它们匹配。我基本上直接从Stripe网站复制HTML以及相关的javascript(https://stripe.com/docs/custom-form)。
在我看来,当我使用request.POST['stripeToken']
时,我得到一个MultiValueDictKeyError“stripeToken”。如果我使用request.POST.get('stripeToken')
我在/ orders / checkout处收到错误InvalidRequestError请求req_8ecvcXQcwmM1lk:必须提供源或客户。换句话说,它没有提供令牌来创建客户(或收费)。
显然我做错了,但我找不到。我已经看过我能找到的例子(大多数是几岁)和文档。任何帮助表示赞赏。 .Thanks。
#views.checkout
def checkout(request):
publishable_key = settings.STRIPE_PUBLISHABLE_KEY
if request.method == "POST":
stripe.api_key = settings.STRIPE_SECRET_KEY
token = request.POST.get('stripeToken')
#token = request.POST['stripeToken']
print token
customer = stripe.Customer.create(description='test', source=token)
print customer
stripe.Charge.create(amount=500, currency='usd', source=token, description='test')
return redirect('orders:thanks.html')
context = {'publishable_key': publishable_key}
return render(request, 'orders/checkout.html', context)
#checkout.html
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('{{ publishable_key }}');
function stripeResponseHandler(status, response) {
// Grab the form:
var $form = $('#payment-form');
if (response.error) { // Problem!
// Show the errors on the form:
$form.find('.payment-errors').text(response.error.message);
$form.find('.submit').prop('disabled', false); // Re-enable submission
} else { // Token was created!
// Get the token ID:
var token = response.id;
// Insert the token ID into the form so it gets submitted to the server:
$form.append($('<input type="hidden" name="stripeToken">').val(token));
// Submit the form:
$form.get(0).submit();
}
};
$(function() {
var $form = $('#payment-form');
$form.submit(function(event) {
// Disable the submit button to prevent repeated clicks:
$form.find('.submit').prop('disabled', true);
// Request a token from Stripe:
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from being submitted:
return false;
});
});
</script>
{% endblock %}
{% block content %}
<h3 class="text-center">Credit Card Payment</h3>
<div class="container">
<div class="row">
<form method="post" action="." id="checkout-form">
{% csrf_token %}
<div class="form-group">
<label class="control-label" for="card">Card</label>
<div class="controls">
<input type="text" id="card" class="form-control" data-stripe="number" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="">Expiration (MM/YYYY)</label>
<div class="row">
<div class="col-xs-2">
<input type="text" size="2" data-stripe="exp-month" class="form-control" />
</div>
<div class="col-xs-2">
<input type="text" size="4" data-stripe="exp-year" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<label class="control-label" for="cvc">CVC</label>
<div class="controls">
<input type="text" id="cvc" size="4" class="form-control" data-stripe="cvc" />
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="submit" value="Checkout" class="btn btn-primary" />
</div>
</div>
</form>
</div>
</div>
{% endblock %}
And in settings.py
# Stripe Settings:
STRIPE_SECRET_KEY = 'sk_test_XVDF17ppRvcOciF1xjQryhDX'
STRIPE_PUBLISHABLE_KEY = 'pk_test_RQjZcRmXjpVHfZv5x4KlI6wT'
答案 0 :(得分:0)
您需要将Customer
ID传递给Charge
端点,而不是stripeToken
:
customer = stripe.Customer.create(description='test', source=token)
stripe.Charge.create(customer=customer.id, amount=500, currency='usd', description='test')
更多信息:Saving credit card details for later
此外,您应该使用Ajax或更好的方法将stripeToken
发送到您的应用程序:
// Get the token ID:
var token = response.id;
// Insert the token ID into the form so it gets submitted to the server:
$form.append($('<input type="hidden" name="stripeToken">').val(token));
// Submit the form:
$form.get(0).submit();
这会将所有CC信息发送到您的服务器。 你应该这样:
// Get the token ID:
var token = response.id;
$.ajax({
url: '/api/charge-customer',
type: 'POST',
data: {stripeToken: token},
success: function (data) {
Alert("You have been charged.");
},
error: function (data) {
Alert("Server Error.");
}
});