Add Frontdesk Pro wallet renewal scheduling.
Deploy Ladill Frontdesk / deploy (push) Successful in 58s
Deploy Ladill Frontdesk / deploy (push) Successful in 58s
Monthly auto-renew charges due organizations from the Ladill wallet and downgrades after grace when payment fails. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Services\Frontdesk\ProRenewalService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class RenewProSubscriptionsCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'frontdesk:pro-renew';
|
||||||
|
|
||||||
|
protected $description = 'Charge the Ladill wallet for due Frontdesk Pro subscriptions (and downgrade after the grace window).';
|
||||||
|
|
||||||
|
public function handle(ProRenewalService $renewals): int
|
||||||
|
{
|
||||||
|
$due = $renewals->dueOrganizations();
|
||||||
|
$this->info("Renewing {$due->count()} due organization(s).");
|
||||||
|
$due->each(fn ($organization) => $renewals->renewIfDue($organization));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -80,7 +80,11 @@ class ProController extends Controller
|
|||||||
|
|
||||||
$settings = $organization->settings ?? [];
|
$settings = $organization->settings ?? [];
|
||||||
$settings['plan'] = 'pro';
|
$settings['plan'] = 'pro';
|
||||||
$settings['plan_expires_at'] = now()->addMonth()->toIso8601String();
|
$settings['auto_renew'] = true;
|
||||||
|
$settings['plan_expires_at'] = now()
|
||||||
|
->addDays((int) config('frontdesk.pro.period_days', 30))
|
||||||
|
->toIso8601String();
|
||||||
|
unset($settings['plan_renewal_error']);
|
||||||
$organization->update(['settings' => $settings]);
|
$organization->update(['settings' => $settings]);
|
||||||
|
|
||||||
return redirect()->route('frontdesk.pro.index')
|
return redirect()->route('frontdesk.pro.index')
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Frontdesk;
|
||||||
|
|
||||||
|
use App\Models\Organization;
|
||||||
|
use App\Services\Billing\BillingClient;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
class ProRenewalService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly BillingClient $billing,
|
||||||
|
private readonly PlanService $plans,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/** @return Collection<int, Organization> */
|
||||||
|
public function dueOrganizations(): Collection
|
||||||
|
{
|
||||||
|
return Organization::query()
|
||||||
|
->where('settings->plan', 'pro')
|
||||||
|
->whereNotNull('settings->plan_expires_at')
|
||||||
|
->get()
|
||||||
|
->filter(fn (Organization $organization) => $this->isDue($organization));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDue(Organization $organization): bool
|
||||||
|
{
|
||||||
|
$settings = $organization->settings ?? [];
|
||||||
|
if (($settings['plan'] ?? '') !== 'pro') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (array_key_exists('auto_renew', $settings) && $settings['auto_renew'] === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (empty($settings['plan_expires_at'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Carbon::parse($settings['plan_expires_at'])->lte(now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renewIfDue(Organization $organization): void
|
||||||
|
{
|
||||||
|
if (! $this->isDue($organization)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = $organization->settings ?? [];
|
||||||
|
$price = $this->plans->proPriceMinor();
|
||||||
|
$reference = 'frontdesk-pro-'.$organization->id.'-'.now()->format('YmdHis');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$charged = $this->billing->debit(
|
||||||
|
$organization->owner_ref,
|
||||||
|
$price,
|
||||||
|
'frontdesk_pro_renewal',
|
||||||
|
$reference,
|
||||||
|
'Ladill Frontdesk Pro — monthly renewal',
|
||||||
|
);
|
||||||
|
} catch (\Throwable) {
|
||||||
|
$charged = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($charged) {
|
||||||
|
$settings['plan'] = 'pro';
|
||||||
|
$settings['auto_renew'] = true;
|
||||||
|
$settings['plan_expires_at'] = now()
|
||||||
|
->addDays((int) config('frontdesk.pro.period_days', 30))
|
||||||
|
->toIso8601String();
|
||||||
|
unset($settings['plan_renewal_error']);
|
||||||
|
$organization->update(['settings' => $settings]);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$expires = Carbon::parse($settings['plan_expires_at']);
|
||||||
|
$graceEnd = $expires->copy()->addDays((int) config('frontdesk.pro.grace_days', 3));
|
||||||
|
if ($graceEnd->isPast()) {
|
||||||
|
$settings['plan'] = 'free';
|
||||||
|
unset($settings['plan_expires_at']);
|
||||||
|
$settings['plan_renewal_error'] = 'Suspended after failed renewal.';
|
||||||
|
} else {
|
||||||
|
$settings['plan_renewal_error'] = 'Renewal failed — top up your wallet.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$organization->update(['settings' => $settings]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -120,6 +120,11 @@ return [
|
|||||||
'sms_segment_length' => (int) env('FRONTDESK_SMS_SEGMENT_LENGTH', 160),
|
'sms_segment_length' => (int) env('FRONTDESK_SMS_SEGMENT_LENGTH', 160),
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'pro' => [
|
||||||
|
'grace_days' => (int) env('FRONTDESK_PRO_GRACE_DAYS', 3),
|
||||||
|
'period_days' => (int) env('FRONTDESK_PRO_PERIOD_DAYS', 30),
|
||||||
|
],
|
||||||
|
|
||||||
'plans' => [
|
'plans' => [
|
||||||
'free' => [
|
'free' => [
|
||||||
'label' => 'Free',
|
'label' => 'Free',
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ Schedule::command('frontdesk:mark-expired-badges')->everyFifteenMinutes();
|
|||||||
Schedule::command('frontdesk:mark-devices-offline')->everyFiveMinutes();
|
Schedule::command('frontdesk:mark-devices-offline')->everyFiveMinutes();
|
||||||
Schedule::command('frontdesk:employee-return-alerts')->everyFiveMinutes();
|
Schedule::command('frontdesk:employee-return-alerts')->everyFiveMinutes();
|
||||||
Schedule::command('frontdesk:send-daily-reports')->dailyAt('07:00');
|
Schedule::command('frontdesk:send-daily-reports')->dailyAt('07:00');
|
||||||
|
Schedule::command('frontdesk:pro-renew')->dailyAt('02:15')->withoutOverlapping();
|
||||||
|
|||||||
@@ -86,6 +86,28 @@ class FrontdeskProTest extends TestCase
|
|||||||
$this->assertSame('pro', $this->organization->fresh()->settings['plan']);
|
$this->assertSame('pro', $this->organization->fresh()->settings['plan']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_pro_renew_extends_subscription_when_wallet_charges(): void
|
||||||
|
{
|
||||||
|
$this->organization->update([
|
||||||
|
'settings' => [
|
||||||
|
'onboarded' => true,
|
||||||
|
'plan' => 'pro',
|
||||||
|
'auto_renew' => true,
|
||||||
|
'plan_expires_at' => now()->subDay()->toIso8601String(),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Http::fake([
|
||||||
|
'billing.test/debit' => Http::response(['ok' => true]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('frontdesk:pro-renew')->assertSuccessful();
|
||||||
|
|
||||||
|
$settings = $this->organization->fresh()->settings;
|
||||||
|
$this->assertSame('pro', $settings['plan']);
|
||||||
|
$this->assertTrue(now()->parse($settings['plan_expires_at'])->isFuture());
|
||||||
|
}
|
||||||
|
|
||||||
public function test_pro_sidebar_shows_active_label_after_upgrade(): void
|
public function test_pro_sidebar_shows_active_label_after_upgrade(): void
|
||||||
{
|
{
|
||||||
$this->organization->update([
|
$this->organization->update([
|
||||||
|
|||||||
Reference in New Issue
Block a user