14
14
use GuzzleHttp \Client ;
15
15
use Illuminate \Support \Facades \Config ;
16
16
17
- class Paystack {
18
-
17
+ class Paystack
18
+ {
19
19
/**
20
20
* Transaction Verification Successful
21
21
*/
@@ -28,13 +28,13 @@ class Paystack {
28
28
29
29
/**
30
30
* Issue Secret Key from your Paystack Dashboard
31
- * @var mixed
31
+ * @var string
32
32
*/
33
33
protected $ secretKey ;
34
34
35
35
/**
36
36
* Instance of Client
37
- * @var object
37
+ * @var Client
38
38
*/
39
39
protected $ client ;
40
40
@@ -73,7 +73,6 @@ public function setBaseUrl()
73
73
74
74
/**
75
75
* Get secret key from Paystack config file
76
- * @return void
77
76
*/
78
77
public function setKey ()
79
78
{
@@ -82,22 +81,26 @@ public function setKey()
82
81
83
82
/**
84
83
* Set options for making the Client request
85
- * @return void
86
84
*/
87
85
private function setRequestOptions ()
88
86
{
89
87
$ authBearer = 'Bearer ' . $ this ->secretKey ;
90
88
91
- $ this ->client = new Client (['base_uri ' => $ this ->baseUrl ,
92
- 'headers ' => [
93
- 'Authorization ' => $ authBearer ,
94
- 'Content-Type ' => 'application/json ' ,
95
- 'Accept ' => 'application/json '
96
- ]]);
89
+ $ this ->client = new Client (
90
+ [
91
+ 'base_uri ' => $ this ->baseUrl ,
92
+ 'headers ' => [
93
+ 'Authorization ' => $ authBearer ,
94
+ 'Content-Type ' => 'application/json ' ,
95
+ 'Accept ' => 'application/json '
96
+ ]
97
+ ]
98
+ );
97
99
}
98
100
99
101
/**
100
102
* Initiate a payment request to Paystack
103
+ * @return Paystack
101
104
*/
102
105
public function makePaymentRequest ()
103
106
{
@@ -110,7 +113,7 @@ public function makePaymentRequest()
110
113
"last_name " => request ()->last_name ,
111
114
"callback_url " => request ()->callback_url
112
115
];
113
-
116
+
114
117
// Remove the fields which were not sent (value would be null)
115
118
array_filter ($ data );
116
119
@@ -120,18 +123,30 @@ public function makePaymentRequest()
120
123
}
121
124
122
125
126
+ /**
127
+ * @param string $relativeUrl
128
+ * @param string $method
129
+ * @param array $body
130
+ * @return Paystack
131
+ * @throws IsNullException
132
+ */
123
133
private function setHttpResponse ($ relativeUrl , $ method , $ body = [])
124
134
{
125
- if (is_null ($ method )){
135
+ if (is_null ($ method )) {
126
136
throw new IsNullException ("Empty method not allowed " );
127
137
}
128
138
129
- $ this ->response = $ this ->client ->{strtolower ($ method )}($ this ->baseUrl . $ relativeUrl , ["body " => json_encode ($ body )]);
139
+ $ this ->response = $ this ->client ->{strtolower ($ method )}(
140
+ $ this ->baseUrl . $ relativeUrl ,
141
+ ["body " => json_encode ($ body )]
142
+ );
143
+
130
144
return $ this ;
131
145
}
132
146
133
147
/**
134
148
* Get the authorization url from the callback response
149
+ * @return Paystack
135
150
*/
136
151
public function getAuthorizationUrl ()
137
152
{
@@ -144,7 +159,6 @@ public function getAuthorizationUrl()
144
159
145
160
/**
146
161
* Hit Paystack Gateway to Verify that the transaction is valid
147
- * @return void
148
162
*/
149
163
private function verifyTransactionAtGateway ()
150
164
{
@@ -165,8 +179,7 @@ public function isTransactionVerificationValid()
165
179
166
180
$ result = $ this ->getResponse ()['message ' ];
167
181
168
- switch ($ result )
169
- {
182
+ switch ($ result ) {
170
183
case self ::VS :
171
184
$ validate = true ;
172
185
break ;
@@ -275,8 +288,8 @@ private function getData()
275
288
/**
276
289
* Create a plan
277
290
*/
278
- public function createPlan (){
279
-
291
+ public function createPlan ()
292
+ {
280
293
$ data = [
281
294
"name " => request ()->name ,
282
295
"description " => request ()->desc ,
@@ -298,7 +311,8 @@ public function createPlan(){
298
311
* @param $plan_code
299
312
* @return array
300
313
*/
301
- public function fetchPlan ($ plan_code ){
314
+ public function fetchPlan ($ plan_code )
315
+ {
302
316
$ this ->setRequestOptions ();
303
317
return $ this ->setHttpResponse ('/plan/ ' . $ plan_code , 'GET ' , [])->getResponse ();
304
318
}
@@ -308,7 +322,8 @@ public function fetchPlan($plan_code){
308
322
* @param $plan_code
309
323
* @return array
310
324
*/
311
- public function updatePlan ($ plan_code ){
325
+ public function updatePlan ($ plan_code )
326
+ {
312
327
$ data = [
313
328
"name " => request ()->name ,
314
329
"description " => request ()->desc ,
@@ -325,9 +340,9 @@ public function updatePlan($plan_code){
325
340
326
341
/**
327
342
* Create a customer
328
- * @return array
329
343
*/
330
- public function createCustomer (){
344
+ public function createCustomer ()
345
+ {
331
346
$ data = [
332
347
"email " => request ()->email ,
333
348
"first_name " => request ()->fname ,
@@ -357,7 +372,8 @@ public function fetchCustomer($customer_id)
357
372
* @param $customer_id
358
373
* @return array
359
374
*/
360
- public function updateCustomer ($ customer_id ){
375
+ public function updateCustomer ($ customer_id )
376
+ {
361
377
$ data = [
362
378
"email " => request ()->email ,
363
379
"first_name " => request ()->fname ,
@@ -372,10 +388,11 @@ public function updateCustomer($customer_id){
372
388
}
373
389
374
390
/**
375
- * Export tranactions in .CSV
391
+ * Export transactions in .CSV
376
392
* @return array
377
393
*/
378
- public function exportTransactions (){
394
+ public function exportTransactions ()
395
+ {
379
396
$ data = [
380
397
"from " => request ()->from ,
381
398
"to " => request ()->to ,
@@ -388,9 +405,9 @@ public function exportTransactions(){
388
405
389
406
/**
390
407
* Create a subscription to a plan from a customer.
391
- * @return array
392
408
*/
393
- public function createSubscription (){
409
+ public function createSubscription ()
410
+ {
394
411
$ data = [
395
412
"customer " => request ()->customer , //Customer email or code
396
413
"plan " => request ()->plan ,
@@ -405,7 +422,8 @@ public function createSubscription(){
405
422
* Enable a subscription using the subscription code and token
406
423
* @return array
407
424
*/
408
- public function enableSubscription (){
425
+ public function enableSubscription ()
426
+ {
409
427
$ data = [
410
428
"code " => request ()->code ,
411
429
"token " => request ()->token ,
@@ -419,7 +437,8 @@ public function enableSubscription(){
419
437
* Disable a subscription using the subscription code and token
420
438
* @return array
421
439
*/
422
- public function disableSubscription (){
440
+ public function disableSubscription ()
441
+ {
423
442
$ data = [
424
443
"code " => request ()->code ,
425
444
"token " => request ()->token ,
@@ -431,7 +450,7 @@ public function disableSubscription(){
431
450
432
451
/**
433
452
* Fetch details about a certain subscription
434
- * @param $subscription_id
453
+ * @param mixed $subscription_id
435
454
* @return array
436
455
*/
437
456
public function fetchSubscription ($ subscription_id )
@@ -442,9 +461,9 @@ public function fetchSubscription($subscription_id)
442
461
443
462
/**
444
463
* Create pages you can share with users using the returned slug
445
- * @return array
446
464
*/
447
- public function createPage (){
465
+ public function createPage ()
466
+ {
448
467
$ data = [
449
468
"name " => request ()->name ,
450
469
"description " => request ()->description ,
@@ -467,7 +486,7 @@ public function getAllPages()
467
486
468
487
/**
469
488
* Fetch details about a certain page using its id or slug
470
- * @param $page_id
489
+ * @param mixed $page_id
471
490
* @return array
472
491
*/
473
492
public function fetchPage ($ page_id )
@@ -481,7 +500,8 @@ public function fetchPage($page_id)
481
500
* @param $page_id
482
501
* @return array
483
502
*/
484
- public function updatePage ($ page_id ){
503
+ public function updatePage ($ page_id )
504
+ {
485
505
$ data = [
486
506
"name " => request ()->name ,
487
507
"description " => request ()->description ,
@@ -491,8 +511,4 @@ public function updatePage($page_id){
491
511
$ this ->setRequestOptions ();
492
512
return $ this ->setHttpResponse ('/page/ ' .$ page_id , 'PUT ' , $ data )->getResponse ();
493
513
}
494
-
495
514
}
496
-
497
-
498
-
0 commit comments