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