From 822ef4b168c840d6ea584109e944e60bef086361 Mon Sep 17 00:00:00 2001 From: Noxo <142592979+noxoua@users.noreply.github.com> Date: Wed, 18 Oct 2023 22:24:40 +0300 Subject: [PATCH 1/3] Using a config instead of a model directly --- src/Models/Concerns/HasSubscriptions.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Models/Concerns/HasSubscriptions.php b/src/Models/Concerns/HasSubscriptions.php index 14b3fe1..d754444 100644 --- a/src/Models/Concerns/HasSubscriptions.php +++ b/src/Models/Concerns/HasSubscriptions.php @@ -194,7 +194,8 @@ public function giveTicketFor($featureName, $expiration = null, ?float $charges new LogicException('The tickets are not enabled in the configs.'), ); - $feature = Feature::whereName($featureName)->firstOrFail(); + $featureModel = config('soulbscription.models.feature'); + $feature = $featureModel::whereName($featureName)->firstOrFail(); $featureTicket = $this->featureTickets() ->make([ @@ -386,7 +387,8 @@ protected function loadTicketFeatures(): Collection return $this->loadedTicketFeatures; } - return $this->loadedTicketFeatures = Feature::with([ + $featureModel = config('soulbscription.models.feature'); + return $this->loadedTicketFeatures = $featureModel::with([ 'tickets' => fn (HasMany $query) => $query->withoutExpired()->whereMorphedTo('subscriber', $this), ]) ->whereHas( From dae9332281203c8dbd61deb641101b1d51cdf400 Mon Sep 17 00:00:00 2001 From: Noxo <142592979+noxoua@users.noreply.github.com> Date: Thu, 19 Oct 2023 13:45:12 +0300 Subject: [PATCH 2/3] Fixed giveTicketFor method --- src/Models/Concerns/HasSubscriptions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/Concerns/HasSubscriptions.php b/src/Models/Concerns/HasSubscriptions.php index d754444..a895e0e 100644 --- a/src/Models/Concerns/HasSubscriptions.php +++ b/src/Models/Concerns/HasSubscriptions.php @@ -187,7 +187,7 @@ public function switchTo(Plan $plan, $expiration = null, $immediately = true): S * @throws LogicException * @throws ModelNotFoundException */ - public function giveTicketFor($featureName, $expiration = null, ?float $charges = null): FeatureTicket + public function giveTicketFor($featureId, $expiration = null, ?float $charges = null): FeatureTicket { throw_unless( config('soulbscription.feature_tickets'), @@ -195,7 +195,7 @@ public function giveTicketFor($featureName, $expiration = null, ?float $charges ); $featureModel = config('soulbscription.models.feature'); - $feature = $featureModel::whereName($featureName)->firstOrFail(); + $feature = $featureModel::whereKey($featureId)->firstOrFail(); $featureTicket = $this->featureTickets() ->make([ From 2378d20702883bcf54138808d5f72e714924e6a0 Mon Sep 17 00:00:00 2001 From: Noxo <142592979+noxoua@users.noreply.github.com> Date: Thu, 19 Oct 2023 13:51:45 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cc0a7db..06a4941 100644 --- a/README.md +++ b/README.md @@ -457,10 +457,11 @@ Finally, open the `soulbscription.php` file and set the `feature_tickets` flag t #### Creating Tickets -To create a ticket, you can use the method `giveTicketFor`. This method expects the feature name, the expiration and optionally a number of charges (you can ignore it when creating tickets for not consumable features): +To create a ticket, you can use the method `giveTicketFor`. This method expects the feature id, the expiration and optionally a number of charges (you can ignore it when creating tickets for not consumable features): ```php -$subscriber->giveTicketFor('deploy-minutes', today()->addMonth(), 10); +$featureId = Feature::whereName('deploy-minutes')->first()->id; +$subscriber->giveTicketFor($featureId, today()->addMonth(), 10); ``` > This method will fire a `FeatureTicketCreated($subscriber, Feature $feature, FeatureTicket $featureTicket)` event. @@ -476,9 +477,9 @@ class UserFeatureTrialController extends Controller { public function store(FeatureTrialRequest $request, User $user) { - $featureName = $request->input('feature_name'); + $featureId = $request->input('feature_id'); $expiration = today()->addDays($request->input('trial_days')); - $user->giveTicketFor($featureName, $expiration); + $user->giveTicketFor($featureId, $expiration); return redirect()->route('admin.users.show', $user); } @@ -490,7 +491,7 @@ class UserFeatureTrialController extends Controller You can create tickets that never expire, so your subscribers will receive access to them forever: ```php -$subscriber->giveTicketFor('deploy-minutes', null, 10); +$subscriber->giveTicketFor($featureId, null, 10); ``` > Don't forget to remove these tickets when your user cancels his subscription. Otherwise, they will be able to consume the charges forever.