|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the Laravel Paystack package. |
| 4 | + * |
| 5 | + * (c) israel Edet <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace PayStack\Controller\Component; |
| 11 | + |
| 12 | +use Cake\Controller\Component; |
| 13 | +use Cake\Controller\ComponentRegistry; |
| 14 | + |
| 15 | +/** |
| 16 | + * PayStack component |
| 17 | + */ |
| 18 | +class PayStackComponent extends Component |
| 19 | +{ |
| 20 | + |
| 21 | + /** |
| 22 | + * Default configuration. |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + protected $_defaultConfig = []; |
| 27 | + public $components = ['CurlConnection']; |
| 28 | + |
| 29 | + private $secretKey = PaystackTestSecretKey; |
| 30 | + private $publicKey = PaystackTestPublicKey; |
| 31 | + private $url = "https://api.paystack.co/transaction/initialize"; |
| 32 | + private $verifyUrl = "https://api.paystack.co/transaction/verify/"; |
| 33 | + |
| 34 | + /* Initiate a payment request to Paystack |
| 35 | + * Included the option to pass the payload to this method for situations |
| 36 | + * when the payload is built on the fly (not passed to the controller from a view) |
| 37 | + * @param payload to be sent to paystack |
| 38 | + * @return array|false |
| 39 | + */ |
| 40 | + public function payWithPaystack($postdata){ |
| 41 | + $result = array(); |
| 42 | + |
| 43 | + $options = array( |
| 44 | + CURLOPT_URL => $this->url, |
| 45 | + CURLOPT_POST => 1, |
| 46 | + CURLOPT_POSTFIELDS=>json_encode($postdata), |
| 47 | + CURLOPT_RETURNTRANSFER=>true, |
| 48 | + CURLOPT_HTTPHEADER => [ |
| 49 | + 'Authorization: Bearer '.$this->secretKey, |
| 50 | + 'Content-Type: application/json', |
| 51 | + ], |
| 52 | + ); |
| 53 | + $request = $this->CurlConnection->payStackConnection($options); //make reques via Curl |
| 54 | + if ($request) { |
| 55 | + $result = json_decode($request); |
| 56 | + |
| 57 | + if(!$result->status){ |
| 58 | + // there was an error from the API |
| 59 | + die('API returned error: ' . $result->message); |
| 60 | + }else{ |
| 61 | + |
| 62 | + return $result; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Hit Paystack Gateway to Verify that the transaction is valid |
| 70 | + * @param String $reference |
| 71 | + * @return array|false |
| 72 | + */ |
| 73 | + public function callback($reference,$user_id){ |
| 74 | + $options = array( |
| 75 | + CURLOPT_URL => $this->verifyUrl . rawurlencode($reference), |
| 76 | + CURLOPT_RETURNTRANSFER => true, |
| 77 | + CURLOPT_HTTPHEADER => [ |
| 78 | + "accept: application/json", |
| 79 | + "authorization: Bearer ".$this->secretKey, |
| 80 | + "cache-control: no-cache" |
| 81 | + ], |
| 82 | + ); |
| 83 | + |
| 84 | + $response = $this->CurlConnection->payStackConnection($options); |
| 85 | + $tranx = json_decode($response); |
| 86 | + |
| 87 | + if(!$tranx->status){ |
| 88 | + // there was an error from the API |
| 89 | + die('API returned error: ' . $tranx->message); |
| 90 | + } |
| 91 | + |
| 92 | + if('success' == $tranx->data->status){ |
| 93 | + // transaction was successful... |
| 94 | + return $transx; |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments