Speed up QR create/update by cutting billing round-trips.
Deploy Ladill QR Plus / deploy (push) Successful in 2m2s

Create was making three sequential public HTTPS billing calls (~400ms each)
while holding a DB transaction open for image generation. Use loopback DNS
forcing, cache balances briefly, return balance from debit, regenerate images
only when style changes, and keep remote work outside the DB transaction.
This commit is contained in:
isaacclad
2026-07-16 21:25:16 +00:00
parent d83a314bee
commit 02e6778ccf
7 changed files with 261 additions and 64 deletions
+69
View File
@@ -0,0 +1,69 @@
<?php
namespace Tests\Unit;
use App\Services\Billing\BillingClient;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class BillingClientDebitTest extends TestCase
{
public function test_debit_returns_payload_and_caches_balance(): void
{
config([
'billing.api_url' => 'https://ladill.com/api/billing',
'billing.api_key' => 'test-key',
'billing.force_ip' => '',
]);
Http::fake([
'https://ladill.com/api/billing/debit' => Http::response([
'transaction_id' => 1,
'reference' => 'R1',
'type' => 'debit',
'amount_minor' => 500,
'balance_after_minor' => 1200,
'service' => 'qr',
], 201),
'https://ladill.com/api/billing/balance*' => Http::response([
'balance_minor' => 9999,
'currency' => 'GHS',
]),
]);
Cache::flush();
$client = app(BillingClient::class);
$result = $client->debit('user-1', 500, 'qr', 'qr_create', 'REF-1', 9, 'test');
$this->assertIsArray($result);
$this->assertSame(1200, $result['balance_after_minor']);
// Should use debit response cache, not hit balance endpoint.
$this->assertSame(1200, $client->balanceMinor('user-1'));
Http::assertSentCount(1);
}
public function test_can_afford_uses_balance_not_extra_endpoint(): void
{
config([
'billing.api_url' => 'https://ladill.com/api/billing',
'billing.api_key' => 'test-key',
'billing.force_ip' => '',
]);
Http::fake([
'https://ladill.com/api/billing/balance*' => Http::response([
'balance_minor' => 500,
'currency' => 'GHS',
]),
]);
Cache::flush();
$client = app(BillingClient::class);
$this->assertTrue($client->canAfford('user-1', 500));
$this->assertFalse($client->canAfford('user-1', 501));
Http::assertSentCount(1);
}
}