PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.2
Booking for Appointments and Events Calendar – Amelia v2.2
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 3 months ago AbstractWhatsAppNotificationService.php 3 months ago ApplicationNotificationService.php 3 months ago AppointmentNotificationService.php 3 months ago BasicWhatsAppNotificationService.php 6 months ago EmailNotificationService.php 6 months ago NotificationHelperService.php 6 months ago SMSAPIService.php 3 months ago SMSNotificationService.php 3 months ago
SMSAPIService.php
328 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\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 AmeliaVendor\PHPMailer\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 return json_decode($response);
90 }
91
92 /**
93 * @param $data
94 *
95 * @return mixed
96 */
97 public function signUp($data)
98 {
99 $route = 'auth/signup';
100
101 $response = $this->sendRequest($route, false, $data);
102
103 if ($response->status === self::STATUS_STRING_OK) {
104 $this->authorizeUser($response->token);
105 }
106
107 return $response;
108 }
109
110 /**
111 * @param $data
112 *
113 * @return mixed
114 */
115 public function signIn($data)
116 {
117 $route = 'auth/signin';
118
119 $response = $this->sendRequest($route, false, $data);
120
121 if ($response->status === self::STATUS_STRING_OK) {
122 $this->authorizeUser($response->token);
123 }
124
125 return $response;
126 }
127
128 /**
129 * @return mixed
130 */
131 public function getUserInfo()
132 {
133 $route = 'auth/info';
134
135 return $this->sendRequest($route, true);
136 }
137
138 /**
139 * @param $data
140 *
141 * @return mixed
142 */
143 public function forgotPassword($data)
144 {
145 $route = 'auth/password/forgot';
146
147 // TODO: Redesign - Remove "redesign" from the URL
148 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&activeTab=sms';
149
150 return $this->sendRequest($route, false, $data);
151 }
152
153 /**
154 * @param $data
155 *
156 * @return mixed
157 */
158 public function resetPassword($data)
159 {
160 $route = 'auth/password/reset';
161
162 return $this->sendRequest($route, false, $data);
163 }
164
165 /**
166 * @param $data
167 *
168 * @return mixed
169 */
170 public function changePassword($data)
171 {
172 $route = '/auth/password/change';
173
174 return $this->sendRequest($route, true, $data);
175 }
176
177 /**
178 * @param $data
179 *
180 * @return mixed
181 */
182 public function getCountryPriceList($data)
183 {
184 $route = '/sms/prices/' . strtoupper($data['selectedCountry']);
185
186 return $this->sendRequest($route, true);
187 }
188
189 /**
190 * @param $data
191 *
192 * @return mixed
193 *
194 * @throws QueryExecutionException
195 * @throws ContainerException
196 * @throws NotFoundException
197 */
198 public function testNotification($data)
199 {
200 $route = '/sms/send';
201
202 /** @var SettingsService $settingsService */
203 $settingsService = $this->container->get('domain.settings.service');
204
205 /** @var EmailNotificationService $notificationService */
206 $notificationService = $this->container->get('application.emailNotification.service');
207
208 /** @var PlaceholderService $placeholderService */
209 $placeholderService = $this->container->get("application.placeholder.{$data['type']}.service");
210
211 $appointmentsSettings = $settingsService->getCategorySettings('appointments');
212
213 $notification = $notificationService->getById($data['notificationTemplate']);
214
215 $dummyData = $placeholderService->getPlaceholdersDummyData('sms');
216
217 $isForCustomer = $notification->getSendTo()->getValue() === NotificationSendTo::CUSTOMER;
218
219 $placeholderStringRec = 'recurring' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
220 $placeholderStringPack = 'package' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms';
221
222 $dummyData['recurring_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringRec], $dummyData);
223 $dummyData['package_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringPack], $dummyData);
224
225
226 $body = $placeholderService->applyPlaceholders(
227 $notification->getContent()->getValue(),
228 $dummyData
229 );
230
231 $data = [
232 'to' => $data['recipientPhone'],
233 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'),
234 'body' => $body
235 ];
236
237 return $this->sendRequest($route, true, $data);
238 }
239
240 /**
241 * @param $to
242 * @param $body
243 * @param $callbackUrl
244 *
245 * @return mixed
246 */
247 public function send($to, $body, $callbackUrl)
248 {
249 $route = '/sms/send';
250
251 /** @var SettingsService $settingsService */
252 $settingsService = $this->container->get('domain.settings.service');
253
254 $data = [
255 'to' => $to,
256 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'),
257 'body' => $body,
258 'callbackUrl' => $callbackUrl
259 ];
260
261 return $this->sendRequest($route, true, $data);
262 }
263
264 /**
265 * @param $data
266 *
267 * @return mixed
268 */
269 public function refreshSMSHistory($data)
270 {
271 $route = "/sms/refresh/{$data['logId']}";
272
273 return $this->sendRequest($route, true);
274 }
275
276 /**
277 * @param $data
278 *
279 * @return mixed
280 */
281 public function paymentCheckout($data)
282 {
283 $route = '/payment/checkout';
284
285 // TODO: Redesign - Remove "redesign" from the URL
286 $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&activeTab=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