0

我正在创建一个用户订阅计划,为此我在 laravel 5.2 中使用收银员包。我遵循 laravel 文档https://laravel.com/docs/5.2/billing中给出的教程中提供的确切方法。但我得到了错误

ErrorException in FacebookScraperController.php line 1767: 
Undefined variable: creditCardToken

我的控制器代码:

$user = User::find(2);
$res = $user->newSubscription('main', 'monthly')->create($creditCardToken);
dd($res);

我应该在 $creditCardToken 变量中传递什么值。我试图在这个变量中给出卡片的详细信息。但是出现错误。请帮帮我。

4

1 回答 1

1

您需要在此处使用入卡时生成的令牌传递订阅计划。这是您可以遵循的步骤。创建一个视图页面:

<form action="/subscription" method="POST" id="payment-form">
    <span class="payment-errors"></span>

    <div class="form-row">
        <label>
            <span>Card Number</span>
            <input type="text" size="20" data-stripe="number">
        </label>
    </div>

    <div class="form-row">
        <label>
            <span>Expiration (MM/YY)</span>
            <input type="text" size="2" data-stripe="exp_month">
        </label>
        <span> / </span>
        <input type="text" size="2" data-stripe="exp_year">
    </div>

    <div class="form-row">
        <label>
            <span>CVC</span>
            <input type="text" size="4" data-stripe="cvc">
        </label>
    </div>


    <input type="submit" class="submit" value="Submit Payment">
</form>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
    Stripe.setPublishableKey('pk_test_TSGgkchoa9iQU4ZQ628a8Auz');
</script>
<script>
    $(function() {
        var $form = $('#payment-form');
        $form.submit(function(event) {
            // Disable the submit button to prevent repeated clicks:
            $form.find('.submit').prop('disabled', true);

            // Request a token from Stripe:
            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from being submitted:
            return false;
        });
    });
    function stripeResponseHandler(status, response) {
        // Grab the form:
        var $form = $('#payment-form');

        if (response.error) { // Problem!

            // Show the errors on the form:
            $form.find('.payment-errors').text(response.error.message);
            $form.find('.submit').prop('disabled', false); // Re-enable submission

        } else { // Token was created!

            // Get the token ID:
            var token = response.id;

            // Insert the token ID into the form so it gets submitted to the server:
            $form.append($('<input type="hidden" name="stripeToken">').val(token));

            // Submit the form:
            $form.get(0).submit();
        }
    };
</script>

在你的控制器中:

public function subscription(Request $request)
    {
        $user = User::find(1);
        $creditCardToken = $request->stripeToken;
        $res = $user->newSubscription('main', 'pro')
            ->trialDays(30)
            ->create($creditCardToken, [
            'plan' => 'pro',
            'email' => $user->email,

        ]);

    }
于 2016-09-02T12:12:50.117 回答