Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed giveTicketFor method #83

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ 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'),
new LogicException('The tickets are not enabled in the configs.'),
);

$feature = Feature::whereName($featureName)->firstOrFail();
$featureModel = config('soulbscription.models.feature');
$feature = $featureModel::whereKey($featureId)->firstOrFail();

$featureTicket = $this->featureTickets()
->make([
Expand Down Expand Up @@ -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(
Expand Down