-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.php
More file actions
237 lines (204 loc) · 6.01 KB
/
Transaction.php
File metadata and controls
237 lines (204 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Matscode\Paystack\Resources;
use GuzzleHttp\Exception\GuzzleException;
use Matscode\Paystack\Exceptions\InvalidArgumentException;
use Matscode\Paystack\Exceptions\JsonException;
use Matscode\Paystack\Interfaces\ResourceInterface;
use Matscode\Paystack\Traits\ResourcePath;
use Matscode\Paystack\Utility\Helpers;
use Matscode\Paystack\Utility\HTTP\HTTPClient;
use Matscode\Paystack\Utility\Text;
use stdClass;
class Transaction implements ResourceInterface
{
use ResourcePath;
protected $httpClient, $callbackUrl, $data;
/**
* @throws \Exception
*/
public function __construct(HTTPClient $HTTPClient)
{
$this->setBasePath('transaction');
$this->setReference('REF-' . ($this->data['reference'] ?? Text::uniqueStr()));
$this->httpClient = $HTTPClient;
}
/**
* This method must be called to request for payment. which return an initial transaction obj
*
* @link https://paystack.com/docs/api/#transaction-initialize
*
* @param array $data
* @return stdClass
* @throws GuzzleException
* @throws JsonException
*/
public function initialize(array $data = []): stdClass
{
$data = array_merge($this->data, $data);
// check required properties
if (!($data['email'] ?? null)
|| !(($data['amount'] ?? null)
|| ($data['plan'] ?? null))) {
throw new InvalidArgumentException('(email) and (amount | plan) are required!');
}
$data['callback_url'] = $data['callback_url'] ?? $this->callbackUrl;
$this->data = []; // empty the bag
return Helpers::JSONStringToObj($this->httpClient->post($this->makePath('initialize'), [
'json' => $data
])->getBody());
}
/**
* Is used to Check if a transaction is successful and return the transaction object data
*
* @link https://paystack.com/docs/api/#transaction-verify
*
* @param string $referenceCode
*
* @return StdClass
* @throws GuzzleException
* @throws JsonException
*/
public function verify(string $referenceCode): StdClass
{
return Helpers::JSONStringToObj($this->httpClient->get($this->makePath('verify/' . $referenceCode))->getBody());
}
/**
* Like verify(), but it only checks to see if a transactions is successful returning boolean
*
* @param string $reference
*
* @return bool
* @throws GuzzleException
* @throws JsonException
*/
public function isSuccessful(string $reference): bool
{
$response = $this->verify($reference);
$isSuccessful = false;
// check if transaction is successful
if (isset($response->data)
&& is_object($response->data)
&& $response->status
&& $response->data->status == 'success'
) {
$isSuccessful = true;
}
return $isSuccessful;
}
/**
* Add metadata to the request data
*
* Method can be called chained more than once. Last call with repeated property overwrites the former
*
* @param array $metadata
* @return $this
*/
public function addMetadata(array $metadata = []): Transaction
{
$this->data['metadata'] = array_merge($this->data['metadata'] ?? [], $metadata);
return $this;
}
/**
* Ignore setting amount when setting plan and vice versa. Plan takes precedence
*
* @param string $plan
* @return $this
*/
public function setPlan(string $plan): Transaction
{
// set amount to 0 to Invalidate amount error when setting a plan
$this->data['amount'] = 0;
$this->data['plan'] = $plan;
return $this;
}
/**
* @param string $email
*
* @return $this
*/
public function setEmail(string $email): Transaction
{
// setting the email
$this->data['email'] = $email;
return $this;
}
public function getEmail(): string
{
return $this->data['email'];
}
/**
* Set transaction amount in kobo(NGN) or pesewas(GHS)
*
* NOTE: Setting plan overwrites your defined amount
*
* @param int $amount
*
* @return $this
*/
public function setAmount(int $amount): Transaction
{
$this->data['amount'] = $amount;
return $this;
}
/**
* @return int
*/
public function getAmount(): ?int
{
return $this->data['amount'] ?? null;
}
/**
* Sets the transaction reference code/id
*
* @param string $reference
* @return Transaction
*/
public function setReference(string $reference): Transaction
{
$this->data['reference'] = $reference;
return $this;
}
/**
* Returns the transaction code/id used
*
* @param bool $afterInitialize
*
* @return null
*
*/
public function getReference(bool $afterInitialize = false)
{
return $this->data['reference'];
}
/**
* To set callback URL, can be used to override callback URL set on paystack dashboard
*
* @param string $callbackUrl
*
* @return $this
*/
public function setCallbackUrl(string $callbackUrl): Transaction
{
$this->callbackUrl = $callbackUrl;
return $this;
}
/**
* Get the list of all transactions
*
* @link https://paystack.com/docs/api/#transaction-list
*
* @param int $numberOfRecords number of record per page
* @param int $page page number
* @param array $otherOptions
* @return stdClass
* @throws GuzzleException
* @throws JsonException
*/
public function list(int $numberOfRecords = 50, int $page = 1, array $otherOptions = []): stdClass
{
return Helpers::JSONStringToObj($this->httpClient->get($this->makePath() . '?' . http_build_query($otherOptions + [
'perPage' => $numberOfRecords,
'page' => $page
]))->getBody());
}
}