├── .gitignore ├── lib └── Paystack │ ├── Interfaces │ ├── IResource.php │ ├── IResponse.php │ ├── Http │ │ └── IClient.php │ └── IRequest.php │ ├── Subscription.php │ ├── Customer.php │ ├── Plan.php │ ├── Response.php │ ├── Paystack.php │ ├── Http │ └── HttpClient.php │ ├── Transaction.php │ ├── Request.php │ └── Traits │ └── ResourceTrait.php ├── tests ├── TestCase.php ├── CustomerTest.php └── PaystackTest.php ├── .travis.yml ├── phpunit.xml ├── example └── index.php ├── composer.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | .idea 3 | composer.lock 4 | -------------------------------------------------------------------------------- /lib/Paystack/Interfaces/IResource.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/Paystack/Subscription.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertEquals(true,true); 17 | } 18 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests 5 | 6 | 7 | 8 | 9 | src/ 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /lib/Paystack/Interfaces/IResponse.php: -------------------------------------------------------------------------------- 1 | assertEquals($actualApiKey, $expectedApiKey); 25 | } 26 | } -------------------------------------------------------------------------------- /lib/Paystack/Customer.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/Paystack/Plan.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /example/index.php: -------------------------------------------------------------------------------- 1 | 'damiperfect@yahoo.com', 11 | // 'first_name' => 'Perfecto', 12 | // 'last_name' => 'Makanjuo', 13 | // 'phone' => '08064474572' 14 | // ]); 15 | // 16 | // echo 'Customer\'s first name is: '.$customer->first_name; 17 | 18 | // $payment = \Paystack\Transaction::initialize([ 19 | // 'email' => 'damiperfect@gmail.com', 20 | // 'amount' => '3000' 21 | // ]); 22 | 23 | $transaction = \Paystack\Transaction::verify('3beczdakli'); 24 | echo $transaction->amount; 25 | } 26 | catch(Exception $e) 27 | { 28 | echo $e->getMessage(); 29 | } 30 | 31 | ?> -------------------------------------------------------------------------------- /lib/Paystack/Response.php: -------------------------------------------------------------------------------- 1 | setCode($code); 23 | $this->setBody($body); 24 | } 25 | 26 | public function getCode() 27 | { 28 | return $this->_code; 29 | } 30 | 31 | public function getBody() 32 | { 33 | return $this->_body; 34 | } 35 | 36 | private function setCode($code) 37 | { 38 | $this->_code = $code; 39 | } 40 | 41 | private function setBody($body) 42 | { 43 | $this->_body = $body; 44 | } 45 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "perfectmak/paystack-php", 3 | "type": "library", 4 | "description": "Paystack Library for PHP", 5 | "keywords": ["paystack","payment"], 6 | "homepage": "https://github.com/Seldaek/monolog", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Perfect Makanju", 11 | "email": "damiperfect@gmail.com", 12 | "homepage": "http://about.me/perfectmak", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.5", 18 | "nategood/httpful": "*" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "~4.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "": "lib", 26 | "PaystackTest\\": "test" 27 | }, 28 | "classmap": [ 29 | "tests/TestCase.php" 30 | ] 31 | } 32 | } -------------------------------------------------------------------------------- /lib/Paystack/Paystack.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/Paystack/Interfaces/IRequest.php: -------------------------------------------------------------------------------- 1 | addHeader('Authorization', 'Bearer '.Paystack::getApiKey()) 36 | ->uri($request->getUrl()) 37 | ->method($request->getType()) 38 | ->addHeaders($request->getHeaders()) 39 | ->body($request->getBody()) 40 | ->sendsJson() 41 | ->send(); 42 | 43 | $iResponse = new Response($response->code, $response->body); 44 | return $iResponse; 45 | } 46 | } -------------------------------------------------------------------------------- /lib/Paystack/Transaction.php: -------------------------------------------------------------------------------- 1 | _attributes; 39 | } 40 | 41 | public static function get($id) 42 | { 43 | return self::_get(self::url('/'.$id)); 44 | } 45 | 46 | public static function all() 47 | { 48 | throw new \Exception(); 49 | } 50 | } 51 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Paystack PHP 2 | 3 | [![Build Status](https://secure.travis-ci.org/perfectmak/paystack-php.png?branch=master)](http://travis-ci.org/perfectmak/paystack-php) 4 | 5 | Paystack PHP is a library for using the [Paystack](http://paystack.com) API from PHP. 6 | 7 | While there are other PHP libraries for Paystack, this library is designed to make it less cumbersome to implement a 8 | payment flow on the Paystack payment platform. 9 | 10 | ## Installation 11 | 12 | To install using composer 13 | ``` 14 | composer install perfectmak/paystack-php 15 | ``` 16 | 17 | ## Usage 18 | First you initialize the library with your secret key 19 | 20 | ```php 21 | \Paystack\Paystack::init('__secret_key_here__'); 22 | ``` 23 | 24 | ### Transaction 25 | 26 | #### Initialize a transaction 27 | ```php 28 | $payment = \Paystack\Transaction::initialize([ 29 | 'email' => 'jame@gosling.com', 30 | 'amount' => '3000' 31 | ]); 32 | ``` 33 | 34 | ### Customer 35 | 36 | #### Create Customer 37 | ```php 38 | $customer = \Paystack\Customer::create([ 39 | 'email' => 'google@gosling.com', 40 | 'first_name' => 'Perfect', 41 | 'last_name' => 'Makanju', 42 | 'phone' => 'xxxxxxx' 43 | ]); 44 | 45 | echo 'Customer\'s first name is: '.$customer->first_name; 46 | ``` 47 | 48 | ## Todo 49 | This library is far from complete and not yet stable. So I don't advice using it yet. 50 | 51 | - Finish up all the resources 52 | - Tests for each Resource 53 | - Fix travis build script -------------------------------------------------------------------------------- /lib/Paystack/Request.php: -------------------------------------------------------------------------------- 1 | _headers[$key] = $value; 33 | 34 | return $this; 35 | } 36 | 37 | public function getHeader($key) 38 | { 39 | return $this->_headers[$key]; 40 | } 41 | 42 | public function getHeaders() 43 | { 44 | return $this->_headers; 45 | } 46 | 47 | public function setBody($body) 48 | { 49 | $this->_body = $body; 50 | 51 | return $this; 52 | } 53 | 54 | public function getBody() 55 | { 56 | return $this->_body; 57 | } 58 | 59 | public function setUrl($url) 60 | { 61 | $this->_url = $url; 62 | 63 | return $this; 64 | } 65 | 66 | public function getUrl() 67 | { 68 | return $this->_url; 69 | } 70 | 71 | public function setType($type) 72 | { 73 | $this->_type = $type; 74 | 75 | return $this; 76 | } 77 | 78 | public function getType() 79 | { 80 | return $this->_type; 81 | } 82 | 83 | 84 | public static function setClient(IClient $client) 85 | { 86 | self::$_httpClient = $client; 87 | } 88 | 89 | public static function getClient() 90 | { 91 | return self::$_httpClient; 92 | } 93 | 94 | public function send() 95 | { 96 | if(!is_null(self::$_httpClient)) 97 | return self::$_httpClient->sendRequest($this); 98 | 99 | throw new \Exception('No HttpClient specified for requests'); 100 | } 101 | } -------------------------------------------------------------------------------- /lib/Paystack/Traits/ResourceTrait.php: -------------------------------------------------------------------------------- 1 | _attributes = $params; 24 | else{ 25 | $this->_attributes = json_decode(json_encode($params), true); 26 | } 27 | } 28 | 29 | 30 | /** 31 | * @param $url 32 | * @param array $params 33 | * @return static 34 | * @throws \Exception 35 | */ 36 | protected static function _create($url, array $params) 37 | { 38 | $response = (new Request()) 39 | ->setUrl(Paystack::api($url)) 40 | ->setType(IRequest::TYPE_POST) 41 | ->setBody(json_encode($params)) 42 | ->send(); 43 | 44 | 45 | if($response->getCode() === IResponse::CODE_VALIDATION_ERROR) 46 | throw new \Exception($response->getBody()->message); 47 | 48 | return new static($response->getBody()->data); 49 | } 50 | 51 | protected static function _get($url) 52 | { 53 | $response = (new Request()) 54 | ->setUrl(Paystack::api($url)) 55 | ->setType(IRequest::TYPE_GET) 56 | ->send(); 57 | 58 | 59 | if($response->getCode() === IResponse::CODE_VALIDATION_ERROR) 60 | throw new \Exception($response->getBody()->message); 61 | 62 | return new static($response->getBody()->data); 63 | } 64 | 65 | public function __get($key) 66 | { 67 | if(array_key_exists($key, $this->_attributes)) 68 | { 69 | return $this->_attributes[$key]; 70 | } 71 | else 72 | return null; 73 | } 74 | 75 | /** 76 | * @param $key 77 | * @param $value 78 | * @todo Should check in a list of properties defined in Resource class 79 | */ 80 | public function __set($key, $value) 81 | { 82 | $this->_attributes[$key] = $value; 83 | } 84 | 85 | protected static function url($path = ''){ 86 | return rtrim(self::$resourceUrl, '/').'/' 87 | .ltrim($path, '/'); 88 | } 89 | } --------------------------------------------------------------------------------