Roberto Santini 1 mese fa
parent
commit
81296b87e0

+ 6
- 1
app/Http/Controllers/AcquistoServizioPublicController.php Vedi File

@@ -161,9 +161,14 @@ class AcquistoServizioPublicController extends Controller
161 161
         } catch (\Throwable $e) {
162 162
             report($e);
163 163
 
164
+            $msg = $e->getMessage();
165
+            if (str_contains($msg, 'Resolving timed out') || str_contains($msg, 'cURL error 28')) {
166
+                $msg = 'Timeout di rete verso il gateway di pagamento (DNS). Riprova tra poco o contatta il supporto.';
167
+            }
168
+
164 169
             return redirect()->route('shop.create', $prodotto->id)
165 170
                 ->withInput()
166
-                ->withErrors(['provider' => 'Errore avvio pagamento: '.$e->getMessage()]);
171
+                ->withErrors(['provider' => 'Errore avvio pagamento: '.$msg]);
167 172
         }
168 173
 
169 174
         return redirect()->away($url);

+ 29
- 19
app/Services/AcquistoServizioPaymentService.php Vedi File

@@ -97,24 +97,26 @@ class AcquistoServizioPaymentService
97 97
         $token = $this->paypalAccessToken();
98 98
         $base = $this->paypalBaseUrl();
99 99
 
100
-        $response = Http::withToken($token)->post($base.'/v2/checkout/orders', [
101
-            'intent' => 'CAPTURE',
102
-            'purchase_units' => [[
103
-                'reference_id' => (string) $acquisto->id,
104
-                'custom_id' => $acquisto->token,
105
-                'amount' => [
106
-                    'currency_code' => 'EUR',
107
-                    'value' => number_format((float) $acquisto->totale, 2, '.', ''),
100
+        $response = $this->paypalHttp()
101
+            ->withToken($token)
102
+            ->post($base.'/v2/checkout/orders', [
103
+                'intent' => 'CAPTURE',
104
+                'purchase_units' => [[
105
+                    'reference_id' => (string) $acquisto->id,
106
+                    'custom_id' => $acquisto->token,
107
+                    'amount' => [
108
+                        'currency_code' => 'EUR',
109
+                        'value' => number_format((float) $acquisto->totale, 2, '.', ''),
110
+                    ],
111
+                    'description' => $acquisto->prodotto?->nome ?: ('Servizio #'.$acquisto->id),
112
+                ]],
113
+                'application_context' => [
114
+                    'return_url' => route('shop.paypal.return', $acquisto->token),
115
+                    'cancel_url' => route('shop.cancel', $acquisto->token),
116
+                    'brand_name' => config('app.name'),
117
+                    'user_action' => 'PAY_NOW',
108 118
                 ],
109
-                'description' => $acquisto->prodotto?->nome ?: ('Servizio #'.$acquisto->id),
110
-            ]],
111
-            'application_context' => [
112
-                'return_url' => route('shop.paypal.return', $acquisto->token),
113
-                'cancel_url' => route('shop.cancel', $acquisto->token),
114
-                'brand_name' => config('app.name'),
115
-                'user_action' => 'PAY_NOW',
116
-            ],
117
-        ]);
119
+            ]);
118 120
 
119 121
         if (! $response->successful()) {
120 122
             Log::error('PayPal create order failed', ['body' => $response->body()]);
@@ -139,7 +141,9 @@ class AcquistoServizioPaymentService
139 141
     {
140 142
         $token = $this->paypalAccessToken();
141 143
         $base = $this->paypalBaseUrl();
142
-        $response = Http::withToken($token)->withBody('{}', 'application/json')
144
+        $response = $this->paypalHttp()
145
+            ->withToken($token)
146
+            ->withBody('{}', 'application/json')
143 147
             ->post($base.'/v2/checkout/orders/'.$orderId.'/capture');
144 148
 
145 149
         if (! $response->successful()) {
@@ -214,7 +218,8 @@ class AcquistoServizioPaymentService
214 218
     private function paypalAccessToken(): string
215 219
     {
216 220
         $base = $this->paypalBaseUrl();
217
-        $response = Http::asForm()
221
+        $response = $this->paypalHttp()
222
+            ->asForm()
218 223
             ->withBasicAuth(config('services.paypal.client_id'), config('services.paypal.secret'))
219 224
             ->post($base.'/v1/oauth2/token', ['grant_type' => 'client_credentials']);
220 225
 
@@ -225,6 +230,11 @@ class AcquistoServizioPaymentService
225 230
         return $response->json('access_token');
226 231
     }
227 232
 
233
+    private function paypalHttp(): \Illuminate\Http\Client\PendingRequest
234
+    {
235
+        return Http::timeout(30)->connectTimeout(10);
236
+    }
237
+
228 238
     private function paypalBaseUrl(): string
229 239
     {
230 240
         return config('services.paypal.mode') === 'live'

+ 19
- 10
app/Services/AimonSmsService.php Vedi File

@@ -24,16 +24,25 @@ class AimonSmsService
24 24
         $sender = $sender ?? (string) config('services.aimon.sender', 'ElephanTech');
25 25
         $body = mb_substr($body, 0, 600);
26 26
 
27
-        $response = Http::asForm()
28
-            ->timeout(30)
29
-            ->post((string) config('services.aimon.send_url'), [
30
-                'authlogin' => config('services.aimon.login'),
31
-                'authpasswd' => config('services.aimon.password'),
32
-                'sender' => base64_encode($sender),
33
-                'body' => base64_encode($body),
34
-                'destination' => $destination,
35
-                'id_api' => (int) config('services.aimon.id_api'),
36
-            ]);
27
+        try {
28
+            $response = Http::asForm()
29
+                ->timeout(30)
30
+                ->connectTimeout(10)
31
+                ->post((string) config('services.aimon.send_url'), [
32
+                    'authlogin' => config('services.aimon.login'),
33
+                    'authpasswd' => config('services.aimon.password'),
34
+                    'sender' => base64_encode($sender),
35
+                    'body' => base64_encode($body),
36
+                    'destination' => $destination,
37
+                    'id_api' => (int) config('services.aimon.id_api'),
38
+                ]);
39
+        } catch (\Illuminate\Http\Client\ConnectionException $e) {
40
+            throw new RuntimeException(
41
+                'Aimon SMS non raggiungibile (timeout DNS/rete). '.$e->getMessage(),
42
+                0,
43
+                $e
44
+            );
45
+        }
37 46
 
38 47
         $responseBody = trim((string) $response->body());
39 48
 

+ 40
- 13
app/Services/SkebbySmsService.php Vedi File

@@ -47,11 +47,20 @@ class SkebbySmsService
47 47
         $auth = $this->authHeaders();
48 48
         $this->assertHasCredits($auth, $messageType);
49 49
 
50
-        $response = Http::withHeaders($auth)
51
-            ->acceptJson()
52
-            ->asJson()
53
-            ->timeout(30)
54
-            ->post($this->baseUrl().'sms', $payload);
50
+        try {
51
+            $response = Http::withHeaders($auth)
52
+                ->acceptJson()
53
+                ->asJson()
54
+                ->timeout(30)
55
+                ->connectTimeout(10)
56
+                ->post($this->baseUrl().'sms', $payload);
57
+        } catch (\Illuminate\Http\Client\ConnectionException $e) {
58
+            throw new RuntimeException(
59
+                'Skebby non raggiungibile (timeout DNS/rete verso api.skebby.it). Riprova tra poco. '.$e->getMessage(),
60
+                0,
61
+                $e
62
+            );
63
+        }
55 64
 
56 65
         if ($response->status() !== 201) {
57 66
             $message = data_get($response->json(), 'result')
@@ -121,10 +130,19 @@ class SkebbySmsService
121 130
         $lastError = null;
122 131
 
123 132
         foreach (['login', 'token'] as $endpoint) {
124
-            $response = Http::withBasicAuth($username, $password)
125
-                ->acceptJson()
126
-                ->timeout(30)
127
-                ->get($base.$endpoint);
133
+            try {
134
+                $response = Http::withBasicAuth($username, $password)
135
+                    ->acceptJson()
136
+                    ->timeout(30)
137
+                    ->connectTimeout(10)
138
+                    ->get($base.$endpoint);
139
+            } catch (\Illuminate\Http\Client\ConnectionException $e) {
140
+                throw new RuntimeException(
141
+                    'Skebby non raggiungibile (timeout DNS/rete verso api.skebby.it). '.$e->getMessage(),
142
+                    0,
143
+                    $e
144
+                );
145
+            }
128 146
 
129 147
             $status = $response->status();
130 148
             $body = trim((string) $response->body());
@@ -174,10 +192,19 @@ class SkebbySmsService
174 192
      */
175 193
     private function assertHasCredits(array $auth, string $messageType): void
176 194
     {
177
-        $response = Http::withHeaders($auth)
178
-            ->acceptJson()
179
-            ->timeout(20)
180
-            ->get($this->baseUrl().'status');
195
+        try {
196
+            $response = Http::withHeaders($auth)
197
+                ->acceptJson()
198
+                ->timeout(20)
199
+                ->connectTimeout(10)
200
+                ->get($this->baseUrl().'status');
201
+        } catch (\Illuminate\Http\Client\ConnectionException $e) {
202
+            throw new RuntimeException(
203
+                'Skebby non raggiungibile (timeout DNS/rete verso api.skebby.it). '.$e->getMessage(),
204
+                0,
205
+                $e
206
+            );
207
+        }
181 208
 
182 209
         if ($response->status() !== 200) {
183 210
             return; // non bloccare se status non disponibile

+ 14
- 4
app/Services/SmsService.php Vedi File

@@ -93,6 +93,11 @@ class SmsService
93 93
 
94 94
             return ['ok' => true, 'message_id' => $messageId, 'provider' => $provider];
95 95
         } catch (\Throwable $e) {
96
+            $friendly = $e->getMessage();
97
+            if (str_contains($friendly, 'Resolving timed out') || str_contains($friendly, 'cURL error 28')) {
98
+                $friendly = 'Timeout di rete verso il provider SMS (DNS). Verifica connettività del server verso api.skebby.it / secure.apisms.it.';
99
+            }
100
+
96 101
             Log::error('SMS: invio firma consegna fallito', [
97 102
                 'acquisto_id' => $acquisto->id,
98 103
                 'provider' => $provider,
@@ -103,11 +108,11 @@ class SmsService
103 108
 
104 109
             $acquisto->mergeMeta([
105 110
                 'sms_firma_provider' => $provider,
106
-                'sms_firma_error' => mb_substr($e->getMessage(), 0, 500),
111
+                'sms_firma_error' => mb_substr($friendly, 0, 500),
107 112
             ]);
108 113
             $acquisto->save();
109 114
 
110
-            return ['ok' => false, 'error' => $e->getMessage(), 'provider' => $provider];
115
+            return ['ok' => false, 'error' => $friendly, 'provider' => $provider];
111 116
         }
112 117
     }
113 118
 
@@ -178,6 +183,11 @@ class SmsService
178 183
 
179 184
             return ['ok' => true, 'message_id' => $messageId, 'provider' => $provider];
180 185
         } catch (\Throwable $e) {
186
+            $friendly = $e->getMessage();
187
+            if (str_contains($friendly, 'Resolving timed out') || str_contains($friendly, 'cURL error 28')) {
188
+                $friendly = 'Timeout di rete verso il provider SMS (DNS). Verifica connettività del server verso api.skebby.it / secure.apisms.it.';
189
+            }
190
+
181 191
             Log::error('SMS: invio firma riconsegna fallito', [
182 192
                 'acquisto_id' => $acquisto->id,
183 193
                 'provider' => $provider,
@@ -188,11 +198,11 @@ class SmsService
188 198
 
189 199
             $acquisto->mergeMeta([
190 200
                 'sms_firma_riconsegna_provider' => $provider,
191
-                'sms_firma_riconsegna_error' => mb_substr($e->getMessage(), 0, 500),
201
+                'sms_firma_riconsegna_error' => mb_substr($friendly, 0, 500),
192 202
             ]);
193 203
             $acquisto->save();
194 204
 
195
-            return ['ok' => false, 'error' => $e->getMessage(), 'provider' => $provider];
205
+            return ['ok' => false, 'error' => $friendly, 'provider' => $provider];
196 206
         }
197 207
     }
198 208
 }

Loading…
Annulla
Salva