Skip to content

Commit 9e86129

Browse files
committed
update library
1 parent 1bef93d commit 9e86129

File tree

8 files changed

+56
-127
lines changed

8 files changed

+56
-127
lines changed

examples/.gitignore

Whitespace-only changes.

src/ResourceRegistry.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
/**
33
* @ignore
44
*/
5+
56
namespace Matscode\Paystack;
67

7-
class ResourceRegistry {
8-
public static $registry = [
9-
'transaction' => \Matscode\Paystack\Resources\Transaction::class
8+
use Matscode\Paystack\Resources;
9+
10+
class ResourceRegistry
11+
{
12+
public static array $registry = [
13+
'transaction' => Resources\Transaction::class,
14+
'bank' => Resources\Bank::class,
1015
];
11-
}
16+
}

src/Resources/Transaction.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@
99

1010
namespace Matscode\Paystack\Resources;
1111

12+
use GuzzleHttp\Exception\GuzzleException;
1213
use Matscode\Paystack\Exceptions\InvalidArgumentException;
14+
use Matscode\Paystack\Exceptions\JsonException;
1315
use Matscode\Paystack\Interfaces\ResourceInterface;
1416
use Matscode\Paystack\Traits\ResourcePath;
1517
use Matscode\Paystack\Utility\Helpers;
1618
use Matscode\Paystack\Utility\HTTP\HTTPClient;
1719
use Matscode\Paystack\Utility\Text;
20+
use stdClass;
1821

1922
class Transaction implements ResourceInterface
2023
{
2124
use ResourcePath;
2225

2326
protected $httpClient, $callbackUrl, $data;
2427

28+
/**
29+
* @throws \Exception
30+
*/
2531
public function __construct(HTTPClient $HTTPClient)
2632
{
2733
$this->setBasePath('transaction');
@@ -36,10 +42,11 @@ public function __construct(HTTPClient $HTTPClient)
3642
* @link https://paystack.com/docs/api/#transaction-initialize
3743
*
3844
* @param array $data
39-
* @return mixed|\stdClass
40-
* @throws \GuzzleHttp\Exception\GuzzleException
45+
* @return stdClass
46+
* @throws GuzzleException
47+
* @throws JsonException
4148
*/
42-
public function initialize(array $data = []): \stdClass
49+
public function initialize(array $data = []): stdClass
4350
{
4451
$data = array_merge($this->data, $data);
4552

@@ -54,9 +61,9 @@ public function initialize(array $data = []): \stdClass
5461

5562
$this->data = []; // empty the bag
5663

57-
return Helpers::responseToObj($this->httpClient->post($this->makePath('initialize'), [
64+
return Helpers::JSONStringToObj($this->httpClient->post($this->makePath('initialize'), [
5865
'json' => $data
59-
]));
66+
])->getBody());
6067
}
6168

6269
/**
@@ -66,12 +73,13 @@ public function initialize(array $data = []): \stdClass
6673
*
6774
* @param string $referenceCode
6875
*
69-
* @return \StdClass
70-
* @throws \GuzzleHttp\Exception\GuzzleException
76+
* @return StdClass
77+
* @throws GuzzleException
78+
* @throws JsonException
7179
*/
72-
public function verify(string $referenceCode): \StdClass
80+
public function verify(string $referenceCode): StdClass
7381
{
74-
return Helpers::responseToObj($this->httpClient->get($this->makePath('verify/' . $referenceCode)));
82+
return Helpers::JSONStringToObj($this->httpClient->get($this->makePath('verify/' . $referenceCode))->getBody());
7583
}
7684

7785
/**
@@ -80,7 +88,8 @@ public function verify(string $referenceCode): \StdClass
8088
* @param string $reference
8189
*
8290
* @return bool
83-
* @throws \GuzzleHttp\Exception\GuzzleException
91+
* @throws GuzzleException
92+
* @throws JsonException
8493
*/
8594
public function isSuccessful(string $reference): bool
8695
{
@@ -91,7 +100,7 @@ public function isSuccessful(string $reference): bool
91100
// check if transaction is successful
92101
if (isset($response->data)
93102
&& is_object($response->data)
94-
&& $response->status == true
103+
&& $response->status
95104
&& $response->data->status == 'success'
96105
) {
97106
$isSuccessful = true;
@@ -123,15 +132,15 @@ public function addMetadata(array $metadata = []): Transaction
123132
*/
124133
public function setPlan(string $plan): Transaction
125134
{
126-
// set amount to 0 to Invalid amount error when setting a plan
135+
// set amount to 0 to Invalidate amount error when setting a plan
127136
$this->data['amount'] = 0;
128137
$this->data['plan'] = $plan;
129138

130139
return $this;
131140
}
132141

133142
/**
134-
* @param $email
143+
* @param string $email
135144
*
136145
* @return $this
137146
*/
@@ -193,7 +202,7 @@ public function setReference(string $reference): Transaction
193202
* @return null
194203
*
195204
*/
196-
public function getReference($afterInitialize = false)
205+
public function getReference(bool $afterInitialize = false)
197206
{
198207
return $this->data['reference'];
199208
}
@@ -220,15 +229,16 @@ public function setCallbackUrl(string $callbackUrl): Transaction
220229
* @param int $numberOfRecords number of record per page
221230
* @param int $page page number
222231
* @param array $otherOptions
223-
* @return \StdClass
224-
* @throws \GuzzleHttp\Exception\GuzzleException
232+
* @return stdClass
233+
* @throws GuzzleException
234+
* @throws JsonException
225235
*/
226-
public function list($numberOfRecords = 50, $page = 1, $otherOptions = []): \StdClass
236+
public function list(int $numberOfRecords = 50, int $page = 1, array $otherOptions = []): stdClass
227237
{
228-
return Helpers::responseToObj($this->httpClient->get($this->makePath() . '?' . http_build_query($otherOptions + [
238+
return Helpers::JSONStringToObj($this->httpClient->get($this->makePath() . '?' . http_build_query($otherOptions + [
229239
'perPage' => $numberOfRecords,
230240
'page' => $page
231-
])));
241+
]))->getBody());
232242
}
233243

234-
}
244+
}

src/Utility/Debug.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Utility/HTTP/HTTPClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class HTTPClient extends Client
1010
{
1111

12-
private $apiBaseUrl = 'https://api.paystack.co/'; // with trailing slash
12+
private string $apiBaseUrl = 'https://api.paystack.co/'; // with trailing slash
1313

1414
public function __construct(string $secretKey)
1515
{
@@ -24,6 +24,6 @@ public function __construct(string $secretKey)
2424

2525
public function requestFactory(array $messageComponent)
2626
{
27-
// TODO: Validate the need for this function before implementing...
27+
// TODO: Justify the need for this function before implementing...
2828
}
29-
}
29+
}

src/Utility/Helpers.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,32 @@
66
namespace Matscode\Paystack\Utility;
77

88
use Matscode\Paystack\Exceptions\JsonException;
9-
use Psr\Http\Message\ResponseInterface;
109

1110
final class Helpers
1211
{
13-
/**
14-
* Convert PSR7 Stream to string
15-
*
16-
* @param $stream
17-
* @return string
18-
*/
19-
public static function streamToString($stream): string
20-
{
21-
return (string)$stream;
22-
}
23-
2412
/**
2513
* Parse response PSR7 stream to Obj
2614
*
27-
* @param $stream
15+
* @param $jsonString
2816
* @return \StdClass
2917
* @throws JsonException
3018
*/
31-
public static function JSONStreamToObj($stream): \StdClass
19+
public static function JSONStringToObj($jsonString): \StdClass
3220
{
33-
return self::parseJSON(self::streamToString($stream));
21+
return self::parseJSON((string)$jsonString);
3422
}
3523

3624
/**
3725
* Parse JSON string to Object
3826
*
3927
* @param string $string Valid JSON string to parse
40-
* @param bool $asObject Parses JSON string as StdClass Object by default, set to false to parse as associate array
28+
* @param bool $asObject Parses JSON string as StdClass Object by default, set to false if you want to parse as associate array
4129
*
42-
* @return \StdClass
4330
* @throws JsonException
4431
*/
45-
public static function parseJSON(string $string, bool $asObject = true): \StdClass
32+
public static function parseJSON(string $string, bool $asObject = true): \stdClass
4633
{
47-
if(!$string){
34+
if (!$string) {
4835
return json_decode('{}', !$asObject, 4);
4936
}
5037
// limit json string parse depth
@@ -86,15 +73,4 @@ public static function parseJSON(string $string, bool $asObject = true): \StdCla
8673

8774
return $decodedJson;
8875
}
89-
90-
/**
91-
* Parse PSR7 Response to Obj
92-
*
93-
* @param ResponseInterface $response
94-
* @return \StdClass
95-
*/
96-
public static function responseToObj(ResponseInterface $response): \StdClass
97-
{
98-
return self::JSONStreamToObj($response->getBody());
99-
}
100-
}
76+
}

src/Utility/Http.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Utility/Text.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
class Text
1414
{
1515
/**
16-
*
17-
* @author Hackan <hackan@gmail.com>
18-
* @link https://php.net/manual/en/function.uniqid.php#120123
1916
*
2017
* @param int $length
2118
*
2219
* @param int $capsMix
2320
*
24-
* @return bool|string
21+
* @return string
2522
* @throws \Exception
23+
*@author Hackan <hackan@gmail.com>
24+
* @link https://php.net/manual/en/function.uniqid.php#120123
25+
*
2626
*/
27-
public static function uniqueStr($length = 15, $capsMix = 5 )
28-
{
27+
public static function uniqueStr(int $length = 11, int $capsMix = 5 ): string
28+
{
2929
// uniqid gives 15 chars, but you could adjust it to your needs.
3030
if ( function_exists( "random_bytes" ) ) {
3131
$bytes = random_bytes( ceil( $length / 2 ) );
@@ -52,4 +52,4 @@ public static function removeSlashes(string $string ): string
5252
{
5353
return trim( $string, '/' );
5454
}
55-
}
55+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy