1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digikraaft\Paystack\ApiOperations; |
4
|
|
|
|
5
|
|
|
use Digikraaft\Paystack\Exceptions\InvalidArgumentException; |
6
|
|
|
use Digikraaft\Paystack\Exceptions\IsNullException; |
7
|
|
|
use Digikraaft\Paystack\Paystack; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait for resources that need to make API requests. |
12
|
|
|
*/ |
13
|
|
|
trait Request |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Instance of Client. |
17
|
|
|
*/ |
18
|
|
|
protected static $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Response from requests made to Paystack. |
22
|
|
|
* |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected static $response; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param null|array|mixed $params The list of parameters to validate |
29
|
|
|
* @param bool $required |
30
|
|
|
* |
31
|
|
|
* @throws InvalidArgumentException if $params exists and is not an array |
32
|
|
|
*/ |
33
|
|
|
public static function validateParams($params = null, $required = false): void |
34
|
|
|
{ |
35
|
|
|
if ($required) { |
36
|
|
|
if (empty($params) || ! is_array($params)) { |
37
|
|
|
$message = 'The parameter passed must be an array and must not be empty'; |
38
|
|
|
|
39
|
|
|
throw new InvalidArgumentException($message); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
if ($params && ! is_array($params)) { |
43
|
|
|
$message = 'The parameter passed must be an array'; |
44
|
|
|
|
45
|
|
|
throw new InvalidArgumentException($message); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $method HTTP method ('get', 'post', etc.) |
51
|
|
|
* @param string $url URL for the request |
52
|
|
|
* @param array $params list of parameters for the request |
53
|
|
|
* @param string $return_type return array or object accepted values: 'arr' and 'obj' |
54
|
|
|
* |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
* @throws IsNullException |
57
|
|
|
* |
58
|
|
|
* @return array|object (the JSON response as array or object) |
59
|
|
|
*/ |
60
|
|
|
public static function staticRequest($method, $url, $params = [], $return_type = 'obj') |
61
|
|
|
{ |
62
|
|
|
if ($return_type != 'arr' && $return_type != 'obj') { |
63
|
|
|
throw new InvalidArgumentException('Return type can only be obj or arr'); |
64
|
|
|
} |
65
|
|
|
static::setHttpResponse($method, $url, $params); |
66
|
|
|
|
67
|
|
|
if ($return_type == 'arr') { |
68
|
|
|
return static::getResponseData(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return \Digikraaft\Paystack\Util\Util::convertArrayToObject(static::getResponse()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set options for making the Client request. |
76
|
|
|
*/ |
77
|
|
|
protected static function setRequestOptions(): void |
78
|
|
|
{ |
79
|
|
|
$authBearer = 'Bearer '.Paystack::$apiKey; |
80
|
|
|
|
81
|
|
|
static::$client = new Client( |
82
|
|
|
[ |
83
|
|
|
'base_uri' => Paystack::$apiBase, |
84
|
|
|
'headers' => [ |
85
|
|
|
'Authorization' => $authBearer, |
86
|
|
|
'Content-Type' => 'application/json', |
87
|
|
|
'Accept' => 'application/json', |
88
|
|
|
], |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $url |
95
|
|
|
* @param string $method |
96
|
|
|
* @param array $body |
97
|
|
|
* |
98
|
|
|
* @throws IsNullException |
99
|
|
|
*/ |
100
|
|
|
private static function setHttpResponse($method, $url, $body = []): \GuzzleHttp\Psr7\Response |
101
|
|
|
{ |
102
|
|
|
if (is_null($method)) { |
|
|
|
|
103
|
|
|
throw new IsNullException('Empty method not allowed'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
static::setRequestOptions(); |
107
|
|
|
|
108
|
|
|
static::$response = static::$client->{strtolower($method)}( |
109
|
|
|
Paystack::$apiBase.'/'.$url, |
110
|
|
|
['body' => json_encode($body)] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return static::$response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the data response from an API operation. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private static function getResponse(): array |
122
|
|
|
{ |
123
|
|
|
return json_decode(static::$response->getBody(), true); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the data response from a get operation. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
private static function getResponseData(): array |
132
|
|
|
{ |
133
|
|
|
return json_decode(static::$response->getBody(), true)['data']; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|