Laravel + stripe Call to a member function create() on null "subscription()->create"

4.4k views Asked by At

On my controller

public function postJoin(Request $request){

    $user=Auth::user();

    $plan=$request['plan'];

    $user->subscription($plan)->create($request['token'],['email'=>$users->email]);
    return Redirect::action('subscripton')->with('notice','Ahora estás registrado');
}

so the error is "Call to a member function create() on null" and when I do a

dd($user->subscription($plan));

its says also NULL.

the problem is the subscription function. Any help Please?

here is where I send to stripe with Jquery

$(document).ready(function(){

Stripe.setPublishableKey('MYKEY');

$('#subscription-form button').on('click', function(){
    var form                =   $('#subscription-form');
    var submit              =   form.find('button');
    var submitInitialText   =   submit.text();
    submit.attr('disabled','disabled').text('Espere un momento...');

    Stripe.card.createToken(form, function(status, response){
        var token;
        if (response.error) {
            //esta parte no responde si hay error
            form.find('.stripe-errors').text(response.error.message);
            submit.removeAttr('disabled');
            submit.text(submitInitialText);
        }else{

            token=response.id;
            form.append($('<input type="hidden" name="token">').val(token));
            form.submit();
        }
    });
});

});

my view

 @extends('layouts.app')

 @section('content')
 <div class="container">
     <div class="row">
        <div class="col-md-8 col-md-offset-2">
             <div class="panel panel-default">
                 <div class="panel-heading">Suscribirse</div>

            <div class="panel-body">    
                          {{Form::open(array('url'=>'subscription/join','id'=>'subscription-form'))}}


                            @include('subscription.partials.card')
                            <button class="btn btn-primary">Suscribirse</button>
                            <!-- {{Form::submit('Suscribirse',['class'=>'btn btn-primary'])}} -->
            {{Form::token()}}
            {{Form::close()}}
            </div>
        </div>
    </div>
</div>

@endsection

this is my Form view

   <div class="stripe-errors panel"></div>
                <div class="form-group">
                     {{Form::label('escoge un plan')}}
                     {{Form::select('plan', ['Premium' => 'Grande ($5,000 MXN)', 'Vip' => 'Vip (5,500 MXN)'],null,['placeholder'=>'escoga un plan','class'=>'form-control'])}}
                </div>
                <div class="form-group">
                     {{Form::label('tarjeta de credito')}}
                     {{Form::text('number',null, ['placeholder'=>'0000-0000-0000-0000','class'=>'form-control','data-stripe'=>'number'])}}
                </div>
                <div class="form-group">
                     {{Form::label('Año de expiracion')}}
                     {{Form::text('año',null, ['placeholder'=>'00','class'=>'form-control','data-stripe'=>'exp-year'])}}
            </div>
            <div class="form-group">
                     {{Form::label('mes de expiracion')}}
                     {{Form::text('mes',null, ['placeholder'=>'00','class'=>'form-control','data-stripe'=>'exp-month'])}}
                </div>
                <div class="form-group">
                     {{Form::label('CVC')}}
                     {{Form::text('cvc',null, ['placeholder'=>'000','class'=>'form-control','data-stripe'=>'cvc'])}}
                </div>      
2

There are 2 answers

1
Alexey Mezenin On BEST ANSWER

Always check if user logged in or not:

if (auth()->check()) {
    // Do something.
}

Another thing is you should use relation like this:

$user->subscription()->create(....);
0
Kevin Tamez On

I solve the problem with this line->

$user->newSubscription('main', $plan)->create($creditCardToken, ['email' =>$email,]);

but now it says "This customer has no attached payment source".

myDasbord refresh the customers, but theres no payments.