Files
isaaccladandCursor 4dcc46e319
Deploy Ladill POS / deploy (push) Successful in 37s
Make POS payment routing plan-aware.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 19:47:11 +00:00

184 lines
6.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Pos\ProSubscription;
use App\Models\User;
use App\Services\Pos\SubscriptionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class PosProTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->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,
'pay.api_url' => 'https://ladill.com/api/pay',
'pay.api_key' => 'pos-pay-key',
]);
}
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_free_and_invalid_owner_gateway_fall_back_to_ladill_pay(): void
{
Http::fake(['*/api/pay/checkouts' => Http::response([
'reference' => 'LP-POS-FALLBACK',
'checkout_url' => 'https://checkout.paystack.com/fallback',
], 201)]);
$user = $this->user();
\App\Models\PaymentGatewaySetting::create([
'owner_ref' => $user->public_id,
'provider' => 'paystack',
'public_key' => 'invalid',
'secret_key' => 'invalid',
'is_active' => true,
]);
$result = app(\App\Services\Payments\MerchantGatewayService::class)->initializeCheckout(
$user, 1000, 'GHS', 'seller@example.com', 'https://pos.test/callback', 'POSP-OLD',
);
$this->assertSame('ladill_pay', $result['provider']);
$this->assertSame('LP-POS-FALLBACK', $result['reference']);
}
public function test_downgrade_disables_owner_gateway_without_deleting_credentials(): void
{
$user = $this->user();
ProSubscription::create([
'user_id' => $user->id, 'plan' => 'pro', 'status' => 'active',
'price_minor' => 7900, 'current_period_end' => now()->addMonth(),
]);
$setting = \App\Models\PaymentGatewaySetting::create([
'owner_ref' => $user->public_id, 'provider' => 'paystack',
'public_key' => 'pk_test_saved', 'secret_key' => 'sk_test_saved', 'is_active' => true,
]);
$this->assertTrue(app(\App\Services\Payments\MerchantGatewayService::class)->shouldUseOwnerGateway($user));
ProSubscription::where('user_id', $user->id)->update(['status' => 'past_due']);
$this->assertFalse(app(\App\Services\Payments\MerchantGatewayService::class)->shouldUseOwnerGateway($user->fresh()));
$this->assertDatabaseHas('payment_gateway_settings', ['id' => $setting->id, 'is_active' => true]);
}
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);
}
}