Laravel 4和收银台

时间:2014-09-03 13:41:49

标签: javascript php laravel laravel-4 stripe-payments

我正在尝试将Laravel CashierJavascript一起使用,因此信用卡数据不会提交给服务器。
当我这样做时,我总是会收到一条错误,上面写着“This customer has no attached card”。

我做过研究,这通常意味着没有发布到Stripe的内容,我认为这可能是我的JS的一个问题。
我将在下面发布我的代码,如果你有答案,请发信息!

谢谢;)

signup.blade.php

@extends("masters.default")

@section("content")

@if( $errors->any() )
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        {{ implode("", $errors->all('<p>:message</p>') ) }}
    </div>
@endif

<div id="signupbox" style="margin-top:50px" class="mainbox mainbox col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2">
    <div class="panel panel-info">
        <div class="panel-heading">
            <div class="panel-title">Sign Up</div>

            <div style="float:right; font-size: 85%; position: relative; top:-10px">
                {{ HTML::link( '/login', 'Login' ) }}
            </div>
        </div>  
        <div class="panel-body" >
            {{ Form::open( [ 'route' => 'signup.store', 'class' => 'form-horizontal', 'role' => 'form', 'id' => 'subscription-form' ] )}}
                <div class="payment-errors"></div>
                <div id="signupalert" style="display:none" class="alert alert-danger">
                    <p>Error:</p>
                    <span></span>
                </div>

                <div class="form-group">
                    {{ Form::label( 'subscription', 'Subscription plan', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-9">
                        {{ Form::select( 'subscription' , [ 'basic' => '(BASIC) 10$ per month', 'gold' => '(GOLD) 40$ per month' ], 'gold', [ 'class' => 'form-control' ])}}
                    </div>
                </div>

                <div class="form-group">
                    {{ Form::label( 'email', 'Email', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-9">
                        {{ Form::email('email', '', [ 'class' => 'form-control', 'placeholder' => 'Email address' ])}}
                    </div>
                </div>

                <div class="form-group">
                    {{ Form::label( 'password', 'Password', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-9">
                        {{ Form::password('password', [ 'class' => 'form-control', 'placeholder' => 'Password' ])}}
                    </div>
                </div>

                <div class="form-group">
                    {{ Form::label( 'password_confirmation', 'Re-type Password', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-9">
                        {{ Form::password('password_confirmation', [ 'class' => 'form-control', 'placeholder' => 'Re-type Password' ])}}
                    </div>
                </div>

                <div class="divider"></div>

                <div class="form-group">
                    {{ Form::label( 'ccn', 'Credit card number', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-9">
                        {{ Form::text('ccn', '', [ 'class' => 'form-control', 'data-stripe' => 'number' ])}}
                    </div>
                </div>

                <div class="form-group">
                    {{ Form::label( 'expiration', 'Expiration date', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-6">
                        {{ Form::selectMonth('month', 'junuary', [ 'class' => 'form-control', 'data-stripe' => 'exp-month' ])}}
                    </div>
                    <div class="col-md-3">
                        {{ Form::selectRange('year', 2014, 2029, 2014, [ 'class' => 'form-control', 'data-stripe' => 'exp-year' ])}}
                    </div>
                </div>

                <div class="form-group">
                    {{ Form::label( 'cvc', 'CVC number', [ 'class' => 'col-md-3 control-label' ])}}
                    <div class="col-md-3">
                        {{ Form::text('cvc', '', [ 'class' => 'form-control', 'data-stripe' => 'cvc' ])}}
                    </div>
                </div>

                <div class="form-group">
                    <!-- Button -->                                        
                    <div class="col-md-offset-3 col-md-9">
                        {{ Form::button('<i class="glyphicon glyphicon-hand-right"></i> &nbsp Sign Up', [ 'type' => 'submit', 'id'  => 'btn-signup', 'class' => 'btn btn-info'] ) }}
                    </div>
                </div>
            {{ Form::close() }}
         </div> <!-- Panel body -->
    </div> <!-- Panel -->
 </div> <!-- Signup box -->

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script>
    Stripe.setPublishableKey('pk_test_HdoLAxfuWl53vjchF01fMphS ');
    jQuery(function($) {
        $('#subscription-form').submit(function(event) {
            var $form = $(this);

            // Disable the submit button to prevent repeated clicks
            $form.find('button').prop('disabled', true);

            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from submitting with the default action
            return false;
        });
    });

    var stripeResponseHandler = function(status, response) {
        var $form = $('#subscription-form');

        if (response.error) {
            // Show the errors on the form
            $form.find('.payment-errors').text(response.error.message);
            $form.find('button').prop('disabled', false);
        } else {
            // token contains id, last4, and card type
            var token = response.id;
            // Insert the token into the form so it gets submitted to the server
            $form.append($('<input type="hidden" name="stripeToken" />').val(token));
            // and submit
            $form.get(0).submit();
        }
    };
</script>
@stop

SignupController:

<?php

class SignupController extends BaseController{

    public function index( ){
        if( Auth::check() )
            return Redirect::to("/");

        return View::make('signup');
    }

    public function store(){
        $user = new User;
        $user->email = Input::get( 'email' );
        $user->password = Hash::make( Input::get( 'password' ) );
        $user->save();
        $user->subscription(Input::get( 'subscription' ))->create( Input::get( 'stripeToken' ) );

        return 'you are now registred';
    }

}

感谢您提供任何可能的帮助/见解。

1 个答案:

答案 0 :(得分:0)

如果您要将表单数据发布到控制器,可以试试这个:

{{ Form::open(array('url' => 'subscribe_route', 'class' => 'form-horizontal', 
'method' => 'POST', 'action' => 'SignupController@store')) }}

这种方法最适合我。