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