-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBase.php
More file actions
152 lines (127 loc) · 3.28 KB
/
Base.php
File metadata and controls
152 lines (127 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
*
* Description
*
* @package Paystack
* @category Source
* @author Michael Akanji <matscode@gmail.com>
* @date 2017-06-26
* @copyright (c) 2016 - 2017, TECRUM (http://www.tecrum.com)
*
*/
namespace Matscode\Paystack;
use Matscode\Paystack\CURL;
use Matscode\Paystack\Utility\Text;
class Base
{
private
$_apiBaseUrl = 'https://api.paystack.co/', // with trailing slash
$_curl,
$_secretKey,
$_endPoint,
/*Getting Error Infomation*/
$_errorMessages = [];
public
$resource,
$action,
$args,
$data,
// response from the endpoint
$response;
public function __construct( $secretKey )
{
// save key in memory
$this->_secretKey = $secretKey;
return $this;
}
public function setResource( $resource )
{
$this->resource = $resource;
return $this;
}
public function setAction( $action, array $args = [] )
{
if ( ! is_array( $args ) ) {
throw new \Exception( 'Action arguments can only be of datatype Array' );
}
$this->action = $action;
$this->args = $args;
return $this;
}
/**
* Initiate Request to the paystack RESTful API and return response Obj
*
* @param array $withData
* @param string $requestMethod
* @param bool $returnArray set to true to return response as associate array
*
* @todo Utilize the third argument..
*
* @return mixed
* @throws \Exception
*/
public function sendRequest( array $withData = [], $requestMethod = 'POST', $returnArray = false )
{
if ( ! is_array( $withData ) ) {
throw new \Exception( 'sendRequest arguments can only be of datatype Array' );
}
$this->data = $withData;
$this->_endPoint = $this->_apiBaseUrl .
Text::removeSlashes( $this->resource ) . '/' .
Text::removeSlashes( $this->action );
// append parameters to endPoint
if ( count( $this->args ) > 0 ) {
$this->_endPoint .= '/' . implode( '/', $this->args );
}
// send the request and return result as json object
$this->_curl =
( new CURL(
$this->_endPoint,
$requestMethod ) )
->setRequestHeader( 'Authorization', 'Bearer ' . $this->_secretKey );
$this->response =
json_decode(
$this->_curl
->run( $this->data, 'json' ) );
return $this->response;
}
/**
* @return mixed
*/
public function getEndPoint()
{
// this works only after executing sendRequest
return $this->_endPoint;
}
/**
* @param mixed $errorMessages
*/
public function setErrorMessages( $errorMessages )
{
//if errorMessages is string
if ( is_string( $errorMessages ) ) {
$this->_errorMessages[] = $errorMessages;
}
//if errorMessages is array
if ( is_array( $errorMessages ) ) {
$this->_errorMessages = array_merge( $this->_errorMessages, $errorMessages );
}
}
/**
* @param bool $toString
* @param string $delimiter
*
* @return array|string
*/
public function getErrorMessages( $toString = false, $delimiter = '<br>' )
{
$errorMessages = $this->_errorMessages;
if ( $toString ) {
// return errorMessage as String
unset( $errorMessages ); //to avoid datatype conflict
$errorMessages = join( $delimiter, $this->_errorMessages );
}
return $errorMessages;
}
}