renoki-co / jetstream-cashier-billing-portal

Cashierstream is a simple Spark alternative written for Laravel Jetstream, with the super-power of tracking plan quotas, like seats or projects number on a per-plan basis
Apache License 2.0
163 stars 20 forks source link

Question/Feature: Is it possible to use models other than User (e.g. Team)? #2

Closed benferns closed 3 years ago

benferns commented 3 years ago

I saw SubscriptionController.php references

$user = $request->user();

But noticed elsewhere the more generic Model class was used. Is there a way to configure the package to use the CASHIER_MODEL specified in the env vars?

benferns commented 3 years ago

I've been doing some thinking on this and I can see how its a bit tough to know which model in particular (if its not the user). I'm not the most-experienced laravel coder, so I may be missing standardised approaches to this kind of problem.

Maybe an option to allow setting of the model via the route params, to allow the dev in question to link correctly? Or an overridable ->billableModel() method on User via a trait that returns the specific cashier model they want (though it could be awkward if the billable model is in no way linked to particular users)?

For now I've taken a hacky approach with my project, duplicated the controllers and replaced all $request->user() calls with $request->user()->currentTeam and that got me 95% of the way there since most of the Billable Model references are generic. (I suspect options for Team and User models would actually cover 80%+ of use-cases).

Thanks for the work on the project - its invaluable being able to move off of Spark while they are in limbo.

rennokki commented 3 years ago

@60days How about configuring it directly in the service provider. Something like:

BillingPortal::setBillable(function ($request) {
    return $request->user()->currentTeam; // $request->user() by default
});
rennokki commented 3 years ago

I have tagged v1.2.0