@@ -35,7 +35,7 @@ class Router
3535 const HTTP_CODE_KEY = 'httpcode ' ;
3636 const BODY_KEY = 'body ' ;
3737
38- /**
38+ /**
3939 * moveArgsToSentargs
4040 * Insert description here
4141 *
@@ -58,14 +58,14 @@ private function moveArgsToSentargs(
5858
5959
6060
61- // check if interface supports args
61+ // check if interface supports args
6262 if (array_key_exists (RouteInterface:: ARGS_KEY , $ interface )) {
63- // to allow args to be specified in the payload, filter them out and put them in sentargs
63+ // to allow args to be specified in the payload, filter them out and put them in sentargs
6464 $ sentargs = (!$ sentargs ) ? [ ] : $ sentargs ; // Make sure $sentargs is not null
6565 $ args = $ interface [RouteInterface::ARGS_KEY ];
6666 while (list ($ key , $ value ) = each ($ payload )) {
67- // check that a value was specified
68- // with a key that was expected as an arg
67+ // check that a value was specified
68+ // with a key that was expected as an arg
6969 if (in_array ($ key , $ args )) {
7070 $ sentargs [$ key ] = $ value ;
7171 unset($ payload [$ key ]);
@@ -74,7 +74,7 @@ private function moveArgsToSentargs(
7474 }
7575 }
7676
77- /**
77+ /**
7878 * putArgsIntoEndpoint
7979 * Insert description here
8080 *
@@ -90,13 +90,13 @@ private function moveArgsToSentargs(
9090 */
9191 private function putArgsIntoEndpoint (&$ endpoint , $ sentargs )
9292 {
93- // substitute sentargs in endpoint
93+ // substitute sentargs in endpoint
9494 while (list ($ key , $ value ) = each ($ sentargs )) {
9595 $ endpoint = str_replace ('{ ' . $ key . '} ' , $ value , $ endpoint );
9696 }
9797 }
9898
99- /**
99+ /**
100100 * callViaCurl
101101 * Insert description here
102102 *
@@ -123,14 +123,15 @@ private function callViaCurl($interface, $payload = [ ], $sentargs = [ ])
123123
124124 $ headers = ["Authorization " =>"Bearer " . $ this ->secret_key ];
125125 $ body = '' ;
126- if (($ method === RouteInterface::POST_METHOD )||
127- ($ method === RouteInterface::PUT_METHOD )) {
126+ if (($ method === RouteInterface::POST_METHOD )
127+ || ($ method === RouteInterface::PUT_METHOD )
128+ ) {
128129 $ headers ["Content-Type " ] = "application/json " ;
129130 $ body = json_encode ($ payload );
130131 } elseif ($ method === RouteInterface::GET_METHOD ) {
131132 $ endpoint = $ endpoint . '? ' . http_build_query ($ payload );
132133 }
133- // Use Guzzle if found, else use Curl
134+ // Use Guzzle if found, else use Curl
134135 if ($ this ->use_guzzle && class_exists ('\\GuzzleHttp \\Client ' ) && class_exists ('\\GuzzleHttp \\Psr7 \\Request ' )) {
135136 $ request = new \GuzzleHttp \Psr7 \Request (strtoupper ($ method ), $ endpoint , $ headers , $ body );
136137 $ client = new \GuzzleHttp \Client ();
@@ -146,10 +147,10 @@ private function callViaCurl($interface, $payload = [ ], $sentargs = [ ])
146147 return $ response ;
147148
148149 } else {
149- //open connection
150+ //open connection
150151
151152 $ ch = \curl_init ();
152- // set url
153+ // set url
153154 \curl_setopt ($ ch , \CURLOPT_URL , $ endpoint );
154155
155156 if ($ method === RouteInterface::POST_METHOD || $ method === RouteInterface::PUT_METHOD ) {
@@ -158,34 +159,43 @@ private function callViaCurl($interface, $payload = [ ], $sentargs = [ ])
158159
159160 \curl_setopt ($ ch , \CURLOPT_POSTFIELDS , $ body );
160161 }
161- //flatten the headers
162+ //flatten the headers
162163 $ flattened_headers = [];
163164 while (list ($ key , $ value ) = each ($ headers )) {
164165 $ flattened_headers [] = $ key . ": " . $ value ;
165166 }
166167 \curl_setopt ($ ch , \CURLOPT_HTTPHEADER , $ flattened_headers );
167168 \curl_setopt ($ ch , \CURLOPT_RETURNTRANSFER , 1 );
168169 \curl_setopt ($ ch , \CURLOPT_HEADER , 1 );
170+
171+ // Make sure CURL_SSLVERSION_TLSv1_2 is defined as 6
172+ // Curl must be able to use TLSv1.2 to connect
173+ // to Paystack servers
174+
175+ if (!defined ('CURL_SSLVERSION_TLSV1_2 ' )) {
176+ define ('CURL_SSLVERSION_TLSV1_2 ' , 6 );
177+ }
178+ curl_setopt ($ ch , CURLOPT_SSLVERSION , CURL_SSLVERSION_TLSV1_2 );
169179
170180 $ response = \curl_exec ($ ch );
171181
172182 if (\curl_errno ($ ch )) { // should be 0
173- // curl ended with an error
183+ // curl ended with an error
174184 \curl_close ($ ch );
175185 return [[],[],0 ];
176186 }
177187
178188 $ code = \curl_getinfo ($ ch , \CURLINFO_HTTP_CODE );
179189
180- // Then, after your \curl_exec call:
190+ // Then, after your \curl_exec call:
181191 $ header_size = \curl_getinfo ($ ch , \CURLINFO_HEADER_SIZE );
182192 $ header = substr ($ response , 0 , $ header_size );
183193 $ header = $ this ->headersFromLines (explode ("\n" , trim ($ header )));
184194 $ body = substr ($ response , $ header_size );
185195 $ body = json_decode ($ body , true );
186196
187197
188- //close connection
198+ //close connection
189199 \curl_close ($ ch );
190200
191201 return [
@@ -208,7 +218,7 @@ private function headersFromLines($lines)
208218 return $ headers ;
209219 }
210220
211- /**
221+ /**
212222 * __call
213223 * Insert description here
214224 *
@@ -228,12 +238,12 @@ public function __call($methd, $sentargs)
228238 if (array_key_exists ($ method , $ this ->methods ) && is_callable ($ this ->methods [$ method ])) {
229239 return call_user_func_array ($ this ->methods [$ method ], $ sentargs );
230240 } else {
231- // User attempted to call a function that does not exist
241+ // User attempted to call a function that does not exist
232242 throw new \Exception ('Function " ' . $ method . '" does not exist for " ' . $ this ->route . "'. " );
233243 }
234244 }
235245
236- /**
246+ /**
237247 * A magic resource object that can make method calls to API
238248 *
239249 * @param $route
@@ -247,13 +257,13 @@ public function __construct($route, $paystackObj)
247257 $ this ->use_guzzle = $ paystackObj ->use_guzzle ;
248258
249259 $ mets = get_class_methods ($ this ->route_class );
250- // add methods to this object per method, except root
260+ // add methods to this object per method, except root
251261 foreach ($ mets as $ mtd ) {
252262 if ($ mtd === 'root ' ) {
253- // skip root method
263+ // skip root method
254264 continue ;
255265 }
256- /**
266+ /**
257267 * array
258268 * Insert description here
259269 *
@@ -273,7 +283,7 @@ public function __construct($route, $paystackObj)
273283 array $ sentargs = [ ]
274284 ) use ($ mtd ) {
275285 $ interface = call_user_func ($ this ->route_class . ':: ' . $ mtd );
276- // TODO: validate params and sentargs against definitions
286+ // TODO: validate params and sentargs against definitions
277287 return $ this ->callViaCurl ($ interface , $ params , $ sentargs );
278288 };
279289 $ this ->methods [$ mtd ] = \Closure::bind ($ mtdFunc , $ this , get_class ());
0 commit comments