Skip to content

Commit e9df56b

Browse files
Merge pull request #4 from iamfunsho/master
Up-to-date new functions for laravel-paystack
2 parents 4b40fd1 + b85cf02 commit e9df56b

File tree

4 files changed

+259
-26
lines changed

4 files changed

+259
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build
22
vendor
33
.DS_Store
4-
composer.lock
4+
composer.lock
5+
.idea

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
{
88
"name": "unicodeveloper",
99
"email": "prosperotemuyiwa@gmail.com"
10+
},
11+
{
12+
"name": "iamfunsho",
13+
"email": "info@devfunsho.com"
1014
}
1115
],
1216
"minimum-stability": "stable",

src/Exceptions/isNullException.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Paystack package.
5+
*
6+
* (c) Prosper Otemuyiwa <prosperotemuyiwa@gmail.com>
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\Exceptions;
13+
14+
use Exception;
15+
16+
class IsNullException extends Exception {
17+
18+
}

src/Paystack.php

Lines changed: 235 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,45 +98,33 @@ private function setRequestOptions()
9898

9999
/**
100100
* Initiate a payment request to Paystack
101-
* @return Unicodeveloper\Paystack\Paystack
102101
*/
103102
public function makePaymentRequest()
104-
{
105-
$this->setResponse('/transaction/initialize');
106-
107-
return $this;
108-
}
109-
110-
/**
111-
* Make the client request and get the response
112-
* @param string $relativeUrl
113-
* @return Unicodeveloper\Paystack\Paystack
114-
*/
115-
public function setResponse($relativeUrl)
116103
{
117104
$data = [
118105
"amount" => intval(request()->amount),
119106
"reference" => request()->reference,
120107
"email" => request()->email
121108
];
122109

123-
$this->response = $this->client->post($this->baseUrl . $relativeUrl, [
124-
'body' => json_encode($data)
125-
]);
110+
$this->setHttpResponse('/transaction/initialize', 'POST', $data);
126111

127112
return $this;
128113
}
129114

130-
private function setGetResponse($relativeUrl)
115+
116+
private function setHttpResponse($relativeUrl, $method, $body = [])
131117
{
132-
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
118+
if(is_null($method)){
119+
throw new IsNullException("Empty method not allowed");
120+
}
133121

122+
$this->response = $this->client->{strtolower($method)}($this->baseUrl . $relativeUrl, ["body" => json_encode($body)]);
134123
return $this;
135124
}
136125

