简体   繁体   中英

Laravel 7 Cashier subscription: Call to a member function create() on null

I'm having 2 products here's one of them

在此处输入图像描述

I have implemented the default payment form of stripe https://stripe.com/docs/stripe-js . User class uses billable like this

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
use Notifiable;
use Billable;

.env contains correct data

STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

Now I'm trying to make the user subscribe to this plan this way:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cartalyst\Stripe\Stripe;
use App\User;

class PaymentController extends Controller
{
    public function store(Request $request){
        $user = auth()->user();
        $user->subscription('prod_HeC7XMT2SVe21K')->create($request->stripeToken);
    }
}

But I get

Call to a member function create() on null

Why is that and how to fix it?

Note: I didn't use the secret key anywhere except in the.env file as I followed some tutorials don't know if that's correct.

You can try to create the subscription in the following way:

$subscription = $user->newSubscription('plan_name' , 'plan_id'); 
$subscription->create('payment_method', ['other_parameters']);

replace
'plan_name' with the name of the plan created on stripe dashboard.
'plan_id' with the id of the plan selected.
'other_parameters' can include user's email, phone_number etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM