Skip to content

Commit 71c1a1e

Browse files
committed
refactor traits with package's HttpClientException.php
1 parent da9c163 commit 71c1a1e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MusahMusah\LaravelMultipaymentGateways\Exceptions;
4+
5+
use Exception;
6+
7+
class HttpClientException extends Exception
8+
{
9+
protected $message = 'An error occurred while sending the request to the payment gateway';
10+
}

src/Traits/ConsumesExternalServices.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\GuzzleException;
7+
use MusahMusah\LaravelMultipaymentGateways\Exceptions\HttpClientException;
78
use MusahMusah\LaravelMultipaymentGateways\Exceptions\HttpMethodFoundException;
89

910
trait ConsumesExternalServices
@@ -16,8 +17,7 @@ trait ConsumesExternalServices
1617
/**
1718
* Send a request to any service.
1819
*
19-
*
20-
* @throws GuzzleException|HttpMethodFoundException
20+
* @throws GuzzleException|HttpMethodFoundException|HttpClientException
2121
*/
2222
public function makeRequest(string $method, string $requestUrl, array|string $formParams = [], bool $isJsonRequest = false, array $queryParams = [], array $headers = [], bool $skipResolve = false): mixed
2323
{
@@ -31,15 +31,19 @@ public function makeRequest(string $method, string $requestUrl, array|string $fo
3131
$this->resolveAuthorization($queryParams, $formParams, $headers);
3232
}
3333

34-
$response = $client->request($method, $requestUrl, [
35-
$isJsonRequest ? 'json' : 'form_params' => $formParams,
36-
'headers' => [
37-
...$headers,
38-
'Content-Type' => $isJsonRequest ? 'application/json' : 'application/x-www-form-urlencoded',
39-
'Accept' => 'application/json',
40-
],
41-
'query' => $queryParams,
42-
]);
34+
try {
35+
$response = $client->request($method, $requestUrl, [
36+
$isJsonRequest ? 'json' : 'form_params' => $formParams,
37+
'headers' => [
38+
...$headers,
39+
'Content-Type' => $isJsonRequest ? 'application/json' : 'application/x-www-form-urlencoded',
40+
'Accept' => 'application/json',
41+
],
42+
'query' => $queryParams,
43+
]);
44+
} catch (GuzzleException $e) {
45+
throw new HttpClientException($e->getMessage());
46+
}
4347

4448
$this->response = $response->getBody()->getContents();
4549

@@ -55,7 +59,7 @@ public function makeRequest(string $method, string $requestUrl, array|string $fo
5559
*/
5660
private function validateRequest(string $method): void
5761
{
58-
if (! in_array($method, ['GET', 'POST', 'PUT', 'DELETE'])) {
62+
if (! in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])) {
5963
throw new HttpMethodFoundException('Method not found');
6064
}
6165
}

0 commit comments

Comments
 (0)