PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.21
Booking for Appointments and Events Calendar – Amelia v1.2.21
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Square / FetchAccessTokenSquareCommandHandler.php
ameliabooking / src / Application / Commands / Square Last commit date
DisconnectFromSquareAccountCommand.php 2 years ago DisconnectFromSquareAccountCommandHandler.php 2 years ago FetchAccessTokenSquareCommand.php 2 years ago FetchAccessTokenSquareCommandHandler.php 2 years ago GetSquareAuthURLCommand.php 2 years ago GetSquareAuthURLCommandHandler.php 2 years ago SquarePaymentCommand.php 2 years ago SquarePaymentCommandHandler.php 2 years ago SquarePaymentNotifyCommand.php 2 years ago SquarePaymentNotifyCommandHandler.php 2 years ago SquareRefundWebhookCommand.php 2 years ago SquareRefundWebhookCommandHandler.php 2 years 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