Skip to content

Commit

Permalink
Resume Subscription button
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jan 20, 2025
1 parent 2a62825 commit 69bb2da
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 5 deletions.
42 changes: 42 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,43 @@ public function change(Request $request, Tier $tier)
));
}

public function renew(Request $request)
{
try {
$this->subscription->user($request->user())
->renew();
$request->user()->log(UserLog::TYPE_SUB_RENEW);

$flash = 'subscribed';
$routeOptions = ['success' => 1];

return redirect()
->route('settings.subscription', $routeOptions)
->withSuccess(__('settings.subscription.success.' . $flash))
->with('sub_tracking', $flash)
->with('sub_value', $this->subscription->subscriptionValue())
;
} 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: 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
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
2 changes: 2 additions & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
'cancel' => [
'grace' => [
'text' => 'Your subscription is already set to end on :date, after which your premium campaigns will revert to standard campaigns and other benefits related to supporting Kanka will be disabled.',
'renew' => 'However, you can choose to renew your subscription to enjoy the benefits without interruptions.',
'title' => 'Grace period',
],
'options' => [
Expand Down Expand Up @@ -244,6 +245,7 @@
'actions' => [
'cancel' => 'Cancel subscription',
'downgrading' => 'Please contact us for downgrading',
'renew' => 'Renew subscription',
'rollback' => 'Change to Kobold',
'subscribe' => 'Change to :tier monthly',
'subscribe_annual' => 'Change to :tier yearly',
Expand Down
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]) }}">
{{ __('settings.subscription.subscription.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
22 changes: 22 additions & 0 deletions resources/views/settings/subscription/renew.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php /** @var \App\Models\User $user */ ?>
@php
$endDate = $user->subscription('kanka')->ends_at->isoFormat('MMMM D, Y');
@endphp
<x-dialog.header>
{{ __('settings.subscription.cancel.grace.title') }}
</x-dialog.header>

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

<a class="btn2 btn-lg btn-block btn-primary btn-outline" data-toggle="dialog" data-target="subscribe-confirm" data-url="{{ route('settings.subscription.renew') }}">
{{ __('settings.subscription.subscription.actions.renew') }}
</a>
</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::get('/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

0 comments on commit 69bb2da

Please sign in to comment.