AbstractNotificationService.php
1 year ago
AbstractWhatsAppNotificationService.php
2 years ago
ApplicationNotificationService.php
1 year ago
AppointmentNotificationService.php
1 year ago
BasicWhatsAppNotificationService.php
2 years ago
EmailNotificationService.php
1 year ago
NotificationHelperService.php
4 years ago
SMSAPIService.php
2 years ago
SMSNotificationService.php
1 year ago
SMSAPIService.php
327 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Application\Services\Notification; |
| 8 | |
| 9 | use AmeliaBooking\Application\Services\Placeholder\PlaceholderService; |
| 10 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 11 | use AmeliaBooking\Domain\ValueObjects\String\NotificationSendTo; |
| 12 | use AmeliaBooking\Infrastructure\Common\Container; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 15 | use AmeliaPHPMailer\PHPMailer\Exception; |
| 16 | use Interop\Container\Exception\ContainerException; |
| 17 | |
| 18 | /** |
| 19 | * Class SMSAPIService |
| 20 | * |
| 21 | * @package AmeliaBooking\Application\Services\Notification |
| 22 | */ |
| 23 | class SMSAPIService |
| 24 | { |
| 25 | /** @var string */ |
| 26 | const STATUS_STRING_OK = 'OK'; |
| 27 | |
| 28 | /** @var Container */ |
| 29 | private $container; |
| 30 | |
| 31 | /** |
| 32 | * ProviderApplicationService constructor. |
| 33 | * |
| 34 | * @param Container $container |
| 35 | * |
| 36 | * @throws \InvalidArgumentException |
| 37 | */ |
| 38 | public function __construct(Container $container) |
| 39 | { |
| 40 | $this->container = $container; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param $route |
| 45 | * @param $authorize |
| 46 | * @param $data |
| 47 | * |
| 48 | * @return mixed |
| 49 | */ |
| 50 | public function sendRequest($route, $authorize, $data = null) |
| 51 | { |
| 52 | $ch = curl_init(AMELIA_SMS_API_URL . $route); |
| 53 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 54 | curl_setopt($ch, CURLOPT_TIMEOUT, 20); |
| 55 | |
| 56 | // If there is data, request will be POST request, otherwise it will be GET |
| 57 | if ($data) { |
| 58 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 59 | } |
| 60 | |
| 61 | /** @var SettingsService $settingsService */ |
| 62 | $settingsService = $this->container->get('domain.settings.service'); |
| 63 | |
| 64 | // If authorization is needed, send token to the request header |
| 65 | if ($authorize) { |
| 66 | $authorization = 'Authorization: Bearer ' . $settingsService->getSetting('notifications', 'smsApiToken'); |
| 67 | |
| 68 | curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]); |
| 69 | } |
| 70 | |
| 71 | $response = curl_exec($ch); |
| 72 | |
| 73 | if (curl_errno($ch)) { |
| 74 | $settingsService->setSetting('notifications', 'smsSignedIn', false); |
| 75 | $settingsService->setSetting('notifications', 'smsApiToken', ''); |
| 76 | |
| 77 | $error = curl_error($ch); |
| 78 | |
| 79 | $errorNo = curl_errno($ch); |
| 80 | |
| 81 | $errorStr = curl_strerror(curl_errno($ch)); |
| 82 | |
| 83 | curl_close($ch); |
| 84 | |
| 85 | return ['status' => null, 'error' => [$errorNo, $errorStr, $error]]; |
| 86 | } |
| 87 | |
| 88 | curl_close($ch); |
| 89 | |
| 90 | return json_decode($response); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param $data |
| 95 | * |
| 96 | * @return mixed |
| 97 | */ |
| 98 | public function signUp($data) |
| 99 | { |
| 100 | $route = 'auth/signup'; |
| 101 | |
| 102 | $response = $this->sendRequest($route, false, $data); |
| 103 | |
| 104 | if ($response->status === self::STATUS_STRING_OK) { |
| 105 | $this->authorizeUser($response->token); |
| 106 | } |
| 107 | |
| 108 | return $response; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param $data |
| 113 | * |
| 114 | * @return mixed |
| 115 | */ |
| 116 | public function signIn($data) |
| 117 | { |
| 118 | $route = 'auth/signin'; |
| 119 | |
| 120 | $response = $this->sendRequest($route, false, $data); |
| 121 | |
| 122 | if ($response->status === self::STATUS_STRING_OK) { |
| 123 | $this->authorizeUser($response->token); |
| 124 | } |
| 125 | |
| 126 | return $response; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return mixed |
| 131 | */ |
| 132 | public function getUserInfo() |
| 133 | { |
| 134 | $route = 'auth/info'; |
| 135 | |
| 136 | return $this->sendRequest($route, true); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param $data |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function forgotPassword($data) |
| 145 | { |
| 146 | $route = 'auth/password/forgot'; |
| 147 | |
| 148 | $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications¬ificationTab=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 | $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications¬ificationTab=sms'; |
| 286 | |
| 287 | return $this->sendRequest($route, true, $data); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param $data |
| 292 | * |
| 293 | * @return mixed |
| 294 | */ |
| 295 | public function paymentComplete($data) |
| 296 | { |
| 297 | $route = '/payment/complete'; |
| 298 | |
| 299 | return $this->sendRequest($route, true, $data); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @param $data |
| 304 | * |
| 305 | * @return mixed |
| 306 | */ |
| 307 | public function getPaymentHistory($data) |
| 308 | { |
| 309 | $route = '/payment/history'; |
| 310 | |
| 311 | return $this->sendRequest($route, true, $data); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param $token |
| 316 | */ |
| 317 | private function authorizeUser($token) |
| 318 | { |
| 319 | /** @var SettingsService $settingsService */ |
| 320 | $settingsService = $this->container->get('domain.settings.service'); |
| 321 | |
| 322 | $settingsService->setSetting('notifications', 'smsSignedIn', true); |
| 323 | |
| 324 | $settingsService->setSetting('notifications', 'smsApiToken', $token); |
| 325 | } |
| 326 | } |
| 327 |