Search code examples
phplaravellaravel-5.5laravel-cashier

Laravel Cashier cancel() throws a BadMethodCallException


I used Laravel Cashier to handle my user's subscription, however when I try to do the basic cancellation $user->subscription('main')->cancel(), an exception is being thrown

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::asStripeCustomer() in

\try\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:2483

Stack trace:

\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1470): Illuminate\Database\Eloquent\Builder->__call('asStripeCustome...', Array)

\try\vendor\laravel\cashier\src\Subscription.php(345): Illuminate\Database\Eloquent\Model->__call('asStripeCustome...', Array)

\try\vendor\laravel\cashier\src\Subscription.php(256): Laravel\Cashier\Subscription->asStripeSubscription()

I setup the Model correctly and I uses the Billable trait so I really have no idea what's really causing this error

App\User.php

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Cashier\Billable;

class User extends Authenticatable{
   use Billable;

   ...
}

App\Http\Controllers\UserController.php

public function cancelSubscription(Request $request)
{
    $user = $request->user();

    try {
        if ($user->subscription('main')->onTrial()) {
            $user->subscription('main')->cancelNow();
        } else {
            $user->subscription('main')->cancel();
        }
    } catch (\Exception $e) {
        \Log::error($e);
        return [
            'success' => 0,
            'message' => "Something went wrong while trying cancel your subscription. Please try again later."
        ];
    }

Any help and hints will be greatly appreciated, thanks in advance!


Solution

  • My bad, I just found out that it was actually with my stripe configuration on /config/services.php as I have two models for my Users (because I'm also using another package other than laravel-cashier to handle payments via Authorize.net on which I ended up creating different Models for them to work)

    'stripe' => [
        // 'model' => App\AnetUser::class, => this actually caused the error as
                   // ->asStripeCustomer() doesn't exists on an Authorize.net's Billable trait 
        'model' => App\User::class,
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
    ],
    

    I feel so stupid. XD