fix: preserve KOOP identity and payment eligibility when using a single API

This commit is contained in:
nurafrinaalimi16
2026-09-23 11:01:07 +08:00
parent 63231a23af
commit bdce66bae5
3 changed files with 79 additions and 1 deletions
+11 -1
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\CompanyStatus;
use Illuminate\Support\Facades\Http;
class CustomerInfoService
@@ -26,6 +27,15 @@ class CustomerInfoService
return $requests;
});
return [$responses[0]->json(), isset($responses[1]) ? $responses[1]->json() : []];
$customer = $responses[0]->json();
if ($pkbUrl === $koopUrl) {
// Preserve the source company: payment rules depend on this identity.
return config('api.company') == CompanyStatus::COM_KOOPPKB
? [[], $customer]
: [$customer, []];
}
return [$customer, $responses[1]->json()];
}
}
+2
View File
@@ -4,6 +4,8 @@ $url = env('API_URL', 'https://apiujrah.erahn.com.my');
return [
'url' => $url,
// API Ujrah was the KOOP source before the endpoints were unified.
'company' => \App\Models\CompanyStatus::COM_KOOPPKB,
// Keep existing KOOP callers on the same customer API.
'url_koop' => $url,
];
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature;
use App\Models\CompanyStatus;
use App\Services\PembiayaanService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class SingleApiPembiayaanTest extends TestCase
{
/** @dataProvider paymentCases */
public function test_single_api_preserves_company_and_payment_restrictions($company, $pending, $ujrahBlocked, $disabled)
{
config([
'api.url' => 'https://customer.example',
'api.url_koop' => 'https://customer.example/',
'api.company' => $company,
]);
Auth::shouldReceive('user')->andReturn(['icno' => 'test-customer']);
$record = [
'norujukan' => 'TEST-001', 'kodcaw' => 'X03', 'tempoh' => 1,
'bakipjm' => 350, 'hargajualan' => 1, 'tagbaru' => $ujrahBlocked ? 1 : 0,
'lelong_tawarruq' => 1, 't_lelong' => '',
];
Http::fake(function ($request) use ($record, $pending, $ujrahBlocked) {
$path = parse_url($request->url(), PHP_URL_PATH);
// A trailing slash in the alias must not affect the fake response.
$path = '/' . ltrim($path, '/');
if ($path === '/portal/customer/info') {
return Http::response(['gadai' => [$record], 'lelong' => []]);
}
if (strpos($path, '/api/upahtawarruq/') === 0) {
return Http::response($ujrahBlocked ? ['keuntungan' => 3, 'ujrah' => 0.5] : 3.5);
}
if ($path === '/api/cawangan') {
return Http::response([['kodcaw' => 'X03', 'details_caw' => 'Xtra']]);
}
if ($path === '/portal/searchsagonline/') {
return Http::response(json_encode($pending));
}
if ($path === '/api/payable-ujrah-branches') {
return Http::response($ujrahBlocked ? [['kodcaw' => 'X03', 'gadaian_ujrah_payable' => 0]] : []);
}
throw new \RuntimeException('Unexpected request: ' . $request->url());
});
$rows = (new PembiayaanService)->getPembiayaan(null);
$this->assertCount(1, $rows);
$row = array_values($rows)[0];
$this->assertSame($company, $row->tag_syarikat);
$this->assertSame($disabled, $row->disabled_payment);
}
public function paymentCases()
{
return [
'eligible KOOP' => [CompanyStatus::COM_KOOPPKB, false, false, false],
'pending payment' => [CompanyStatus::COM_KOOPPKB, true, false, true],
'blocked ujrah branch' => [CompanyStatus::COM_KOOPPKB, false, true, true],
'PKB branch restriction' => [CompanyStatus::COM_PKB, false, false, true],
];
}
}