Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Cashier paymentMethods() return always empty object result

  • Cashier Version: ^10.5
  • Laravel Version: ^6.0
  • PHP Version: 7.3.5
  • Database Driver & Version:

Description:

paymentMethods() retrieve always array with empty object.

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

Result : https://www.screencast.com/t/aqenaud77A

Also using Stripe PaymentMethod lib it's works.

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);
    }

Result :
https://www.screencast.com/t/X14ane7WyqS

GitHub : here

like image 692
Hiren Ghodasara Avatar asked Jan 31 '26 03:01

Hiren Ghodasara


2 Answers

$paymentMethods = $user->paymentMethods()->map(function($paymentMethod){
        return $paymentMethod->asStripePaymentMethod();
    });
like image 184
Carlos Cuamatzin Avatar answered Feb 01 '26 19:02

Carlos Cuamatzin


Here's a solution:

$paymentMethods = [];

foreach ($user->paymentMethods() as $paymentMethod) {
    $paymentMethods[] = $paymentMethod->asStripePaymentMethod();
}

Just loop through the payment methods and using $paymentMethod->asStripePaymentMethod() will get the payment methods for the user.

like image 38
Vectrobyte Avatar answered Feb 01 '26 19:02

Vectrobyte