1 | <?php |
||
18 | class Cbs |
||
19 | { |
||
20 | /** |
||
21 | * Issue Secret Key from CBS. |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $secretKey; |
||
25 | |||
26 | /** |
||
27 | * Issue Client ID from CBS. |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $clientId; |
||
31 | |||
32 | /** |
||
33 | * Issue URL from CBS. |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $baseUrl; |
||
37 | |||
38 | /** |
||
39 | * Issue Revenue Head from CBS. |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $revenueHeads; |
||
43 | |||
44 | /** |
||
45 | * Issue Category ID from CBS Admin. |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $categoryId; |
||
49 | |||
50 | /** |
||
51 | * Response from requests made to CBS. |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $response; |
||
55 | |||
56 | /** |
||
57 | * Hashed Key. |
||
58 | * @var mixed |
||
59 | */ |
||
60 | protected $signature; |
||
61 | |||
62 | /** |
||
63 | * Payment Url - CBS payment page. |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $url; |
||
67 | |||
68 | /** |
||
69 | * Response from CBS. |
||
70 | * @var mixed |
||
71 | */ |
||
72 | protected $invoice = []; |
||
73 | |||
74 | /** |
||
75 | * Instance of Client. |
||
76 | * @var Client |
||
77 | */ |
||
78 | protected $client; |
||
79 | |||
80 | public function __construct() |
||
86 | |||
87 | public function setUrl() |
||
91 | |||
92 | /** |
||
93 | * Get secret key from CBS config file. |
||
94 | */ |
||
95 | public function setConstant() |
||
102 | |||
103 | protected function checkConstant() |
||
121 | |||
122 | protected function setSignature($amount, $callback) |
||
130 | |||
131 | /** |
||
132 | * Set options for making the Client request. |
||
133 | */ |
||
134 | private function setRequestOptions() |
||
146 | |||
147 | /** |
||
148 | * @param $relativeUri |
||
149 | * @param string $method |
||
150 | * @param array $body |
||
151 | * |
||
152 | * @return \Infinitypaul\Cbs\Cbs |
||
153 | * @throws \Infinitypaul\Cbs\Exceptions\NotSetException |
||
154 | */ |
||
155 | private function setHttpResponse($relativeUri, $method, $body = []) |
||
167 | |||
168 | private function getResponse() |
||
172 | |||
173 | protected function setUser($data) |
||
174 | { |
||
175 | if (isset($data['payerID'])) { |
||
176 | $user = ['PayerId' => $data['payerID']]; |
||
177 | } else { |
||
178 | $user = [ |
||
179 | 'Recipient' => $data['full_name'], |
||
180 | 'Email' => $data['email'], |
||
181 | 'Address' => $data['address'], |
||
182 | 'PhoneNumber' => $data['mobile_number'], |
||
183 | 'TaxPayerIdentificationNumber' => isset($data['tin']) ? $data['tin'] : '', |
||
184 | ]; |
||
185 | } |
||
186 | |||
187 | return [ |
||
188 | 'TaxEntity' => $user, |
||
189 | 'Amount' => intval($data['amount']), |
||
190 | 'InvoiceDescription' => $data['description'], |
||
191 | 'CategoryId' => $this->categoryId, |
||
192 | ]; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Initiate a payment request to Cbs |
||
197 | * Included the option to pass the payload to this method for situations |
||
198 | * when the payload is built on the fly (not passed to the controller from a view). |
||
199 | * |
||
200 | * @param array $data |
||
201 | * |
||
202 | * @return \Infinitypaul\Cbs\Cbs |
||
203 | * @throws \Infinitypaul\Cbs\Exceptions\NotSetException |
||
204 | */ |
||
205 | public function generateInvoice($data) |
||
206 | { |
||
207 | if (empty($data)) { |
||
208 | $TaxEntityInvoice = $this->setUser(request()->toArray()); |
||
209 | $callback = request()->callback; |
||
210 | $quantity = request()->quantity; |
||
211 | $ExternalRefNumber = request()->has('externalRefNumber') ? request()->externalRefNumber : null; |
||
212 | } else { |
||
213 | $TaxEntityInvoice = $this->setUser($data); |
||
214 | $callback = $data['callback']; |
||
215 | $quantity = $data['quantity']; |
||
216 | $ExternalRefNumber = isset($data['externalRefNumber']) ? $data['externalRefNumber'] : null; |
||
217 | } |
||
218 | |||
219 | $data = [ |
||
220 | 'RevenueHeadId' => $this->revenueHeads, |
||
221 | 'TaxEntityInvoice' => $TaxEntityInvoice, |
||
222 | 'CallBackURL' => $callback, |
||
223 | 'RequestReference' => ReferenceNumber::getHashedToken(), |
||
224 | 'Quantity' => $quantity, |
||
225 | 'ExternalRefNumber' => $ExternalRefNumber, |
||
226 | |||
227 | ]; |
||
228 | |||
229 | $this->setSignature($TaxEntityInvoice['Amount'], $callback); |
||
230 | array_filter($data); |
||
231 | |||
232 | $this->setHttpResponse('/api/v1/invoice/create', 'POST', $data); |
||
233 | |||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Set the invoice data from the callback response. |
||
239 | * |
||
240 | * @param array $data |
||
241 | * |
||
242 | * @return \Infinitypaul\Cbs\Cbs |
||
243 | * @throws \Infinitypaul\Cbs\Exceptions\NotSetException |
||
244 | */ |
||
245 | public function setInvoice($data = []) |
||
246 | { |
||
247 | $this->generateInvoice($data); |
||
248 | $this->invoice = $this->getResponse(); |
||
249 | |||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get the invoice data from the callback response. |
||
255 | */ |
||
256 | public function getData() |
||
257 | { |
||
258 | return $this->invoice; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Get the invoice payment url from the callback response. |
||
263 | */ |
||
264 | public function redirectNow() |
||
265 | { |
||
266 | return redirect($this->invoice['ResponseObject']['PaymentURL']); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Compute Mac Address. |
||
271 | * |
||
272 | * @param $invoiceNumber |
||
273 | * @param $paymentRef |
||
274 | * @param $amount |
||
275 | * |
||
276 | * |
||
277 | * @param $RequestReference |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | protected function computeMac($invoiceNumber, $paymentRef, $amount, $RequestReference) |
||
282 | { |
||
283 | $amount = number_format((float) $amount, 2, '.', ''); |
||
284 | $string = $invoiceNumber.$paymentRef.$amount.$RequestReference; |
||
285 | |||
286 | return base64_encode(hash_hmac('sha256', $string, $this->secretKey, true)); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Get Payment details if the transaction was verified successfully. |
||
291 | * |
||
292 | * @throws \Infinitypaul\Cbs\Exceptions\InvalidPostException |
||
293 | */ |
||
294 | public function getPaymentData() |
||
303 | } |
||
304 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: