首页 文章

Stripe API没有收取正确的金额

提问于
浏览
2

我正在使用Laravel开发一个网站 . 我需要将Stripe支付网关集成到我的网站中 . 我成功实施了付款流程,但没有收取正确的金额 .

这是我的HTML表单和JavaScript:

<script src="https://js.stripe.com/v3/"></script>
<div class="container">
    <div style="margin-top: 100px;">
    {!! Form::open([ 'url' => url('checkout/charge'), 'id' => 'payment-form' ]) !!}
        <div style="color: red" id="card-errors">

        </div>
        <div class="form-row">
            <label for="card-element">
                Credit or debit card
            </label>
            <div id="card-element">
                <!-- A Stripe Element will be inserted here. -->
            </div>

            <!-- Used to display form errors. -->
            <div id="card-errors" role="alert"></div>
        </div>

        <button>Submit Payment</button>
    {!! Form::close() !!}
    </div>
</div>
<script type="text/javascript">
    $(function(){
        var secret = $('#stripe-secret').val();
        var stripe = Stripe(secret);
        // Create an instance of Elements.
        var elements = stripe.elements();

        // Custom styling can be passed to options when creating an Element.
        // (Note that this demo uses a wider set of styles than the guide below.)
        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'
            }
        };

    // 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');


    //Error live
    card.addEventListener('change', function(event) {
        var displayError = document.getElementById('card-errors');
        if (event.error) {
            displayError.textContent = event.error.message;
        } else {
            displayError.textContent = '';
        }
    });



    //Create token
    // Create a token or display an error when the form is submitted.
    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();
    }
})
</script>

如您所见,Stripe的令牌随后会提交给服务器 . 为了能够从服务器端使用Stripe客户端,我安装了它运行此命令 .

composer require stripe/stripe-php

这是我的指控行动

function charge(Request $request)
    {
        Stripe::setApiKey(env('STRIPE_SECRET'));

        $customer = Customer::create(array(
            'email' => "my-email@gmail.com",
            'source'  => $request->stripeToken
        ));


        $charge = Charge::create(array(
            'customer' => $customer->id,
            'amount'   => 70,
            'currency' => 'usd'
        ));

        return "Charge successful, you get the course";
    }

付款实施工作正常 . 但问题是,正如你在代码中看到的那样,我通过了70的金额 . 我试图收取70美元 . 货币是美元 . 但在我提交表格后,当我检查仪表板时,它只收取0.7美元 . 如果我通过5美元,则收费0.5 . 它将实际数量乘以0.01 .

这是截图:

enter image description here

如何修复我的代码以收取正确的金额?

1 回答

  • 3

    Stripe确实以美分收取金额 . 所以,如果你想要70美元,你需要做7000美分

相关问题