无法使用条纹创建信用卡令牌(django)

时间:2014-12-27 04:51:29

标签: javascript python html django stripe-payments

我正在使用django作为网络应用程序而我正在尝试使用条带处理付款,我在创建令牌时遇到问题(我认为这是一个问题)。这是我的结帐页面的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>

然后是我正在使用的视图。

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'))

我收到的错误消息是“您已经为'卡'传递了空白字符串。您应该从请求中删除'card'参数或提供非空值。”我假设这只是意味着我没有正确创建信用卡令牌,但我可能是错的。任何帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:0)

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
  },

您可以在the stripe documentation中查看其余参数。

您应该在视图中传递card={'id': stripeToken}