laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.38k stars 679 forks source link

Cant access the owner and paymentMethod property when using the defaultPaymentMethod() #839

Closed carizzapanopio closed 4 years ago

carizzapanopio commented 4 years ago

I am getting an error "Cannot access protected property Laravel\Cashier\PaymentMethod::$owner" $user->defaultPaymentMethod() results to this. image

However, I couldnt access $owner and $paymentMethod property.

php version: 7.2 laravel: 5.8 laravel cashier: 10.5 stripe-php: 7.14

driesvints commented 4 years ago

We'll need a little more code here to reproduce the problem.

carizzapanopio commented 4 years ago

$user_card_details = $user->defaultPaymentMethod(); var_dump($user_card_details); var_dump($user_card_details::$owner);

@driesvints

driesvints commented 4 years ago

var_dump($user_card_details::$owner);

The owner property isn't accessible indeed. But we can add a getter for it. It's btw the same instance as the $user property. The paymentMethod property can be accessed by asStripePaymentMethod.

0dp commented 4 years ago

Sorry to barge in here. I am experiencing a similar issue with laravel 6 and cashier 10

To create a subscription:

$user = User::find(1);
$user->newSubscription('default', 'premium')->create($paymentMethod);

to get the $paymentMethod

$paymentMethods = $user->paymentMethods();

But as experienced, paymentMethod is a protected property.

carizzapanopio commented 4 years ago

Hello @0dp I was able to access the paymentMethod through this: $user->defaultPaymentMethod()->asStripePaymentMethod();

0dp commented 4 years ago

Hi @carizzapanopio thanks for pitching in. Unfortunately this approach did not work for me since asStripePaymentMethod is not part of $user?

I feel like a huge looser, because I just wanted to get it to work so I wrote a Guzzle method to fetch the payment_methods id from the customer id 😭

I noticed that $user->defaultPaymentMethod() returns an object but create() wants a string 🧐 It does hold the paymentmethod🤷‍♂️

carizzapanopio commented 4 years ago

hi @0dp . if you check the contents if $user->defaultPaymentMethod()->asStripePaymentMethod(), this wil return the payment method object. if the create method needs a string, pass the payment method id instead.

$paymentMethod = $user->defaultPaymentMethod()->asStripePaymentMethod();
$user->newSubscription('default', 'premium')->create($paymentMethod->id);
0dp commented 4 years ago

Thanks! This was really helpful 🙏

driesvints commented 4 years ago

Sent in a PR for this: https://github.com/laravel/cashier/pull/877