1 | <?php |
||
23 | class Router |
||
24 | { |
||
25 | |||
26 | private $route; |
||
27 | private $route_class; |
||
28 | private $secret_key; |
||
29 | private $methods; |
||
30 | private $use_guzzle=false; |
||
31 | |||
32 | const ID_KEY = 'id'; |
||
33 | const PAYSTACK_API_ROOT = 'https://api.paystack.co'; |
||
34 | const HEADER_KEY = 'header'; |
||
35 | const HTTP_CODE_KEY = 'httpcode'; |
||
36 | const BODY_KEY = 'body'; |
||
37 | |||
38 | /** |
||
39 | * moveArgsToSentargs |
||
40 | * Insert description here |
||
41 | * |
||
42 | * @param $interface |
||
43 | * @param $payload |
||
44 | * @param $sentargs |
||
45 | * |
||
46 | * @return |
||
47 | * |
||
48 | * @access |
||
49 | * @static |
||
50 | * @see |
||
51 | * @since |
||
52 | */ |
||
53 | private function moveArgsToSentargs( |
||
76 | |||
77 | /** |
||
78 | * putArgsIntoEndpoint |
||
79 | * Insert description here |
||
80 | * |
||
81 | * @param $endpoint |
||
82 | * @param $sentargs |
||
83 | * |
||
84 | * @return |
||
85 | * |
||
86 | * @access |
||
87 | * @static |
||
88 | * @see |
||
89 | * @since |
||
90 | */ |
||
91 | private function putArgsIntoEndpoint(&$endpoint, $sentargs) |
||
98 | |||
99 | /** |
||
100 | * callViaCurl |
||
101 | * Insert description here |
||
102 | * |
||
103 | * @param $interface |
||
104 | * @param $payload |
||
105 | * @param $sentargs |
||
106 | * |
||
107 | * @return |
||
108 | * |
||
109 | * @access |
||
110 | * @static |
||
111 | * @see |
||
112 | * @since |
||
113 | */ |
||
114 | private function callViaCurl($interface, $payload = [ ], $sentargs = [ ]) |
||
115 | { |
||
116 | |||
117 | |||
118 | $endpoint = Router::PAYSTACK_API_ROOT . $interface[RouteInterface::ENDPOINT_KEY]; |
||
119 | $method = $interface[RouteInterface::METHOD_KEY]; |
||
120 | |||
121 | $this->moveArgsToSentargs($interface, $payload, $sentargs); |
||
122 | $this->putArgsIntoEndpoint($endpoint, $sentargs); |
||
123 | |||
124 | $headers = ["Authorization"=>"Bearer " . $this->secret_key ]; |
||
125 | $body = ''; |
||
126 | if (($method === RouteInterface::POST_METHOD)|| |
||
127 | ($method === RouteInterface::PUT_METHOD)) { |
||
128 | $headers["Content-Type"] = "application/json"; |
||
129 | $body = json_encode($payload); |
||
130 | } elseif ($method === RouteInterface::GET_METHOD) { |
||
131 | $endpoint = $endpoint . '?' . http_build_query($payload); |
||
132 | } |
||
133 | // Use Guzzle if found, else use Curl |
||
134 | if ($this->use_guzzle && class_exists('\\GuzzleHttp\\Client') && class_exists('\\GuzzleHttp\\Psr7\\Request')) { |
||
135 | $request = new \GuzzleHttp\Psr7\Request(strtoupper($method), $endpoint, $headers, $body); |
||
136 | $client = new \GuzzleHttp\Client(); |
||
137 | try { |
||
138 | $response = $client->send($request); |
||
139 | } catch (\GuzzleHttp\Exception\RequestException $e) { |
||
140 | if ($e->hasResponse()) { |
||
141 | $response = $e->getResponse(); |
||
142 | } else { |
||
143 | $response = null; |
||
144 | } |
||
145 | } |
||
146 | return $response; |
||
147 | |||
148 | } else { |
||
149 | //open connection |
||
150 | |||
151 | $ch = \curl_init(); |
||
152 | // set url |
||
153 | \curl_setopt($ch, \CURLOPT_URL, $endpoint); |
||
154 | |||
155 | if ($method === RouteInterface::POST_METHOD || $method === RouteInterface::PUT_METHOD) { |
||
156 | ($method === RouteInterface:: POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true); |
||
157 | ($method === RouteInterface ::PUT_METHOD) && \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
158 | |||
159 | \curl_setopt($ch, \CURLOPT_POSTFIELDS, $body); |
||
160 | } |
||
161 | //flatten the headers |
||
162 | $flattened_headers = []; |
||
163 | while (list($key, $value) = each($headers)) { |
||
164 | $flattened_headers[] = $key . ": " . $value; |
||
165 | } |
||
166 | \curl_setopt($ch, \CURLOPT_HTTPHEADER, $flattened_headers); |
||
167 | \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
||
168 | \curl_setopt($ch, \CURLOPT_HEADER, 1); |
||
169 | |||
170 | $response = \curl_exec($ch); |
||
171 | |||
172 | if (\curl_errno($ch)) { // should be 0 |
||
173 | // curl ended with an error |
||
174 | \curl_close($ch); |
||
175 | return [[],[],0]; |
||
176 | } |
||
177 | |||
178 | $code = \curl_getinfo($ch, \CURLINFO_HTTP_CODE); |
||
179 | |||
180 | // Then, after your \curl_exec call: |
||
181 | $header_size = \curl_getinfo($ch, \CURLINFO_HEADER_SIZE); |
||
182 | $header = substr($response, 0, $header_size); |
||
183 | $header = $this->headersFromLines(explode("\n", trim($header))); |
||
184 | $body = substr($response, $header_size); |
||
185 | $body = json_decode($body, true); |
||
186 | |||
187 | |||
188 | //close connection |
||
189 | \curl_close($ch); |
||
190 | |||
191 | return [ |
||
192 | 0 => $header, 1 => $body, 2=> $code, |
||
193 | Router::HEADER_KEY => $header, Router::BODY_KEY => $body, |
||
194 | Router::HTTP_CODE_KEY=>$code]; |
||
195 | } |
||
196 | |||
197 | } |
||
198 | |||
199 | private function headersFromLines($lines) |
||
210 | |||
211 | /** |
||
212 | * __call |
||
213 | * Insert description here |
||
214 | * |
||
215 | * @param $methd |
||
216 | * @param $sentargs |
||
217 | * |
||
218 | * @return |
||
219 | * |
||
220 | * @access |
||
221 | * @static |
||
222 | * @see |
||
223 | * @since |
||
224 | */ |
||
225 | public function __call($methd, $sentargs) |
||
235 | |||
236 | /** |
||
237 | * A magic resource object that can make method calls to API |
||
238 | * |
||
239 | * @param $route |
||
240 | * @param $paystackObj - A YabaCon\Paystack Object |
||
241 | */ |
||
242 | public function __construct($route, $paystackObj) |
||
282 | } |
||
283 |