AddPaymentCommand.php
7 months ago
AddPaymentCommandHandler.php
7 months ago
CalculatePaymentAmountCommand.php
7 months ago
CalculatePaymentAmountCommandHandler.php
1 week ago
DeletePaymentCommand.php
7 months ago
DeletePaymentCommandHandler.php
7 months ago
GetPaymentCommand.php
7 months ago
GetPaymentCommandHandler.php
4 months ago
GetPaymentsCommand.php
7 months ago
GetPaymentsCommandHandler.php
1 month ago
UpdatePaymentCommand.php
7 months ago
UpdatePaymentCommandHandler.php
7 months ago
CalculatePaymentAmountCommandHandler.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Payment; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Services\Payment\PaymentApplicationService; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 15 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\BookingType; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 18 | use AmeliaBooking\Infrastructure\Services\Payment\SquareService; |
| 19 | use Exception; |
| 20 | |
| 21 | /** |
| 22 | * Class CalculatePaymentAmountCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Payment |
| 25 | */ |
| 26 | class CalculatePaymentAmountCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @param CalculatePaymentAmountCommand $command |
| 30 | * |
| 31 | * @return CommandResult |
| 32 | * @throws QueryExecutionException |
| 33 | * @throws InvalidArgumentException |
| 34 | * @throws Exception |
| 35 | */ |
| 36 | public function handle(CalculatePaymentAmountCommand $command) |
| 37 | { |
| 38 | $result = new CommandResult(); |
| 39 | |
| 40 | $this->checkMandatoryFields($command); |
| 41 | |
| 42 | $requestData = $this->getAppointmentData($command->getFields()); |
| 43 | |
| 44 | /** @var SettingsService $settingsService */ |
| 45 | $settingsService = $this->container->get('domain.settings.service'); |
| 46 | |
| 47 | /** @var ReservationServiceInterface $reservationService */ |
| 48 | $reservationService = $this->container->get('application.reservation.service')->get($command->getField('type')); |
| 49 | |
| 50 | /** @var PaymentApplicationService $paymentAS */ |
| 51 | $paymentAS = $this->container->get('application.payment.service'); |
| 52 | |
| 53 | $squareSettings = $settingsService->getCategorySettings('payments')['square']; |
| 54 | |
| 55 | $reservation = $reservationService->getNew(true, true, true); |
| 56 | |
| 57 | $reservationService->processBooking( |
| 58 | $result, |
| 59 | $requestData, |
| 60 | $reservation, |
| 61 | false |
| 62 | ); |
| 63 | |
| 64 | if ($result->getResult() === CommandResult::RESULT_ERROR) { |
| 65 | return $result; |
| 66 | } |
| 67 | |
| 68 | $transfers = []; |
| 69 | |
| 70 | $paymentAS->setTransfers( |
| 71 | $requestData['payment'], |
| 72 | $reservation, |
| 73 | new BookingType($command->getField('type')), |
| 74 | $transfers, |
| 75 | false |
| 76 | ); |
| 77 | |
| 78 | $paymentAmount = $reservationService->getReservationPaymentAmount($reservation); |
| 79 | |
| 80 | $countryCode = null; |
| 81 | if ($squareSettings['enabled']) { |
| 82 | /** @var SquareService $squareService */ |
| 83 | $squareService = $this->container->get('infrastructure.payment.square.service'); |
| 84 | |
| 85 | $countryCode = $squareService->getCountryCodeByLocationId($squareSettings['locationId']); |
| 86 | } |
| 87 | |
| 88 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 89 | $result->setData( |
| 90 | [ |
| 91 | 'amount' => $paymentAmount, |
| 92 | 'currency' => $settingsService->getCategorySettings('payments')['currency'], |
| 93 | 'transfers' => $transfers, |
| 94 | 'countryCode' => $countryCode, |
| 95 | ] |
| 96 | ); |
| 97 | |
| 98 | return $result; |
| 99 | } |
| 100 | } |
| 101 |