1
+ <?php
2
+ /**
3
+ *
4
+ * Description
5
+ *
6
+ * @package Paystack
7
+ * @category Source
8
+ * @author Michael Akanji <matscode@gmail.com>
9
+ * @date 2017-06-26
10
+ * @copyright (c) 2016 - 2017, TECRUM (http://www.tecrum.com)
11
+ *
12
+ */
13
+
14
+ namespace Matscode \Paystack ;
15
+
16
+ use Matscode \Paystack \CURL ;
17
+ use Matscode \Paystack \Utility \Text ;
18
+
19
+ class Base
20
+ {
21
+
22
+ private
23
+ $ _apiBaseUrl = 'https://api.paystack.co/ ' ,
24
+ $ _curl ,
25
+ $ _secretKey ,
26
+ $ _endPoint ;
27
+
28
+ public
29
+ $ resource ,
30
+ $ action ,
31
+ $ args ,
32
+ $ data ,
33
+ // response from the endpoint
34
+ $ response ;
35
+
36
+ public function __construct ( $ secretKey )
37
+ {
38
+ // save key in memory
39
+ $ this ->_secretKey = $ secretKey ;
40
+
41
+ return $ this ;
42
+ }
43
+
44
+ public function setResource ( $ resource )
45
+ {
46
+ $ this ->resource = $ resource ;
47
+
48
+ return $ this ;
49
+ }
50
+
51
+ public function setAction ( $ action , array $ args = [] )
52
+ {
53
+ if ( ! is_array ( $ args ) ) {
54
+ throw new \Exception ( 'Action arguments can only be of datatype Array ' );
55
+ }
56
+
57
+ $ this ->action = $ action ;
58
+ $ this ->args = $ args ;
59
+
60
+ return $ this ;
61
+ }
62
+
63
+ /**
64
+ * Initiate Request to the paystack RESTful API and return response Obj
65
+ *
66
+ * @param array $withData
67
+ * @param string $requestMethod
68
+ * @param bool $returnArray set to true to return response as associate array
69
+ *
70
+ * @todo Utilize the third argument..
71
+ *
72
+ * @return mixed
73
+ * @throws \Exception
74
+ */
75
+ public function sendRequest ( array $ withData = [], $ requestMethod = 'POST ' , $ returnArray = false )
76
+ {
77
+ if ( ! is_array ( $ withData ) ) {
78
+ throw new \Exception ( 'sendRequest arguments can only be of datatype Array ' );
79
+ }
80
+
81
+ $ this ->data = $ withData ;
82
+
83
+ $ this ->_endPoint = $ this ->_apiBaseUrl .
84
+ Text::removeSlashes ( $ this ->resource ) . '/ ' .
85
+ Text::removeSlashes ( $ this ->action );
86
+ // append parameters to endPoint
87
+ if ( count ( $ this ->args ) > 0 ) {
88
+ $ this ->_endPoint .= '/ ' . implode ( '/ ' , $ this ->args );
89
+ }
90
+
91
+ // send the request and return result as json object
92
+ $ this ->_curl =
93
+ ( new CURL (
94
+ $ this ->_endPoint ,
95
+ $ requestMethod ) )
96
+ ->setRequestHeader ( 'Authorization ' , 'Bearer ' . $ this ->_secretKey );
97
+
98
+ $ this ->response =
99
+ json_decode (
100
+ $ this ->_curl
101
+ ->run ( $ this ->data , 'json ' ) );
102
+
103
+ return $ this ->response ;
104
+ }
105
+
106
+ /**
107
+ * @return mixed
108
+ */
109
+ public function getEndPoint ()
110
+ {
111
+ // this works only after executing sendRequest
112
+ return $ this ->_endPoint ;
113
+ }
114
+
115
+ }
0 commit comments