dr897777 2018-06-03 10:14
浏览 62
已采纳

Laravel Cashier cancel()抛出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!

  • 写回答

2条回答 默认 最新

  • dongshanfan1941 2018-06-03 11:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探