withoutMiddleware(EnsurePlatformSession::class); config([ 'billing.api_url' => 'https://ladill.com/api/billing', 'billing.api_key' => 'pos-billing-key', 'pos.pro.enabled' => true, 'pos.pro.price_minor' => 7900, 'pos.plans.pro.price_minor' => 7900, 'pos.plans.enterprise.price_minor' => 24900, ]); } private function user(): User { return User::factory()->create(['public_id' => 'usr_'.uniqid()]); } public function test_subscribe_charges_wallet_and_activates(): void { Http::fake(['*/debit' => Http::response(['id' => 1], 201)]); $user = $this->user(); $svc = app(SubscriptionService::class); [$ok] = $svc->subscribe($user); $this->assertTrue($ok); $this->assertTrue($svc->isPro($user)); Http::assertSent(fn ($r) => str_contains($r->url(), '/debit') && $r['service'] === 'pos'); } public function test_free_user_cannot_use_restaurant_mode(): void { $this->assertFalse(app(SubscriptionService::class)->canUseRestaurantMode($this->user())); } public function test_free_user_cannot_use_payment_gateway(): void { $user = $this->user(); $svc = app(SubscriptionService::class); $this->assertFalse($svc->canUsePaymentGateway($user)); $this->assertFalse(app(\App\Services\Payments\MerchantGatewayService::class)->isConfigured($user)); } public function test_pro_user_can_use_payment_gateway_when_configured(): void { Http::fake(['*/debit' => Http::response(['id' => 1], 201)]); $user = $this->user(); $svc = app(SubscriptionService::class); $svc->subscribe($user); \App\Models\PaymentGatewaySetting::create([ 'owner_ref' => $user->public_id, 'provider' => \App\Models\PaymentGatewaySetting::PROVIDER_PAYSTACK, 'public_key' => 'pk_test', 'secret_key' => 'sk_test', 'is_active' => true, ]); $this->assertTrue($svc->canUsePaymentGateway($user)); $this->assertTrue(app(\App\Services\Payments\MerchantGatewayService::class)->isConfigured($user)); } public function test_free_user_settings_ignores_gateway_credentials(): void { $user = $this->user(); \App\Models\PosLocation::create([ 'owner_ref' => $user->public_id, 'name' => 'Main', 'currency' => 'GHS', ]); $this->actingAs($user) ->put(route('pos.settings.update'), [ 'name' => 'Main', 'currency' => 'GHS', 'service_style' => 'retail', 'printer_paper_mm' => 80, 'gateway_provider' => \App\Models\PaymentGatewaySetting::PROVIDER_PAYSTACK, 'gateway_public_key' => 'pk_free_blocked', 'gateway_secret_key' => 'sk_free_blocked', 'gateway_is_active' => '1', ]) ->assertRedirect() ->assertSessionHas('success'); $this->assertNull( \App\Models\PaymentGatewaySetting::query()->where('owner_ref', $user->public_id)->first() ); } public function test_subscribe_enterprise_charges_wallet_and_activates(): void { Http::fake(['*/debit' => Http::response(['id' => 1], 201)]); $user = $this->user(); $svc = app(SubscriptionService::class); [$ok] = $svc->subscribeEnterprise($user); $this->assertTrue($ok); $this->assertTrue($svc->isEnterprise($user)); $sub = ProSubscription::where('user_id', $user->id)->first(); $this->assertSame('enterprise', $sub->plan); $this->assertSame(24900, $sub->price_minor); } public function test_renew_suspends_after_grace_when_unpaid(): void { Http::fake(['*/debit' => Http::response(['balance_minor' => 0], 402)]); $user = $this->user(); $sub = ProSubscription::create([ 'user_id' => $user->id, 'status' => 'active', 'price_minor' => 7900, 'auto_renew' => true, 'started_at' => now()->subDays(40), 'current_period_end' => now()->subDays(5), ]); app(SubscriptionService::class)->renewIfDue($sub->fresh()); $this->assertSame('past_due', $sub->fresh()->status); } }