1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Laravel Paystack package. |
5
|
|
|
* |
6
|
|
|
* (c) Prosper Otemuyiwa <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unicodeveloper\Paystack; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use Illuminate\Support\Facades\Config; |
16
|
|
|
use Unicodeveloper\Paystack\Exceptions\IsNullException; |
17
|
|
|
use Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException; |
18
|
|
|
|
19
|
|
|
class Paystack |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Transaction Verification Successful |
23
|
|
|
*/ |
24
|
|
|
const VS = 'Verification successful'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Invalid Transaction reference |
28
|
|
|
*/ |
29
|
|
|
const ITF = "Invalid transaction reference"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Issue Secret Key from your Paystack Dashboard |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $secretKey; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Instance of Client |
39
|
|
|
* @var Client |
40
|
|
|
*/ |
41
|
|
|
protected $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Response from requests made to Paystack |
45
|
|
|
* @var mixed |
46
|
|
|
*/ |
47
|
|
|
protected $response; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Paystack API base Url |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $baseUrl; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Authorization Url - Paystack payment page |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $authorizationUrl; |
60
|
|
|
|
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
$this->setKey(); |
64
|
|
|
$this->setBaseUrl(); |
65
|
|
|
$this->setRequestOptions(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Base Url from Paystack config file |
70
|
|
|
*/ |
71
|
|
|
public function setBaseUrl() |
72
|
|
|
{ |
73
|
|
|
$this->baseUrl = Config::get('paystack.paymentUrl'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get secret key from Paystack config file |
78
|
|
|
*/ |
79
|
|
|
public function setKey() |
80
|
|
|
{ |
81
|
|
|
$this->secretKey = Config::get('paystack.secretKey'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set options for making the Client request |
86
|
|
|
*/ |
87
|
|
|
private function setRequestOptions() |
88
|
|
|
{ |
89
|
|
|
$authBearer = 'Bearer '. $this->secretKey; |
90
|
|
|
|
91
|
|
|
$this->client = new Client( |
92
|
|
|
[ |
93
|
|
|
'base_uri' => $this->baseUrl, |
94
|
|
|
'headers' => [ |
95
|
|
|
'Authorization' => $authBearer, |
96
|
|
|
'Content-Type' => 'application/json', |
97
|
|
|
'Accept' => 'application/json' |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
|
106
|
|
|
* Initiate a payment request to Paystack |
107
|
|
|
* Included the option to pass the payload to this method for situations |
108
|
|
|
* when the payload is built on the fly (not passed to the controller from a view) |
109
|
|
|
* @return Paystack |
110
|
|
|
*/ |
111
|
|
|
|
112
|
|
|
public function makePaymentRequest( $data = null) |
113
|
|
|
{ |
114
|
|
|
if ( $data == null ) { |
115
|
|
|
|
116
|
|
|
$quantity = intval(request()->quantity ?? 1); |
117
|
|
|
|
118
|
|
|
$data = array_filter([ |
119
|
|
|
"amount" => intval(request()->amount) * $quantity, |
120
|
|
|
"reference" => request()->reference, |
121
|
|
|
"email" => request()->email, |
122
|
|
|
"plan" => request()->plan, |
123
|
|
|
"first_name" => request()->first_name, |
124
|
|
|
"last_name" => request()->last_name, |
125
|
|
|
"callback_url" => request()->callback_url, |
126
|
|
|
"currency" => (request()->currency != "" ? request()->currency : "NGN"), |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
Paystack allows for transactions to be split into a subaccount - |
130
|
|
|
The following lines trap the subaccount ID - as well as the ammount to charge the subaccount (if overriden in the form) |
131
|
|
|
both values need to be entered within hidden input fields |
132
|
|
|
*/ |
133
|
|
|
"subaccount" => request()->subaccount, |
134
|
|
|
"transaction_charge" => request()->transaction_charge, |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Paystack allows for transaction to be split into multi accounts(subaccounts) |
138
|
|
|
* The following lines trap the split ID handling the split |
139
|
|
|
* More details here: https://paystack.com/docs/payments/multi-split-payments/#using-transaction-splits-with-payments |
140
|
|
|
*/ |
141
|
|
|
"split_code" => request()->split_code, |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Paystack allows transaction to be split into multi account(subaccounts) on the fly without predefined split |
145
|
|
|
* form need an input field: <input type="hidden" name="split" value="{{ json_encode($split) }}" > |
146
|
|
|
* array must be set up as: |
147
|
|
|
* $split = [ |
148
|
|
|
* "type" => "percentage", |
149
|
|
|
* "currency" => "KES", |
150
|
|
|
* "subaccounts" => [ |
151
|
|
|
* { "subaccount" => "ACCT_li4p6kte2dolodo", "share" => 10 }, |
152
|
|
|
* { "subaccount" => "ACCT_li4p6kte2dolodo", "share" => 30 }, |
153
|
|
|
* ], |
154
|
|
|
* "bearer_type" => "all", |
155
|
|
|
* "main_account_share" => 70, |
156
|
|
|
* ] |
157
|
|
|
* More details here: https://paystack.com/docs/payments/multi-split-payments/#dynamic-splits |
158
|
|
|
*/ |
159
|
|
|
"split" => request()->split, |
160
|
|
|
/* |
161
|
|
|
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url |
162
|
|
|
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" > |
163
|
|
|
* array must be set up as: |
164
|
|
|
* $array = [ 'custom_fields' => [ |
165
|
|
|
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"], |
166
|
|
|
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"], |
167
|
|
|
* . |
168
|
|
|
* . |
169
|
|
|
* . |
170
|
|
|
* ] |
171
|
|
|
* ] |
172
|
|
|
*/ |
173
|
|
|
'metadata' => request()->metadata |
174
|
|
|
]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->setHttpResponse('/transaction/initialize', 'POST', $data); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $relativeUrl |
185
|
|
|
* @param string $method |
186
|
|
|
* @param array $body |
187
|
|
|
* @return Paystack |
188
|
|
|
* @throws IsNullException |
189
|
|
|
*/ |
190
|
|
|
private function setHttpResponse($relativeUrl, $method, $body = []) |
191
|
|
|
{ |
192
|
|
|
if (is_null($method)) { |
193
|
|
|
throw new IsNullException("Empty method not allowed"); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$this->response = $this->client->{strtolower($method)}( |
197
|
|
|
$this->baseUrl . $relativeUrl, |
198
|
|
|
["body" => json_encode($body)] |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the authorization url from the callback response |
206
|
|
|
* @return Paystack |
207
|
|
|
*/ |
208
|
|
|
public function getAuthorizationUrl() |
209
|
|
|
{ |
210
|
|
|
$this->makePaymentRequest(); |
211
|
|
|
|
212
|
|
|
$this->url = $this->getResponse()['data']['authorization_url']; |
|
|
|
|
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the authorization callback response |
219
|
|
|
* In situations where Laravel serves as an backend for a detached UI, the api cannot redirect |
220
|
|
|
* and might need to take different actions based on the success or not of the transaction |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getAuthorizationResponse($data) |
224
|
|
|
{ |
225
|
|
|
$this->makePaymentRequest($data); |
226
|
|
|
|
227
|
|
|
$this->url = $this->getResponse()['data']['authorization_url']; |
228
|
|
|
|
229
|
|
|
return $this->getResponse(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Hit Paystack Gateway to Verify that the transaction is valid |
234
|
|
|
*/ |
235
|
|
|
private function verifyTransactionAtGateway() |
236
|
|
|
{ |
237
|
|
|
$transactionRef = request()->query('trxref'); |
238
|
|
|
|
239
|
|
|
$relativeUrl = "/transaction/verify/{$transactionRef}"; |
240
|
|
|
|
241
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* True or false condition whether the transaction is verified |
246
|
|
|
* @return boolean |
247
|
|
|
*/ |
248
|
|
|
public function isTransactionVerificationValid() |
249
|
|
|
{ |
250
|
|
|
$this->verifyTransactionAtGateway(); |
251
|
|
|
|
252
|
|
|
$result = $this->getResponse()['message']; |
253
|
|
|
|
254
|
|
|
switch ($result) { |
255
|
|
|
case self::VS: |
256
|
|
|
$validate = true; |
257
|
|
|
break; |
258
|
|
|
case self::ITF: |
259
|
|
|
$validate = false; |
260
|
|
|
break; |
261
|
|
|
default: |
262
|
|
|
$validate = false; |
263
|
|
|
break; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $validate; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get Payment details if the transaction was verified successfully |
271
|
|
|
* @return json |
272
|
|
|
* @throws PaymentVerificationFailedException |
273
|
|
|
*/ |
274
|
|
|
public function getPaymentData() |
275
|
|
|
{ |
276
|
|
|
if ($this->isTransactionVerificationValid()) { |
277
|
|
|
return $this->getResponse(); |
278
|
|
|
} else { |
279
|
|
|
throw new PaymentVerificationFailedException("Invalid Transaction Reference"); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Fluent method to redirect to Paystack Payment Page |
285
|
|
|
*/ |
286
|
|
|
public function redirectNow() |
287
|
|
|
{ |
288
|
|
|
return redirect($this->url); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get Access code from transaction callback respose |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function getAccessCode() |
296
|
|
|
{ |
297
|
|
|
return $this->getResponse()['data']['access_code']; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Generate a Unique Transaction Reference |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
|
|
public function genTranxRef() |
305
|
|
|
{ |
306
|
|
|
return TransRef::getHashedToken(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get all the customers that have made transactions on your platform |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
public function getAllCustomers() |
314
|
|
|
{ |
315
|
|
|
$this->setRequestOptions(); |
316
|
|
|
|
317
|
|
|
return $this->setHttpResponse("/customer", 'GET', [])->getData(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get all the plans that you have on Paystack |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
public function getAllPlans() |
325
|
|
|
{ |
326
|
|
|
$this->setRequestOptions(); |
327
|
|
|
|
328
|
|
|
return $this->setHttpResponse("/plan", 'GET', [])->getData(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get all the transactions that have happened overtime |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
|
|
public function getAllTransactions() |
336
|
|
|
{ |
337
|
|
|
$this->setRequestOptions(); |
338
|
|
|
|
339
|
|
|
return $this->setHttpResponse("/transaction", 'GET', [])->getData(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get the whole response from a get operation |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
private function getResponse() |
347
|
|
|
{ |
348
|
|
|
return json_decode($this->response->getBody(), true); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Get the data response from a get operation |
353
|
|
|
* @return array |
354
|
|
|
*/ |
355
|
|
|
private function getData() |
356
|
|
|
{ |
357
|
|
|
return $this->getResponse()['data']; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Create a plan |
362
|
|
|
*/ |
363
|
|
View Code Duplication |
public function createPlan() |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
$data = [ |
366
|
|
|
"name" => request()->name, |
367
|
|
|
"description" => request()->desc, |
368
|
|
|
"amount" => intval(request()->amount), |
369
|
|
|
"interval" => request()->interval, |
370
|
|
|
"send_invoices" => request()->send_invoices, |
371
|
|
|
"send_sms" => request()->send_sms, |
372
|
|
|
"currency" => request()->currency, |
373
|
|
|
]; |
374
|
|
|
|
375
|
|
|
$this->setRequestOptions(); |
376
|
|
|
|
377
|
|
|
return $this->setHttpResponse("/plan", 'POST', $data)->getResponse(); |
378
|
|
|
|
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Fetch any plan based on its plan id or code |
383
|
|
|
* @param $plan_code |
384
|
|
|
* @return array |
385
|
|
|
*/ |
386
|
|
|
public function fetchPlan($plan_code) |
387
|
|
|
{ |
388
|
|
|
$this->setRequestOptions(); |
389
|
|
|
return $this->setHttpResponse('/plan/' . $plan_code, 'GET', [])->getResponse(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Update any plan's details based on its id or code |
394
|
|
|
* @param $plan_code |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
|
View Code Duplication |
public function updatePlan($plan_code) |
|
|
|
|
398
|
|
|
{ |
399
|
|
|
$data = [ |
400
|
|
|
"name" => request()->name, |
401
|
|
|
"description" => request()->desc, |
402
|
|
|
"amount" => intval(request()->amount), |
403
|
|
|
"interval" => request()->interval, |
404
|
|
|
"send_invoices" => request()->send_invoices, |
405
|
|
|
"send_sms" => request()->send_sms, |
406
|
|
|
"currency" => request()->currency, |
407
|
|
|
]; |
408
|
|
|
|
409
|
|
|
$this->setRequestOptions(); |
410
|
|
|
return $this->setHttpResponse('/plan/' . $plan_code, 'PUT', $data)->getResponse(); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Create a customer |
415
|
|
|
*/ |
416
|
|
View Code Duplication |
public function createCustomer() |
|
|
|
|
417
|
|
|
{ |
418
|
|
|
$data = [ |
419
|
|
|
"email" => request()->email, |
420
|
|
|
"first_name" => request()->fname, |
421
|
|
|
"last_name" => request()->lname, |
422
|
|
|
"phone" => request()->phone, |
423
|
|
|
"metadata" => request()->additional_info /* key => value pairs array */ |
424
|
|
|
|
425
|
|
|
]; |
426
|
|
|
|
427
|
|
|
$this->setRequestOptions(); |
428
|
|
|
return $this->setHttpResponse('/customer', 'POST', $data)->getResponse(); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Fetch a customer based on id or code |
433
|
|
|
* @param $customer_id |
434
|
|
|
* @return array |
435
|
|
|
*/ |
436
|
|
|
public function fetchCustomer($customer_id) |
437
|
|
|
{ |
438
|
|
|
$this->setRequestOptions(); |
439
|
|
|
return $this->setHttpResponse('/customer/'. $customer_id, 'GET', [])->getResponse(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Update a customer's details based on their id or code |
444
|
|
|
* @param $customer_id |
445
|
|
|
* @return array |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function updateCustomer($customer_id) |
|
|
|
|
448
|
|
|
{ |
449
|
|
|
$data = [ |
450
|
|
|
"email" => request()->email, |
451
|
|
|
"first_name" => request()->fname, |
452
|
|
|
"last_name" => request()->lname, |
453
|
|
|
"phone" => request()->phone, |
454
|
|
|
"metadata" => request()->additional_info /* key => value pairs array */ |
455
|
|
|
|
456
|
|
|
]; |
457
|
|
|
|
458
|
|
|
$this->setRequestOptions(); |
459
|
|
|
return $this->setHttpResponse('/customer/'. $customer_id, 'PUT', $data)->getResponse(); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Export transactions in .CSV |
464
|
|
|
* @return array |
465
|
|
|
*/ |
466
|
|
View Code Duplication |
public function exportTransactions() |
|
|
|
|
467
|
|
|
{ |
468
|
|
|
$data = [ |
469
|
|
|
"from" => request()->from, |
470
|
|
|
"to" => request()->to, |
471
|
|
|
'settled' => request()->settled |
472
|
|
|
]; |
473
|
|
|
|
474
|
|
|
$this->setRequestOptions(); |
475
|
|
|
return $this->setHttpResponse('/transaction/export', 'GET', $data)->getResponse(); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Create a subscription to a plan from a customer. |
480
|
|
|
*/ |
481
|
|
View Code Duplication |
public function createSubscription() |
|
|
|
|
482
|
|
|
{ |
483
|
|
|
$data = [ |
484
|
|
|
"customer" => request()->customer, //Customer email or code |
485
|
|
|
"plan" => request()->plan, |
486
|
|
|
"authorization" => request()->authorization_code |
487
|
|
|
]; |
488
|
|
|
|
489
|
|
|
$this->setRequestOptions(); |
490
|
|
|
return $this->setHttpResponse('/subscription', 'POST', $data)->getResponse(); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Get all the subscriptions made on Paystack. |
495
|
|
|
* |
496
|
|
|
* @return array |
497
|
|
|
*/ |
498
|
|
|
public function getAllSubscriptions() |
499
|
|
|
{ |
500
|
|
|
$this->setRequestOptions(); |
501
|
|
|
|
502
|
|
|
return $this->setHttpResponse("/subscription", 'GET', [])->getData(); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Get customer subscriptions |
507
|
|
|
* |
508
|
|
|
* @param integer $customer_id |
509
|
|
|
* @return array |
510
|
|
|
*/ |
511
|
|
|
public function getCustomerSubscriptions($customer_id) |
512
|
|
|
{ |
513
|
|
|
$this->setRequestOptions(); |
514
|
|
|
|
515
|
|
|
return $this->setHttpResponse('/subscription?customer=' . $customer_id, 'GET', [])->getData(); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Get plan subscriptions |
520
|
|
|
* |
521
|
|
|
* @param integer $plan_id |
522
|
|
|
* @return array |
523
|
|
|
*/ |
524
|
|
|
public function getPlanSubscriptions($plan_id) |
525
|
|
|
{ |
526
|
|
|
$this->setRequestOptions(); |
527
|
|
|
|
528
|
|
|
return $this->setHttpResponse('/subscription?plan=' . $plan_id, 'GET', [])->getData(); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Enable a subscription using the subscription code and token |
533
|
|
|
* @return array |
534
|
|
|
*/ |
535
|
|
View Code Duplication |
public function enableSubscription() |
|
|
|
|
536
|
|
|
{ |
537
|
|
|
$data = [ |
538
|
|
|
"code" => request()->code, |
539
|
|
|
"token" => request()->token, |
540
|
|
|
]; |
541
|
|
|
|
542
|
|
|
$this->setRequestOptions(); |
543
|
|
|
return $this->setHttpResponse('/subscription/enable', 'POST', $data)->getResponse(); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Disable a subscription using the subscription code and token |
548
|
|
|
* @return array |
549
|
|
|
*/ |
550
|
|
View Code Duplication |
public function disableSubscription() |
|
|
|
|
551
|
|
|
{ |
552
|
|
|
$data = [ |
553
|
|
|
"code" => request()->code, |
554
|
|
|
"token" => request()->token, |
555
|
|
|
]; |
556
|
|
|
|
557
|
|
|
$this->setRequestOptions(); |
558
|
|
|
return $this->setHttpResponse('/subscription/disable', 'POST', $data)->getResponse(); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Fetch details about a certain subscription |
563
|
|
|
* @param mixed $subscription_id |
564
|
|
|
* @return array |
565
|
|
|
*/ |
566
|
|
|
public function fetchSubscription($subscription_id) |
567
|
|
|
{ |
568
|
|
|
$this->setRequestOptions(); |
569
|
|
|
return $this->setHttpResponse('/subscription/'.$subscription_id, 'GET', [])->getResponse(); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Create pages you can share with users using the returned slug |
574
|
|
|
*/ |
575
|
|
View Code Duplication |
public function createPage() |
|
|
|
|
576
|
|
|
{ |
577
|
|
|
$data = [ |
578
|
|
|
"name" => request()->name, |
579
|
|
|
"description" => request()->description, |
580
|
|
|
"amount" => request()->amount |
581
|
|
|
]; |
582
|
|
|
|
583
|
|
|
$this->setRequestOptions(); |
584
|
|
|
return $this->setHttpResponse('/page', 'POST', $data)->getResponse(); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Fetches all the pages the merchant has |
589
|
|
|
* @return array |
590
|
|
|
*/ |
591
|
|
|
public function getAllPages() |
592
|
|
|
{ |
593
|
|
|
$this->setRequestOptions(); |
594
|
|
|
return $this->setHttpResponse('/page', 'GET', [])->getResponse(); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Fetch details about a certain page using its id or slug |
599
|
|
|
* @param mixed $page_id |
600
|
|
|
* @return array |
601
|
|
|
*/ |
602
|
|
|
public function fetchPage($page_id) |
603
|
|
|
{ |
604
|
|
|
$this->setRequestOptions(); |
605
|
|
|
return $this->setHttpResponse('/page/'.$page_id, 'GET', [])->getResponse(); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Update the details about a particular page |
610
|
|
|
* @param $page_id |
611
|
|
|
* @return array |
612
|
|
|
*/ |
613
|
|
View Code Duplication |
public function updatePage($page_id) |
|
|
|
|
614
|
|
|
{ |
615
|
|
|
$data = [ |
616
|
|
|
"name" => request()->name, |
617
|
|
|
"description" => request()->description, |
618
|
|
|
"amount" => request()->amount |
619
|
|
|
]; |
620
|
|
|
|
621
|
|
|
$this->setRequestOptions(); |
622
|
|
|
return $this->setHttpResponse('/page/'.$page_id, 'PUT', $data)->getResponse(); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Creates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
627
|
|
|
* |
628
|
|
|
* @return array |
629
|
|
|
*/ |
630
|
|
|
|
631
|
|
View Code Duplication |
public function createSubAccount(){ |
|
|
|
|
632
|
|
|
$data = [ |
633
|
|
|
"business_name" => request()->business_name, |
634
|
|
|
"settlement_bank" => request()->settlement_bank, |
635
|
|
|
"account_number" => request()->account_number, |
636
|
|
|
"percentage_charge" => request()->percentage_charge, |
637
|
|
|
"primary_contact_email" => request()->primary_contact_email, |
638
|
|
|
"primary_contact_name" => request()->primary_contact_name, |
639
|
|
|
"primary_contact_phone" => request()->primary_contact_phone, |
640
|
|
|
"metadata" => request()->metadata, |
641
|
|
|
'settlement_schedule' => request()->settlement_schedule |
642
|
|
|
]; |
643
|
|
|
|
644
|
|
|
$this->setRequestOptions(); |
645
|
|
|
return $this->setHttpResponse('/subaccount', 'POST', array_filter($data))->getResponse(); |
646
|
|
|
|
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Fetches details of a subaccount |
651
|
|
|
* @param subaccount code |
652
|
|
|
* @return array |
653
|
|
|
*/ |
654
|
|
|
public function fetchSubAccount($subaccount_code){ |
655
|
|
|
|
656
|
|
|
$this->setRequestOptions(); |
657
|
|
|
return $this->setHttpResponse("/subaccount/{$subaccount_code}","GET",[])->getResponse(); |
658
|
|
|
|
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* Lists all the subaccounts associated with the account |
663
|
|
|
* @param $per_page - Specifies how many records to retrieve per page , $page - SPecifies exactly what page to retrieve |
664
|
|
|
* @return array |
665
|
|
|
*/ |
666
|
|
|
public function listSubAccounts($per_page,$page){ |
667
|
|
|
|
668
|
|
|
$this->setRequestOptions(); |
669
|
|
|
return $this->setHttpResponse("/subaccount/?perPage=".(int) $per_page."&page=".(int) $page,"GET")->getResponse(); |
670
|
|
|
|
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* Updates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
676
|
|
|
* @param subaccount code |
677
|
|
|
* @return array |
678
|
|
|
*/ |
679
|
|
|
|
680
|
|
View Code Duplication |
public function updateSubAccount($subaccount_code){ |
|
|
|
|
681
|
|
|
$data = [ |
682
|
|
|
"business_name" => request()->business_name, |
683
|
|
|
"settlement_bank" => request()->settlement_bank, |
684
|
|
|
"account_number" => request()->account_number, |
685
|
|
|
"percentage_charge" => request()->percentage_charge, |
686
|
|
|
"description" => request()->description, |
687
|
|
|
"primary_contact_email" => request()->primary_contact_email, |
688
|
|
|
"primary_contact_name" => request()->primary_contact_name, |
689
|
|
|
"primary_contact_phone" => request()->primary_contact_phone, |
690
|
|
|
"metadata" => request()->metadata, |
691
|
|
|
'settlement_schedule' => request()->settlement_schedule |
692
|
|
|
]; |
693
|
|
|
|
694
|
|
|
$this->setRequestOptions(); |
695
|
|
|
return $this->setHttpResponse("/subaccount/{$subaccount_code}", "PUT", array_filter($data))->getResponse(); |
696
|
|
|
|
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: