Skip to content

Commit de0e027

Browse files
feat(paystack): Add Facade, ServiceProvider, Exceptions and Paystack Mother class
1 parent 0993919 commit de0e027

File tree

5 files changed

+244
-8
lines changed

5 files changed

+244
-8
lines changed

resources/config/paystack.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,42 @@
22

33
return [
44

5-
'apiKey' => '',
5+
/**
6+
*
7+
*
8+
*/
9+
'publicKey' => 'pk_test_489661a873de9d88380ceb58ad994d39a08255d9',
10+
11+
/**
12+
*
13+
*
14+
*/
15+
'secretKey' => 'sk_test_15636445eb184d45e6c742e53dee2df4685ed18c',
16+
17+
/**
18+
*
19+
*
20+
*/
21+
'merchantID' => '',
22+
23+
/**
24+
*
25+
*
26+
*/
27+
'paymentURL' => 'https://api.paystack.co/transaction/initialize',
28+
29+
/**
30+
* The route where paystack will redirect to after the payment has been made
31+
*
32+
*/
33+
'callbackURL' => '',
34+
35+
/**
36+
* Optional email address of the merchant
37+
*
38+
*/
39+
'merchantEmail' => ''
40+
641

7-
'apiSecret' => ''
842

943
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Unicodeveloper\Paystack\Exceptions;
4+
5+
use Exception;
6+
7+
class PaymentVerificationFailedException extends Exception {
8+
9+
}

src/Facades/PaystackFacade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
namespace Unicodeveloper\Quotes\Facades;
3+
namespace Unicodeveloper\Paystack\Facades;
44

55
use Illuminate\Support\Facades\Facade;
66

7-
class QuotesFacade extends Facade {
7+
class PaystackFacade extends Facade {
88
/**
99
* Get the registered name of the component.
1010
*
1111
* @return string
1212
*/
1313
protected static function getFacadeAccessor()
1414
{
15-
return 'laravel-quotes';
15+
return 'laravel-paystack';
1616
}
1717
}

src/Paystack.php

Lines changed: 191 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,198 @@
11
<?php
22

3-
namespace Unicodeveloper\Quotes;
3+
namespace Unicodeveloper\Paystack;
44

55
use GuzzleHttp\Client;
66

7-
class Quotes {
7+
class Paystack {
88

9+
/**
10+
* Transaction Verification Successful
11+
*/
12+
const VS = 'Verification successful';
13+
14+
/**
15+
* Invalid Transaction reference
16+
*/
17+
const ITF = "Invalid transaction reference";
18+
19+
20+
/**
21+
* Issue Secret Key from your Paystack Dashboard
22+
* @var mixed
23+
*/
24+
protected $secretKey;
25+
26+
27+
/**
28+
* Instance of Client
29+
* @var object
30+
*/
31+
protected $client;
32+
33+
/**
34+
* Response from requests made to Paystack
35+
* @var mixed
36+
*/
37+
protected $response;
38+
39+
/**
40+
* Paystack API base Url
41+
* @var string
42+
*/
43+
protected $baseUrl = 'https://api.paystack.co';
44+
45+
/**
46+
* Authorization Url - Paystack payment page
47+
* @var string
48+
*/
49+
protected $authorizationUrl;
50+
51+
public function __construct()
52+
{
53+
$this->setKey();
54+
$this->setRequestOptions();
55+
}
56+
57+
/**
58+
* Get secret key from paystack config file
59+
* @return void
60+
*/
61+
public function setKey()
62+
{
63+
$this->secretKey = config('paystack.secretKey');
64+
}
65+
66+
/**
67+
* Set options for making the Client request
68+
* @return void
69+
*/
70+
private function setRequestOptions()
71+
{
72+
$authBearer = 'Bearer '. $this->secretKey;
73+
74+
$this->client = new Client(['base_uri' => $this->baseUrl]);
75+
76+
//Set a single header using path syntax
77+
$this->client->setDefaultOption('headers', [
78+
'Authorization' => $authBearer,
79+
'Content-Type' => 'application/json',
80+
'Accept' => 'application/json'
81+
]);
82+
}
83+
84+
/**
85+
* Initiate a payment request to Paystack
86+
* @return Unicodeveloper\Paystack\Paystack
87+
*/
88+
public function makePaymentRequest()
89+
{
90+
$this->setResponse('/transaction/initialize');
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* Make the client request and get the response
97+
* @param string $relativeUrl
98+
* @return Unicodeveloper\Paystack\Paystack
99+
*/
100+
public function setResponse($relativeUrl)
101+
{
102+
$data = [
103+
"amount" => intval(request()->amount),
104+
"reference" => request()->reference,
105+
"email" => request()->email
106+
];
107+
108+
$this->response = $this->client->post($this->baseUrl . $relativeUrl, [
109+
'body' => json_encode($data)
110+
]);
111+
112+
return $this;
113+
}
114+
115+
/**
116+
* Get the authorization url from the callback response
117+
* @return Unicodeveloper\Paystack\Paystack
118+
*/
119+
public function getAuthorizationUrl()
120+
{
121+
$this->makePayment();
122+
123+
$this->url = $this->response->json()["data"]["authorization_url"];
124+
125+
return $this;
126+
}
127+
128+
/**
129+
* Hit Paystack Gateway to Verify that the transaction is valid
130+
* @return void
131+
*/
132+
private function verifyTransactionAtGateway()
133+
{
134+
$transactionRef = request()->query('trxref');
135+
136+
$relativeUrl = "/transaction/verify/{$transactionRef}";
137+
138+
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
139+
}
140+
141+
/**
142+
* True or false condition whether the transaction is verified
143+
* @return boolean
144+
*/
145+
public function isTransactionVerificationValid()
146+
{
147+
$this->verifyTransactionAtGateway();
148+
149+
$result = $this->response->json()["message"];
150+
151+
switch($result)
152+
{
153+
case self::VS:
154+
$validate = true;
155+
break;
156+
case self:ITF:
157+
$validate = false;
158+
break;
159+
default:
160+
$validate = false;
161+
break;
162+
}
163+
164+
return $validate;
165+
}
166+
167+
/**
168+
* Get Payment details if the transaction was verified successfully
169+
* @throws Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException
170+
* @return json
171+
*/
172+
public function getPaymentData()
173+
{
174+
if($this->isTransactionVerificationValid()) {
175+
return $this->response->json();
176+
} else {
177+
throw new PaymentVerificationFailedException("Invalid Transaction Reference");
178+
}
179+
}
180+
181+
/**
182+
* Fluent method to redirect to Paystack Payment Page
183+
* @return Illuminate\Support\Redirect
184+
*/
185+
public function redirectNow()
186+
{
187+
return redirect($this->url);
188+
}
189+
190+
/**
191+
* Get Access code from transaction callback respose
192+
* @return string
193+
*/
194+
public function getAccessCode()
195+
{
196+
return $this->response->json()["data"]["access_code"];
197+
}
9198
}

src/PaystackServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ class PaystackServiceProvider extends ServiceProvider {
1313
*/
1414
protected $defer = false;
1515

16+
public function boot()
17+
{
18+
}
19+
1620
/**
1721
* Register the application services.
1822
*
1923
* @return void
2024
*/
2125
public function register()
2226
{
23-
$this->app->bind('laravel-paystack', function() {
27+
$this->app->bind('laravel-paystack', function() {
2428

2529
return new Paystack;
2630

0 commit comments

Comments
 (0)