Vourcherify

This commit is contained in:
Dillon
2023-05-30 22:02:09 +08:00
parent 44deab4542
commit 20d6ca47dc
39 changed files with 1102 additions and 35 deletions
@@ -9,12 +9,16 @@ 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;
@@ -74,6 +78,17 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
/** @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
@@ -86,8 +101,12 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
* @param UpdatesTransactionStatus $updatesTransactionStatus
* @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor
* @param RecalculatesWalletBalance $recalculatesWalletBalance
* @param FetchesVoucher $fetchesVoucher
* @param CreatesVoucher $createsVoucher
* @param CreatesVoucherRedemption $createsVoucherRedemption
* @param RedeemVoucher $redeemVoucher
*/
public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, 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, FetchesVoucher $fetchesVoucher, CreatesVoucher $createsVoucher, CreatesVoucherRedemption $createsVoucherRedemption, RedeemsVoucher $redeemsVoucher)
{
$this->fetchBookingQuotation = $fetchBookingQuotation;
$this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit;
@@ -99,6 +118,10 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
$this->updatesTransactionStatus = $updatesTransactionStatus;
$this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor;
$this->recalculatesWalletBalance = $recalculatesWalletBalance;
$this->fetchesVoucher = $fetchesVoucher;
$this->createsVoucher = $createsVoucher;
$this->createsVoucherRedemption = $createsVoucherRedemption;
$this->redeemsVoucher = $redeemsVoucher;
}
/**
@@ -108,6 +131,12 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
*/
public function logic(Request $request) : JsonResponse
{
$voucherCode = $request->input('voucher_code'); //cief to, json_encode and json_decode ???
// if($voucherReqInput){
// $voucherObject = json_decode($voucherReqInput);
// $validatedVoucherObject = new ValidatedVoucherObject($voucherObject->metadata->name, $voucherObject->code, $voucherObject->discount->type, $voucherObject->discount->amount_off);
// }
$booking = Booking::find($request->route('id'));
$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')]);
@@ -116,7 +145,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
if($conversionObject->getAmount() > round($outstanding, 2)) throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.');
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject);
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject, $voucherCode);
$paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company);
@@ -164,6 +193,30 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
$this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED);
}
//Voucherify
if($voucherCode){
$employee = $booking->company->employees()->first();
$result = $this->redeemsVoucher->execute($voucherCode, $configurations->getSubTotal(), $employee);
$voucher = $result->voucher;
$redemptionId = $result->id;
//Update Database
$voucherValue = 0.00;
if(isset($voucher->discount->amount_off)){
$voucherValue =$voucher->discount->amount_off;
}
if($voucherValue == 0.00){
if(isset($voucher->discount->percent_off)){
$voucherValue =$voucher->discount->percent_off;
}
}
$voucherObject= new VoucherObject($voucher->code, isset($voucher->metadata->name) ? $voucher->metadata->name : "", $voucher->discount->type, $voucherValue);
$voucher = $this->createsVoucher->execute($voucherObject);
if(!$voucher) $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]);
$this->createsVoucherRedemption->execute($transaction, $voucher, $redemptionId, $configurations->getVoucherDiscountAmount());
}
return $this->resourceResponse(new TransactionResource($transaction));
}