Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class Paystack { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Transaction Verification Successful |
||
| 21 | */ |
||
| 22 | const VS = 'Verification successful'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Invalid Transaction reference |
||
| 26 | */ |
||
| 27 | const ITF = "Invalid transaction reference"; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Issue Secret Key from your Paystack Dashboard |
||
| 31 | * @var mixed |
||
| 32 | */ |
||
| 33 | protected $secretKey; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Instance of Client |
||
| 37 | * @var object |
||
| 38 | */ |
||
| 39 | protected $client; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Response from requests made to Paystack |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | protected $response; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Paystack API base Url |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $baseUrl; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Authorization Url - Paystack payment page |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $authorizationUrl; |
||
| 58 | |||
| 59 | public function __construct() |
||
| 60 | { |
||
| 61 | $this->setKey(); |
||
| 62 | $this->setBaseUrl(); |
||
| 63 | $this->setRequestOptions(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get Base Url from Paystack config file |
||
| 68 | */ |
||
| 69 | public function setBaseUrl() |
||
| 70 | { |
||
| 71 | $this->baseUrl = Config::get('paystack.paymentUrl'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get secret key from Paystack config file |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function setKey() |
||
| 79 | { |
||
| 80 | $this->secretKey = Config::get('paystack.secretKey'); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Set options for making the Client request |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | private function setRequestOptions() |
||
| 88 | { |
||
| 89 | $authBearer = 'Bearer '. $this->secretKey; |
||
| 90 | |||
| 91 | $this->client = new Client(['base_uri' => $this->baseUrl, |
||
| 92 | 'headers' => [ |
||
| 93 | 'Authorization' => $authBearer, |
||
| 94 | 'Content-Type' => 'application/json', |
||
| 95 | 'Accept' => 'application/json' |
||
| 96 | ]]); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initiate a payment request to Paystack |
||
| 101 | * @return Unicodeveloper\Paystack\Paystack |
||
| 102 | */ |
||
| 103 | public function makePaymentRequest() |
||
| 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) |
||
| 129 | |||
| 130 | private function setGetResponse($relativeUrl, $body = array()) |
||
| 131 | { |
||
| 132 | $this->response = $this->client->get($this->baseUrl . $relativeUrl, $body); |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the authorization url from the callback response |
||
| 139 | * @return Unicodeveloper\Paystack\Paystack |
||
| 140 | */ |
||
| 141 | public function getAuthorizationUrl() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | private function verifyTransactionAtGateway() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * True or false condition whether the transaction is verified |
||
| 165 | * @return boolean |
||
| 166 | */ |
||
| 167 | public function isTransactionVerificationValid() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get Payment details if the transaction was verified successfully |
||
| 191 | * @return json |
||
| 192 | * @throws PaymentVerificationFailedException |
||
| 193 | */ |
||
| 194 | public function getPaymentData() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Fluent method to redirect to Paystack Payment Page |
||
| 205 | * @return Illuminate\Support\Redirect |
||
| 206 | */ |
||
| 207 | public function redirectNow() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get Access code from transaction callback respose |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getAccessCode() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Generate a Unique Transaction Reference |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function genTranxRef() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get all the customers that have made transactions on your platform |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function getAllCustomers() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get all the plans that you have on Paystack |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getAllPlans() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get all the transactions that have happened overtime |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getAllTransactions() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the whole response from a get operation |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | private function getResponse() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the data response from a get operation |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | private function getData() |
||
| 280 | |||
| 281 | //Edits by Funsho http://devfunsho.com | @iamfusnho - 22/05/2016 |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Create a plan |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function createPlan(){ |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Fetch any plan based on its plan id or code |
||
| 308 | * @param $plan_code |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function fetchPlan($plan_code){ |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Update any plan's details based on its id or code |
||
| 321 | * @param $plan_code |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function updatePlan($plan_code){ |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Create a customer |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function createCustomer(){ |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Fetch a customer based on id or code |
||
| 367 | * @param $customer_id |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function fetchCustomer($customer_id){ |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Update a customer's details based on their id or code |
||
| 380 | * @param $customer_id |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function updateCustomer($customer_id){ |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Export tranactions in .CSV |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function exportTransactions(){ |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Create a subscription to a plan from a customer. |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | View Code Duplication | public function createSubscription(){ |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Enable a subscription using the subscription code and token |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | View Code Duplication | public function enableSubscription(){ |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Disable a subscription using the subscription code and token |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | View Code Duplication | public function disableSubscription(){ |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Fetch details about a certain subscription |
||
| 472 | * @param $subscription_id |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | public function fetchSubscription($subscription_id){ |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Create pages you can share with users using the returned slug |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | View Code Duplication | public function createPage(){ |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Fetches all the pages the merchant has |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | public function getAllPages(){ |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Fetch details about a certain page using its id or slug |
||
| 516 | * @param $page_id |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public function fetchPage($page_id){ |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Update the details about a particular page |
||
| 529 | * @param $page_id |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | View Code Duplication | public function updatePage($page_id){ |
|
| 546 | |||
| 547 | } |
||
| 548 | |||
| 551 |
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: