Stripe API未按正确金额收费。

3

我正在使用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。 以下是屏幕截图: 如何修复我的代码以收取正确的金额?

1
Stripe的金额以分为单位,因此它是7000。 - rchatburn
我可以看一下官方文档吗? - Wai Yan Hein
1
@WaiYanHein,甚至在Laravel文档中都有提到:https://laravel.com/docs/5.5/billing#single-charges - Kenny Horna
非常感谢您。 - Wai Yan Hein
@WaiYanHein 当然可以 https://stripe.com/docs/api#charge_object-amount - rchatburn
2个回答

5

Stripe使用美分计费。因此,如果您想要70美元,您需要支付7000美分。


0
将数值发送到一个函数,该函数将金额乘以100,将其转换为美元、欧元等单个单位。
$charge = Charge::create(array(
            'customer' => $customer->id,
            'amount'   => calculateRealNumber(70),
            'currency' => 'usd'
));

function calculateRealNumber($amount) {
    return (($amount)*100);
}

这更适合于动态费用值,您可以在 'amount' => calculateRealNumber($body->amount) 中放置一个变量。对于常量金额来说有点过度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接