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
|
|
|
use Illuminate\Support\Facades\Config; |
16
|
|
|
use Unicodeveloper\Paystack\Exceptions\IsNullException; |
17
|
|
|
use Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException; |
18
|
|
|
|
19
|
|
|
class Paystack |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Transaction Verification Successful |
23
|
|
|
*/ |
24
|
|
|
const VS = 'Verification successful'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Invalid Transaction reference |
28
|
|
|
*/ |
29
|
|
|
const ITF = "Invalid transaction reference"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Issue Secret Key from your Paystack Dashboard |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $secretKey; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Instance of Client |
39
|
|
|
* @var Client |
40
|
|
|
*/ |
41
|
|
|
protected $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Response from requests made to Paystack |
45
|
|
|
* @var mixed |
46
|
|
|
*/ |
47
|
|
|
protected $response; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Paystack API base Url |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $baseUrl; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Authorization Url - Paystack payment page |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $authorizationUrl; |
60
|
|
|
|
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
$this->setKey(); |
64
|
|
|
$this->setBaseUrl(); |
65
|
|
|
$this->setRequestOptions(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Base Url from Paystack config file |
70
|
|
|
*/ |
71
|
|
|
public function setBaseUrl() |
72
|
|
|
{ |
73
|
|
|
$this->baseUrl = Config::get('paystack.paymentUrl'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get secret key from Paystack config file |
78
|
|
|
*/ |
79
|
|
|
public function setKey() |
80
|
|
|
{ |
81
|
|
|
$this->secretKey = Config::get('paystack.secretKey'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set options for making the Client request |
86
|
|
|
*/ |
87
|
|
|
private function setRequestOptions() |
88
|
|
|
{ |
89
|
|
|
$authBearer = 'Bearer '. $this->secretKey; |
90
|
|
|
|
91
|
|
|
$this->client = new Client( |
92
|
|
|
[ |
93
|
|
|
'base_uri' => $this->baseUrl, |
94
|
|
|
'headers' => [ |
95
|
|
|
'Authorization' => $authBearer, |
96
|
|
|
'Content-Type' => 'application/json', |
97
|
|
|
'Accept' => 'application/json' |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Initiate a payment request to Paystack |
105
|
|
|
* @return Paystack |
106
|
|
|
*/ |
107
|
|
|
public function makePaymentRequest() |
108
|
|
|
{ |
109
|
|
|
$data = [ |
110
|
|
|
"amount" => intval(request()->amount), |
111
|
|
|
"reference" => request()->reference, |
112
|
|
|
"email" => request()->email, |
113
|
|
|
"plan" => request()->plan, |
114
|
|
|
"first_name" => request()->first_name, |
115
|
|
|
"last_name" => request()->last_name, |
116
|
|
|
"callback_url" => request()->callback_url, |
117
|
|
|
/* |
118
|
|
|
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url |
119
|
|
|
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" > |
120
|
|
|
*array must be set up as: $array = [ 'custom_fields' => [ |
121
|
|
|
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"], |
122
|
|
|
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"], |
123
|
|
|
* . |
124
|
|
|
* . |
125
|
|
|
* . |
126
|
|
|
* ] |
127
|
|
|
* |
128
|
|
|
* ] |
129
|
|
|
*/ |
130
|
|
|
'metadata' => request()->metadata |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
// Remove the fields which were not sent (value would be null) |
134
|
|
|
array_filter($data); |
135
|
|
|
|
136
|
|
|
$this->setHttpResponse('/transaction/initialize', 'POST', $data); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $relativeUrl |
144
|
|
|
* @param string $method |
145
|
|
|
* @param array $body |
146
|
|
|
* @return Paystack |
147
|
|
|
* @throws IsNullException |
148
|
|
|
*/ |
149
|
|
|
private function setHttpResponse($relativeUrl, $method, $body = []) |
150
|
|
|
{ |
151
|
|
|
if (is_null($method)) { |
152
|
|
|
throw new IsNullException("Empty method not allowed"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->response = $this->client->{strtolower($method)}( |
156
|
|
|
$this->baseUrl . $relativeUrl, |
157
|
|
|
["body" => json_encode($body)] |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the authorization url from the callback response |
165
|
|
|
* @return Paystack |
166
|
|
|
*/ |
167
|
|
|
public function getAuthorizationUrl() |
168
|
|
|
{ |
169
|
|
|
$this->makePaymentRequest(); |
170
|
|
|
|
171
|
|
|
$this->url = $this->getResponse()['data']['authorization_url']; |
|
|
|
|
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Hit Paystack Gateway to Verify that the transaction is valid |
178
|
|
|
*/ |
179
|
|
|
private function verifyTransactionAtGateway() |
180
|
|
|
{ |
181
|
|
|
$transactionRef = request()->query('trxref'); |
182
|
|
|
|
183
|
|
|
$relativeUrl = "/transaction/verify/{$transactionRef}"; |
184
|
|
|
|
185
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* True or false condition whether the transaction is verified |
190
|
|
|
* @return boolean |
191
|
|
|
*/ |
192
|
|
|
public function isTransactionVerificationValid() |
193
|
|
|
{ |
194
|
|
|
$this->verifyTransactionAtGateway(); |
195
|
|
|
|
196
|
|
|
$result = $this->getResponse()['message']; |
197
|
|
|
|
198
|
|
|
switch ($result) { |
199
|
|
|
case self::VS: |
200
|
|
|
$validate = true; |
201
|
|
|
break; |
202
|
|
|
case self::ITF: |
203
|
|
|
$validate = false; |
204
|
|
|
break; |
205
|
|
|
default: |
206
|
|
|
$validate = false; |
207
|
|
|
break; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $validate; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get Payment details if the transaction was verified successfully |
215
|
|
|
* @return json |
216
|
|
|
* @throws PaymentVerificationFailedException |
217
|
|
|
*/ |
218
|
|
|
public function getPaymentData() |
219
|
|
|
{ |
220
|
|
|
if ($this->isTransactionVerificationValid()) { |
221
|
|
|
return $this->getResponse(); |
222
|
|
|
} else { |
223
|
|
|
throw new PaymentVerificationFailedException("Invalid Transaction Reference"); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Fluent method to redirect to Paystack Payment Page |
229
|
|
|
*/ |
230
|
|
|
public function redirectNow() |
231
|
|
|
{ |
232
|
|
|
return redirect($this->url); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get Access code from transaction callback respose |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function getAccessCode() |
240
|
|
|
{ |
241
|
|
|
return $this->getResponse()['data']['access_code']; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generate a Unique Transaction Reference |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function genTranxRef() |
249
|
|
|
{ |
250
|
|
|
return TransRef::getHashedToken(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get all the customers that have made transactions on your platform |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
public function getAllCustomers() |
258
|
|
|
{ |
259
|
|
|
$this->setRequestOptions(); |
260
|
|
|
|
261
|
|
|
return $this->setHttpResponse("/customer", 'GET', [])->getData(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get all the plans that you have on Paystack |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
|
public function getAllPlans() |
269
|
|
|
{ |
270
|
|
|
$this->setRequestOptions(); |
271
|
|
|
|
272
|
|
|
return $this->setHttpResponse("/plan", 'GET', [])->getData(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get all the transactions that have happened overtime |
277
|
|
|
* @return array |
278
|
|
|
*/ |
279
|
|
|
public function getAllTransactions() |
280
|
|
|
{ |
281
|
|
|
$this->setRequestOptions(); |
282
|
|
|
|
283
|
|
|
return $this->setHttpResponse("/transaction", 'GET', [])->getData(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get the whole response from a get operation |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
|
|
private function getResponse() |
291
|
|
|
{ |
292
|
|
|
return json_decode($this->response->getBody(), true); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get the data response from a get operation |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
private function getData() |
300
|
|
|
{ |
301
|
|
|
return $this->getResponse()['data']; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Create a plan |
306
|
|
|
*/ |
307
|
|
View Code Duplication |
public function createPlan() |
|
|
|
|
308
|
|
|
{ |
309
|
|
|
$data = [ |
310
|
|
|
"name" => request()->name, |
311
|
|
|
"description" => request()->desc, |
312
|
|
|
"amount" => intval(request()->amount), |
313
|
|
|
"interval" => request()->interval, |
314
|
|
|
"send_invoices" => request()->send_invoices, |
315
|
|
|
"send_sms" => request()->send_sms, |
316
|
|
|
"currency" => request()->currency, |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
$this->setRequestOptions(); |
320
|
|
|
|
321
|
|
|
$this->setHttpResponse("/plan", 'POST', $data); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Fetch any plan based on its plan id or code |
326
|
|
|
* @param $plan_code |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
|
public function fetchPlan($plan_code) |
330
|
|
|
{ |
331
|
|
|
$this->setRequestOptions(); |
332
|
|
|
return $this->setHttpResponse('/plan/' . $plan_code, 'GET', [])->getResponse(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Update any plan's details based on its id or code |
337
|
|
|
* @param $plan_code |
338
|
|
|
* @return array |
339
|
|
|
*/ |
340
|
|
View Code Duplication |
public function updatePlan($plan_code) |
|
|
|
|
341
|
|
|
{ |
342
|
|
|
$data = [ |
343
|
|
|
"name" => request()->name, |
344
|
|
|
"description" => request()->desc, |
345
|
|
|
"amount" => intval(request()->amount), |
346
|
|
|
"interval" => request()->interval, |
347
|
|
|
"send_invoices" => request()->send_invoices, |
348
|
|
|
"send_sms" => request()->send_sms, |
349
|
|
|
"currency" => request()->currency, |
350
|
|
|
]; |
351
|
|
|
|
352
|
|
|
$this->setRequestOptions(); |
353
|
|
|
return $this->setHttpResponse('/plan/' . $plan_code, 'PUT', $data)->getResponse(); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Create a customer |
358
|
|
|
*/ |
359
|
|
View Code Duplication |
public function createCustomer() |
|
|
|
|
360
|
|
|
{ |
361
|
|
|
$data = [ |
362
|
|
|
"email" => request()->email, |
363
|
|
|
"first_name" => request()->fname, |
364
|
|
|
"last_name" => request()->lname, |
365
|
|
|
"phone" => request()->phone, |
366
|
|
|
"metadata" => request()->additional_info /* key => value pairs array */ |
367
|
|
|
|
368
|
|
|
]; |
369
|
|
|
|
370
|
|
|
$this->setRequestOptions(); |
371
|
|
|
return $this->setHttpResponse('/customer', 'POST', $data)->getResponse(); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Fetch a customer based on id or code |
376
|
|
|
* @param $customer_id |
377
|
|
|
* @return array |
378
|
|
|
*/ |
379
|
|
|
public function fetchCustomer($customer_id) |
380
|
|
|
{ |
381
|
|
|
$this->setRequestOptions(); |
382
|
|
|
return $this->setHttpResponse('/customer/'. $customer_id, 'GET', [])->getResponse(); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Update a customer's details based on their id or code |
387
|
|
|
* @param $customer_id |
388
|
|
|
* @return array |
389
|
|
|
*/ |
390
|
|
View Code Duplication |
public function updateCustomer($customer_id) |
|
|
|
|
391
|
|
|
{ |
392
|
|
|
$data = [ |
393
|
|
|
"email" => request()->email, |
394
|
|
|
"first_name" => request()->fname, |
395
|
|
|
"last_name" => request()->lname, |
396
|
|
|
"phone" => request()->phone, |
397
|
|
|
"metadata" => request()->additional_info /* key => value pairs array */ |
398
|
|
|
|
399
|
|
|
]; |
400
|
|
|
|
401
|
|
|
$this->setRequestOptions(); |
402
|
|
|
return $this->setHttpResponse('/customer/'. $customer_id, 'PUT', $data)->getResponse(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Export transactions in .CSV |
407
|
|
|
* @return array |
408
|
|
|
*/ |
409
|
|
|
public function exportTransactions() |
410
|
|
|
{ |
411
|
|
|
$data = [ |
412
|
|
|
"from" => request()->from, |
413
|
|
|
"to" => request()->to, |
414
|
|
|
'settled' => request()->settled |
415
|
|
|
]; |
416
|
|
|
|
417
|
|
|
$this->setRequestOptions(); |
418
|
|
|
return $this->setHttpResponse('/transaction/export', 'GET', $data)->getResponse(); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Create a subscription to a plan from a customer. |
423
|
|
|
*/ |
424
|
|
View Code Duplication |
public function createSubscription() |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
$data = [ |
427
|
|
|
"customer" => request()->customer, //Customer email or code |
428
|
|
|
"plan" => request()->plan, |
429
|
|
|
"authorization" => request()->authorization_code |
430
|
|
|
]; |
431
|
|
|
|
432
|
|
|
$this->setRequestOptions(); |
433
|
|
|
$this->setHttpResponse('/subscription', 'POST', $data); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get all the subscriptions made on Paystack. |
438
|
|
|
* |
439
|
|
|
* @return array |
440
|
|
|
*/ |
441
|
|
|
public function getAllSubscriptions() |
442
|
|
|
{ |
443
|
|
|
$this->setRequestOptions(); |
444
|
|
|
|
445
|
|
|
return $this->setHttpResponse("/subscription", 'GET', [])->getData(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Get customer subscriptions |
450
|
|
|
* |
451
|
|
|
* @param integer $customer_id |
452
|
|
|
* @return array |
453
|
|
|
*/ |
454
|
|
|
public function getCustomerSubscriptions($customer_id) |
455
|
|
|
{ |
456
|
|
|
$this->setRequestOptions(); |
457
|
|
|
|
458
|
|
|
return $this->setHttpResponse('/subscription?customer=' . $customer_id, 'GET', [])->getData(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Get plan subscriptions |
463
|
|
|
* |
464
|
|
|
* @param integer $plan_id |
465
|
|
|
* @return array |
466
|
|
|
*/ |
467
|
|
|
public function getPlanSubscriptions($plan_id) |
468
|
|
|
{ |
469
|
|
|
$this->setRequestOptions(); |
470
|
|
|
|
471
|
|
|
return $this->setHttpResponse('/subscription?plan=' . $plan_id, 'GET', [])->getData(); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Enable a subscription using the subscription code and token |
476
|
|
|
* @return array |
477
|
|
|
*/ |
478
|
|
View Code Duplication |
public function enableSubscription() |
|
|
|
|
479
|
|
|
{ |
480
|
|
|
$data = [ |
481
|
|
|
"code" => request()->code, |
482
|
|
|
"token" => request()->token, |
483
|
|
|
]; |
484
|
|
|
|
485
|
|
|
$this->setRequestOptions(); |
486
|
|
|
return $this->setHttpResponse('/subscription/enable', 'POST', $data)->getResponse(); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Disable a subscription using the subscription code and token |
491
|
|
|
* @return array |
492
|
|
|
*/ |
493
|
|
View Code Duplication |
public function disableSubscription() |
|
|
|
|
494
|
|
|
{ |
495
|
|
|
$data = [ |
496
|
|
|
"code" => request()->code, |
497
|
|
|
"token" => request()->token, |
498
|
|
|
]; |
499
|
|
|
|
500
|
|
|
$this->setRequestOptions(); |
501
|
|
|
return $this->setHttpResponse('/subscription/disable', 'POST', $data)->getResponse(); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Fetch details about a certain subscription |
506
|
|
|
* @param mixed $subscription_id |
507
|
|
|
* @return array |
508
|
|
|
*/ |
509
|
|
|
public function fetchSubscription($subscription_id) |
510
|
|
|
{ |
511
|
|
|
$this->setRequestOptions(); |
512
|
|
|
return $this->setHttpResponse('/subscription/'.$subscription_id, 'GET', [])->getResponse(); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Create pages you can share with users using the returned slug |
517
|
|
|
*/ |
518
|
|
View Code Duplication |
public function createPage() |
|
|
|
|
519
|
|
|
{ |
520
|
|
|
$data = [ |
521
|
|
|
"name" => request()->name, |
522
|
|
|
"description" => request()->description, |
523
|
|
|
"amount" => request()->amount |
524
|
|
|
]; |
525
|
|
|
|
526
|
|
|
$this->setRequestOptions(); |
527
|
|
|
$this->setHttpResponse('/page', 'POST', $data); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Fetches all the pages the merchant has |
532
|
|
|
* @return array |
533
|
|
|
*/ |
534
|
|
|
public function getAllPages() |
535
|
|
|
{ |
536
|
|
|
$this->setRequestOptions(); |
537
|
|
|
return $this->setHttpResponse('/page', 'GET', [])->getResponse(); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Fetch details about a certain page using its id or slug |
542
|
|
|
* @param mixed $page_id |
543
|
|
|
* @return array |
544
|
|
|
*/ |
545
|
|
|
public function fetchPage($page_id) |
546
|
|
|
{ |
547
|
|
|
$this->setRequestOptions(); |
548
|
|
|
return $this->setHttpResponse('/page/'.$page_id, 'GET', [])->getResponse(); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Update the details about a particular page |
553
|
|
|
* @param $page_id |
554
|
|
|
* @return array |
555
|
|
|
*/ |
556
|
|
View Code Duplication |
public function updatePage($page_id) |
|
|
|
|
557
|
|
|
{ |
558
|
|
|
$data = [ |
559
|
|
|
"name" => request()->name, |
560
|
|
|
"description" => request()->description, |
561
|
|
|
"amount" => request()->amount |
562
|
|
|
]; |
563
|
|
|
|
564
|
|
|
$this->setRequestOptions(); |
565
|
|
|
return $this->setHttpResponse('/page/'.$page_id, 'PUT', $data)->getResponse(); |
566
|
|
|
} |
567
|
|
|
} |
568
|
|
|
|
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: