Laravel Cashier paymentMethods() 返回始终为空的对象结果

Hir*_*ara 2 php stripe-payments laravel-5 laravel-cashier

  • 收银台版本:^10.5
  • Laravel 版本:^6.0
  • PHP 版本:7.3.5
  • 数据库驱动程序和版本:

描述:

paymentMethods() 总是用空对象检索数组。

public function userAllPaymentMethods(Request $request)
    {
        $user = User::find(5);
        $paymentMethod = $user->paymentMethods();
        return response($paymentMethod);
    }
Run Code Online (Sandbox Code Playgroud)

结果:https : //www.screencast.com/t/aqenaud77A

也使用 Stripe PaymentMethod lib 它的工作原理。

public function userAllPaymentMethods(Request $request)
    {
        $user = User::find(5);
        \Stripe\Stripe::setApiKey('{{KEY}}');
        $paymentMethod = \Stripe\PaymentMethod::all([
            'customer' =>  $user->stripe_id,
            'type' => 'card',
        ]);

        return response($paymentMethod);
    }
Run Code Online (Sandbox Code Playgroud)

结果:https :
//www.screencast.com/t/X14ane7WyqS

GitHub:这里

小智 6

$paymentMethods = $user->paymentMethods()->map(function($paymentMethod){
        return $paymentMethod->asStripePaymentMethod();
    });
Run Code Online (Sandbox Code Playgroud)

  • 希望它能解决问题,但请添加对代码的解释,以便用户能够完全理解他/她真正想要的内容。 (3认同)