`$product` should be nullable in `subscription()->swap(...)`
Closed this issue · 5 comments
I think the $product
variable should be nullable in LemonSqueezy\Laravel\Subscription.php
public function swap(string $product, string $variant, array $attributes = []): self
In the package README it is suggested to use only the variant id to swap a subscribed user to a different plan:
use App\Models\User;
$user = User::find(1);
$user->subscription()->swap('variant-id');
And according to the LS docs supplying only the variant id should work as well (which also worked in my tests with the API). My understanding is that it requires either:
- $variant or
- $product AND $variant
Proposed solution:
Update method signature to public function swap(?string $product, string $variant, array $attributes = []): self
and only conditionally merge it to the payload. Because sending 'product_id' => null
to LS seems to produce an error.
An alternative would be to change the README to $user->subscription()->swap('variant-id', 'product-id')
which also swaps the subscription but seems less clean as the product id technically isn't necessary for the API to work.
(and big ❤ thanks for this package!)
Heya. It seems I made a mistake in the docs. A product id and a variant id are both always required. You're saying you can only use a variant id? Because if that's true then something has changed since I last tested this myself as I always was required to use both.
Yeah, it works with only a variant id.
It's possible they changed the behavior at some point. Someone from LS might be able to answer that. But only providing the variant id is also in accordance with their current docs:
product_id
The ID of the Product Object you want to switch this subscription to. If set, requires a Variant Object ID.variant_id
The ID of the Variant Object you want to switch this subscription to. Required if product_id set.
I'm gonna have to try that out myself first. In the meantime I've updated the docs to the current implementation. Thanks for filing this.
Of course, I'm happy to help.
In case you decide that it should be changed, don't forget that the signature of swapAndInvoice(string $product, string $variant): self
should also be changed.
I did some more looking into this and found out that a product ID is required when swapping to a different product alltogether. When swapping to a different variant for the same product you don't need to provide a product ID.
So I've decided to just keep things in place and always require a product ID for now to not overcomplicate things. Hopefully Lemon Squeezy will at some point not require the product ID any longer when doing subscription updates.
I've added a note to the docs: 6e08ca8