简体   繁体   English

条纹支付表不适用于 Django,无法检索令牌

[英]Stripe payment form not working on Django, cannot retreive a token

I am trying to add a payment page to my site, I copied the code from the stripe documentation, however when I press on the pay button nothing happens.我正在尝试向我的网站添加付款页面,我从条带文档中复制了代码,但是当我按下付款按钮时,没有任何反应。 Through debugging, the returned token is empty.通过调试,返回的token为空。 The thing is when I tried the code in JsFiddle it worked, but when I add it to my app, there is no response, the console is empty so I'm lost in troubleshooting.问题是当我在 JsFiddle 中尝试代码时它起作用了,但是当我将它添加到我的应用程序时,没有响应,控制台是空的,所以我在故障排除中迷失了方向。 Here is the html file:这是html文件:

{% block content %}
<form   method="POST" id="payment-form" >
{% csrf_token %}
  <input type="hidden" name="token" />
    <div class="group">
      <label>
        <span>Card</span>
        <div id="card-element" class="field"></div>
          <div id="card-errors" role="alert"></div>
      </label>
    </div>
    <div class="group">
      <label>
        <span>Name</span>
        <input id="name" name="name" class="field" placeholder="Jane Doe" />
      </label>
    </div>
    <div class="group">
      <label>
        <span>Address</span>
        <input id="address-line1" name="address_line1" class="field" placeholder="77 Winchester Lane" />
      </label>
      <label>
        <span>Address (cont.)</span>
        <input id="address-line2" name="address_line2" class="field" placeholder="" />
      </label>
      <label>
        <span>City</span>
        <input id="address-city" name="address_city" class="field" placeholder="Coachella" />
      </label>
      <label>
        <span>State</span>
        <input id="address-state" name="address_state" class="field" placeholder="CA" />
      </label>
      <label>
        <span>ZIP</span>
        <input id="address-zip" name="address_zip" class="field" placeholder="92236" />
      </label>
      <label>
        <span>Country</span>
        <input id="address-country" name="address_country" class="field" placeholder="United States" />
      </label>
    </div>
    <button type="submit">Pay $25</button>
    <div class="outcome">
      <div class="error"></div>
      <div class="success">
        Success! Your Stripe token is <span class="token"></span>
      </div>
    </div>
  </form>




{% endblock %}

{% block body_scripts %}
<script >
var stripe = Stripe('stripeKeyGoesHere');
var elements = stripe.elements();

var card = elements.create('card', {
  hidePostalCode: true,
  style: {
    base: {
      iconColor: '#666EE8',
      color: '#31325F',
      lineHeight: '40px',
      fontWeight: 300,
      fontFamily: 'Helvetica Neue',
      fontSize: '15px',

      '::placeholder': {
        color: '#CFD7E0',
      },
    },
  }
});
card.mount('#card-element');

function setOutcome(result) {
  var successElement = document.querySelector('.success');
  var errorElement = document.querySelector('.error');
  successElement.classList.remove('visible');
  errorElement.classList.remove('visible');
console.log(result);
  if (result.token) {
    // In this example, we're simply displaying the token
    successElement.querySelector('.token').textContent = result.token.id;
    successElement.classList.add('visible');

    // In a real integration, you'd submit the form with the token to your backend server
    var form = document.querySelector('form');
    form.querySelector('input[name="token"]').setAttribute('value', result.token.id);
    form.submit();
  } else if (result.error) {
    errorElement.textContent = result.error.message;
    errorElement.classList.add('visible');
  }
}

card.on('change', function(event) {
  setOutcome(event);
});

document.querySelector('form').addEventListener('submit', function(e) {
  e.preventDefault();
  var options = {
    name: document.getElementById('name').value,
    address_line1: document.getElementById('address-line1').value,
    address_line2: document.getElementById('address-line2').value,
    address_city: document.getElementById('address-city').value,
    address_state: document.getElementById('address-state').value,
    address_zip: document.getElementById('address-zip').value,
    address_country: document.getElementById('address-country').value,
  };
  stripe.createToken(card, options).then(setOutcome);
});

</script>

and here is the view:这是视图:

def payments(request):
    if request.user.profile.is_seller is False:
        key = settings.STRIPE_PUBLIC_KEY
        if request.method == 'POST':
            stripe.api_key = settings.STRIPE_SECRET_KEY
            #form = CreateCustomer(request.POST)
            token = request.POST.get('token')
            print(token)
            #if form.is_valid():
                #something
            return render(request, 'payment_card.html', {'key': key})
        else:
            print('it is a get')
            return render(request, 'payment_card.html', {'key':key})

    else:
        raise PermissionDenied

When you say that nothing happens when you click the button, could you see, when debugging (using the browser Dev tools?) if the form submission actually happened?当您说单击按钮时没有任何反应时,您是否可以在调试(使用浏览器开发工具?)时看到表单提交是否真的发生了?

Also, can you try to add a console.log(result) in your setOutcome function right before if (result.token) { } ?另外,您可以尝试在if (result.token) { }之前在setOutcome函数中添加console.log(result)吗?

When you are debugging your server side code, can you see your controller/view/handler, ie payments(request) being called when if the form is submitted ?当你调试你的服务器端代码时,你能看到你的控制器/视图/处理程序,即在提交表单时调用payments(request)吗? Can you check the content of the request.POST dictionary ?你能检查 request.POST 字典的内容吗?

The idea is to check if a POST request is actually made when you submit the form (client side using browser Dev tools), and if your server side controller does actually receives/handle the request and how the received POST request looks like (specifically the request.POST dictionary).这个想法是检查当您提交表单时是否确实发出了 POST 请求(客户端使用浏览器开发工具),以及您的服务器端控制器是否确实接收/处理了请求以及接收到的 POST 请求的样子(特别是request.POST 字典)。

The issue was never solved as I did not find a way to debug and find where is the problem, however the Stripe team helped me build another form and here it is, fortunately working with Django 1.11, hopefully it will help someone running into the same issue:该问题从未解决,因为我没有找到调试方法并找到问题所在,但是 Stripe 团队帮助我构建了另一种表单,幸运的是,它与 Django 1.11 一起使用,希望它能帮助遇到相同问题的人问题:

var stripe = Stripe('stripe key');
var elements = stripe.elements();

var card = elements.create('card', {
  hidePostalCode: true,
  style: {
    base: {
      iconColor: '#666EE8',
      color: '#31325F',
      lineHeight: '40px',
      fontWeight: 300,
      fontFamily: 'Helvetica Neue',
      fontSize: '15px',
      border:'2px',

      '::placeholder': {
        color: '#CFD7E0',
      },
    },
  }
});
card.mount('#card-element');

function setOutcome(result) {
  var successElement = document.querySelector('.success');
  var errorElement = document.querySelector('.error');
  successElement.classList.remove('visible');
  errorElement.classList.remove('visible');

  if (result.token) {
    // In this example, we're simply displaying the token
    successElement.querySelector('.token').textContent = result.token.id;
    successElement.classList.add('visible');

    // In a real integration, you'd submit the form with the token to your backend server
    //var form = document.querySelector('form');
    //form.querySelector('input[name="token"]').setAttribute('value', result.token.id);
    //form.submit();
  } else if (result.error) {
    errorElement.textContent = result.error.message;
    errorElement.classList.add('visible');
  }
}

card.on('change', function(event) {
  setOutcome(event);
});

document.querySelector('form').addEventListener('submit', function(e) {
  e.preventDefault();
  var options = {
    name: document.getElementById('name').value,
    address_line1: document.getElementById('address-line1').value,
    address_line2: document.getElementById('address-line2').value,
    address_city: document.getElementById('address-city').value,
    address_state: document.getElementById('address-state').value,
    address_zip: document.getElementById('address-zip').value,
    address_country: document.getElementById('address-country').value,
  };
  stripe.createToken(card, options).then(setOutcome);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM