Stripe API 未收取正确的金额

Wai*_*ein 3 php laravel stripe-payments

我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

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

composer require stripe/stripe-php
Run Code Online (Sandbox Code Playgroud)

这是我的充电动作

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";
    }
Run Code Online (Sandbox Code Playgroud)

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

这是屏幕截图:

在此处输入图片说明

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

小智 5

Stripe 确实以美分为单位收取费用。所以如果你想要 70 美元 你需要做 7000 美分