条纹支付-Javascript没有插入输入

stripe payments - Javascript not inserting input

本文关键字:插入 输入 -Javascript      更新时间:2023-09-26

我一直在尝试使用stripe接受付款,并试图根据我找到的指南为其制作一个粗略的原型,但我似乎无法使其工作。名为"stripeToken"的新输入从不在提交之后插入。这导致我的PHP脚本永远无法执行。我试图理解为什么它从不插入。以下是脚本:

Javascript:(在页首)

    <script src="https://js.stripe.com/v2/"></script>
    <script type="text/javascript">
        Stripe.setPublishableKey('mykeyishere');
    </script>
    <script>
        // Event Listeners
        $('#payment-form').on('submit', generateToken);
        var generateToken = function (e) {
            var form = $(this);
            // No pressing the buy now button more than once
            form.find('button').prop('disabled', true);
            // Create the token, based on the form object
            Stripe.create(form, stripeResponseHandler);
            // Prevent the form from submitting
            e.preventDefault();
        };
    </script>

HTML/Javascript:(在头部和表单中都尝试过JS)

<form action="index.php" method="POST" id="payment-form">
                <script>
                    var stripeResponseHandler = function (status, response) {
                        var form = $('#payment-form');
                        // Any validation errors?
                        if (response.error) {
                            // Show the user what they did wrong
                            form.find('.payment-errors').text(response.error.message);
                            // Make the submit clickable again
                            form.find('button').prop('disabled', false);
                        } else {
                            // Otherwise, we're good to go! Submit the form.
                            // Insert the unique token into the form
                            $('<input>', {
                                'type': 'hidden',
                                'name': 'stripeToken',
                                'value': response.id
                            }).appendTo(form);
                            // Call the native submit method on the form
                            // to keep the submission from being canceled
                            form.get(0).submit();
                        }
                    };</script>
                <span class="payment-errors"></span>
                <div class="row">
                    <label>
                        <span>Card Number</span>
                        <input type="text" data-stripe="number">
                    </label>
                </div>
                <div class="row">
                    <label>
                        <span>CVC</span>
                        <input type="text" data-stripe="cvc">
                    </label>
                </div>
                <div class="row">
                    <label>
                        <span>Expiration (MM/YYYY)</span>
                        <input type="text" data-stripe="exp-month">
                    </label>
                    <input type="text" data-stripe="exp-year">
                </div>
                <button type="submit">Submit</button>
            </form>

您应该从表单中删除该脚本标记,并将其放在另一个脚本标记旁边。

也可以尝试在文档中包装您的事件绑定。准备

   $(document).ready(function(){
    $('#payment-form').on('submit', generateToken);
     var stripeResponseHandler = function (status, response) {
                        var form = $('#payment-form');
                        // Any validation errors?
                        if (response.error) {
                            // Show the user what they did wrong
                            form.find('.payment-errors').text(response.error.message);
                            // Make the submit clickable again
                            form.find('button').prop('disabled', false);
                        } else {
                            // Otherwise, we're good to go! Submit the form.
                            // Insert the unique token into the form
                            $('<input>', {
                                'type': 'hidden',
                                'name': 'stripeToken',
                                'value': response.id
                            }).appendTo(form);
                            // Call the native submit method on the form
                            // to keep the submission from being canceled
                            form.get(0).submit();
                        }
                    };
            var generateToken = function (e) {
                var form = $(this);
                // No pressing the buy now button more than once
                form.find('button').prop('disabled', true);
                // Create the token, based on the form object
                Stripe.create(form, stripeResponseHandler);
                // Prevent the form from submitting
                e.preventDefault();
            };

    });

根据我的猜测(这不是一个好的猜测),#付款表单没有正确绑定,因为脚本在dom准备好之前就已经运行了?

还有一件事引起了我的注意。您有e.preventDefault(),它可以阻止表单的提交,但是您有一个response处理程序。是否调用了该响应处理程序?有没有什么请求出去条纹然后又回来了?

查看您的网络窗口,看看是否发生了这种情况。表单只能以.get(0).submit()的形式提交;响应处理程序的一部分,所以在stripe完成之后。