Add backend validation when customer attempt to pay. Blocks payment that should be disabled but passes the front end validations

This commit is contained in:
Afiq Hamzah
2024-07-25 15:49:26 +08:00
parent d01521a036
commit fe19187ff1
9 changed files with 460 additions and 1 deletions
+1 -1
View File
@@ -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)
{
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace App\Http\Requests;
use App\Http\Controllers\InformationController;
use App\Rules\PaymentRules\CawanganPermitUjrahPayment;
use App\Rules\PaymentRules\NorujukanBukanFasa2;
use App\Rules\PaymentRules\NorujukanContractNotExceed12Month;
use App\Rules\PaymentRules\NorujukanNotAttemptPaymentRecently;
use App\Rules\PaymentRules\NorujukanTidakAkanDilelong;
use App\Rules\PaymentRules\PaymentNotUjrah;
use App\Rules\PaymentRules\PilihMinimumSatuSAG;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Http;
class PaymentRequest extends FormRequest
{
public $selectedPembiayaan;
public $pembiayaanAkanDilelong;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$this->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();
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Rules\PaymentRules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Http;
class CawanganPermitUjrahPayment implements Rule
{
public $pembiayaan;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($pembiayaan)
{
$this->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!';
}
}
@@ -0,0 +1,58 @@
<?php
namespace App\Rules\PaymentRules;
use App\Http\Controllers\InformationController;
use Illuminate\Contracts\Validation\Rule;
class NorujukanBukanFasa2 implements Rule
{
public $pembiayaanAkanDilelong;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($pembiayaanAkanDilelong)
{
$this->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';
}
}
@@ -0,0 +1,45 @@
<?php
namespace App\Rules\PaymentRules;
use App\Http\Controllers\InformationController;
use Illuminate\Contracts\Validation\Rule;
class NorujukanContractNotExceed12Month implements Rule
{
public $pembiayaan;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($pembiayaan)
{
$this->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.';
}
}
@@ -0,0 +1,72 @@
<?php
namespace App\Rules\PaymentRules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\Client\Pool;
use Illuminate\Support\Facades\Http;
class NorujukanNotAttemptPaymentRecently implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$norujukanToPay = json_decode($value);
$norujukanRecentlyAttemptPayment = $this->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!';
}
}
@@ -0,0 +1,64 @@
<?php
namespace App\Rules\PaymentRules;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;
class NorujukanTidakAkanDilelong implements Rule
{
public $pembiayaanAkanDilelong;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($pembiayaanAkanDilelong)
{
$this->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';
}
}
@@ -0,0 +1,50 @@
<?php
namespace App\Rules\PaymentRules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Http;
class PaymentNotUjrah implements Rule
{
public $pembiayaan;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($pembiayaan)
{
$this->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!';
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Rules\PaymentRules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Http;
class PilihMinimumSatuSAG implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$norujukanToPay = json_decode($value);
return sizeof($norujukanToPay) > 0;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Sila pilih salah satu checkbox di atas terlebih dahulu!';
}
}