|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Laravel Paystack package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Prosper Otemuyiwa <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Unicodeveloper\Paystack; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
|
|
16
|
|
|
class Paystack { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Transaction Verification Successful |
|
20
|
|
|
*/ |
|
21
|
|
|
const VS = 'Verification successful'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Invalid Transaction reference |
|
25
|
|
|
*/ |
|
26
|
|
|
const ITF = "Invalid transaction reference"; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Issue Secret Key from your Paystack Dashboard |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $secretKey; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Instance of Client |
|
36
|
|
|
* @var object |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $client; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Response from requests made to Paystack |
|
42
|
|
|
* @var mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $response; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Paystack API base Url |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $baseUrl; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Authorization Url - Paystack payment page |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $authorizationUrl; |
|
57
|
|
|
|
|
58
|
|
|
public function __construct() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->setKey(); |
|
61
|
|
|
$this->setBaseUrl(); |
|
62
|
|
|
$this->setRequestOptions(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get Base Url from Paystack config file |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setBaseUrl() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->baseUrl = config('paystack.paymentUrl'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get secret key from Paystack config file |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setKey() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->secretKey = config('paystack.secretKey'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set options for making the Client request |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
private function setRequestOptions() |
|
87
|
|
|
{ |
|
88
|
|
|
$authBearer = 'Bearer '. $this->secretKey; |
|
89
|
|
|
|
|
90
|
|
|
$this->client = new Client(['base_uri' => $this->baseUrl]); |
|
91
|
|
|
|
|
92
|
|
|
$this->client->setDefaultOption('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() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->setResponse('/transaction/initialize'); |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
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) |
|
116
|
|
|
{ |
|
117
|
|
|
$data = [ |
|
118
|
|
|
"amount" => intval(request()->amount), |
|
119
|
|
|
"reference" => request()->reference, |
|
120
|
|
|
"email" => request()->email |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
$this->response = $this->client->post($this->baseUrl . $relativeUrl, [ |
|
124
|
|
|
'body' => json_encode($data) |
|
125
|
|
|
]); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function setGetResponse($relativeUrl) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the authorization url from the callback response |
|
139
|
|
|
* @return Unicodeveloper\Paystack\Paystack |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getAuthorizationUrl() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->makePaymentRequest(); |
|
144
|
|
|
|
|
145
|
|
|
$this->url = $this->response->json()["data"]["authorization_url"]; |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Hit Paystack Gateway to Verify that the transaction is valid |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
private function verifyTransactionAtGateway() |
|
155
|
|
|
{ |
|
156
|
|
|
$transactionRef = request()->query('trxref'); |
|
157
|
|
|
|
|
158
|
|
|
$relativeUrl = "/transaction/verify/{$transactionRef}"; |
|
159
|
|
|
|
|
160
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* True or false condition whether the transaction is verified |
|
165
|
|
|
* @return boolean |
|
166
|
|
|
*/ |
|
167
|
|
|
public function isTransactionVerificationValid() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->verifyTransactionAtGateway(); |
|
170
|
|
|
|
|
171
|
|
|
$result = $this->response->json()["message"]; |
|
172
|
|
|
|
|
173
|
|
|
switch ($result) |
|
174
|
|
|
{ |
|
175
|
|
|
case self::VS: |
|
176
|
|
|
$validate = true; |
|
177
|
|
|
break; |
|
178
|
|
|
case self:ITF: |
|
179
|
|
|
$validate = false; |
|
180
|
|
|
break; |
|
181
|
|
|
default: |
|
182
|
|
|
$validate = false; |
|
183
|
|
|
break; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $validate; |
|
187
|
|
|
} |
|
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() |
|
195
|
|
|
{ |
|
196
|
|
|
if ($this->isTransactionVerificationValid()) { |
|
197
|
|
|
return $this->response->json(); |
|
198
|
|
|
} else { |
|
199
|
|
|
throw new PaymentVerificationFailedException("Invalid Transaction Reference"); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Fluent method to redirect to Paystack Payment Page |
|
205
|
|
|
* @return Illuminate\Support\Redirect |
|
206
|
|
|
*/ |
|
207
|
|
|
public function redirectNow() |
|
208
|
|
|
{ |
|
209
|
|
|
return redirect($this->url); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get Access code from transaction callback respose |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getAccessCode() |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->response->json()["data"]["access_code"]; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Generate a Unique Transaction Reference |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
|
|
public function genTranxRef() |
|
226
|
|
|
{ |
|
227
|
|
|
return TransRef::getHashedToken(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get all the customers that have made transactions on your platform |
|
232
|
|
|
* @return array |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getAllCustomers() |
|
235
|
|
|
{ |
|
236
|
|
|
$this->setRequestOptions(); |
|
237
|
|
|
|
|
238
|
|
|
return $this->setGetResponse("/customer")->getData(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get all the plans that you have on Paystack |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getAllPlans() |
|
246
|
|
|
{ |
|
247
|
|
|
$this->setRequestOptions(); |
|
248
|
|
|
|
|
249
|
|
|
return $this->setGetResponse("/plan")->getData(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Get all the transactions that have happened overtime |
|
254
|
|
|
* @return array |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getAllTransactions() |
|
257
|
|
|
{ |
|
258
|
|
|
$this->setRequestOptions(); |
|
259
|
|
|
|
|
260
|
|
|
return $this->setGetResponse("/transaction")->getData(); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Get the response from a get operation |
|
265
|
|
|
* @return array |
|
266
|
|
|
*/ |
|
267
|
|
|
private function getData() |
|
268
|
|
|
{ |
|
269
|
|
|
return $this->response->json()["data"]; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} |
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: