AddPaymentCommand.php
8 months ago
AddPaymentCommandHandler.php
8 months ago
CalculatePaymentAmountCommand.php
8 months ago
CalculatePaymentAmountCommandHandler.php
5 days ago
DeletePaymentCommand.php
8 months ago
DeletePaymentCommandHandler.php
8 months ago
GetPaymentCommand.php
8 months ago
GetPaymentCommandHandler.php
4 months ago
GetPaymentsCommand.php
8 months ago
GetPaymentsCommandHandler.php
1 month ago
UpdatePaymentCommand.php
8 months ago
UpdatePaymentCommandHandler.php
8 months ago
CalculatePaymentAmountCommandHandler.php
85 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\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 14 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use AmeliaBooking\Infrastructure\Services\Payment\SquareService; |
| 17 | use Exception; |
| 18 | |
| 19 | /** |
| 20 | * Class CalculatePaymentAmountCommandHandler |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Commands\Payment |
| 23 | */ |
| 24 | class CalculatePaymentAmountCommandHandler extends CommandHandler |
| 25 | { |
| 26 | /** |
| 27 | * @param CalculatePaymentAmountCommand $command |
| 28 | * |
| 29 | * @return CommandResult |
| 30 | * @throws QueryExecutionException |
| 31 | * @throws InvalidArgumentException |
| 32 | * @throws Exception |
| 33 | */ |
| 34 | public function handle(CalculatePaymentAmountCommand $command) |
| 35 | { |
| 36 | $result = new CommandResult(); |
| 37 | |
| 38 | $this->checkMandatoryFields($command); |
| 39 | |
| 40 | $requestData = $this->getAppointmentData($command->getFields()); |
| 41 | |
| 42 | /** @var SettingsService $settingsService */ |
| 43 | $settingsService = $this->container->get('domain.settings.service'); |
| 44 | |
| 45 | /** @var ReservationServiceInterface $reservationService */ |
| 46 | $reservationService = $this->container->get('application.reservation.service')->get($command->getField('type')); |
| 47 | |
| 48 | $squareSettings = $settingsService->getCategorySettings('payments')['square']; |
| 49 | |
| 50 | $reservation = $reservationService->getNew(true, true, true); |
| 51 | |
| 52 | $reservationService->processBooking( |
| 53 | $result, |
| 54 | $requestData, |
| 55 | $reservation, |
| 56 | false |
| 57 | ); |
| 58 | |
| 59 | if ($result->getResult() === CommandResult::RESULT_ERROR) { |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | $paymentAmount = $reservationService->getReservationPaymentAmount($reservation); |
| 64 | |
| 65 | $countryCode = null; |
| 66 | if ($squareSettings['enabled']) { |
| 67 | /** @var SquareService $squareService */ |
| 68 | $squareService = $this->container->get('infrastructure.payment.square.service'); |
| 69 | |
| 70 | $countryCode = $squareService->getCountryCodeByLocationId($squareSettings['locationId']); |
| 71 | } |
| 72 | |
| 73 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 74 | $result->setData( |
| 75 | [ |
| 76 | 'amount' => $paymentAmount, |
| 77 | 'currency' => $settingsService->getCategorySettings('payments')['currency'], |
| 78 | 'countryCode' => $countryCode, |
| 79 | ] |
| 80 | ); |
| 81 | |
| 82 | return $result; |
| 83 | } |
| 84 | } |
| 85 |