django - Stripe 客户是在没有信用卡信息的情况下创建的

标签 django django-forms stripe-payments

我正在尝试将 stripe 设置为我的支付网关,但遇到了一些问题。

我希望我的用户输入他们的信用卡信息,以便稍后收费。 (又名创建客户)

输入所有信息并按 Enter 键后,我的客户已在 Stripe.com 上创建,但信用卡信息未保存(因此我以后无法收费)。 在我的数据库中, token 被保存,当我尝试向客户收费时,我从 Stripe api 得到答案“无法收费,用户没有信用卡”...

这是我的代码:

View .py

import stripe

def paiements(request):

    stripe.api_key = "sk_test_mytestapikey"
    utilisateur = request.user

    cc_save = Stripetoken.objects.filter(utilisateur= request.user).exists() #check if the user have already a stripe token.

    if request.method == 'POST': 

        token_cc = request.POST.get('stripeToken')  #request.POST['stripeToken'] give me an error.



        customer = stripe.Customer.create(
                card = token_cc,
                description = utilisateur.email,
                email = utilisateur.email
            )

        Stripetoken.objects.create(utilisateur= request.user, token = customer.id , description= request.user.email) #save the customer information and token to charge later on

        return HttpResponseRedirect('/frontprop/tb/') # Redirect after POST

    args = {} 
    args.update(csrf(request))
    args['cc_save'] = cc_save

   return render_to_response('b_param_paiement.html', args)

和我的 html 页面:

  <script type="text/javascript" src="https://js.stripe.com/v2/"></script>

  <!-- jQuery is used only for this example; it isn't required to use Stripe -->

  <script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('pk_test_withmyid');

    var stripeResponseHandler = function(status, response) {
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        // and re-submit
        $form.get(0).submit();
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });
  </script>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#toggle").click(function(){
    $("#cbinfo").toggle();
  });
});
</script>




 <form action="/frontprop/tb/param/paiements/" method="POST" id="payment-form">  {% csrf_token %}
    <span class="payment-errors"></span>

    <div class="form-row">
      <label>
        <span>Votre numero de carte bancaire:</span>
        <input class="form-control" type="text" size="20" data-stripe="number"/>
      </label>
    </div>

    <div class="form-row">
      <label>
        <span>CVC:</span>
        <input class="form-control" type="text" size="4" data-stripe="cvc"/>
      </label>
    </div>

    <div class="form-inline">
      <label>
        <span>Expiration (MM/YYYY):</span> <br>
        <input class="form-control" type="text" size="2" data-stripe="exp-month"/>
      </label>
      <span> / </span>
      <input class="form-control" type="text" size="4" data-stripe="exp-year"/>
   </div>
 <br>
    <button id="stripebutton" class="btn btn-primary" type="submit">Enregistrer la carte</button>
  </form>

仅供引用:我已经关注了这个https://stripe.com/docs/tutorials/forms

感谢您的帮助

最佳答案

您需要创建一个免费计划并将其分配给该计划,稍后您可以根据需要向该客户收取一次性费用。

关于django - Stripe 客户是在没有信用卡信息的情况下创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633879/

相关文章:

ios - Stripe - STPPaymentCardTextField - 如何删除或隐藏 CVC 代码字段?

python - 如何禁用 Django UserCreationForm 中用户名字段的自动对焦?

sql - django 中的全外连接

mysql - 如何使用 gitlab CI/CD 设置与 MySql 数据库的连接

基本模板上的 Django 表单

Django 1.11 CreateView传递URL参数

google-cloud-functions - 将 Google Secret Manager 与 Firebase Functions 和 Stripe 结合使用(顶级)

bash - 从 Bash 脚本为 Stripe 自动输入密码

python - 神秘的 Apache Django WSGI 错误

django - 将类添加到表单字段 Django ModelForm