3
3
namespace TeamInfinityDev \SmsNotify \Services ;
4
4
5
5
use Illuminate \Support \Facades \Http ;
6
+ use Illuminate \Support \Facades \Log ;
6
7
use TeamInfinityDev \SmsNotify \Exceptions \NotifyException ;
7
8
8
9
class NotifyService
@@ -13,6 +14,9 @@ class NotifyService
13
14
protected $ baseUrl ;
14
15
protected $ retryAttempts ;
15
16
protected $ retryDelay ;
17
+ protected $ httpOptions ;
18
+ protected $ logRequests ;
19
+ protected $ mockResponses ;
16
20
17
21
public function __construct ()
18
22
{
@@ -22,6 +26,9 @@ public function __construct()
22
26
$ this ->baseUrl = config ('sms-notify.api.base_url ' );
23
27
$ this ->retryAttempts = config ('sms-notify.defaults.retry_attempts ' , 3 );
24
28
$ this ->retryDelay = config ('sms-notify.defaults.retry_delay ' , 1 );
29
+ $ this ->httpOptions = config ('sms-notify.http ' , []);
30
+ $ this ->logRequests = config ('sms-notify.development.log_requests ' , false );
31
+ $ this ->mockResponses = config ('sms-notify.development.mock_responses ' , false );
25
32
26
33
$ this ->validateConfig ();
27
34
}
@@ -38,6 +45,60 @@ protected function validateConfig()
38
45
}
39
46
}
40
47
48
+ /**
49
+ * Get HTTP client with configuration
50
+ *
51
+ * @return \Illuminate\Http\Client\PendingRequest
52
+ */
53
+ protected function getHttpClient ()
54
+ {
55
+ $ client = Http::timeout ($ this ->httpOptions ['timeout ' ] ?? 30 )
56
+ ->connectTimeout ($ this ->httpOptions ['connect_timeout ' ] ?? 10 )
57
+ ->retry ($ this ->retryAttempts , $ this ->retryDelay * 1000 );
58
+
59
+ // SSL verification - can be disabled for local development
60
+ if (isset ($ this ->httpOptions ['verify ' ])) {
61
+ $ client = $ client ->withOptions (['verify ' => $ this ->httpOptions ['verify ' ]]);
62
+ }
63
+
64
+ // Allow redirects
65
+ if (isset ($ this ->httpOptions ['allow_redirects ' ])) {
66
+ $ client = $ client ->withOptions (['allow_redirects ' => $ this ->httpOptions ['allow_redirects ' ]]);
67
+ }
68
+
69
+ // Add User-Agent
70
+ $ client = $ client ->withHeaders ([
71
+ 'User-Agent ' => 'Laravel-SMS-Notify/1.1.1 ' ,
72
+ 'Accept ' => 'application/json ' ,
73
+ ]);
74
+
75
+ return $ client ;
76
+ }
77
+
78
+ /**
79
+ * Log request details if enabled
80
+ *
81
+ * @param string $method
82
+ * @param string $url
83
+ * @param array $data
84
+ * @param array $response
85
+ */
86
+ protected function logRequest (string $ method , string $ url , array $ data , array $ response )
87
+ {
88
+ if ($ this ->logRequests ) {
89
+ Log::info ('SMS Notify API Request ' , [
90
+ 'method ' => $ method ,
91
+ 'url ' => $ url ,
92
+ 'data ' => array_merge ($ data , [
93
+ 'api_key ' => '***HIDDEN*** ' , // Hide sensitive data
94
+ 'user_id ' => substr ($ this ->userId , 0 , 3 ) . '*** '
95
+ ]),
96
+ 'response_status ' => $ response ['status_code ' ] ?? 'unknown ' ,
97
+ 'response_success ' => $ response ['success ' ] ?? false ,
98
+ ]);
99
+ }
100
+ }
101
+
41
102
/**
42
103
* Format phone number
43
104
*
@@ -74,6 +135,22 @@ public function send($to, string $message, array $options = []): array
74
135
$ numbers = is_array ($ to ) ? $ to : [$ to ];
75
136
$ formattedNumbers = array_map ([$ this , 'formatNumber ' ], $ numbers );
76
137
138
+ // Return mock response in development
139
+ if ($ this ->mockResponses ) {
140
+ $ mockResponse = [
141
+ 'success ' => true ,
142
+ 'data ' => [
143
+ 'message_id ' => 'mock_ ' . uniqid (),
144
+ 'status ' => 'queued ' ,
145
+ 'recipients ' => count ($ formattedNumbers ),
146
+ ],
147
+ 'status_code ' => 200 ,
148
+ ];
149
+
150
+ $ this ->logRequest ('POST ' , $ this ->baseUrl . '/send ' , ['to ' => $ formattedNumbers , 'message ' => $ message ], $ mockResponse );
151
+ return $ mockResponse ;
152
+ }
153
+
77
154
$ payload = array_merge ([
78
155
'user_id ' => $ this ->userId ,
79
156
'api_key ' => $ this ->apiKey ,
@@ -83,35 +160,51 @@ public function send($to, string $message, array $options = []): array
83
160
], $ options );
84
161
85
162
try {
86
- $ response = Http::retry ($ this ->retryAttempts , $ this ->retryDelay * 1000 )
87
- ->post ($ this ->baseUrl . '/send ' , $ payload );
88
-
89
- if ($ response ->status () === 401 ) {
90
- throw NotifyException::apiError ('Invalid credentials ' , 401 );
91
- }
92
-
93
- if ($ response ->status () === 429 ) {
94
- throw NotifyException::rateLimitExceeded ();
95
- }
163
+ $ response = $ this ->getHttpClient ()->post ($ this ->baseUrl . '/send ' , $ payload );
96
164
97
- return [
165
+ $ result = [
98
166
'success ' => $ response ->successful (),
99
167
'data ' => $ response ->json (),
100
168
'status_code ' => $ response ->status (),
101
169
];
102
- } catch (NotifyException $ e ) {
103
- return [
170
+
171
+ // Handle specific error cases
172
+ if ($ response ->status () === 401 ) {
173
+ $ result ['success ' ] = false ;
174
+ $ result ['error ' ] = 'Invalid credentials ' ;
175
+ } elseif ($ response ->status () === 429 ) {
176
+ $ result ['success ' ] = false ;
177
+ $ result ['error ' ] = 'API rate limit exceeded. Please try again later. ' ;
178
+ } elseif (!$ response ->successful ()) {
179
+ $ result ['success ' ] = false ;
180
+ $ result ['error ' ] = $ response ->json ()['message ' ] ?? 'API request failed ' ;
181
+ }
182
+
183
+ $ this ->logRequest ('POST ' , $ this ->baseUrl . '/send ' , $ payload , $ result );
184
+ return $ result ;
185
+
186
+ } catch (\Illuminate \Http \Client \ConnectionException $ e ) {
187
+ $ error = [
104
188
'success ' => false ,
105
- 'error ' => $ e ->getMessage (),
106
- 'status_code ' => $ e -> getCode () ,
189
+ 'error ' => ' Connection failed: ' . $ e ->getMessage () . ' . Check your internet connection and SSL settings. ' ,
190
+ 'status_code ' => 0 ,
107
191
];
192
+
193
+ $ this ->logRequest ('POST ' , $ this ->baseUrl . '/send ' , $ payload , $ error );
194
+ return $ error ;
195
+
108
196
} catch (\Exception $ e ) {
109
- return [
197
+ $ error = [
110
198
'success ' => false ,
111
- 'error ' => $ e ->getMessage (),
199
+ 'error ' => ' Request failed: ' . $ e ->getMessage (),
112
200
'status_code ' => $ e ->getCode (),
113
201
];
202
+
203
+ $ this ->logRequest ('POST ' , $ this ->baseUrl . '/send ' , $ payload , $ error );
204
+ return $ error ;
114
205
}
115
206
}
116
207
208
+
209
+
117
210
}
0 commit comments