AbstractNotificationService.php
6 months ago
AbstractWhatsAppNotificationService.php
6 months ago
ApplicationNotificationService.php
6 months ago
AppointmentNotificationService.php
6 months ago
BasicWhatsAppNotificationService.php
6 months ago
EmailNotificationService.php
6 months ago
NotificationHelperService.php
6 months ago
SMSAPIService.php
6 months ago
SMSNotificationService.php
6 months ago
SMSAPIService.php
330 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 | 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 | // 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 | */ |
| 200 | public function testNotification($data) |
| 201 | { |
| 202 | $route = '/sms/send'; |
| 203 | |
| 204 | /** @var SettingsService $settingsService */ |
| 205 | $settingsService = $this->container->get('domain.settings.service'); |
| 206 | |
| 207 | /** @var EmailNotificationService $notificationService */ |
| 208 | $notificationService = $this->container->get('application.emailNotification.service'); |
| 209 | |
| 210 | /** @var PlaceholderService $placeholderService */ |
| 211 | $placeholderService = $this->container->get("application.placeholder.{$data['type']}.service"); |
| 212 | |
| 213 | $appointmentsSettings = $settingsService->getCategorySettings('appointments'); |
| 214 | |
| 215 | $notification = $notificationService->getById($data['notificationTemplate']); |
| 216 | |
| 217 | $dummyData = $placeholderService->getPlaceholdersDummyData('sms'); |
| 218 | |
| 219 | $isForCustomer = $notification->getSendTo()->getValue() === NotificationSendTo::CUSTOMER; |
| 220 | |
| 221 | $placeholderStringRec = 'recurring' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms'; |
| 222 | $placeholderStringPack = 'package' . 'Placeholders' . ($isForCustomer ? 'Customer' : '') . 'Sms'; |
| 223 | |
| 224 | $dummyData['recurring_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringRec], $dummyData); |
| 225 | $dummyData['package_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringPack], $dummyData); |
| 226 | |
| 227 | |
| 228 | $body = $placeholderService->applyPlaceholders( |
| 229 | $notification->getContent()->getValue(), |
| 230 | $dummyData |
| 231 | ); |
| 232 | |
| 233 | $data = [ |
| 234 | 'to' => $data['recipientPhone'], |
| 235 | 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'), |
| 236 | 'body' => $body |
| 237 | ]; |
| 238 | |
| 239 | return $this->sendRequest($route, true, $data); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @param $to |
| 244 | * @param $body |
| 245 | * @param $callbackUrl |
| 246 | * |
| 247 | * @return mixed |
| 248 | */ |
| 249 | public function send($to, $body, $callbackUrl) |
| 250 | { |
| 251 | $route = '/sms/send'; |
| 252 | |
| 253 | /** @var SettingsService $settingsService */ |
| 254 | $settingsService = $this->container->get('domain.settings.service'); |
| 255 | |
| 256 | $data = [ |
| 257 | 'to' => $to, |
| 258 | 'from' => $settingsService->getSetting('notifications', 'smsAlphaSenderId'), |
| 259 | 'body' => $body, |
| 260 | 'callbackUrl' => $callbackUrl |
| 261 | ]; |
| 262 | |
| 263 | return $this->sendRequest($route, true, $data); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @param $data |
| 268 | * |
| 269 | * @return mixed |
| 270 | */ |
| 271 | public function refreshSMSHistory($data) |
| 272 | { |
| 273 | $route = "/sms/refresh/{$data['logId']}"; |
| 274 | |
| 275 | return $this->sendRequest($route, true); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @param $data |
| 280 | * |
| 281 | * @return mixed |
| 282 | */ |
| 283 | public function paymentCheckout($data) |
| 284 | { |
| 285 | $route = '/payment/checkout'; |
| 286 | |
| 287 | // TODO: Redesign - Remove "redesign" from the URL |
| 288 | $data['redirectUrl'] = AMELIA_PAGE_URL . 'wpamelia-notifications&activeTab=sms'; |
| 289 | |
| 290 | return $this->sendRequest($route, true, $data); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param $data |
| 295 | * |
| 296 | * @return mixed |
| 297 | */ |
| 298 | public function paymentComplete($data) |
| 299 | { |
| 300 | $route = '/payment/complete'; |
| 301 | |
| 302 | return $this->sendRequest($route, true, $data); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @param $data |
| 307 | * |
| 308 | * @return mixed |
| 309 | */ |
| 310 | public function getPaymentHistory($data) |
| 311 | { |
| 312 | $route = '/payment/history'; |
| 313 | |
| 314 | return $this->sendRequest($route, true, $data); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @param $token |
| 319 | */ |
| 320 | private function authorizeUser($token) |
| 321 | { |
| 322 | /** @var SettingsService $settingsService */ |
| 323 | $settingsService = $this->container->get('domain.settings.service'); |
| 324 | |
| 325 | $settingsService->setSetting('notifications', 'smsSignedIn', true); |
| 326 | |
| 327 | $settingsService->setSetting('notifications', 'smsApiToken', $token); |
| 328 | } |
| 329 | } |
| 330 |