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

Resume Subscription button #991

Merged
merged 4 commits into from
Jan 20, 2025
Merged
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
1 change: 0 additions & 1 deletion app/Enums/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ enum Permission: int
case PostTemplates = 18;
case Bookmarks = 19;
}

1 change: 0 additions & 1 deletion app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
return redirect()->route('dashboard', $this->campaign)->with(
'error_raw',
__('campaigns/modules.errors.disabled', [
'name' => $this->getEntityType()->plural(),

Check failure on line 114 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined method App\Http\Controllers\CrudController::getEntityType().
'fix' => '<a href="' . route('campaign.modules', [$this->campaign, '#' . $this->getEntityType()->code]) . '">' . __('crud.fix-this-issue') . '</a>',

Check failure on line 115 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined method App\Http\Controllers\CrudController::getEntityType().
])
);
}
Expand Down Expand Up @@ -300,7 +300,7 @@
}
public function crudCreate($params = [])
{
$this->authorize('create', [$this->getEntityType(), $this->campaign]);

Check failure on line 303 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined method App\Http\Controllers\CrudController::getEntityType().

if ($this->hasLimitCheck) {
// @phpstan-ignore-next-line
Expand Down Expand Up @@ -357,7 +357,7 @@
*/
public function crudStore(Request $request, bool $redirectToCreated = false)
{
$this->authorize('create', [$this->getEntityType(), $this->campaign]);

Check failure on line 360 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined method App\Http\Controllers\CrudController::getEntityType().


// For ajax requests, send back that the validation succeeded, so we can really send the form to be saved.
Expand Down Expand Up @@ -478,7 +478,7 @@
*/
public function crudEdit(Model|MiscModel $model)
{
$this->authorize('update', $model->entity);

Check failure on line 481 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Access to an undefined property Illuminate\Database\Eloquent\Model::$entity.

/** @var MiscModel $model */
$editingUsers = null;
Expand Down Expand Up @@ -519,7 +519,7 @@
*/
public function crudUpdate(Request $request, Model|MiscModel $model)
{
$this->authorize('update', $model->entity);

Check failure on line 522 in app/Http/Controllers/CrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Access to an undefined property Illuminate\Database\Eloquent\Model::$entity.

// For ajax requests, send back that the validation succeeded, so we can really send the form to be saved.
if (request()->ajax()) {
Expand Down Expand Up @@ -696,7 +696,6 @@

/**
* Load a list of templates the user can create new entities from
* @param MiscModel $model
*/
protected function loadTemplates(EntityType $entityType): Collection
{
Expand Down
43 changes: 43 additions & 0 deletions app/Http/Controllers/Settings/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Services\Users\CurrencyService;
use App\Services\Users\EmailValidationService;
use App\Models\User;
use App\Models\UserLog;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -115,6 +116,10 @@ public function change(Request $request, Tier $tier)

// If the user has a cancelled sub still ending
if ($user->subscribed('kanka') && $user->subscription('kanka')->onGracePeriod() && !$user->hasPayPal()) {
if ($tier->isCurrent($user)) {
return view('settings.subscription.renew')
->with('user', $user);
}
return view('settings.subscription.change_blocked')
->with('user', $user);
}
Expand Down Expand Up @@ -169,6 +174,44 @@ public function change(Request $request, Tier $tier)
));
}

public function renew(Request $request)
{
if ($request->ajax()) {
return response()->json([]);
}
try {
$this->subscription
->user($request->user())
->renew();
$request->user()->log(UserLog::TYPE_SUB_RENEW);

$routeOptions = [];

return redirect()
->route('settings.subscription', $routeOptions)
->withSuccess(__('subscriptions.renew.success'))
;
} catch (IncompletePayment $exception) {
session()->put('subscription_callback', $request->get('payment_id'));
return redirect()->route(
'cashier.payment',
// @phpstan-ignore-next-line
[$exception->payment->id, 'redirect' => route('settings.subscription.callback')]
);
} catch (TranslatableException $e) {
return redirect()
->route('settings.subscription')
->with('error_raw', $e->getTranslatedMessage())
;
} catch (Exception $e) {
// Error? json
return response()->json([
'error' => true,
'message' => $e->getMessage(),
]);
}
}

/**
* Subscribe
*/
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/SubscriptionEndJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function handle()
{
/** @var ?User $user */
$user = User::find($this->userId);
if (empty($user) || $this->userId == 27078) {
// User deleted their account already.
if (empty($user) || $this->userId == 27078 || $user->subscribed('kanka')) {
// User deleted their account already or renewed their subscription.
return;
}

Expand Down
1 change: 0 additions & 1 deletion app/Models/Scopes/AclScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Enums\Permission;
use App\Facades\CampaignLocalization;
use App\Facades\Permissions;
use App\Models\CampaignPermission;
use App\Models\Entity;
use App\Models\Post;
use App\Models\MiscModel;
Expand Down
1 change: 1 addition & 0 deletions app/Models/UserLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class UserLog extends Model
public const TYPE_SUB_DOWNGRADE = 13;
public const TYPE_SUB_FAIL = 15;
public const TYPE_SUB_PAYPAL = 16;
public const TYPE_SUB_RENEW = 17;

public const TYPE_CAMPAIGN_NEW = 20;
public const TYPE_CAMPAIGN_JOIN = 21;
Expand Down
5 changes: 0 additions & 5 deletions app/Policies/MiscPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace App\Policies;

use App\Facades\CampaignLocalization;
use App\Facades\UserCache;
use App\Models\Campaign;
use App\Facades\EntityPermission;
use App\Models\CampaignPermission;
use App\Models\Entity;
use App\Models\MiscModel;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Auth;

class MiscPolicy
{
Expand Down
1 change: 0 additions & 1 deletion app/Services/Entity/PrivacyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Services\Entity;

use App\Enums\Permission;
use App\Models\CampaignPermission;
use App\Models\CampaignRole;
use App\Models\Entity;
use App\Models\User;
Expand Down
23 changes: 12 additions & 11 deletions app/Services/Permissions/EntityPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

class EntityPermission
{
use UserAware;
use CampaignAware;
use EntityAware;
use EntityTypeAware;
use CampaignAware;
use UserAware;

protected array $cached = [];

Expand Down Expand Up @@ -64,18 +64,18 @@ public function can(Permission $permission): bool
$perm = $this->cached[$key];
}

// dump('module permission');
// dump($key);
// dump($this->cached);
// dump('module permission');
// dump($key);
// dump($this->cached);
if (!isset($this->entity)) {
// dd('no entity');
// dd('no entity');
return $perm;
}

// Search for entity
$entityKey = '_' . $permission->value . '_' . $this->entity->id;
// dump('check entity');
// dd($entityKey);
// dump('check entity');
// dd($entityKey);
if (isset($this->cached[$entityKey])) {
return $this->cached[$entityKey];
}
Expand Down Expand Up @@ -106,6 +106,7 @@ public function hasPermission(
}
$key = $entityType . '_' . $action;


// if ($action === Permission::Bookmarks->value) {
// dump($key = $entityType . '_' . $action);
// dump($this->cached);
Expand All @@ -118,8 +119,8 @@ public function hasPermission(

// Check if we have permission to do this action for exactly this entity
if (!empty($entity)) {
// dump('i have an entity?');
// dump($entity);
// dump('i have an entity?');
// dump($entity);

//Check if $entity is an entity type.
if (isset($entity->type_id)) {
Expand All @@ -128,7 +129,7 @@ public function hasPermission(
//dump('misc object');
$entityKey = '_' . $action . '_' . $entity->id;
}
// dump('entity key ' . $entityKey);
// dump('entity key ' . $entityKey);
if (isset($this->cached[$entityKey])) {
$perm = $this->cached[$entityKey];
}
Expand Down
2 changes: 0 additions & 2 deletions app/Services/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

use App\Facades\Avatar;
use App\Facades\Mentions;
use App\Facades\Module;
use App\Models\Calendar;
use App\Models\Entity;
use App\Models\EntityAsset;
use App\Models\EntityType;
use App\Models\MiscModel;
use App\Services\Entity\NewService;
use App\Traits\CampaignAware;
use App\Traits\EntityTypeAware;
use App\Traits\UserAware;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down
6 changes: 6 additions & 0 deletions app/Services/SubscriptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public function subscribe(string $paymentID): self
return $this;
}

public function renew(): void
{
$this->user->subscription('kanka')->resume();
}


/**
* Setup the user's pledge, role, discord
* @return $this
Expand Down
1 change: 0 additions & 1 deletion app/Traits/GuestAuthTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Enums\Permission;
use App\Facades\CampaignLocalization;
use App\Facades\EntityPermission;
use App\Models\CampaignPermission;
use App\Models\Entity;

trait GuestAuthTrait
Expand Down
10 changes: 10 additions & 0 deletions lang/en/subscriptions/renew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'title' => 'Subscription renewal',
'helper' => 'However, you can choose to renew your subscription to enjoy the benefits without interruptions.',
'success' => '',
'actions' => [
'renew' => 'Renew subscription',
],
];
12 changes: 9 additions & 3 deletions resources/views/settings/subscription/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,15 @@
</div>
@if (!$tier->isFree() && $tier->isCurrent($user) && $user->subscribed('kanka') && !$hasManual)
<div class="self-bottom">
<a class="btn2 btn-block btn-sm btn-error " data-toggle="dialog" data-target="subscribe-confirm" data-url="{{ route('settings.subscription.unsubscribe') }}">
{{ __('settings.subscription.subscription.actions.cancel') }}
</a>
@if ($user->subscription('kanka')?->onGracePeriod())
<a class="btn2 btn-block btn-sm btn-primary " data-toggle="dialog" data-target="subscribe-confirm" data-url="{{ route('settings.subscription.change', [$tier]) }}">
{{ __('subscriptions/renew.actions.renew') }}
</a>
@else
<a class="btn2 btn-block btn-sm btn-error " data-toggle="dialog" data-target="subscribe-confirm" data-url="{{ route('settings.subscription.unsubscribe') }}">
{{ __('settings.subscription.subscription.actions.cancel') }}
</a>
@endif
</div>
@endif
</div>
Expand Down
32 changes: 32 additions & 0 deletions resources/views/settings/subscription/renew.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php /** @var \App\Models\User $user */ ?>
@php
$endDate = $user->subscription('kanka')->ends_at->isoFormat('MMMM D, Y');
@endphp
<x-dialog.header>
{{ __('subscriptions/renew.title') }}
</x-dialog.header>

<article class="text-center max-w-xl container">
<x-grid type="1/1">
<x-helper>
<p class="text-left">
{!! __('settings.subscription.cancel.grace.text', ['date' => '<span class="text-error">' . $endDate . '</span>'])!!}
</p>
</x-helper>
<x-helper>
<p class="text-left">
{!! __('subscriptions/renew.helper')!!}
</p>
</x-helper>
</x-grid>

<x-form :action="['settings.subscription.renew']">

<x-buttons.confirm type="primary">
<x-icon class="fa-solid fa-repeat" />
<span>
{{ __('subscriptions/renew.actions.renew') }}
</span>
</x-buttons.confirm>
</x-form>
</article>
1 change: 1 addition & 0 deletions routes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

Route::get('/subscription', [SubscriptionController::class, 'index'])->name('settings.subscription');
Route::get('/subscription/change/{tier}', [SubscriptionController::class, 'change'])->name('settings.subscription.change');
Route::post('/subscription/renew', [SubscriptionController::class, 'renew'])->name('settings.subscription.renew');
Route::get('/subscription/callback', [SubscriptionController::class, 'callback'])->name('settings.subscription.callback');
Route::post('/subscription/change/{tier}', [SubscriptionController::class, 'subscribe'])->name('settings.subscription.subscribe');
Route::get('/subscription/unsubscribe', [CancellationController::class, 'index'])->name('settings.subscription.unsubscribe');
Expand Down
Loading