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 / Services / Notification / SMSAPIService.php
ameliabooking / src / Application / Services / Notification Last commit date
AbstractNotificationService.php 2 months ago AbstractWhatsAppNotificationService.php 3 months ago ApplicationNotificationService.php 2 months ago AppointmentNotificationService.php 2 weeks ago BasicWhatsAppNotificationService.php 6 months ago EmailNotificationService.php 1 month ago NotificationHelperService.php 6 months ago SMSAPIService.php 2 months ago SMSNotificationService.php 1 month ago
SMSAPIService.php
365 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\Application\Services\Notification;
9
10 use AmeliaBooking\Application\Services\Placeholder\PlaceholderService;
11 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
12 use AmeliaBooking\Domain\Services\Settings\SettingsService;
13 use AmeliaBooking\Domain\ValueObjects\String\NotificationSendTo;
14 use AmeliaBooking\Infrastructure\Common\Container;
15 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\Repository\Notification\NotificationSMSHistoryRepository;
18 use AmeliaVendor\PHPMailer\PHPMailer\Exception;
19 use Interop\Container\Exception\ContainerException;
20
21 /**
22 * Class SMSAPIService
23 *
24 * @package AmeliaBooking\Application\Services\Notification
25 */
26 class SMSAPIService
27 {
28 /** @var string */
29 public const STATUS_STRING_OK = 'OK';
30
31 /** @var Container */
32 private $container;
33
34 /**
35 * ProviderApplicationService constructor.
36 *
37 * @param Container $container
38 *
39 * @throws \InvalidArgumentException
40 */
41 public function __construct(Container $container)
42 {
43 $this->container = $container;
44 }
45
46 /**
47 * @param $route
48 * @param $authorize
49 * @param $data
50 *
51 * @return mixed
52 */
53 public function sendRequest($route, $authorize, $data = null)
54 {
55 $ch = curl_init(AMELIA_SMS_API_URL . $route);
56 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
57 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
58
59 // If there is data, request will be POST request, otherwise it will be GET
60 if ($data) {
61 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
62 }
63
64 /** @var SettingsService $settingsService */
65 $settingsService = $this->container->get('domain.settings.service');
66
67 // If authorization is needed, send token to the request header
68 if ($authorize) {
69 $authorization = 'Authorization: Bearer ' . $settingsService->getSetting('notifications', 'smsApiToken');
70
71 curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]);
72 }
73
74 $response = curl_exec($ch);
75
76 if (curl_errno($ch)) {
77 $settingsService->setSetting('notifications', 'smsSignedIn', false);
78 $settingsService->setSetting('notifications', 'smsApiToken', '');
79
80 $error = curl_error($ch);
81
82 $errorNo = curl_errno($ch);
83
84 $errorStr = curl_strerror(curl_errno($ch));
85
86 curl_close($ch);
87
88 return ['status' => null, 'error' => [$errorNo, $errorStr, $error]];
89 }
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 // TODO: Redesign - Remove "redesign" from the URL
150 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&activeTab=sms';
151
152 return $this->sendRequest($route, false, $data);
153 }
154
155 /**
156 * @param $data
157 *
158 * @return mixed
159 */
160 public function resetPassword($data)
161 {
162 $route = 'auth/password/reset';
163
164 return $this->sendRequest($route, false, $data);
165 }
166
167 /**
168 * @param $data
169 *
170 * @return mixed
171 */
172 public function changePassword($data)
173 {
174 $route = '/auth/password/change';
175
176 return $this->sendRequest($route, true, $data);
177 }
178
179 /**
180 * @param $data
181 *
182 * @return mixed
183 */
184 public function getCountryPriceList($data)
185 {
186 $route = '/sms/prices/' . strtoupper($data['selectedCountry']);
187
188 return $this->sendRequest($route, true);
189 }
190
191 /**
192 * @param $data
193 *
194 * @return mixed
195 *
196 * @throws QueryExecutionException
197 * @throws ContainerException
198 * @throws NotFoundException
199 * @throws \Exception
200 */
201 public function testNotification($data)
202 {
203 $route = '/sms/send';
204
205 /** @var SettingsService $settingsService */
206 $settingsService = $this->container->get('domain.settings.service');
207
208 /** @var EmailNotificationService $notificationService */
209 $notificationService = $this->container->get('application.emailNotification.service');
210
211 /** @var PlaceholderService $placeholderService */
212 $placeholderService = $this->container->get("application.placeholder.{$data['type']}.service");
213
214 /** @var NotificationSMSHistoryRepository $notificationsSMSHistoryRepo */
215 $notificationsSMSHistoryRepo = $this->container->get('domain.notificationSMSHistory.repository');
216
217 $appointmentsSettings = $settingsService->getCategorySettings('appointments');
218
219 $notification = $notificationService->getById($data['notificationTemplate']);
220
221 $dummyData = $placeholderService->getPlaceholdersDummyData('sms');
222
223 $isForCustomer = $notification->getSendTo()->getValue() === NotificationSendTo::CUSTOMER;
224
225 $placeholderStringRec = 'recurring' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
226 $placeholderStringPack = 'package' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
227
228 $dummyData['recurring_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringRec], $dummyData);
229 $dummyData['package_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringPack], $dummyData);
230
231
232 $body = $placeholderService->applyPlaceholders(
233 $notification->getContent()->getValue(),
234 $dummyData
235 );
236
237 $alphaSenderId = $settingsService->getSetting('notifications', 'smsAlphaSenderId');
238
239 $historyId = $notificationsSMSHistoryRepo->add(
240 [
241 'notificationId' => $notification->getId()->getValue(),
242 'userId' => null,
243 'appointmentId' => null,
244 'eventId' => null,
245 'packageCustomerId' => null,
246 'text' => $body,
247 'phone' => $data['recipientPhone'],
248 'alphaSenderId' => $alphaSenderId
249 ]
250 );
251
252 $data = [
253 'to' => $data['recipientPhone'],
254 'from' => $alphaSenderId,
255 'body' => $body,
256 'callbackUrl' => AMELIA_ACTION_URL . '/notifications/sms/history/' . $historyId
257 ];
258
259 $apiResponse = $this->sendRequest($route, true, $data);
260
261 if ($apiResponse->status === 'OK') {
262 $notificationsSMSHistoryRepo->update(
263 $historyId,
264 [
265 'logId' => $apiResponse->message->logId ?? null,
266 'status' => $apiResponse->message->status,
267 'price' => $apiResponse->message->price ?? null,
268 'dateTime' => DateTimeService::getNowDateTimeInUtc(),
269 'segments' => $apiResponse->message->segments ?? null,
270 ]
271 );
272 }
273
274 return $apiResponse;
275 }
276
277 /**
278 * @param $to
279 * @param $body
280 * @param $callbackUrl
281 *
282 * @return mixed
283 */
284 public function send($to, $body, $callbackUrl)
285 {
286 $route = '/sms/send';
287
288 /** @var SettingsService $settingsService */
289 $settingsService = $this->container->get('domain.settings.service');
290
291 $data = [
292 'to' => $to,
293 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'),
294 'body' => $body,
295 'callbackUrl' => $callbackUrl
296 ];
297
298 return $this->sendRequest($route, true, $data);
299 }
300
301 /**
302 * @param $data
303 *
304 * @return mixed
305 */
306 public function refreshSMSHistory($data)
307 {
308 $route = "/sms/refresh/{$data['logId']}";
309
310 return $this->sendRequest($route, true);
311 }
312
313 /**
314 * @param $data
315 *
316 * @return mixed
317 */
318 public function paymentCheckout($data)
319 {
320 $route = '/payment/checkout';
321
322 // TODO: Redesign - Remove "redesign" from the URL
323 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&activeTab=sms';
324
325 return $this->sendRequest($route, true, $data);
326 }
327
328 /**
329 * @param $data
330 *
331 * @return mixed
332 */
333 public function paymentComplete($data)
334 {
335 $route = '/payment/complete';
336
337 return $this->sendRequest($route, true, $data);
338 }
339
340 /**
341 * @param $data
342 *
343 * @return mixed
344 */
345 public function getPaymentHistory($data)
346 {
347 $route = '/payment/history';
348
349 return $this->sendRequest($route, true, $data);
350 }
351
352 /**
353 * @param $token
354 */
355 private function authorizeUser($token)
356 {
357 /** @var SettingsService $settingsService */
358 $settingsService = $this->container->get('domain.settings.service');
359
360 $settingsService->setSetting('notifications', 'smsSignedIn', true);
361
362 $settingsService->setSetting('notifications', 'smsApiToken', $token);
363 }
364 }
365