PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
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 / Infrastructure / Services / Payment / SquareMiddlewareService.php
ameliabooking / src / Infrastructure / Services / Payment Last commit date
CurrencyService.php 7 months ago SquareMiddlewareService.php 1 week ago SquareService.php 1 week ago StarterPaymentService.php 7 months ago
SquareMiddlewareService.php
192 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Services\Payment;
9
10 use AmeliaBooking\Domain\Services\Logger\LoggerInterface;
11 use AmeliaBooking\Domain\Services\Settings\SettingsService;
12
13 /**
14 * Class SquareMiddlewareService
15 */
16 class SquareMiddlewareService
17 {
18 /**
19 * @var string
20 */
21 private $middlewareApiUrl;
22
23 /**
24 * @var LoggerInterface
25 */
26 private $logger;
27
28 /**
29 * SquareMiddlewareService constructor.
30 *
31 * @param SettingsService $settingsService
32 * @param LoggerInterface $logger
33 */
34 public function __construct(SettingsService $settingsService, LoggerInterface $logger)
35 {
36 $squareSettings = $settingsService->getCategorySettings('payments')['square'];
37 $this->middlewareApiUrl = $squareSettings['testMode'] ?
38 'https://middleware-dev.wpamelia.com/' : AMELIA_MIDDLEWARE_URL;
39 $this->logger = $logger->channel(LoggerInterface::CHANNEL_PAYMENT);
40 }
41
42 /**
43 *
44 * @return boolean
45 *
46 * @throws \Exception
47 */
48 public function decrypt($savedAccessToken)
49 {
50 $ch = curl_init($this->middlewareApiUrl . 'square/decrypt');
51
52 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53
54 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
55
56 curl_setopt(
57 $ch,
58 CURLOPT_POSTFIELDS,
59 [
60 'access_token' => $savedAccessToken['access_token'],
61 'refresh_token' => $savedAccessToken['refresh_token']
62 ]
63 );
64
65 $response = curl_exec($ch);
66
67 if ($response && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
68 $response = json_decode($response, true);
69 set_transient(
70 'amelia_square_access_token',
71 ['access_token' => $response['result']['access_token'], 'refresh_token' => $response['result']['refresh_token']],
72 604800
73 );
74 } else {
75 $response = null;
76 }
77
78 return $response['result'];
79 }
80
81 /**
82 *
83 * @return array
84 *
85 * @throws \Exception
86 */
87 public function getAccessToken($savedAccessToken)
88 {
89 $accessToken = get_transient('amelia_square_access_token');
90 if ($accessToken === false) {
91 $accessToken = $this->decrypt($savedAccessToken);
92 }
93 return $accessToken;
94 }
95
96 /**
97 *
98 * @return boolean
99 *
100 * @throws \Exception
101 */
102 public function disconnectAccount($savedAccessToken, $testMode)
103 {
104 $accessToken = $this->getAccessToken($savedAccessToken);
105
106 $ch = curl_init($this->middlewareApiUrl . 'square/revoke?testMode=' . $testMode);
107
108 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
109
110 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
111
112 curl_setopt(
113 $ch,
114 CURLOPT_POSTFIELDS,
115 [
116 'access_token' => $accessToken['access_token'],
117 ]
118 );
119
120 $response = curl_exec($ch);
121
122 return true;
123 }
124
125
126 /**
127 *
128 * @param string $savedAccessToken
129 * @param bool $testMode
130 *
131 * @return array|null
132 *
133 * @throws \Exception
134 */
135 public function refreshAccessToken($savedAccessToken, $testMode)
136 {
137 $accessToken = $this->getAccessToken($savedAccessToken);
138
139 $ch = curl_init($this->middlewareApiUrl . 'square/refresh?testMode=' . $testMode);
140
141 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
142
143 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
144
145 curl_setopt(
146 $ch,
147 CURLOPT_POSTFIELDS,
148 [
149 'refresh_token' => $accessToken['refresh_token'],
150 ]
151 );
152
153 $response = curl_exec($ch);
154
155 if ($response && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
156 $response = json_decode($response, true);
157 } else {
158 $this->logger->error(
159 'Square access token refresh failed',
160 [
161 'gateway' => 'square',
162 'curl_error' => curl_error($ch),
163 'http_status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
164 ]
165 );
166
167 $response = null;
168 }
169
170 return $response;
171 }
172
173 public function getAuthUrl($testMode)
174 {
175 $ch = curl_init(
176 $this->middlewareApiUrl . 'square/authorization/url?testMode=' . $testMode . '&admin_ajax_url=' . urlencode(admin_url('admin-ajax.php', ''))
177 );
178
179 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
180
181 $response = curl_exec($ch);
182
183 $url = null;
184 if ($response) {
185 $response = json_decode($response, true);
186 $url = $response['result'];
187 }
188
189 return $url;
190 }
191 }
192