使用Javascript无效条带请求无附加付款

时间:2018-06-11 18:41:52

标签: javascript php stripe-payments

标题是自我解释的。我的所有条带请求都失败了。我几次都在阅读他们的文档而且我不确切地知道我失败的地方,因为它是他们在网站上提供的完全相同的例子。我已经设置了几个断点来尝试调试java脚本,我可以看到正在创建令牌。但不知何故,它没有被发送到服务器。抓我的头。我究竟做错了什么?

错误

  

注意:未定义索引:stripeToken in   第31行的C:\ xampp \ htdocs \ MyApp \ subscription.php

     

致命错误:未捕获的异常' Stripe \ Error \ InvalidRequest'同   消息'此客户没有附加的付款来源'

表格

    <form name="payment-form" enctype="multipart/form-data" accept-charset="UTF-8" action="subscription.php" method="post" id="payment-form">
                            <div class="form-row">
                              <label for="card-element">
                                Credit or debit card
                              </label>
                              <div id="card-element"></div>

                              <div id="card-errors" role="alert"></div>
                            </div>
</form>

JS

var stripe = Stripe('pk_test_key');
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
var style = {
  base: {
    // Add your base input styles here. For example:
    fontSize: '16px',
    color: "#32325d",
  }
};

// Create an instance of the card Element
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
    var displayError = document.getElementById('card-errors');

if (event.error) {
        displayError.textContent = event.error.message;
    } else {
        displayError.textContent = '';
    }
});

// Handle form submission
var form = document.getElementById('payment-form');

form.addEventListener('submitmystripepayment', function(event) {
    event.preventDefault();

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


// Send Stripe Token to Server
function stripeTokenHandler(token) {
    // Insert the token ID into the form so it gets submitted to the server
    var form = document.getElementById('payment-form');

// Add Stripe Token to hidden input
    var hiddenInput = document.createElement('input');
    hiddenInput.setAttribute('type', 'hidden');
    hiddenInput.setAttribute('id', 'stripeToken');
    hiddenInput.setAttribute('name', 'stripeToken');
    hiddenInput.setAttribute('value', token.id);
    form.appendChild(hiddenInput);

// Submit form
 if (document.getElementById("stripeToken").value === ""){
form.submit();
console.log(token.id);
}else{
    console.log('form not submitted');
}}

0 个答案:

没有答案