Voucherify phase 2

This commit is contained in:
Dillon
2023-07-17 21:29:19 +08:00
parent 46ae50b569
commit c7927847c4
162 changed files with 6063 additions and 298 deletions
@@ -8,24 +8,20 @@ use App\Classes\General\Abstracts\AbstractControllerLogic;
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\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\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;
use App\Http\Resources\TransactionResource;
use App\Classes\Modules\Transactions\Processors\CreateCashBackTransactionProcessor;
use App\Classes\Modules\Vouchers\Processors\Voucherify\BookingToVoucherifyProcessor;
use App\Models\Booking;
use App\Models\Transaction;
@@ -78,17 +74,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
/** @var RecalculatesWalletBalance */
private $recalculatesWalletBalance;
/** @var FetchesVoucher */
private $fetchesVoucher;
/** @var BookingToVoucherifyProcessor */
private $bookingToVoucherifyProcessor;
/** @var CreatesVoucher */
private $createsVoucher;
/** @var CreatesVoucherRedemption */
private $createsVoucherRedemption;
/** @var RedeemsVoucher */
private $redeemsVoucher;
/**
* CreateBookingPaymentLogic constructor.
* @param FetchesBookingQuotation $fetchBookingQuotation
@@ -101,12 +89,9 @@ 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
* @param BookingToVoucherifyProcessor $bookingToVoucherifyProcessor
*/
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)
public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance, BookingToVoucherifyProcessor $bookingToVoucherifyProcessor)
{
$this->fetchBookingQuotation = $fetchBookingQuotation;
$this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit;
@@ -118,10 +103,7 @@ 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;
$this->bookingToVoucherifyProcessor = $bookingToVoucherifyProcessor;
}
/**
@@ -131,11 +113,7 @@ 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);
// }
$voucherCode = $request->input('voucher_code');
$booking = Booking::find($request->route('id'));
@@ -189,34 +167,12 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic
$transaction = $this->createsTransaction->execute($booking, $object);
// $cash_back_transaction = $this->createCashBackTransactionProcessor->execute($transaction);
$this->bookingToVoucherifyProcessor->execute($booking->company->employees()->first(), $transaction, $booking->company->id, $configurations->getSubTotal(), $configurations->getVoucherDiscountAmount(), $voucherCode);
if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET){
$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));
}