创建条带客户时没有信用卡信息

时间:2014-05-13 14:20:51

标签: django django-forms stripe-payments

我正在尝试将stripe设置为我的payement网关并遇到一些问题。

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

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

这是我的代码:

views.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

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

正如Tom所说,加载/发布表单时存在Javascript问题。 我必须在stripe.js。

之后移动到右上角

感谢汤姆我已经准备好了:)