PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.38
Booking for Appointments and Events Calendar – Amelia v1.2.38
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 / Services / Notification / SMSAPIService.php
ameliabooking / src / Application / Services / Notification Last commit date
AbstractNotificationService.php 1 year ago AbstractWhatsAppNotificationService.php 1 year ago ApplicationNotificationService.php 9 months ago AppointmentNotificationService.php 9 months ago BasicWhatsAppNotificationService.php 1 year ago EmailNotificationService.php 9 months ago NotificationHelperService.php 1 year ago SMSAPIService.php 1 year ago SMSNotificationService.php 9 months ago
SMSAPIService.php
328 lines
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Application\Services\Notification;
9
10 use AmeliaBooking\Application\Services\Placeholder\PlaceholderService;
11 use AmeliaBooking\Domain\Services\Settings\SettingsService;
12 use AmeliaBooking\Domain\ValueObjects\String\NotificationSendTo;
13 use AmeliaBooking\Infrastructure\Common\Container;
14 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
15 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
16 use AmeliaPHPMailer\PHPMailer\Exception;
17 use Interop\Container\Exception\ContainerException;
18
19 /**
20 * Class SMSAPIService
21 *
22 * @package AmeliaBooking\Application\Services\Notification
23 */
24 class SMSAPIService
25 {
26 /** @var string */
27 public const STATUS_STRING_OK = 'OK';
28
29 /** @var Container */
30 private $container;
31
32 /**
33 * ProviderApplicationService constructor.
34 *
35 * @param Container $container
36 *
37 * @throws \InvalidArgumentException
38 */
39 public function __construct(Container $container)
40 {
41 $this->container = $container;
42 }
43
44 /**
45 * @param $route
46 * @param $authorize
47 * @param $data
48 *
49 * @return mixed
50 */
51 public function sendRequest($route, $authorize, $data = null)
52 {
53 $ch = curl_init(AMELIA_SMS_API_URL . $route);
54 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
56
57 // If there is data, request will be POST request, otherwise it will be GET
58 if ($data) {
59 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
60 }
61
62 /** @var SettingsService $settingsService */
63 $settingsService = $this->container->get('domain.settings.service');
64
65 // If authorization is needed, send token to the request header
66 if ($authorize) {
67 $authorization = 'Authorization: Bearer ' . $settingsService->getSetting('notifications', 'smsApiToken');
68
69 curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]);
70 }
71
72 $response = curl_exec($ch);
73
74 if (curl_errno($ch)) {
75 $settingsService->setSetting('notifications', 'smsSignedIn', false);
76 $settingsService->setSetting('notifications', 'smsApiToken', '');
77
78 $error = curl_error($ch);
79
80 $errorNo = curl_errno($ch);
81
82 $errorStr = curl_strerror(curl_errno($ch));
83
84 curl_close($ch);
85
86 return ['status' => null, 'error' => [$errorNo, $errorStr, $error]];
87 }
88
89 curl_close($ch);
90
91 return json_decode($response);
92 }
93
94 /**
95 * @param $data
96 *
97 * @return mixed
98 */
99 public function signUp($data)
100 {
101 $route = 'auth/signup';
102
103 $response = $this->sendRequest($route, false, $data);
104
105 if ($response->status === self::STATUS_STRING_OK) {
106 $this->authorizeUser($response->token);
107 }
108
109 return $response;
110 }
111
112 /**
113 * @param $data
114 *
115 * @return mixed
116 */
117 public function signIn($data)
118 {
119 $route = 'auth/signin';
120
121 $response = $this->sendRequest($route, false, $data);
122
123 if ($response->status === self::STATUS_STRING_OK) {
124 $this->authorizeUser($response->token);
125 }
126
127 return $response;
128 }
129
130 /**
131 * @return mixed
132 */
133 public function getUserInfo()
134 {
135 $route = 'auth/info';
136
137 return $this->sendRequest($route, true);
138 }
139
140 /**
141 * @param $data
142 *
143 * @return mixed
144 */
145 public function forgotPassword($data)
146 {
147 $route = 'auth/password/forgot';
148
149 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&notificationTab=sms';
150
151 return $this->sendRequest($route, false, $data);
152 }
153
154 /**
155 * @param $data
156 *
157 * @return mixed
158 */
159 public function resetPassword($data)
160 {
161 $route = 'auth/password/reset';
162
163 return $this->sendRequest($route, false, $data);
164 }
165
166 /**
167 * @param $data
168 *
169 * @return mixed
170 */
171 public function changePassword($data)
172 {
173 $route = '/auth/password/change';
174
175 return $this->sendRequest($route, true, $data);
176 }
177
178 /**
179 * @param $data
180 *
181 * @return mixed
182 */
183 public function getCountryPriceList($data)
184 {
185 $route = '/sms/prices/' . strtoupper($data['selectedCountry']);
186
187 return $this->sendRequest($route, true);
188 }
189
190 /**
191 * @param $data
192 *
193 * @return mixed
194 *
195 * @throws QueryExecutionException
196 * @throws ContainerException
197 * @throws NotFoundException
198 */
199 public function testNotification($data)
200 {
201 $route = '/sms/send';
202
203 /** @var SettingsService $settingsService */
204 $settingsService = $this->container->get('domain.settings.service');
205
206 /** @var EmailNotificationService $notificationService */
207 $notificationService = $this->container->get('application.emailNotification.service');
208
209 /** @var PlaceholderService $placeholderService */
210 $placeholderService = $this->container->get("application.placeholder.{$data['type']}.service");
211
212 $appointmentsSettings = $settingsService->getCategorySettings('appointments');
213
214 $notification = $notificationService->getById($data['notificationTemplate']);
215
216 $dummyData = $placeholderService->getPlaceholdersDummyData('sms');
217
218 $isForCustomer = $notification->getSendTo()->getValue() === NotificationSendTo::CUSTOMER;
219
220 $placeholderStringRec = 'recurring' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
221 $placeholderStringPack = 'package' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
222
223 $dummyData['recurring_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringRec], $dummyData);
224 $dummyData['package_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringPack], $dummyData);
225
226
227 $body = $placeholderService->applyPlaceholders(
228 $notification->getContent()->getValue(),
229 $dummyData
230 );
231
232 $data = [
233 'to' => $data['recipientPhone'],
234 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'),
235 'body' => $body
236 ];
237
238 return $this->sendRequest($route, true, $data);
239 }
240
241 /**
242 * @param $to
243 * @param $body
244 * @param $callbackUrl
245 *
246 * @return mixed
247 */
248 public function send($to, $body, $callbackUrl)
249 {
250 $route = '/sms/send';
251
252 /** @var SettingsService $settingsService */
253 $settingsService = $this->container->get('domain.settings.service');
254
255 $data = [
256 'to' => $to,
257 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'),
258 'body' => $body,
259 'callbackUrl' => $callbackUrl
260 ];
261
262 return $this->sendRequest($route, true, $data);
263 }
264
265 /**
266 * @param $data
267 *
268 * @return mixed
269 */
270 public function refreshSMSHistory($data)
271 {
272 $route = "/sms/refresh/{$data['logId']}";
273
274 return $this->sendRequest($route, true);
275 }
276
277 /**
278 * @param $data
279 *
280 * @return mixed
281 */
282 public function paymentCheckout($data)
283 {
284 $route = '/payment/checkout';
285
286 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&notificationTab=sms';
287
288 return $this->sendRequest($route, true, $data);
289 }
290
291 /**
292 * @param $data
293 *
294 * @return mixed
295 */
296 public function paymentComplete($data)
297 {
298 $route = '/payment/complete';
299
300 return $this->sendRequest($route, true, $data);
301 }
302
303 /**
304 * @param $data
305 *
306 * @return mixed
307 */
308 public function getPaymentHistory($data)
309 {
310 $route = '/payment/history';
311
312 return $this->sendRequest($route, true, $data);
313 }
314
315 /**
316 * @param $token
317 */
318 private function authorizeUser($token)
319 {
320 /** @var SettingsService $settingsService */
321 $settingsService = $this->container->get('domain.settings.service');
322
323 $settingsService->setSetting('notifications', 'smsSignedIn', true);
324
325 $settingsService->setSetting('notifications', 'smsApiToken', $token);
326 }
327 }
328