Repository navigation
Expand file tree
/
Copy pathPaystackMirror.php
More file actions
318 lines (267 loc) Β· 7.96 KB
/
Copy pathPaystackMirror.php
File metadata and controls
318 lines (267 loc) Β· 7.96 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Coderatio\PaystackMirror;
use Coderatio\PaystackMirror\Actions\Action;
use Coderatio\PaystackMirror\Exceptions\PaystackMirrorException;
use Coderatio\PaystackMirror\Services\CurlHttpResponseService;
use Coderatio\PaystackMirror\Services\ParamsBuilder;
use Coderatio\PaystackMirror\Services\RequestHandlerService;
use DateTime;
use Exception;
class PaystackMirror
{
/**
* Holds the response from an action
* @var $response
*/
protected static $response;
/**
* Holds Paystack secret key
* @var $secretKey
*/
protected static $secretKey;
/**
* Holds details for multiple Paystack account mirroring
* @var array $accountsDetails
*/
protected static $accountsDetails = [];
/**
* PaystackMirror constructor.
* @param null|string $secretKey
*/
public function __construct($secretKey = null)
{
if ($secretKey !== null) {
static::$secretKey = $secretKey;
}
}
/**
* Start mirrowing. Paystack account secret key must be provided.
*
* @param $secretKey
* @return PaystackMirror
*/
public static function boot($secretKey): PaystackMirror
{
static::$secretKey = $secretKey;
return new self();
}
/**
* Mirror any paystack api action. Paystack account secret key
* and action to mirror must be provided.
*
* @param $secretKey
* @param mixed $action
* @param array $data
* @return PaystackMirror
* @throws PaystackMirrorException
*/
public static function run($secretKey, $action, $data = []): PaystackMirror
{
static::$secretKey = $secretKey;
static::abortIfNotValidAction($action);
static::$response = RequestHandlerService::handle(
$secretKey, static::parsedAction($action, $data)
);
return new self();
}
/**
* Run Paystack Mirror on multiple accounts
*
* @param array $accountsDetails
* @param $action
* @param array $parentData
* @return array
*/
public static function runMultipleAccounts(array $accountsDetails, $action, $parentData = []): array
{
if ($accountsDetails instanceof ParamsBuilder) {
$accountsDetails = $accountsDetails->get();
}
return array_map(function ($accountDetails) use ($action, $parentData) {
if ($accountDetails instanceof ParamsBuilder) {
$accountDetails = $accountDetails->get();
}
if (isset($accountDetails['key']) || isset($accountDetails['secret'])) {
$data = $accountDetails['data'] ?? [];
if (!empty($data) && $data instanceof ParamsBuilder) {
$data = $data->get();
}
$key = $accountDetails['key'] ?? $accountDetails['secret'] ?? '';
if (!empty($parentData)) {
if ($parentData instanceof ParamsBuilder) {
$parentData = $parentData->get();
}
$data = $parentData;
}
$request = static::run($key, static::parsedAction($action, $data), $data);
return (object)[
'account' => $key,
'response' => $request->getResponse()
];
}
return (object)[];
}, $accountsDetails);
}
/**
* Set Paystack secret key
*
* @param $secretKey
* @return PaystackMirror
*/
public static function setKey($secretKey): PaystackMirror
{
static::$secretKey = $secretKey;
return new self();
}
/**
* Set multiple Paystack accounts secret keys
*
* @param $accountsDetails
* @return PaystackMirror
*/
public static function setAccounts($accountsDetails): PaystackMirror
{
static::$accountsDetails = $accountsDetails;
return new self();
}
/**
* This function works as the run method but takes
* only an action to mirror.
*
* @param mixed $action
* @param array $data
* @return PaystackMirror
* @throws PaystackMirrorException
*/
public function mirror($action, $data = []): PaystackMirror
{
return static::run(static::$secretKey, $action, $data);
}
/**
* Mirror Paystack mirror on multiple accounts.
*
* @param $action
* @param array $data
* @return array
*/
public function mirrorMultipleAccountsOn($action, $data = []): array
{
return static::runMultipleAccounts(static::$accountsDetails, $action, $data);
}
/**
* This function returns the response from an action
*
* @return CurlHttpResponseService
*/
public function getResponse() : CurlHttpResponseService
{
return static::$response;
}
/**
* Converts Nigerian Naira to Kobo Coin
*
* @param mixed $nairaAmount
* @return int
*/
public static function nairaToKobo($nairaAmount): int
{
return static::getCleanedAmount($nairaAmount) * 100;
}
/**
* Converts Nigerian Kobo Coin to Naira
*
* @param mixed $koboAmount
* @return int
*/
public static function koboToNaira($koboAmount): int
{
return static::getCleanedAmount($koboAmount) / 100;
}
/**
* Generates a random reference.
*
* @return string|null
*/
public static function generateReference(): ?string
{
return mb_strtolower('PM_' . crypt(str_shuffle('PAYSTACK_MIRROR_REF'), 'rf'));
}
/**
* Creates a simple ISO 8601 dates.
*
* @param string $date
* @return string
* @throws Exception
*/
public static function createISO8601Date($date = ''): string
{
if (empty($date)) {
$date = date('Y-m-d h:i:s');
}
return (new DateTime($date))->format(DateTime::ATOM);
}
/**
* @param $shortNaira
* @return float|int
* @throws PaystackMirrorException
*/
public static function shortNairaToKobo($shortNaira)
{
$moneyNotations = [
'k' => 100000,
'm' => 100000000,
'b' => 100000000000,
't' => 100000000000000,
];
foreach($moneyNotations as $shortNotation => $conversionRate) {
$shortNaira = str_replace([',', '/', '|', ':', ';', ' '], '', trim($shortNaira));
$explodedData = explode($shortNotation, mb_strtolower($shortNaira));
if (isset($explodedData[1])) {
return ($conversionRate * $explodedData[0]);
}
}
throw new PaystackMirrorException('Invalid format provided: You should enter something like 1K or 1M or 1B or 1T.');
}
/**
* Convert action to object if $action::class used and
* pass in data provided.
*
* @param $action
* @param $data
* @return Action $action
*/
protected static function parsedAction($action, $data): Action
{
if ($data instanceof ParamsBuilder) {
$data = $data->get();
}
if (is_string($action)) {
$action = new $action($data);
}
if (!empty($data)) {
new $action($data);
}
return $action;
}
/**
* Stop execution if the action is not valid
*
* @param $action
* @return PaystackMirror
* @throws PaystackMirrorException
*/
protected static function abortIfNotValidAction($action): PaystackMirror
{
if (!(new $action() ?? !is_string($action)) instanceof Action) {
$actionClassName = get_class($action);
throw new PaystackMirrorException(
"[{$actionClassName}], must be an instance of " . Action::class
);
}
return new self();
}
private static function getCleanedAmount($amount)
{
return str_replace([',', '.', '/', '|', ':', ';', ' '], '', trim($amount));
}
}