diff --git a/app/Services/CustomerInfoService.php b/app/Services/CustomerInfoService.php index 2c048bf..fc4bb80 100644 --- a/app/Services/CustomerInfoService.php +++ b/app/Services/CustomerInfoService.php @@ -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()]; } } diff --git a/config/api.php b/config/api.php index 9ecec64..8b89034 100644 --- a/config/api.php +++ b/config/api.php @@ -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, ]; diff --git a/tests/Feature/SingleApiPembiayaanTest.php b/tests/Feature/SingleApiPembiayaanTest.php new file mode 100644 index 0000000..3340050 --- /dev/null +++ b/tests/Feature/SingleApiPembiayaanTest.php @@ -0,0 +1,66 @@ + '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], + ]; + } +}