|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yabacon; |
|
4
|
|
|
|
|
5
|
|
|
use \Yabacon\Paystack\Helpers\Router; |
|
6
|
|
|
use \Yabacon\Paystack\Contracts\RouteInterface; |
|
7
|
|
|
use \Yabacon\Paystack\Exception\ValidationException; |
|
8
|
|
|
|
|
9
|
|
|
class Paystack |
|
10
|
|
|
{ |
|
11
|
|
|
public $secret_key; |
|
12
|
|
|
public $use_guzzle = false; |
|
13
|
|
|
public $custom_routes = []; |
|
14
|
|
|
public static $fallback_to_file_get_contents = true; |
|
15
|
|
|
const VERSION="2.1.19"; |
|
16
|
|
|
|
|
17
|
20 |
|
public function __construct($secret_key) |
|
18
|
|
|
{ |
|
19
|
20 |
|
if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) { |
|
20
|
1 |
|
throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.'); |
|
21
|
|
|
} |
|
22
|
19 |
|
$this->secret_key = $secret_key; |
|
23
|
19 |
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function useGuzzle() |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->use_guzzle = true; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
6 |
|
public function useRoutes(array $routes) |
|
31
|
|
|
{ |
|
32
|
6 |
|
foreach ($routes as $route => $class) { |
|
33
|
6 |
|
if (! is_string($route)) { |
|
34
|
1 |
|
throw new \InvalidArgumentException( |
|
35
|
|
|
'Custom routes should map to a route class' |
|
36
|
1 |
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
if (in_array($route, Router::$ROUTES)) { |
|
40
|
1 |
|
throw new \InvalidArgumentException( |
|
41
|
|
|
$route . ' is already an existing defined route' |
|
42
|
1 |
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
if (! in_array(RouteInterface::class, class_implements($class))) { |
|
46
|
1 |
|
throw new \InvalidArgumentException( |
|
47
|
1 |
|
'Custom route class ' . $class . 'should implement ' . RouteInterface::class |
|
48
|
1 |
|
); |
|
49
|
|
|
} |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
$this->custom_routes = $routes; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public static function disableFileGetContentsFallback() |
|
56
|
|
|
{ |
|
57
|
1 |
|
Paystack::$fallback_to_file_get_contents = false; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public static function enableFileGetContentsFallback() |
|
61
|
|
|
{ |
|
62
|
1 |
|
Paystack::$fallback_to_file_get_contents = true; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
public function __call($method, $args) |
|
66
|
|
|
{ |
|
67
|
4 |
|
if ($singular_form = Router::singularFor($method)) { |
|
68
|
1 |
|
return $this->handlePlural($singular_form, $method, $args); |
|
69
|
|
|
} |
|
70
|
3 |
|
return $this->handleSingular($method, $args); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
private function handlePlural($singular_form, $method, $args) |
|
74
|
|
|
{ |
|
75
|
1 |
|
if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) { |
|
76
|
|
|
return $this->{$singular_form}->__call('getList', $args); |
|
77
|
|
|
} |
|
78
|
1 |
|
throw new \InvalidArgumentException( |
|
79
|
1 |
|
'Route "' . $method . '" can only accept an optional array of filters and ' |
|
80
|
1 |
|
.'paging arguments (perPage, page).' |
|
81
|
1 |
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
private function handleSingular($method, $args) |
|
85
|
|
|
{ |
|
86
|
3 |
|
if (count($args) === 1) { |
|
87
|
1 |
|
$args = [[], [ Router::ID_KEY => $args[0] ] ]; |
|
88
|
1 |
|
return $this->{$method}->__call('fetch', $args); |
|
89
|
|
|
} |
|
90
|
2 |
|
throw new \InvalidArgumentException( |
|
91
|
2 |
|
'Route "' . $method . '" can only accept an id or code.' |
|
92
|
2 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @deprecated |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function registerAutoloader() |
|
99
|
|
|
{ |
|
100
|
|
|
trigger_error('Include "src/autoload.php" instead', E_DEPRECATED | E_USER_NOTICE); |
|
101
|
|
|
require_once(__DIR__ . '/../src/autoload.php'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
public function __get($name) |
|
105
|
|
|
{ |
|
106
|
4 |
|
return new Router($name, $this); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|