1 | <?php |
||
17 | class Paystack { |
||
18 | |||
19 | /** |
||
20 | * Transaction Verification Successful |
||
21 | */ |
||
22 | const VS = 'Verification successful'; |
||
23 | |||
24 | /** |
||
25 | * Invalid Transaction reference |
||
26 | */ |
||
27 | const ITF = "Invalid transaction reference"; |
||
28 | |||
29 | /** |
||
30 | * Issue Secret Key from your Paystack Dashboard |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $secretKey; |
||
34 | |||
35 | /** |
||
36 | * Instance of Client |
||
37 | * @var object |
||
38 | */ |
||
39 | protected $client; |
||
40 | |||
41 | /** |
||
42 | * Response from requests made to Paystack |
||
43 | * @var mixed |
||
44 | */ |
||
45 | protected $response; |
||
46 | |||
47 | /** |
||
48 | * Paystack API base Url |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $baseUrl; |
||
52 | |||
53 | /** |
||
54 | * Authorization Url - Paystack payment page |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $authorizationUrl; |
||
58 | |||
59 | public function __construct() |
||
60 | { |
||
61 | $this->setKey(); |
||
62 | $this->setBaseUrl(); |
||
63 | $this->setRequestOptions(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get Base Url from Paystack config file |
||
68 | */ |
||
69 | public function setBaseUrl() |
||
70 | { |
||
71 | $this->baseUrl = Config::get('paystack.paymentUrl'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get secret key from Paystack config file |
||
76 | * @return void |
||
77 | */ |
||
78 | public function setKey() |
||
79 | { |
||
80 | $this->secretKey = Config::get('paystack.secretKey'); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Set options for making the Client request |
||
85 | * @return void |
||
86 | */ |
||
87 | private function setRequestOptions() |
||
88 | { |
||
89 | $authBearer = 'Bearer '. $this->secretKey; |
||
90 | |||
91 | $this->client = new Client(['base_uri' => $this->baseUrl, |
||
92 | 'headers' => [ |
||
93 | 'Authorization' => $authBearer, |
||
94 | 'Content-Type' => 'application/json', |
||
95 | 'Accept' => 'application/json' |
||
96 | ]]); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Initiate a payment request to Paystack |
||
101 | * @return Unicodeveloper\Paystack\Paystack |
||
102 | */ |
||
103 | public function makePaymentRequest() |
||
109 | |||
110 | /** |
||
111 | * Make the client request and get the response |
||
112 | * @param string $relativeUrl |
||
113 | * @return Unicodeveloper\Paystack\Paystack |
||
114 | */ |
||
115 | public function setResponse($relativeUrl) |
||
129 | |||
130 | private function setGetResponse($relativeUrl) |
||
136 | |||
137 | /** |
||
138 | * Get the authorization url from the callback response |
||
139 | * @return Unicodeveloper\Paystack\Paystack |
||
140 | */ |
||
141 | public function getAuthorizationUrl() |
||
149 | |||
150 | /** |
||
151 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
152 | * @return void |
||
153 | */ |
||
154 | private function verifyTransactionAtGateway() |
||
162 | |||
163 | /** |
||
164 | * True or false condition whether the transaction is verified |
||
165 | * @return boolean |
||
166 | */ |
||
167 | public function isTransactionVerificationValid() |
||
188 | |||
189 | /** |
||
190 | * Get Payment details if the transaction was verified successfully |
||
191 | * @throws Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException |
||
192 | * @return json |
||
193 | */ |
||
194 | public function getPaymentData() |
||
202 | |||
203 | /** |
||
204 | * Fluent method to redirect to Paystack Payment Page |
||
205 | * @return Illuminate\Support\Redirect |
||
206 | */ |
||
207 | public function redirectNow() |
||
211 | |||
212 | /** |
||
213 | * Get Access code from transaction callback respose |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getAccessCode() |
||
220 | |||
221 | /** |
||
222 | * Generate a Unique Transaction Reference |
||
223 | * @return string |
||
224 | */ |
||
225 | public function genTranxRef() |
||
229 | |||
230 | /** |
||
231 | * Get all the customers that have made transactions on your platform |
||
232 | * @return array |
||
233 | */ |
||
234 | public function getAllCustomers() |
||
240 | |||
241 | /** |
||
242 | * Get all the plans that you have on Paystack |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getAllPlans() |
||
251 | |||
252 | /** |
||
253 | * Get all the transactions that have happened overtime |
||
254 | * @return array |
||
255 | */ |
||
256 | public function getAllTransactions() |
||
262 | |||
263 | /** |
||
264 | * Get the whole response from a get operation |
||
265 | * @return array |
||
266 | */ |
||
267 | private function getResponse() |
||
271 | |||
272 | /** |
||
273 | * Get the data response from a get operation |
||
274 | * @return array |
||
275 | */ |
||
276 | private function getData() |
||
280 | |||
281 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: