skip to Main Content
  • 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);
        StripeStripe::setApiKey('{{KEY}}');
        $paymentMethod = StripePaymentMethod::all([
            'customer' =>  $user->stripe_id,
            'type' => 'card',
        ]);

        return response($paymentMethod);
    }

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

GitHub : here

4

Answers


  1. The problem is not related to stripe, I think you are getting the right data, the problem is $paymentMethod is not encodable to JSON, that’s why you get an empty response, try adding dd($paymentMethod); before the return statement and check the result.

    Login or Signup to reply.
  2. I have the same issue. didn’t have time to investigate in deep. I just realized that method paymentMethod in cashier/src/Billable.php returns nothing.

    /**
         * Get a collection of the entity's payment methods.
         *
         * @param  array  $parameters
         * @return IlluminateSupportCollection|LaravelCashierPaymentMethod[]
         */
        public function paymentMethods($parameters = [])
        {
            $this->assertCustomerExists();
    
            $parameters = array_merge(['limit' => 24], $parameters);
    
            // "type" is temporarily required by Stripe...
            $paymentMethods = StripePaymentMethod::all(
                ['customer' => $this->stripe_id, 'type' => 'card'] + $parameters,
                $this->stripeOptions()
            );
    
            return collect($paymentMethods->data)->map(function ($paymentMethod) {
                return new PaymentMethod($this, $paymentMethod);
            });
        }
    

    return new PaymentMethod($this, $paymentMethod); this will return nothing instead of $paymentMethod. The solution for me was to overwrite PaymentMethod in mu user model the same way you did with stripe Lib.

    Login or Signup to reply.
  3. $paymentMethods = $user->paymentMethods()->map(function($paymentMethod){
            return $paymentMethod->asStripePaymentMethod();
        });
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search