Merge branch 'master' of gitlab.com:CIEFWorldwideSdnBhd/exchange-2.0 into list-transfer-with-filters

This commit is contained in:
edmondlang
2023-06-21 15:01:34 +08:00
@@ -5,29 +5,27 @@ namespace App\Classes\Modules\Bookings\ControllersLogic;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\DataTransferObjects\CalculationObject;
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;
use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation;
use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;
//use App\Classes\Modules\Vouchers\DataTransferObjects\VoucherObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
//use App\Classes\Modules\Vouchers\Services\FetchesVoucher;
//use App\Classes\Modules\Vouchers\Services\CreatesVoucher;
//use App\Classes\Modules\Vouchers\Services\CreatesVoucherRedemption;
//use App\Classes\Modules\Vouchers\Services\RedeemsVoucher;
use App\Classes\Modules\Wallets\Services\UpdatesWalletBalance;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Http\Resources\TransactionResource;
use App\Classes\Modules\Transactions\Processors\CreateCashBackTransactionProcessor;
use App\Models\Booking;
use App\Models\Transaction;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -35,30 +33,6 @@ use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance;
class CreateBookingPaymentLogic extends AbstractControllerLogic
{
/**
* @param FetchesBookingQuotation $fetchBookingQuotation
* @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit
* @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
* @param CreatesTransaction $createsTransaction
* @param CalculatesBookingOutstanding $calculatesBookingOutstanding
* @param CreatesBillplzBill $createsBillplzBill
* @param UpdatesWalletBalance $updatesWalletBalance
* @param UpdatesTransactionStatus $updatesTransactionStatus
* @param RecalculatesWalletBalance $recalculatesWalletBalance
*/
public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, RecalculatesWalletBalance $recalculatesWalletBalance)
{
$this->fetchBookingQuotation = $fetchBookingQuotation;
$this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit;
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
$this->createsTransaction = $createsTransaction;
$this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
$this->createsBillplzBill = $createsBillplzBill;
$this->updatesWalletBalance = $updatesWalletBalance;
$this->updatesTransactionStatus = $updatesTransactionStatus;
$this->recalculatesWalletBalance = $recalculatesWalletBalance;
}
/**
* @return array
@@ -94,157 +68,104 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
/** @var UpdatesTransactionStatus */
private $updatesTransactionStatus;
/** @var CreateCashBackTransactionProcessor */
private $createCashBackTransactionProcessor;
/** @var RecalculatesWalletBalance */
private $recalculatesWalletBalance;
//
// /** @var FetchesVoucher */
// private $fetchesVoucher;
//
// /** @var CreatesVoucher */
// private $createsVoucher;
//
// /** @var CreatesVoucherRedemption */
// private $createsVoucherRedemption;
//
// /** @var RedeemsVoucher */
// private $redeemsVoucher;
/**
* CreateBookingPaymentLogic constructor.
* @param FetchesBookingQuotation $fetchBookingQuotation
* @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit
* @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
* @param CreatesTransaction $createsTransaction
* @param CalculatesBookingOutstanding $calculatesBookingOutstanding
* @param CreatesBillplzBill $createsBillplzBill
* @param UpdatesWalletBalance $updatesWalletBalance
* @param UpdatesTransactionStatus $updatesTransactionStatus
* @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor
* @param RecalculatesWalletBalance $recalculatesWalletBalance
*/
public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance)
{
$this->fetchBookingQuotation = $fetchBookingQuotation;
$this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit;
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
$this->createsTransaction = $createsTransaction;
$this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
$this->createsBillplzBill = $createsBillplzBill;
$this->updatesWalletBalance = $updatesWalletBalance;
$this->updatesTransactionStatus = $updatesTransactionStatus;
$this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor;
$this->recalculatesWalletBalance = $recalculatesWalletBalance;
}
/**
* @param Request $request
* @return JsonResponse
* @throws MalformedRequestException
*/
public function logic(Request $request) : JsonResponse
{
$booking = Booking::find($request->route('id'));
$conversionObject = $this->createConversionObject($request, $booking);
$conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $request->input('amount'))), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')]);
$this->validatePayment($request, $booking, $conversionObject);
$outstanding = $this->calculatesBookingOutstanding->execute($booking);
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject, $request->input('voucher_code'));
if($conversionObject->getAmount() > round($outstanding, 2)) throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.');
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject);
$paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company);
$billNumber = $this->generatesTransactionBillNumber->execute('PYMT-');
$paymentReference = $this->handlePaymentMethods($request, $booking, $configurations, $billNumber);
$paymentReference = null;
$object = $this->createTransactionObject($booking, $configurations, $billNumber, $paymentReference);
$amount = $configurations->getTotal();
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){
$billPlzBill = $this->createsBillplzBill->execute($booking->company->name, $request->user()->email, 'This payment is made for transfer ref. '.$booking->marking, $configurations->getTotal(), $billNumber, $request->input('bank_code'));
$paymentReference = $billPlzBill->id;
}
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET){
/** @var Wallet $wallet */
$wallet = $booking->company->wallets()->first();
if((float) number_format(($wallet->amount - $amount),2) < 0){
throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.');
}
$transaction_object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], '');
$transaction = $this->createsTransaction->execute($wallet, $transaction_object);
$paymentReference = $billNumber;
$walletBalance = $this->recalculatesWalletBalance->execute($wallet);
$this->updatesWalletBalance->execute($wallet, $walletBalance);
}
$billNumber = $this->generatesTransactionBillNumber->execute('PYMT-');
$object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id,
$configurations->getConfigurations()->getBankId(), $configurations->getConversionObject()->getPaymentMethod(),
$configurations->getTotal(), $configurations->getForeignTotal(), 1,
$configurations->getConversionObject()->getCurrencyId(), $configurations->getConfigurations()->getRate(),
$configurations->getTax(), $configurations->getServiceCharge(), Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_SUBMISSION, [], $paymentReference);
/** @var Transaction $transaction */
$transaction = $this->createsTransaction->execute($booking, $object);
// $cash_back_transaction = $this->createCashBackTransactionProcessor->execute($transaction);
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){
$this->updatesTransactionStatus->execute($transaction, ApprovalStatus::PENDING_VERIFICATION);
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET){
$this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED);
}
// $this->handleVoucherCode($request, $booking, $configurations, $transaction); //todo-important: uncomment this for voucherify
return $this->resourceResponse(new TransactionResource($transaction));
}
private function createConversionObject(Request $request, Booking $booking): CurrencyConversionObject
{
return new CurrencyConversionObject(
floatval(str_replace(',', '', $request->input('amount'))),
$booking->convertible_currency_id,
$booking->service_id,
$booking->fix_currency_id === 1 ? 0:1,
PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')]
);
}
private function validatePayment(Request $request, Booking $booking, CurrencyConversionObject $conversionObject): void
{
$outstanding = $this->calculatesBookingOutstanding->execute($booking);
if($conversionObject->getAmount() > round($outstanding, 2))
throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.');
}
private function handlePaymentMethods(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): ?string
{
$paymentReference = null;
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY)
$paymentReference = $this->handlePaymentGateway($request, $booking, $configurations, $billNumber);
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET)
$paymentReference = $this->handleWalletPayment($request, $booking, $configurations, $billNumber);
return $paymentReference;
}
private function handlePaymentGateway(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): string
{
$billPlzBill = $this->createsBillplzBill->execute(
$booking->company->name,
$request->user()->email,
'This payment is made for transfer ref. '.$booking->marking,
$configurations->getTotal(),
$billNumber,
$request->input('bank_code')
);
return $billPlzBill->id;
}
private function handleWalletPayment(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): string
{
$wallet = $booking->company->wallets()->first();
$amount = $configurations->getTotal();
if((float) number_format(($wallet->amount - $amount),2) < 0)
throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.');
$transactionObject = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], '');
$this->createsTransaction->execute($wallet, $transactionObject);
$walletBalance = $this->recalculatesWalletBalance->execute($wallet);
$this->updatesWalletBalance->execute($wallet, $walletBalance);
return $billNumber;
}
private function createTransactionObject(Booking $booking, CalculationObject $configurations, string $billNumber, ?string $paymentReference): TransactionObject
{
return new TransactionObject(
$billNumber,
TransactionType::PAYMENT,
1,
$booking->company->id,
$configurations->getConfigurations()->getBankId(),
$configurations->getConversionObject()->getPaymentMethod(),
$configurations->getTotal(),
$configurations->getForeignTotal(),
1,
$configurations->getConversionObject()->getCurrencyId(),
$configurations->getConfigurations()->getRate(),
$configurations->getTax(),
$configurations->getServiceCharge(),
Carbon::now()->addMinutes($this->fetchesCompanyPaymentAttemptLimit->execute($booking->company)),
ApprovalStatus::PENDING_SUBMISSION,
[],
$paymentReference
);
}
// private function handleVoucherCode(Request $request, Booking $booking, CalculationObject $configurations, Transaction $transaction): void
// {
// $voucherCode = $request->input('voucher_code');
// if($voucherCode){
// $employee = $booking->company->employees()->first();
//
// $result = $this->redeemsVoucher->execute($voucherCode, $configurations->getSubTotal(), $employee);
// $voucher = $result->voucher;
// $redemptionId = $result->id;
//
// $voucherValue = $voucher->discount->amount_off ?? ($voucher->discount->percent_off ?? 0.00);
// $voucherObject = new VoucherObject($voucher->code, $voucher->metadata->name ?? "", $voucher->discount->type, $voucherValue);
// $voucher = $this->createsVoucher->execute($voucherObject);
//
// if(!$voucher)
// $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]);
//
// $this->createsVoucherRedemption->execute($redemptionId, $voucher->id, $employee->id, $configurations->getVoucherDiscountAmount());
// }
// }
}