Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bottelet's avatar

Stripe & Laravel

Hello i'm getting the error with my Laravel Stripe, im not sure what is causing it, this is my code.

FatalErrorException in Billable.php line 250:
Call to a member function create() on null
<script type="text/javascript">
  // This identifies your website in the createToken call below
  Stripe.setPublishableKey('pk_test_xxxxxxxxxxxxxx');
  // ...
  // jQuery(function($) {
  $('#payment-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;
  });


function stripeResponseHandler(status, response) {
  var $form = $('#payment-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 {
    // response contains id and card, which contains additional card details
    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>


<form action="users/store" method="POST" id="payment-form">
 
  <span class="payment-errors"></span>
    <div class="form-row">
    <label>
      <span>Navn</span>
      <input type="text" size="20" name="name"/>
    </label>
  </div>

      <div class="form-row">
    <label>
      <span>email</span>
      <input type="text" size="20" name="email"/>
    </label>
  </div>

  <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>CVC</span>
      <input type="text" size="4" data-stripe="cvc"/>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YYYY)</span>
      <input type="text" size="2" data-stripe="exp-month"/>
    </label>
    <span> / </span>
    <input type="text" size="4" data-stripe="exp-year"/>
  </div>

  <button type="submit">Submit Payment</button>
</form>
        </div>

</div>

The controller

   public function store(Request $request)
    {
         
        $token = Input::get('stripeToken');

        $user->newSubscription('main', 'testid')->create($token,
            [
                'email' => "test@test.com"
            ]);
        return redirect()->back();


        try
        {
          $customer = \Stripe\Customer::create(array(
            'email' => ,
            'source'  => $_POST['stripeToken'],
            'plan' => 'weekly_box'
          ));

          header('Location: thankyou.html');
          exit;
        }
        catch(Exception $e)
        {
          header('Location:oops.html');
          error_log("unable to sign up customer:" . $_POST['stripeEmail'].
            ", error:" . $e->getMessage());
        }
     
    }
0 likes
8 replies
bobbybouwmann's avatar
Level 88

This code is failing, since you don't have a failed user here

 $user->newSubscription('main', 'testid')->create($token,
[
    'email' => "test@test.com"
]);

return redirect()->back();

You can get a logged in user by doing something like this

$user = $request->user();

// Or the helper functions
$user = auth()->user();

// Or use the facade
$user = Auth::user();
bottelet's avatar

@bobbybouwmann I tried as you say, now i get the error

FatalErrorException in UserControllers.php line 53:
Call to a member function newSubscription() on null

Also does the user have to be logged in, what im trying to achieve is them signing up, while they pay/add their card information.

bobbybouwmann's avatar

I though you already had a logged in user. So before you create a subscription you need to have a user. A subscription is connected to a user and can't live without that. So you need to have these steps

  • Create user/customer
  • Create subscription and assign to user
  • Log the user in
  • Redirect to some page
bottelet's avatar

I do have users in the Database, they just aren't logged in, im currently just testing, is there a way to login a user, without having to create forms etc? @bobbybouwmann

bobbybouwmann's avatar

Yeah you can do this

Auth::loginUsingId($id);

// Or
Auth::loginUsingId(User::first()->id);
bottelet's avatar

Okay so im saving a customer for now like this:

     */
    public function store(Request $request)
    {
        $input = $request->all();
       
      
         
        $user = User::findorFail(3);
        
        $customer = $user->createAsStripeCustomer(Input::get('stripeToken'));
    }

Lets say i wanna charge this speific user i just created in a couple of day for 10 usd... How would i do that?

Something like this?

    public function pay(Request $request)
    {
       $user = User::findorFail(3);
        
        $user->charge(1000, [
            'source' => $token,
              "currency" => "dkk",
        ]);
             
    }

But where should the Token come from, they shoulnd't have to enter their card information agian as i have it. @bobbybouwmann I appreciate the help!

bottelet's avatar

Ah i figured it out, got confused with Laravel's doc that said you needed to call 'source' => $token, so i looked at Stripes docs, which show "customer" => $user->stripe_id and used that instead, which works! Thanks for the help!

Please or to participate in this conversation.