DisconnectFromSquareAccountCommand.php
1 year ago
DisconnectFromSquareAccountCommandHandler.php
1 year ago
FetchAccessTokenSquareCommand.php
1 year ago
FetchAccessTokenSquareCommandHandler.php
1 year ago
GetSquareAuthURLCommand.php
1 year ago
GetSquareAuthURLCommandHandler.php
1 year ago
SquarePaymentCommand.php
1 year ago
SquarePaymentCommandHandler.php
1 year ago
SquarePaymentNotifyCommand.php
1 year ago
SquarePaymentNotifyCommandHandler.php
1 year ago
SquareRefundWebhookCommand.php
1 year ago
SquareRefundWebhookCommandHandler.php
1 year ago
SquarePaymentNotifyCommandHandler.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Square; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Services\Payment\PaymentApplicationService; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Cache\Cache; |
| 10 | use AmeliaBooking\Infrastructure\Repository\Cache\CacheRepository; |
| 11 | use AmeliaBooking\Infrastructure\Services\Payment\SquareService; |
| 12 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 13 | use Exception; |
| 14 | use Interop\Container\Exception\ContainerException; |
| 15 | use Square\Models\Order; |
| 16 | |
| 17 | /** |
| 18 | * Class SquarePaymentNotifyCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Square |
| 21 | */ |
| 22 | class SquarePaymentNotifyCommandHandler extends CommandHandler |
| 23 | { |
| 24 | public $mandatoryFields = [ |
| 25 | 'name' |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * @param SquarePaymentNotifyCommand $command |
| 30 | * |
| 31 | * @return CommandResult |
| 32 | * @throws InvalidArgumentException |
| 33 | * @throws Exception |
| 34 | * @throws ContainerException |
| 35 | */ |
| 36 | public function handle(SquarePaymentNotifyCommand $command) |
| 37 | { |
| 38 | /** @var PaymentApplicationService $paymentAS */ |
| 39 | $paymentAS = $this->container->get('application.payment.service'); |
| 40 | /** @var SquareService $paymentService */ |
| 41 | $paymentService = $this->container->get('infrastructure.payment.square.service'); |
| 42 | /** @var CacheRepository $cacheRepository */ |
| 43 | $cacheRepository = $this->container->get('domain.cache.repository'); |
| 44 | |
| 45 | $this->checkMandatoryFields($command); |
| 46 | |
| 47 | $name = $command->getField('name'); |
| 48 | /** @var Cache $cache */ |
| 49 | $cache = ($data = explode('_', $name)) && isset($data[0], $data[1]) ? |
| 50 | $cacheRepository->getByIdAndName($data[0], $data[1]) : null; |
| 51 | |
| 52 | if (!$cache || !$cache->getPaymentId()) { |
| 53 | $result = new CommandResult(); |
| 54 | $result->setResult(CommandResult::RESULT_ERROR); |
| 55 | $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']); |
| 56 | $result->setData( |
| 57 | [ |
| 58 | 'message' => 'Cache object not saved', |
| 59 | 'paymentSuccessful' => false, |
| 60 | ] |
| 61 | ); |
| 62 | |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | $response = $paymentService->getOrderResponse($command->getField('squareOrderId')); |
| 67 | |
| 68 | if ($response->isError()) { |
| 69 | $result = new CommandResult(); |
| 70 | $result->setResult(CommandResult::RESULT_ERROR); |
| 71 | $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']); |
| 72 | $result->setData( |
| 73 | [ |
| 74 | 'message' => $paymentService->getErrorMessage($response), |
| 75 | 'paymentSuccessful' => false, |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | return $result; |
| 80 | } |
| 81 | |
| 82 | /**@var Order $order */ |
| 83 | $order = $response->getResult()->getOrder(); |
| 84 | |
| 85 | $paymentId = null; |
| 86 | if ($order && $order->getTenders() && sizeof($order->getTenders()) > 0) { |
| 87 | $paymentId = $order->getTenders()[0]->getPaymentId(); |
| 88 | $response = $paymentService->completePayment($paymentId); |
| 89 | } |
| 90 | |
| 91 | $status = 'paid'; |
| 92 | |
| 93 | $result = $paymentAS->updateAppointmentAndCache($data[2], $status, $cache, $paymentId); |
| 94 | $returnUrl = urldecode($command->getField('returnUrl')); |
| 95 | $result->setUrl($returnUrl. (strpos($returnUrl, '?') ? '&' : '?') . 'ameliaCache=' . $name); |
| 96 | return $result; |
| 97 | } |
| 98 | } |
| 99 |