9
9
10
10
namespace Matscode \Paystack \Resources ;
11
11
12
+ use GuzzleHttp \Exception \GuzzleException ;
12
13
use Matscode \Paystack \Exceptions \InvalidArgumentException ;
14
+ use Matscode \Paystack \Exceptions \JsonException ;
13
15
use Matscode \Paystack \Interfaces \ResourceInterface ;
14
16
use Matscode \Paystack \Traits \ResourcePath ;
15
17
use Matscode \Paystack \Utility \Helpers ;
16
18
use Matscode \Paystack \Utility \HTTP \HTTPClient ;
17
19
use Matscode \Paystack \Utility \Text ;
20
+ use stdClass ;
18
21
19
22
class Transaction implements ResourceInterface
20
23
{
21
24
use ResourcePath;
22
25
23
26
protected $ httpClient , $ callbackUrl , $ data ;
24
27
28
+ /**
29
+ * @throws \Exception
30
+ */
25
31
public function __construct (HTTPClient $ HTTPClient )
26
32
{
27
33
$ this ->setBasePath ('transaction ' );
@@ -36,10 +42,11 @@ public function __construct(HTTPClient $HTTPClient)
36
42
* @link https://paystack.com/docs/api/#transaction-initialize
37
43
*
38
44
* @param array $data
39
- * @return mixed|\stdClass
40
- * @throws \GuzzleHttp\Exception\GuzzleException
45
+ * @return stdClass
46
+ * @throws GuzzleException
47
+ * @throws JsonException
41
48
*/
42
- public function initialize (array $ data = []): \ stdClass
49
+ public function initialize (array $ data = []): stdClass
43
50
{
44
51
$ data = array_merge ($ this ->data , $ data );
45
52
@@ -54,9 +61,9 @@ public function initialize(array $data = []): \stdClass
54
61
55
62
$ this ->data = []; // empty the bag
56
63
57
- return Helpers::responseToObj ($ this ->httpClient ->post ($ this ->makePath ('initialize ' ), [
64
+ return Helpers::JSONStringToObj ($ this ->httpClient ->post ($ this ->makePath ('initialize ' ), [
58
65
'json ' => $ data
59
- ]));
66
+ ])-> getBody () );
60
67
}
61
68
62
69
/**
@@ -66,12 +73,13 @@ public function initialize(array $data = []): \stdClass
66
73
*
67
74
* @param string $referenceCode
68
75
*
69
- * @return \StdClass
70
- * @throws \GuzzleHttp\Exception\GuzzleException
76
+ * @return StdClass
77
+ * @throws GuzzleException
78
+ * @throws JsonException
71
79
*/
72
- public function verify (string $ referenceCode ): \ StdClass
80
+ public function verify (string $ referenceCode ): StdClass
73
81
{
74
- return Helpers::responseToObj ($ this ->httpClient ->get ($ this ->makePath ('verify/ ' . $ referenceCode )));
82
+ return Helpers::JSONStringToObj ($ this ->httpClient ->get ($ this ->makePath ('verify/ ' . $ referenceCode ))-> getBody ( ));
75
83
}
76
84
77
85
/**
@@ -80,7 +88,8 @@ public function verify(string $referenceCode): \StdClass
80
88
* @param string $reference
81
89
*
82
90
* @return bool
83
- * @throws \GuzzleHttp\Exception\GuzzleException
91
+ * @throws GuzzleException
92
+ * @throws JsonException
84
93
*/
85
94
public function isSuccessful (string $ reference ): bool
86
95
{
@@ -91,7 +100,7 @@ public function isSuccessful(string $reference): bool
91
100
// check if transaction is successful
92
101
if (isset ($ response ->data )
93
102
&& is_object ($ response ->data )
94
- && $ response ->status == true
103
+ && $ response ->status
95
104
&& $ response ->data ->status == 'success '
96
105
) {
97
106
$ isSuccessful = true ;
@@ -123,15 +132,15 @@ public function addMetadata(array $metadata = []): Transaction
123
132
*/
124
133
public function setPlan (string $ plan ): Transaction
125
134
{
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
127
136
$ this ->data ['amount ' ] = 0 ;
128
137
$ this ->data ['plan ' ] = $ plan ;
129
138
130
139
return $ this ;
131
140
}
132
141
133
142
/**
134
- * @param $email
143
+ * @param string $email
135
144
*
136
145
* @return $this
137
146
*/
@@ -193,7 +202,7 @@ public function setReference(string $reference): Transaction
193
202
* @return null
194
203
*
195
204
*/
196
- public function getReference ($ afterInitialize = false )
205
+ public function getReference (bool $ afterInitialize = false )
197
206
{
198
207
return $ this ->data ['reference ' ];
199
208
}
@@ -220,15 +229,16 @@ public function setCallbackUrl(string $callbackUrl): Transaction
220
229
* @param int $numberOfRecords number of record per page
221
230
* @param int $page page number
222
231
* @param array $otherOptions
223
- * @return \StdClass
224
- * @throws \GuzzleHttp\Exception\GuzzleException
232
+ * @return stdClass
233
+ * @throws GuzzleException
234
+ * @throws JsonException
225
235
*/
226
- public function list ($ numberOfRecords = 50 , $ page = 1 , $ otherOptions = []): \ StdClass
236
+ public function list (int $ numberOfRecords = 50 , int $ page = 1 , array $ otherOptions = []): stdClass
227
237
{
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 + [
229
239
'perPage ' => $ numberOfRecords ,
230
240
'page ' => $ page
231
- ])));
241
+ ]))-> getBody () );
232
242
}
233
243
234
- }
244
+ }
0 commit comments