简体   繁体   English

Laravel:无法根据请求获取条纹令牌

[英]Laravel : Can't get stripe token on request

I have a form in steps, get information -> show information -> stripe checkout -> submit form, 我有一个逐步的表单,获取信息->显示信息->条纹结帐->提交表单,

The problem is that i am removing the stripe checkout form tags 问题是我要删除条纹结帐表单标签

<form action="/charge" method="post" id="payment-form">

And instead i am using the original form tags because i cannot use form inside a form, Plus all the processing should be on one submit. 而我使用的是原始表单标签,因为我无法在表单内使用表单,另外,所有处理都应提交一次。

I have returned the form request data but it have no stripe token. 我已经返回了表单请求数据,但没有条纹令牌。 The other form data is available but there is no token in that. 其他表单数据可用,但其中没有令牌。

What am i missing ? 我想念什么?

A simple process , I have included : 一个简单的过程,我包括:

<script src="https://js.stripe.com/v3/"></script>

And js at the end of the file : 在文件末尾还有js:

var stripe = Stripe('pk_test_73ZtD3qICEkFfyMqegGubJLV');
var elements = stripe.elements();
var style = {
    base: {
        color: '#32325d',
        lineHeight: '18px',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        fontSize: '16px',
        '::placeholder': {
        color: '#aab7c4'
        }
    },
    invalid: {
        color: '#fa755a',
        iconColor: '#fa755a'
    }
};
var card = elements.create('card', {style: style});
card.mount('#card-element');
card.addEventListener('change', function(event) {
    var displayError = document.getElementById('card-errors');
    if (event.error) {
        displayError.textContent = event.error.message;
    } else {
        displayError.textContent = '';
    }
});
var form = document.getElementById('signupFormPetOwner');
form.addEventListener('submit', function(event) {
    event.preventDefault();
    stripe.createToken(card).then(function(result) {
        if (result.error) {
            var errorElement = document.getElementById('card-errors');
            errorElement.textContent = result.error.message;
        } else {
            stripeTokenHandler(result.token);
        }
    });
});

Here is my form where i am integrating stripe : 这是我正在集成stripe的表格:

<form id="signupFormPetOwner" method="post">
    <div class="stepOne">
       ...
    </div>
    <div class="stepTwo">
       ...
    </div>

    <div class="stepThree">
       <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>
    </div>
    <button class="submit">Submit</button>
</form>

And AJAX request : 和AJAX请求:

$.ajax({
    url: "/register",
    type: "post",
    data: new FormData($('form')[0]),
    cache: false,
    contentType: false,
    processData: false,
    beforeSend: function() {
        $('#cover').show();
    },
    success: function(response) {
        $('#cover').hide();
        return true;
    }
});

You should either pass Stripe public key as value of some hidden input within a form, or to append to FormData new pair - name,value 您应该将Stripe公钥作为表单中某些隐藏输入的值传递,或附加到FormData新对-名称,值

    var submittedData  = new FormData($('form')[0]);
    submittedData.append('stripe_key', stripe_key_value);

    $.ajax({
        url: "/register",
        type: "post",
        data: submittedData,
        cache: false,
        contentType: false,
        processData: false,
        beforeSend: function() {
            $('#cover').show();
        },
        success: function(response) {
            $('#cover').hide();
            return true;
         }
    });

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

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