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:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\PaymentRequest;
|
||||
use App\Services\BillplzService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Client\Pool;
|
||||
@@ -12,41 +13,13 @@ use Money\Money;
|
||||
|
||||
class BayaranController extends Controller
|
||||
{
|
||||
|
||||
|
||||
public function PembayaranAction(Request $request)
|
||||
public function PembayaranAction(PaymentRequest $request)
|
||||
{
|
||||
$totalSagInCartInput = $request->totalSagInCartInput;
|
||||
|
||||
$validated = $request->validate([
|
||||
'totalSagInCartInput' => 'required',
|
||||
]);
|
||||
|
||||
$norujukanList = json_decode($request->totalSagInCartInput);
|
||||
|
||||
$arraybayar = array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0], $norujukanList);
|
||||
|
||||
$arraybayar = $this->setUpahTawarruq($arraybayar);
|
||||
|
||||
$norujukanRecentlyAttemptPayment = array_map(
|
||||
fn($x) => Http::arrahn()->get('/portal/searchsagonline/', ['norujukan' => $x])->json(),
|
||||
$norujukanList
|
||||
);
|
||||
|
||||
$isNorujukanRecentlyAttemptPayment = in_array(true, $norujukanRecentlyAttemptPayment);
|
||||
|
||||
if ($isNorujukanRecentlyAttemptPayment) {
|
||||
return redirect()->route('home')->with('errorsagonline', 'Sila Tunggu Sebentar!');
|
||||
}
|
||||
|
||||
$gadaiContractExceed12Month = array_filter($arraybayar, fn($x) => $x['tempoh'] >= 12);
|
||||
|
||||
$isGadaiContractExceed12Month = sizeof($gadaiContractExceed12Month) > 0;
|
||||
|
||||
if ($isGadaiContractExceed12Month) {
|
||||
return redirect()->route('home')->with('error', 'SAG Yang Dipilih Akan/Telah Tamat Kontrak 12 Bulan. Sila Hadir Ke Kaunter Cawangan Untuk Pembayaran/Penebusan.');
|
||||
}
|
||||
|
||||
$request->validated();
|
||||
|
||||
$arraybayar = $this->setUpahTawarruq($request->getPembiayaanFromNorujukan());
|
||||
|
||||
return view('includes.bayaran.pembayaransag', compact('arraybayar'));
|
||||
}
|
||||
|
||||
@@ -87,7 +60,6 @@ class BayaranController extends Controller
|
||||
return Money::MYR($sum())->add($billPlzTransactionFee);
|
||||
};
|
||||
|
||||
|
||||
$bill = $billplzService->createBill([
|
||||
$customerEmail,
|
||||
$customerPhoneNumber,
|
||||
@@ -242,7 +214,7 @@ class BayaranController extends Controller
|
||||
public function successPayment(Request $request)
|
||||
{
|
||||
$transactionOnline = $request->transactionOnline;
|
||||
|
||||
|
||||
$arraygadai = array_map(
|
||||
fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x['norujukan'])->json()[0][0],
|
||||
$transactionOnline
|
||||
@@ -289,31 +261,31 @@ class BayaranController extends Controller
|
||||
|
||||
private function setUpahTawarruq($pembiayaan)
|
||||
{
|
||||
$getJenisKeuntungan = function($x){
|
||||
$getJenisKeuntungan = function ($x) {
|
||||
|
||||
$jenisKeuntungan = [
|
||||
'1' => 'onepercent',
|
||||
'0' => 'asal',
|
||||
];
|
||||
$jenisKeuntungan = [
|
||||
'1' => 'onepercent',
|
||||
'0' => 'asal',
|
||||
];
|
||||
|
||||
return $jenisKeuntungan[$x['tagbaru']];
|
||||
};
|
||||
return $jenisKeuntungan[$x['tagbaru']];
|
||||
};
|
||||
|
||||
$getUpahTawarruqPerPembiayaan = function($x) use($getJenisKeuntungan){
|
||||
$jenisKeuntungan = $getJenisKeuntungan($x);
|
||||
$upahTawarruq = Http::arrahn()->get('/api/upahtawarruq/'.$x['kodcaw'].'/'.$x['norujukan'], ['jeniskeuntungan' => $jenisKeuntungan])->json();
|
||||
$getUpahTawarruqPerPembiayaan = function ($x) use ($getJenisKeuntungan) {
|
||||
$jenisKeuntungan = $getJenisKeuntungan($x);
|
||||
$upahTawarruq = Http::arrahn()->get('/api/upahtawarruq/' . $x['kodcaw'] . '/' . $x['norujukan'], ['jeniskeuntungan' => $jenisKeuntungan])->json();
|
||||
|
||||
if($jenisKeuntungan == 'onepercent'){
|
||||
$x['upah_tawarruq'] = $upahTawarruq['keuntungan'] + $upahTawarruq['ujrah'];
|
||||
}
|
||||
if ($jenisKeuntungan == 'onepercent') {
|
||||
$x['upah_tawarruq'] = $upahTawarruq['keuntungan'] + $upahTawarruq['ujrah'];
|
||||
}
|
||||
|
||||
if($jenisKeuntungan == 'asal'){
|
||||
$x['upah_tawarruq'] = $upahTawarruq;
|
||||
}
|
||||
if ($jenisKeuntungan == 'asal') {
|
||||
$x['upah_tawarruq'] = $upahTawarruq;
|
||||
}
|
||||
|
||||
return $x;
|
||||
};
|
||||
return $x;
|
||||
};
|
||||
|
||||
return array_map($getUpahTawarruqPerPembiayaan, $pembiayaan);
|
||||
return array_map($getUpahTawarruqPerPembiayaan, $pembiayaan);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!';
|
||||
}
|
||||
}
|
||||
@@ -39,12 +39,12 @@
|
||||
<form method="post" id="formtable" action="{{ route('payment.action') }}">
|
||||
@csrf
|
||||
|
||||
@error('totalSagInCart')
|
||||
@error('totalSagInCartInput')
|
||||
<script>
|
||||
$.notify({
|
||||
icon: 'fas fa-exclamation-triangle fa-2x',
|
||||
newest_on_top: true,
|
||||
message: "<strong style='font-size:15px;'>Sila pilih salah satu checkbox di atas terlebih dahulu!</strong>"
|
||||
message: "<strong style='font-size:15px;'>{{ $message }}</strong>"
|
||||
}, {
|
||||
type: 'danger',
|
||||
allow_dismiss: false,
|
||||
|
||||
Reference in New Issue
Block a user