[Cashier/Stripe] How can I know the payment method used for a subscription?
I want to let my users delete a payment method as long as it's not being used for an active subscription. From what I understand, Cashier doesn't offer this out of the box and I'd imagine I'd have to go to Stripe's API.
I looked at the subscription and payment method objects but they don't seem to reference each other. Is there a way to ask "what subscriptions is a given payment method being used for" or "what payment method is being used for a given subscription"?
So you will need to check this if it exists and it matches the payment method that you want to delete. Or you will check the default source for the customer or if this is his only payment source, then don't delete it.
@nakov Oh wow, I was looking at all these IDs in the object examples and I expected one of them to start with pm_. Should've read slowly and I would've seen default_payment_method.
So the idea would be something like this:
$subscriptions = \Stripe\Subscription::all();
foreach ($subscriptions as $subscription) {
if ($subscription->default_payment_method === request('payment_method')) {
// can't delete
}
}
I just gave it a try and default_payment_method is returning null.
I create the customer, add a payment method, add a new one and set it as default, fetch all subscriptions, loop over them and output default_payment_method. Not sure what I'm doing wrong.
@johnrivs you should get just the subscription for the current user not all that exist for your app, and you compare the id I guess, so try dumping what does default payment method returns, also comparing that to what you have as your payment method in the request.
The second link shows you the payment methods for the user, so you can use that one as a fallback if the user does not have a default payment method for the subscription and wants to be asked each time it needs to pay.
@nakov I did pass ['customer' => $user->stripe_id] to all(). I also tried fetching subscriptions one by one.
I can't just compare the customer's current default payment method to the one in the request. Imagine a customer uses payment method A to subscribe to product 1, then adds a new default payment method (B) and uses it to subscribe to product 2 and finally tries to delete payment method A. "Are you trying to delete the current default payment method?" is not enough because in this situation, even though payment method A is no longer the default one I shouldn't allow payment method A to be deleted, since it's being used for active subscription to product 1.
@johnrivs Yes you can because the customers default payment method will be used from then on for any other subscriptions, does not matter for which product was used before.. It is the same when the card expires and the customer enters a new card and at the time one subscription came up for payment, later another, it will use the new card not the old one.
And I said as a fallback check would be that only if the default_payment_method for the subscription has not been added.
@nakov Well.. I got a problem then because I asked Dries Vints and unless I'm understanding either of you wrong, what he says is in opposition to what you just explained.
@johnrivs I am reading the documentation my friend and based on the documentation you have a payment method attached to a subscription which in this case is the default_payment_method on the subscription object.
Then there are Payment methods for the customer and he can have 10 and here the default one can be different than the one used for the subscription.
But then the most important part is, that if a default payment method is not set for the Subscription, THEN it will reach for the default payment method selected by the customer in the Payment methods section.