'Create Refund Booking', 'message' => 'You have successfully created a refund booking' ]; } /** @var FetchesBookingQuotation */ private $fetchBookingQuotation; /** @var FetchesTransaction */ private $fetchesTransaction; /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; /** @var GeneratesTransactionBillNumber */ private $generatesTransactionBillNumber; /** @var CreatesTransaction */ private $createsTransaction; /** * CreateBookingPaymentLogic constructor. * @param FetchesBookingQuotation $fetchBookingQuotation * @param FetchesTransaction $fetchesTransaction * @param UpdatesTransactionStatus $updatesTransactionStatus * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber * @param CreatesTransaction $createsTransaction */ public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction) { $this->fetchBookingQuotation = $fetchBookingQuotation; $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; } /** * @param Request $request * @return JsonResponse * @throws MalformedRequestException */ public function logic(Request $request) : JsonResponse { $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]); $booking = $transaction->owner; $billNumber = $this->generatesTransactionBillNumber->execute('RFD-'); $refund = $transaction->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->sum('original_amount'); if($refund + $request->input('amount') > $transaction->original_amount) throw new MalformedRequestException('Your refund must not be greater than '. $transaction->original_amount .'.'); // $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, $transaction, $request->input('amount')); // $transactionRefundCalculationObject->init(); $refundAmount = bcdiv($request->input('amount'), $transaction->currency_rate, 7); // refund service charges if is fully refund $refundTotal = ($refund + $request->input('amount')) == $transaction->original_amount ? $refundAmount + $transaction->service_charge + $transaction->tax : $refundAmount; $object = new TransactionObject($billNumber, TransactionType::REFUND, 1, $booking->company->id, 1, PaymentMethodType::CASH, $refundTotal, $request->input('amount'), 1, $transaction->original_currency_id, $transaction->currency_rate, 0, 0, null, ApprovalStatus::PENDING_VERIFICATION, [], $transaction->bill_no); $refund_transaction = $this->createsTransaction->execute($transaction, $object); // create supplier refund if ($transaction->transactions()->bills()->first()) { $billNumber = $this->generatesTransactionBillNumber->execute('SRFD-'); $object = new TransactionObject($billNumber, TransactionType::SUPPLIER_REFUND, 1, $booking->company->id, 1, PaymentMethodType::CASH, $refundTotal, $request->input('amount'), 1, $transaction->original_currency_id, $transaction->currency_rate, 0, 0, null, ApprovalStatus::PENDING_VERIFICATION, [], $transaction->bill_no); $transaction = $this->createsTransaction->execute($transaction, $object); } return $this->resourceResponse(new TransactionResource($refund_transaction)); } }