From fe19187ff110b9a064ebc055241cbf11fd304baf Mon Sep 17 00:00:00 2001 From: Afiq Hamzah Date: Thu, 25 Jul 2024 15:49:26 +0800 Subject: [PATCH] Add backend validation when customer attempt to pay. Blocks payment that should be disabled but passes the front end validations --- app/Http/Controllers/BayaranController.php | 2 +- app/Http/Requests/PaymentRequest.php | 65 +++++++++++++++++ .../CawanganPermitUjrahPayment.php | 61 ++++++++++++++++ .../PaymentRules/NorujukanBukanFasa2.php | 58 +++++++++++++++ .../NorujukanContractNotExceed12Month.php | 45 ++++++++++++ .../NorujukanNotAttemptPaymentRecently.php | 72 +++++++++++++++++++ .../NorujukanTidakAkanDilelong.php | 64 +++++++++++++++++ app/Rules/PaymentRules/PaymentNotUjrah.php | 50 +++++++++++++ .../PaymentRules/PilihMinimumSatuSAG.php | 44 ++++++++++++ 9 files changed, 460 insertions(+), 1 deletion(-) create mode 100644 app/Http/Requests/PaymentRequest.php create mode 100644 app/Rules/PaymentRules/CawanganPermitUjrahPayment.php create mode 100644 app/Rules/PaymentRules/NorujukanBukanFasa2.php create mode 100644 app/Rules/PaymentRules/NorujukanContractNotExceed12Month.php create mode 100644 app/Rules/PaymentRules/NorujukanNotAttemptPaymentRecently.php create mode 100644 app/Rules/PaymentRules/NorujukanTidakAkanDilelong.php create mode 100644 app/Rules/PaymentRules/PaymentNotUjrah.php create mode 100644 app/Rules/PaymentRules/PilihMinimumSatuSAG.php diff --git a/app/Http/Controllers/BayaranController.php b/app/Http/Controllers/BayaranController.php index 7de17de..7e70635 100644 --- a/app/Http/Controllers/BayaranController.php +++ b/app/Http/Controllers/BayaranController.php @@ -64,7 +64,7 @@ class BayaranController extends Controller return view('includes.bayaran.senaraibayaransag', compact('belum_tebus','api_url')); } - public function PembayaranAction(Request $request) + public function PembayaranAction(PaymentRequest $request) { diff --git a/app/Http/Requests/PaymentRequest.php b/app/Http/Requests/PaymentRequest.php new file mode 100644 index 0000000..e2212eb --- /dev/null +++ b/app/Http/Requests/PaymentRequest.php @@ -0,0 +1,65 @@ +selectedPembiayaan = $this->getPembiayaanFromNorujukan(); + $this->pembiayaanAkanDilelong = $this->getPembiayaanAkanDilelong(); + + return [ + 'totalSagInCartInput' => [ + new PilihMinimumSatuSAG, + new NorujukanTidakAkanDilelong($this->pembiayaanAkanDilelong), + new NorujukanBukanFasa2($this->pembiayaanAkanDilelong), + new NorujukanNotAttemptPaymentRecently, + new CawanganPermitUjrahPayment($this->selectedPembiayaan), + new PaymentNotUjrah($this->selectedPembiayaan), + new NorujukanContractNotExceed12Month($this->selectedPembiayaan), + ] + ]; + } + + public function getPembiayaanFromNorujukan() + { + $norujukanList = json_decode($this->totalSagInCartInput); + + return array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0], $norujukanList); + } + + public function getPembiayaanAkanDilelong() + { + return (new InformationController())->pembiayaanakandilelong()->getData(); + } +} diff --git a/app/Rules/PaymentRules/CawanganPermitUjrahPayment.php b/app/Rules/PaymentRules/CawanganPermitUjrahPayment.php new file mode 100644 index 0000000..b7ce8ca --- /dev/null +++ b/app/Rules/PaymentRules/CawanganPermitUjrahPayment.php @@ -0,0 +1,61 @@ +pembiayaan = $pembiayaan; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + $getPayableUjrahBranches = fn() => Http::arrahn()->get('/api/payable-ujrah-branches')->json(); + $branchesNotPermitPaymentUjrah = array_filter($getPayableUjrahBranches(), fn($x) => $x['gadaian_ujrah_payable'] == 0); + $kodCawanganBranchesNotPermitPaymentUjrah = array_map(fn($x) => $x['kodcaw'], $branchesNotPermitPaymentUjrah); + + $isNorujukanUjrah = fn($x) => $x['tagbaru'] === 1 ? true : false; + + $isCawanganNotPermitUjrahPayment = function ($x) use ($kodCawanganBranchesNotPermitPaymentUjrah, $isNorujukanUjrah) { + + if ($isNorujukanUjrah($x)) { + return in_array($x['kodcaw'], $kodCawanganBranchesNotPermitPaymentUjrah); + } + + return false; + }; + + $cawanganNotPermitUjrahPayment = array_filter($this->pembiayaan, fn($x) => $isCawanganNotPermitUjrahPayment($x)); + + return sizeof($cawanganNotPermitUjrahPayment) == 0; + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'Tidak dibenarkan pembayaran ujrah!'; + } +} diff --git a/app/Rules/PaymentRules/NorujukanBukanFasa2.php b/app/Rules/PaymentRules/NorujukanBukanFasa2.php new file mode 100644 index 0000000..7c984df --- /dev/null +++ b/app/Rules/PaymentRules/NorujukanBukanFasa2.php @@ -0,0 +1,58 @@ +pembiayaanAkanDilelong = $pembiayaanAkanDilelong; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + $norujukanToPay = json_decode($value); + + $setDefaultFasaIfNotAvailable = function ($x) { + if (!isset($x->fasa)) { + $x->fasa = 1; + } + return $x; + }; + + $pembiayaanWithFasa = array_map($setDefaultFasaIfNotAvailable, $this->pembiayaanAkanDilelong); + $pembiayaanInFasa2 = array_filter($pembiayaanWithFasa, fn($x) => $x->fasa == 2); + $norujukanInFasa2 = array_map(fn($x) => $x->norujukan, $pembiayaanInFasa2); + + $pembiayaanFasa2 = array_filter($norujukanToPay, fn($x) => in_array($x, $norujukanInFasa2)); + + return sizeof($pembiayaanFasa2) == 0; + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'No pembiayaan dalam kategori fasa 2, sila ke kaunter'; + } +} diff --git a/app/Rules/PaymentRules/NorujukanContractNotExceed12Month.php b/app/Rules/PaymentRules/NorujukanContractNotExceed12Month.php new file mode 100644 index 0000000..e9a3adb --- /dev/null +++ b/app/Rules/PaymentRules/NorujukanContractNotExceed12Month.php @@ -0,0 +1,45 @@ +pembiayaan = $pembiayaan; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + $gadaiContractExceed12Month = array_filter($this->pembiayaan, fn($x) => $x['tempoh'] >= 12); + + return sizeof($gadaiContractExceed12Month) == 0; + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'SAG Yang Dipilih Akan/Telah Tamat Kontrak 12 Bulan. Sila Hadir Ke Kaunter Cawangan Untuk Pembayaran/Penebusan.'; + } +} diff --git a/app/Rules/PaymentRules/NorujukanNotAttemptPaymentRecently.php b/app/Rules/PaymentRules/NorujukanNotAttemptPaymentRecently.php new file mode 100644 index 0000000..7ebdde8 --- /dev/null +++ b/app/Rules/PaymentRules/NorujukanNotAttemptPaymentRecently.php @@ -0,0 +1,72 @@ +getNorujukanRecentlyAttemptPayment($norujukanToPay); + + return sizeof($norujukanRecentlyAttemptPayment) == 0; + + } + + private function getNorujukanRecentlyAttemptPayment($norujukanList) + { + $getRecentlyAttemptPayment = function () use ($norujukanList) { + return Http::pool(function (Pool $pool) use ($norujukanList) { + foreach ($norujukanList as $norujukan) { + $pool->get(config('api.url') . '/portal/searchsagonline/', ['norujukan' => $norujukan]); + } + }); + }; + + $recentlyAttemptPaymentResponse = array_map( + fn($x, $y) => ['norujukan' => $x, 'isRecentlyAttemptPayment' => $y->json()], + $norujukanList, + $getRecentlyAttemptPayment() + ); + + $filterRecentlyAttemptPayment = array_filter( + $recentlyAttemptPaymentResponse, + fn($x) => $x['isRecentlyAttemptPayment'] === true + ); + + return array_map(fn($x) => $x['norujukan'], $filterRecentlyAttemptPayment); + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'Sila Tunggu Sebentar!'; + } +} diff --git a/app/Rules/PaymentRules/NorujukanTidakAkanDilelong.php b/app/Rules/PaymentRules/NorujukanTidakAkanDilelong.php new file mode 100644 index 0000000..5fa9d95 --- /dev/null +++ b/app/Rules/PaymentRules/NorujukanTidakAkanDilelong.php @@ -0,0 +1,64 @@ +pembiayaanAkanDilelong = $pembiayaanAkanDilelong; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + + $norujukanToPay = json_decode($value); + + $isCurrentDateGreaterThanEqual3DaysBeforeLelong = function ($x) { + $tarikhLelong = Carbon::parse($x->t_lelong); + $currentDate = Carbon::now(); + + if ($currentDate->gte($tarikhLelong->subDays(3))) { + return true; + } + + return false; + + }; + + $dataFromPembiayaanAkanDilelong = array_filter($this->pembiayaanAkanDilelong, fn($x) => in_array($x->norujukan, $norujukanToPay)); + + $pembiayaanAkanDilelong = array_filter($dataFromPembiayaanAkanDilelong, $isCurrentDateGreaterThanEqual3DaysBeforeLelong); + + return sizeof($pembiayaanAkanDilelong) == 0; + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'No pembiayaan akan dilelong, sila ke kaunter'; + } +} diff --git a/app/Rules/PaymentRules/PaymentNotUjrah.php b/app/Rules/PaymentRules/PaymentNotUjrah.php new file mode 100644 index 0000000..a8a5fae --- /dev/null +++ b/app/Rules/PaymentRules/PaymentNotUjrah.php @@ -0,0 +1,50 @@ +pembiayaan = $pembiayaan; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + $norujukanToPay = json_decode($value); + + $isNorujukanUjrah = fn ($x) => $x['tagbaru'] === 1 ? true : false; + + $norujukanUjrah = array_filter($this->pembiayaan, fn($x) => $isNorujukanUjrah($x)); + + return sizeof($norujukanUjrah) == 0; + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'Tidak dibenarkan pembayaran ujrah!'; + } +} diff --git a/app/Rules/PaymentRules/PilihMinimumSatuSAG.php b/app/Rules/PaymentRules/PilihMinimumSatuSAG.php new file mode 100644 index 0000000..d5a059d --- /dev/null +++ b/app/Rules/PaymentRules/PilihMinimumSatuSAG.php @@ -0,0 +1,44 @@ + 0; + + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'Sila pilih salah satu checkbox di atas terlebih dahulu!'; + } +}