1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Unicodeveloper\Paystack; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; |
8
|
|
|
|
9
|
|
|
class Paystack { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Transaction Verification Successful |
13
|
|
|
*/ |
14
|
|
|
const VS = 'Verification successful'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Invalid Transaction reference |
18
|
|
|
*/ |
19
|
|
|
const ITF = "Invalid transaction reference"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Issue Secret Key from your Paystack Dashboard |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $secretKey; |
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; |
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->setBaseUrl(); |
55
|
|
|
$this->setRequestOptions(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get Base Url from Paystack config file |
60
|
|
|
*/ |
61
|
|
|
public function setBaseUrl() |
62
|
|
|
{ |
63
|
|
|
$this->baseUrl = config('paystack.paymentUrl'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get secret key from Paystack config file |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function setKey() |
71
|
|
|
{ |
72
|
|
|
$this->secretKey = config('paystack.secretKey'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set options for making the Client request |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
private function setRequestOptions() |
80
|
|
|
{ |
81
|
|
|
$authBearer = 'Bearer '. $this->secretKey; |
82
|
|
|
|
83
|
|
|
$this->client = new Client(['base_uri' => $this->baseUrl]); |
84
|
|
|
|
85
|
|
|
$this->client->setDefaultOption('headers', [ |
86
|
|
|
'Authorization' => $authBearer, |
87
|
|
|
'Content-Type' => 'application/json', |
88
|
|
|
'Accept' => 'application/json' |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Initiate a payment request to Paystack |
94
|
|
|
* @return Unicodeveloper\Paystack\Paystack |
95
|
|
|
*/ |
96
|
|
|
public function makePaymentRequest() |
97
|
|
|
{ |
98
|
|
|
$this->setResponse('/transaction/initialize'); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Make the client request and get the response |
105
|
|
|
* @param string $relativeUrl |
106
|
|
|
* @return Unicodeveloper\Paystack\Paystack |
107
|
|
|
*/ |
108
|
|
|
public function setResponse($relativeUrl) |
109
|
|
|
{ |
110
|
|
|
$data = [ |
111
|
|
|
"amount" => intval(request()->amount), |
112
|
|
|
"reference" => request()->reference, |
113
|
|
|
"email" => request()->email |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$this->response = $this->client->post($this->baseUrl . $relativeUrl, [ |
117
|
|
|
'body' => json_encode($data) |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the authorization url from the callback response |
125
|
|
|
* @return Unicodeveloper\Paystack\Paystack |
126
|
|
|
*/ |
127
|
|
|
public function getAuthorizationUrl() |
128
|
|
|
{ |
129
|
|
|
$this->makePaymentRequest(); |
130
|
|
|
|
131
|
|
|
$this->url = $this->response->json()["data"]["authorization_url"]; |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Hit Paystack Gateway to Verify that the transaction is valid |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
private function verifyTransactionAtGateway() |
141
|
|
|
{ |
142
|
|
|
$transactionRef = request()->query('trxref'); |
143
|
|
|
|
144
|
|
|
$relativeUrl = "/transaction/verify/{$transactionRef}"; |
145
|
|
|
|
146
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* True or false condition whether the transaction is verified |
151
|
|
|
* @return boolean |
152
|
|
|
*/ |
153
|
|
|
public function isTransactionVerificationValid() |
154
|
|
|
{ |
155
|
|
|
$this->verifyTransactionAtGateway(); |
156
|
|
|
|
157
|
|
|
$result = $this->response->json()["message"]; |
158
|
|
|
|
159
|
|
|
switch($result) |
160
|
|
|
{ |
161
|
|
|
case self::VS: |
162
|
|
|
$validate = true; |
163
|
|
|
break; |
164
|
|
|
case self:ITF: |
165
|
|
|
$validate = false; |
166
|
|
|
break; |
167
|
|
|
default: |
168
|
|
|
$validate = false; |
169
|
|
|
break; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $validate; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get Payment details if the transaction was verified successfully |
177
|
|
|
* @throws Unicodeveloper\Paystack\Exceptions\PaymentVerificationFailedException |
178
|
|
|
* @return json |
179
|
|
|
*/ |
180
|
|
|
public function getPaymentData() |
181
|
|
|
{ |
182
|
|
|
if($this->isTransactionVerificationValid()) { |
183
|
|
|
return $this->response->json(); |
184
|
|
|
} else { |
185
|
|
|
throw new PaymentVerificationFailedException("Invalid Transaction Reference"); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Fluent method to redirect to Paystack Payment Page |
191
|
|
|
* @return Illuminate\Support\Redirect |
192
|
|
|
*/ |
193
|
|
|
public function redirectNow() |
194
|
|
|
{ |
195
|
|
|
return redirect($this->url); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get Access code from transaction callback respose |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function getAccessCode() |
203
|
|
|
{ |
204
|
|
|
return $this->response->json()["data"]["access_code"]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Generate a Unique Transaction Reference |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function genTranxRef() |
212
|
|
|
{ |
213
|
|
|
try { |
214
|
|
|
|
215
|
|
|
// Generate a version 1 (time-based) UUID object |
216
|
|
|
$uuid1 = Uuid::uuid1(); |
217
|
|
|
echo $uuid1->toString() . "\n"; // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd |
218
|
|
|
|
219
|
|
|
// // Generate a version 3 (name-based and hashed with MD5) UUID object |
220
|
|
|
// $uuid3 = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net'); |
|
|
|
|
221
|
|
|
// echo $uuid3->toString() . "\n"; // i.e. 11a38b9a-b3da-360f-9353-a5a725514269 |
|
|
|
|
222
|
|
|
|
223
|
|
|
// // Generate a version 4 (random) UUID object |
224
|
|
|
// $uuid4 = Uuid::uuid4(); |
|
|
|
|
225
|
|
|
// echo $uuid4->toString() . "\n"; // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a |
|
|
|
|
226
|
|
|
|
227
|
|
|
// // Generate a version 5 (name-based and hashed with SHA1) UUID object |
228
|
|
|
// $uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net'); |
|
|
|
|
229
|
|
|
// echo $uuid5->toString() . "\n"; // i.e. c4a760a8-dbcf-5254-a0d9-6a4474bd1b62 |
|
|
|
|
230
|
|
|
|
231
|
|
|
} catch (UnsatisfiedDependencyException $e) { |
232
|
|
|
|
233
|
|
|
// Some dependency was not met. Either the method cannot be called on a |
234
|
|
|
// 32-bit system, or it can, but it relies on Moontoast\Math to be present. |
235
|
|
|
echo 'Caught exception: ' . $e->getMessage() . "\n"; |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
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: