1 | <?php |
||
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() |
||
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) |
||
122 | |||
123 | /** |
||
124 | * Get the authorization url from the callback response |
||
125 | * @return Unicodeveloper\Paystack\Paystack |
||
126 | */ |
||
127 | public function getAuthorizationUrl() |
||
135 | |||
136 | /** |
||
137 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
138 | * @return void |
||
139 | */ |
||
140 | private function verifyTransactionAtGateway() |
||
148 | |||
149 | /** |
||
150 | * True or false condition whether the transaction is verified |
||
151 | * @return boolean |
||
152 | */ |
||
153 | public function isTransactionVerificationValid() |
||
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() |
||
188 | |||
189 | /** |
||
190 | * Fluent method to redirect to Paystack Payment Page |
||
191 | * @return Illuminate\Support\Redirect |
||
192 | */ |
||
193 | public function redirectNow() |
||
197 | |||
198 | /** |
||
199 | * Get Access code from transaction callback respose |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getAccessCode() |
||
206 | |||
207 | /** |
||
208 | * Generate a Unique Transaction Reference |
||
209 | * @return string |
||
210 | */ |
||
211 | public function genTranxRef() |
||
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: