令牌创建不适用于条带

时间:2018-05-15 08:04:05

标签: javascript token stripe-payments

我正在尝试创建然后提交到我的服务器端代码,但是当我点击提交按钮时,$_POST['stripeToken']不存在,我不知道为什么。

这是我的代码:

<div class="ui main container">
  <form action="paiement.php" class="ui form" id="payment_form" method="post">
    <div class="field">
      <input type="text" name="clientNom" required placeholder="Votre nom" value="John Kennedy">
    </div>
    <div class="field">
      <input type="email" name="clientEmail" required placeholder="votre@email.fr" value="john.kennedy@gmail.com">
    </div>
    <input type="hidden" name="commandeTotal" value="<?php echo $somme ?>">
    <div class="field">
      <input type="tel" name="clientTelephone" required placeholder="votre@email.fr" value="0102030405">
    </div>
    <div class="field">
      <input type="text" placeholder="Votre code de carte bleu" data-stripe="number" value="4242 4242 4242 4242">
    </div>
    <div class="field">
      <input type="text" placeholder="MM" data-stripe="exp_month" value="10">
    </div>
    <div class="field">
      <input type="text" placeholder="YY" data-stripe="exp_year" value="18">
    </div>
    <div class="field">
      <input type="text" placeholder="CVC" data-stripe="cvc" value="123">
    </div>
    <p>
      <button class="ui button" type="submit">Acheter</button>
    </p>
  </form>
</div>
Stripe.setPublishableKey('pk_test_my publishable key')
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the customer that there was an error.
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server.
      stripeTokenHandler(result.token);
    }
  });
});

function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}

服务器端代码只是我表单值的回显。谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

这样做的原因是,在您的JavaScript中,您通过ID payment-form引用表单,但在您的html表单中,您的标识为payment_form(下划线_而不是连字符{{1} })。

顺便说一下,-函数中不需要var form = document.getElementById('payment-form');,因为您已经在之前分配过了。{/ p>