Skip to content

Commit 07b9f8a

Browse files
committed
added a httpclient wrapper to encapsulate all api request
1 parent 71c1a1e commit 07b9f8a

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/Abstracts/BaseGateWay.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MusahMusah\LaravelMultipaymentGateways\Abstracts;
66

77
use MusahMusah\LaravelMultipaymentGateways\Contracts\GatewayContract;
8+
use MusahMusah\LaravelMultipaymentGateways\Services\HttpClientWrapper;
89
use MusahMusah\LaravelMultipaymentGateways\Traits\ConsumesExternalServices;
910

1011
abstract class BaseGateWay implements GatewayContract
@@ -86,4 +87,12 @@ public function getData(): array
8687
{
8788
return $this->getResponse()['data'];
8889
}
90+
91+
/* Instantiate the http client wrapper class to make it available to the gateway classes
92+
*
93+
*/
94+
public function httpClient(): HttpClientWrapper
95+
{
96+
return new HttpClientWrapper(baseUri: $this->baseUri);
97+
}
8998
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MusahMusah\LaravelMultipaymentGateways\Contracts;
4+
5+
interface HttpClientWrapperContract
6+
{
7+
/**
8+
* Send a GET request to the payment gateway
9+
*/
10+
public function get(string $url, array $headers = [], array $query = [], bool $isJsonRequest = true): mixed;
11+
12+
/**
13+
* Send a POST request to the payment gateway
14+
*/
15+
public function post(string $url, array $headers = [], array $formParams = [], array $query = [], bool $isJsonRequest = true): mixed;
16+
17+
/**
18+
* Send a PUT request to the payment gateway
19+
*/
20+
public function put(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed;
21+
22+
/**
23+
* Send a PATCH request to the payment gateway
24+
*/
25+
public function patch(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed;
26+
27+
/**
28+
* Send a DELETE request to the payment gateway
29+
*/
30+
public function delete(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed;
31+
32+
public function decodeResponse(): array;
33+
}

src/Services/HttpClientWrapper.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace MusahMusah\LaravelMultipaymentGateways\Services;
4+
5+
use MusahMusah\LaravelMultipaymentGateways\Contracts\HttpClientWrapperContract;
6+
use MusahMusah\LaravelMultipaymentGateways\Traits\ConsumesExternalServices;
7+
8+
class HttpClientWrapper implements HttpClientWrapperContract
9+
{
10+
use ConsumesExternalServices;
11+
12+
public function __construct(protected $baseUri)
13+
{}
14+
15+
/**
16+
* Send a GET request to the payment gateway
17+
*/
18+
public function get(string $url, array $headers = [], array $query = [], bool $isJsonRequest = true): mixed
19+
{
20+
return $this->makeRequest(
21+
method: 'GET',
22+
requestUrl: $url,
23+
isJsonRequest: $isJsonRequest,
24+
queryParams: $query,
25+
headers: $headers
26+
);
27+
}
28+
29+
/**
30+
* Send a POST request to the payment gateway
31+
*/
32+
public function post(string $url, array $headers = [], array $formParams = [], array $query = [], bool $isJsonRequest = true): mixed
33+
{
34+
return $this->makeRequest(
35+
method: 'POST',
36+
requestUrl: $url,
37+
formParams: $formParams,
38+
isJsonRequest: $isJsonRequest,
39+
queryParams: $query,
40+
headers: $headers
41+
);
42+
}
43+
44+
/**
45+
* Send a PUT request to the payment gateway
46+
*/
47+
public function put(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed
48+
{
49+
return $this->makeRequest(
50+
method: 'PUT',
51+
requestUrl: $url,
52+
formParams: $formParams,
53+
isJsonRequest: $isJsonRequest,
54+
queryParams: $query,
55+
headers: $headers
56+
);
57+
}
58+
59+
/**
60+
* Send a PATCH request to the payment gateway
61+
*/
62+
public function patch(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed
63+
{
64+
return $this->makeRequest(
65+
method: 'PATCH',
66+
requestUrl: $url,
67+
formParams: $formParams,
68+
isJsonRequest: $isJsonRequest,
69+
headers: $headers
70+
);
71+
}
72+
73+
/**
74+
* Send a DELETE request to the payment gateway
75+
*/
76+
public function delete(string $url, array $formParams = [], array $query = [], array $headers = [], bool $isJsonRequest = true): mixed
77+
{
78+
return $this->makeRequest(
79+
method: 'DELETE',
80+
requestUrl: $url,
81+
formParams: $formParams,
82+
isJsonRequest: $isJsonRequest,
83+
queryParams: $query,
84+
headers: $headers
85+
);
86+
}
87+
88+
public function decodeResponse(): array
89+
{
90+
return json_decode($this->response, true);
91+
}
92+
}

0 commit comments

Comments
 (0)