- 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
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.
I have the same issue. didn’t have time to investigate in deep. I just realized that method
paymentMethod
incashier/src/Billable.php
returns nothing.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.Here’s a solution:
Just loop through the payment methods and using
$paymentMethod->asStripePaymentMethod()
will get the payment methods for the user.