PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 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