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
FetchAccessTokenSquareCommandHandler.php
91 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\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Domain\Entity\Entities; |
| 9 | use AmeliaBooking\Infrastructure\Services\Payment\SquareService; |
| 10 | |
| 11 | /** |
| 12 | * Class FetchAccessTokenSquareCommandHandler |
| 13 | * |
| 14 | * @package AmeliaBooking\Application\Commands\Square |
| 15 | */ |
| 16 | class FetchAccessTokenSquareCommandHandler extends CommandHandler |
| 17 | { |
| 18 | /** |
| 19 | * @param FetchAccessTokenSquareCommand $command |
| 20 | * |
| 21 | * @return CommandResult |
| 22 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 23 | * @throws \AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException |
| 24 | * @throws \Interop\Container\Exception\ContainerException |
| 25 | * @throws AccessDeniedException |
| 26 | */ |
| 27 | public function handle(FetchAccessTokenSquareCommand $command) |
| 28 | { |
| 29 | $result = new CommandResult(); |
| 30 | |
| 31 | $this->checkMandatoryFields($command); |
| 32 | |
| 33 | /** @var SquareService $squareService */ |
| 34 | $squareService = $this->container->get('infrastructure.payment.square.service'); |
| 35 | |
| 36 | /** @var \AmeliaBooking\Domain\Services\Settings\SettingsService $settingsService */ |
| 37 | $settingsService = $this->container->get('domain.settings.service'); |
| 38 | |
| 39 | if (!$this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::SETTINGS)) { |
| 40 | throw new AccessDeniedException('You are not allowed to write settings.'); |
| 41 | } |
| 42 | |
| 43 | $accessToken = $command->getFields(); |
| 44 | |
| 45 | $squareSettings = $settingsService->getCategorySettings('payments')['square']; |
| 46 | |
| 47 | if (empty($accessToken) || empty($accessToken['access_token'])) { |
| 48 | $squareSettings['enabled'] = false; |
| 49 | $squareSettings['accessToken'] = null; |
| 50 | $settingsService->setSetting('payments', 'square', $squareSettings); |
| 51 | |
| 52 | $result->setResult(CommandResult::RESULT_ERROR); |
| 53 | $result->setMessage('There has been an error retrieving the access token'); |
| 54 | $result->setUrl(AMELIA_SITE_URL . '/wp-admin/admin.php?page=wpamelia-settings&square=1&square_error=1'); |
| 55 | |
| 56 | return $result; |
| 57 | } |
| 58 | |
| 59 | set_transient('amelia_square_access_token', ['access_token' => $accessToken['decrypted_access_token'], 'refresh_token' => $accessToken['decrypted_refresh_token']], 604800); |
| 60 | |
| 61 | unset($accessToken['decrypted_access_token']); |
| 62 | unset($accessToken['decrypted_refresh_token']); |
| 63 | |
| 64 | $squareSettings['accessToken'] = $accessToken; |
| 65 | |
| 66 | $squareSettings['enabled'] = true; |
| 67 | |
| 68 | $settingsService->setSetting('payments', 'square', $squareSettings); |
| 69 | |
| 70 | $locations = $squareService->getLocations(); |
| 71 | |
| 72 | if ($locations && sizeof($locations)) { |
| 73 | $squareSettings = $settingsService->getCategorySettings('payments')['square']; |
| 74 | $squareSettings['locationId'] = $locations[0]->getId(); |
| 75 | $settingsService->setSetting('payments', 'square', $squareSettings); |
| 76 | } |
| 77 | |
| 78 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 79 | $result->setMessage('Successfully fetched access token'); |
| 80 | $result->setData( |
| 81 | [ |
| 82 | 'locations' => $locations |
| 83 | ] |
| 84 | ); |
| 85 | |
| 86 | $result->setUrl(AMELIA_SITE_URL . '/wp-admin/admin.php?page=wpamelia-settings&square=1'); |
| 87 | |
| 88 | return $result; |
| 89 | } |
| 90 | } |
| 91 |