137126
/**
138127
* Get the authorization url from the callback response
139-
* @return Unicodeveloper\Paystack\Paystack
140128
*/
141129
public function getAuthorizationUrl()
142130
{
@@ -188,8 +176,8 @@ public function isTransactionVerificationValid()
188176

189177
/**
190178
* Get Payment details if the transaction was verified successfully
191-
* @throws Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException
192179
* @return json
180+
* @throws PaymentVerificationFailedException
193181
*/
194182
public function getPaymentData()
195183
{
@@ -202,7 +190,6 @@ public function getPaymentData()
202190

203191
/**
204192
* Fluent method to redirect to Paystack Payment Page
205-
* @return Illuminate\Support\Redirect
206193
*/
207194
public function redirectNow()
208195
{
@@ -235,7 +222,7 @@ public function getAllCustomers()
235222
{
236223
$this->setRequestOptions();
237224

238-
return $this->setGetResponse("/customer")->getData();
225+
return $this->setHttpResponse("/customer", 'GET', [])->getData();
239226
}
240227

241228
/**
@@ -246,7 +233,7 @@ public function getAllPlans()
246233
{
247234
$this->setRequestOptions();
248235

249-
return $this->setGetResponse("/plan")->getData();
236+
return $this->setHttpResponse("/plan", 'GET', [])->getData();
250237
}
251238

252239
/**
@@ -257,7 +244,7 @@ public function getAllTransactions()
257244
{
258245
$this->setRequestOptions();
259246

260-
return $this->setGetResponse("/transaction")->getData();
247+
return $this->setHttpResponse("/transaction", 'GET', [])->getData();
261248
}
262249

263250
/**
@@ -278,4 +265,227 @@ private function getData()
278265
return $this->getResponse()['data'];
279266
}
280267

281-
}
268+
/**
269+
* Create a plan
270+
*/
271+
public function createPlan(){
272+
273+
$data = [
274+
"name" => request()->name,
275+
"description" => request()->desc,
276+
"amount" => intval(request()->amount),
277+
"interval" => request()->interval,
278+
"send_invoices" => request()->send_invoices,
279+
"send_sms" => request()->send_sms,
280+
"currency" => request()->currency,
281+
];
282+
283+
$this->setRequestOptions();
284+
285+
$this->setHttpResponse("/plan", 'POST', $data);
286+
287+
}
288+
289+
/**
290+
* Fetch any plan based on its plan id or code
291+
* @param $plan_code
292+
* @return array
293+
*/
294+
public function fetchPlan($plan_code){
295+
$this->setRequestOptions();
296+
return $this->setHttpResponse('/plan/' . $plan_code, 'GET', [])->getResponse();
297+
}
298+
299+
/**
300+
* Update any plan's details based on its id or code
301+
* @param $plan_code
302+
* @return array
303+
*/
304+
public function updatePlan($plan_code){
305+
$data = [
306+
"name" => request()->name,
307+
"description" => request()->desc,
308+
"amount" => intval(request()->amount),
309+
"interval" => request()->interval,
310+
"send_invoices" => request()->send_invoices,
311+
"send_sms" => request()->send_sms,
312+
"currency" => request()->currency,
313+
];
314+
315+
$this->setRequestOptions();
316+
return $this->setHttpResponse('/plan/' . $plan_code, 'PUT', $data)->getResponse();
317+
}
318+
319+
/**
320+
* Create a customer
321+
* @return array
322+
*/
323+
public function createCustomer(){
324+
$data = [
325+
"email" => request()->email,
326+
"first_name" => request()->fname,
327+
"last_name" => request()->lname,
328+
"phone" => request()->phone,
329+
"metadata" => request()->additional_info /* key => value pairs array */
330+
331+
];
332+
333+
$this->setRequestOptions();
334+
$this->setHttpResponse('/customer', 'POST', $data);
335+
}
336+
337+
/**
338+
* Fetch a customer based on id or code
339+
* @param $customer_id
340+
* @return array
341+
*/
342+
public function fetchCustomer($customer_id)
343+
{
344+
$this->setRequestOptions();
345+
return $this->setHttpResponse('/customer/'. $customer_id, 'GET', [])->getResponse();
346+
}
347+
348+
/**
349+
* Update a customer's details based on their id or code
350+
* @param $customer_id
351+
* @return array
352+
*/
353+
public function updateCustomer($customer_id){
354+
$data = [
355+
"email" => request()->email,
356+
"first_name" => request()->fname,
357+
"last_name" => request()->lname,
358+
"phone" => request()->phone,
359+
"metadata" => request()->additional_info /* key => value pairs array */
360+
361+
];
362+
363+
$this->setRequestOptions();
364+
return $this->setHttpResponse('/customer/'. $customer_id, 'PUT', $data)->getResponse();
365+
}
366+
367+
/**
368+
* Export tranactions in .CSV
369+
* @return array
370+
*/
371+
public function exportTransactions(){
372+
$data = [
373+
"from" => request()->from,
374+
"to" => request()->to,
375+
'settled' => request()->settled
376+
];
377+
378+
$this->setRequestOptions();
379+
return $this->setHttpResponse('/transaction/export', 'GET', $data)->getResponse();
380+
}
381+
382+
/**
383+
* Create a subscription to a plan from a customer.
384+
* @return array
385+
*/
386+
public function createSubscription(){
387+
$data = [
388+
"customer" => request()->customer, //Customer email or code
389+
"plan" => request()->plan,
390+
"authorization" => request()->authorization_code
391+
];
392+
393+
$this->setRequestOptions();
394+
$this->setHttpResponse('/subscription', 'POST', $data);
395+
}
396+
397+
/**
398+
* Enable a subscription using the subscription code and token
399+
* @return array
400+
*/
401+
public function enableSubscription(){
402+
$data = [
403+
"code" => request()->code,
404+
"token" => request()->token,
405+
];
406+
407+
$this->setRequestOptions();
408+
return $this->setHttpResponse('/subscription/enable', 'POST', $data)->getResponse();
409+
}
410+
411+
/**
412+
* Disable a subscription using the subscription code and token
413+
* @return array
414+
*/
415+
public function disableSubscription(){
416+
$data = [
417+
"code" => request()->code,
418+
"token" => request()->token,
419+
];
420+
421+
$this->setRequestOptions();
422+
return $this->setHttpResponse('/subscription/disable', 'POST', $data)->getResponse();
423+
}
424+
425+
/**
426+
* Fetch details about a certain subscription
427+
* @param $subscription_id
428+
* @return array
429+
*/
430+
public function fetchSubscription($subscription_id)
431+
{
432+
$this->setRequestOptions();
433+
return $this->setHttpResponse('/subscription/'.$subscription_id, 'GET', [])->getResponse();
434+
}
435+
436+
/**
437+
* Create pages you can share with users using the returned slug
438+
* @return array
439+
*/
440+
public function createPage(){
441+
$data = [
442+
"name" => request()->name,
443+
"description" => request()->description,
444+
"amount" => request()->amount
445+
];
446+
447+
$this->setRequestOptions();
448+
$this->setHttpResponse('/page', 'POST', $data);
449+
}
450+
451+
/**
452+
* Fetches all the pages the merchant has
453+
* @return array
454+
*/
455+
public function getAllPages()
456+
{
457+
$this->setRequestOptions();
458+
return $this->setHttpResponse('/page', 'GET', [])->getResponse();
459+
}
460+
461+
/**
462+
* Fetch details about a certain page using its id or slug
463+
* @param $page_id
464+
* @return array
465+
*/
466+
public function fetchPage($page_id)
467+
{
468+
$this->setRequestOptions();
469+
return $this->setHttpResponse('/page/'.$page_id, 'GET', [])->getResponse();
470+
}
471+
472+
/**
473+
* Update the details about a particular page
474+
* @param $page_id
475+
* @return array
476+
*/
477+
public function updatePage($page_id){
478+
$data = [
479+
"name" => request()->name,
480+
"description" => request()->description,
481+
"amount" => request()->amount
482+
];
483+
484+
$this->setRequestOptions();
485+
return $this->setHttpResponse('/page/'.$page_id, 'PUT', $data)->getResponse();
486+
}
487+
488+
}
489+
490+
491+

0 commit comments

Comments
 (0)