无需订阅即可与Laravel Cashier一次性付款

时间:2019-06-06 19:23:54

标签: laravel stripe-payments laravel-cashier

我打算使用Laravel Cashier与Stripe一次性付款。它在documentation中说:

  

如果您仅执行“一次性”费用且不提供订阅,则不应使用收银台。

但是,我可以使用以下代码向用户收费而不订阅:

$amount = $request['amount];
Stripe::setApiKey(Config::get('stripe.secret_key'));
$token = $input['stripeToken'];
$user = Auth::user();
return $user->charge($amount * 100, ['source' => $token]);

它奏效了!我想知道这种方法是否有问题?为什么他们建议不要使用收银台?一路上会引起问题吗?请让我知道您的想法吗?

2 个答案:

答案 0 :(得分:1)

按照当前的实施方式,这样做是安全的。您可以在此处查看charge方法的代码:https://github.com/laravel/cashier/blob/822b6535e755fd36dec8ecceb52cc8909c8a953e/src/Billable.php#L37

也就是说,鉴于明确警告说收银员不是打算的这种使用方式,如果charge函数的修改方式破坏了您的应用程序,这是您自己的错在某处。

在这种情况下也没有太多理由使用Cashier。直接使用SDK的Stripe\Charge类将是更简洁的代码,并且您不会冒滥用其他库的风险。

答案 1 :(得分:0)

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="csrf-token" content="{{ csrf_token() }}">

</head>
<body>

        <script src="https://js.stripe.com/v3/"></script>
        <br><br>
        <h1 align="center">One Time Charge</h1>
        <form method = "post" action = "{{route('charge')}}" id="form">
        @csrf

        <div class="form form-cb" align="center"> 

                        Name on Card:    <input type="text" id="card-holder-name" required>  
                        <br>                <br> 
                        <div id="card-element" style="width: 300px;">
                        <!-- A Stripe Element will be inserted here. -->           
                        </div>

                        <!-- Used to display form errors. -->
                        <div id="card-errors" role="alert"></div><br>
                        <button disabled style="background-color:skyblue;color:black">$1.00 USD</button><br><br>  
                        <button id="card-button" style="background-color: green;color:white;font-size:20px;" />Pay
                        </button>   
        </div>

        </form>
<script>

    var stripe = Stripe('Your Stripe-key');
    var elements = stripe.elements();
    var cardElement = elements.create('card');

    cardElement.mount('#card-element');

    // Add an instance of the card UI component into the `card-element` <div>
    var cardHolderName = document.getElementById('card-holder-name');
    var cardButton = document.getElementById('card-button');

    cardButton.addEventListener('click', async (e) => {
    var { paymentMethod, error } = await stripe.createPaymentMethod(
        'card', cardElement, {
            billing_details: { name: cardHolderName.value }
        }
    );
    if (error) {
        console.log('error');
        } else {
                var payment_id = paymentMethod.id;
                createPayment(payment_id);
        }   
    });

    var form = document.getElementById('form');
    form.addEventListener('submit', function(event) {
        event.preventDefault();
    });

    // Submit the form with the token ID.
    function createPayment(payment_id) {
    // Insert the token ID into the form so it gets submitted to the server
    var form = document.getElementById('form');
    var hiddenInput = document.createElement('input');
    hiddenInput.setAttribute('type', 'hidden');
    hiddenInput.setAttribute('name', 'payment_id');
    hiddenInput.setAttribute('value',payment_id);
    form.appendChild(hiddenInput);
    // Submit the form

    form.submit();

    }

</script>


</body>
</html>