Mishek
Mishek

Reputation: 86

Laravel Cashier Stripe Subscription when Billable model is not User

I am using laravel cashier 13.4.4 and I have a problem with update subscription.

In my configuration, my Billable model is not set to User but to Company model. Everything works fine, I mean - create/get/update customer, add/get payment methods, get and create subscription... But when I am trying to use in any way asStripeSubscription or updateStripeSubscription or any other operation which fire this method later (like updateQuantity) I am getting error:

Error: Call to a member function stripe() on null in file /var/www/html/vendor/laravel/cashier/src/Subscription.php on line 1269

#0 /var/www/html/vendor/laravel/cashier/src/Subscription.php(495): Laravel\Cashier\Subscription->updateStripeSubscription(Array)

Part of code from /laravel/cashier/src/Subscription.php:

/**
     * Update the underlying Stripe subscription information for the model.
     *
     * @param  array  $options
     * @return \Stripe\Subscription
     */
    public function updateStripeSubscription(array $options = [])
    {
        return $this->owner->stripe()->subscriptions->update(
            $this->stripe_id, $options
        );
    }

    /**
     * Get the subscription as a Stripe subscription object.
     *
     * @param  array  $expand
     * @return \Stripe\Subscription
     */
    public function asStripeSubscription(array $expand = [])
    {
        return $this->owner->stripe()->subscriptions->retrieve(
            $this->stripe_id, ['expand' => $expand]
        );
    }

$this->owner is null for me.

Anyone had similar problem?

Upvotes: 1

Views: 1889

Answers (1)

Colin
Colin

Reputation: 735

I hit a similar issue but I'm using a 'Team' model as the billable model, not the default 'User' model. It appears Cashier now requires you to make an addition to the AppServiceProvider to inform it of a different billable model. This was implemented in the .env before.

use App\Models\Cashier\User;
use Laravel\Cashier\Cashier;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Cashier::useCustomerModel(Team::class);
}

Upvotes: 1

Related Questions