Skip to content

Commit bb3faf1

Browse files
committed
CURL default SSL version TLSv1.2
This is to match requirement by Paystack servers Update Requirements for Curl, OpenSSL and PHP define CURL_SSLVERSION_TLSv1_2 as 6 if not found, to avoid not defined error
1 parent 1ea1f2a commit bb3faf1

7 files changed

Lines changed: 74 additions & 57 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ All Notable changes to `paystack-php` will be documented in this file.
44

55
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
66

7-
## NEXT - YYYY-MM-DD
7+
## 1.0.2 - 2016-03-10
88

9-
### Added
10-
- Nothing
9+
### Added usage of TLSv1.2
10+
CURL default SSL version TLSv1.2
11+
Update Requirements for Curl, OpenSSL and PHP
12+
define `CURL_SSLVERSION_TLSv1_2` as 6 if not found, to avoid not defined error
1113

1214
### Deprecated
1315
- Nothing

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ A PHP API wrapper for [Paystack](https://paystack.co/).
1111

1212
[![Paystack](img/paystack.png?raw=true "Paystack")](https://paystack.co/)
1313

14+
## Requirements
15+
- Curl 7.34.0 or more recent (Unless using Guzzle)
16+
- PHP 5.4.0 or more recent
17+
- OpenSSL v1.0.1 or more recent
18+
1419
## Install
1520

1621
### Via Composer
@@ -32,7 +37,7 @@ require 'path/to/src/Paystack.php';
3237

3338
Check [ibrahimlawal/paystack-php-sample](https://github.com/ibrahimlawal/paystack-php-sample) for a sample donation page that uses this library
3439

35-
###Default: Uses curl for requests
40+
### Default: Uses curl for requests
3641
``` php
3742
$paystack = new \YabaCon\Paystack('secret_key');
3843

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"extra": {
5252
"branch-alias": {
53-
"dev-master": "1.0.7-dev"
53+
"dev-master": "1.0.8-dev"
5454
}
5555
}
5656
}

src/Paystack.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,32 @@ class Paystack
1414
private $routes;
1515
public $use_guzzle = false;
1616

17-
/**
17+
/**
1818
* Creates a new Paystack object
19+
*
1920
* @param string $secret_key - Secret key for your account with Paystack
2021
*/
2122
public function __construct($secret_key)
2223
{
2324

2425
if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) {
25-
// Should never get here
26+
// Should never get here
2627
throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.');
2728

2829
}
2930
$this->secret_key = $secret_key;
3031
$this->routes = $this->definedRoutes();
3132
}
3233

33-
/**
34+
/**
3435
* Router uses Guzzle for calls if GuzzleHttp is found
3536
*/
3637
public function useGuzzle()
3738
{
3839
$this->use_guzzle = true;
3940
}
4041

41-
/**
42+
/**
4243
* definedRoutes
4344
* gets routes defined in the YabaCon\Paystack\Routes namespace
4445
* since we are using PSR-4. It is safe to assume that all php
@@ -47,8 +48,8 @@ public function useGuzzle()
4748
* @return the list of defined Routes
4849
*
4950
* @access private
50-
* @see YabaCon\Paystack\Routes\Router
51-
* @since 1.0
51+
* @see YabaCon\Paystack\Routes\Router
52+
* @since 1.0
5253
*/
5354
private function definedRoutes()
5455
{
@@ -61,7 +62,7 @@ private function definedRoutes()
6162
}
6263
return $routes;
6364
}
64-
/**
65+
/**
6566
* __call
6667
* Magic Method for getOne on routes
6768
*
@@ -73,36 +74,40 @@ private function definedRoutes()
7374
* @return the result of calling /{route}/get on the api
7475
*
7576
* @access public
76-
* @see YabaCon\Paystack\Routes\Router
77-
* @since 1.0
77+
* @see YabaCon\Paystack\Routes\Router
78+
* @since 1.0
7879
*/
7980
public function __call($method, $args)
8081
{
81-
/*
82-
attempt to call getOne when the route is called directly
83-
translates to /{root}/{get}/{id}
84-
*/
82+
/*
83+
attempt to call getOne when the route is called directly
84+
translates to /{root}/{get}/{id}
85+
*/
8586
if (in_array($method, $this->routes, true)) {
8687
$route = new Router($method, $this);
8788

8889
if (count($args) === 1 && is_integer($args[0])) {
89-
// no params, just one arg... the id
90+
// no params, just one arg... the id
9091
$args = [[], [ Router::ID_KEY => $args[0] ] ];
9192
return $route->__call('getOne', $args);
9293
} elseif (count($args) === 2 && is_integer($args[0]) && is_array($args[1])) {
93-
// there are params, and just one arg... the id
94+
// there are params, and just one arg... the id
9495
$args = [$args[1], [ Router::ID_KEY => $args[0] ] ];
9596
return $route->__call('getOne', $args);
9697
}
9798
}
98-
// Should never get here
99-
throw new \InvalidArgumentException('Route "' .
100-
$method .
101-
'" only accepts an integer id and an optional array of paging arguments.');
99+
// Should never get here
100+
throw new \InvalidArgumentException(
101+
'Route "' .
102+
$method .
103+
'" only accepts an integer id and an optional array of paging arguments.'
104+
);
102105
}
103106

104-
/**
105-
* Call this function to register the custom auto loader for Paystack. * Required only if not using composer. */
107+
/**
108+
* Call this function to register the custom auto loader for Paystack.
109+
* Required only if not using composer.
110+
*/
106111
public static function registerAutoloader()
107112
{
108113
spl_autoload_register(
@@ -118,7 +123,7 @@ function ($class_name) {
118123
);
119124
}
120125

121-
/**
126+
/**
122127
* __get
123128
* Insert description here
124129
*

src/Paystack/Helpers/Router.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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());

src/Paystack/Routes/Plan.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static function root()
3636
* create
3737
* Insert description here
3838
*
39-
*
4039
* @return
4140
*
4241
* @access
@@ -69,7 +68,6 @@ public static function create()
6968
* getOne
7069
* Insert description here
7170
*
72-
*
7371
* @return
7472
*
7573
* @access
@@ -91,7 +89,6 @@ public static function getOne()
9189
* getList
9290
* Insert description here
9391
*
94-
*
9592
* @return
9693
*
9794
* @access
@@ -112,7 +109,6 @@ public static function getList()
112109
* update
113110
* Insert description here
114111
*
115-
*
116112
* @return
117113
*
118114
* @access

0 commit comments

Comments
 (0)