| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace YabaCon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \YabaCon\Paystack\Helpers\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Paystack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public $secret_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $use_guzzle = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Creates a new Paystack object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @param string $secret_key - Secret key for your account with Paystack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct($secret_key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         // Should never get here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             throw new \InvalidArgumentException('A Valid Pastack Secret Key must be a string that starts with \'sk_\'.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |          $this->secret_key = $secret_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |          $this->routes = $this->definedRoutes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * Router uses Guzzle for calls if GuzzleHttp is found | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function useGuzzle() | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |          $this->use_guzzle = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * definedRoutes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * gets routes defined in the YabaCon\Paystack\Routes namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * since we are using PSR-4. It is safe to assume that all php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * files in the folder are names of the classes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * @return the list of defined Routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * @access private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * @see YabaCon\Paystack\Routes\Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private function definedRoutes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $routes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $files = scandir(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Paystack'.DIRECTORY_SEPARATOR.'Routes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             if ('php'===pathinfo($file, PATHINFO_EXTENSION)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $routes[] = strtolower(substr($file, 0, strrpos($file, "."))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * __call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  * Magic Method for getOne on routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  * @param $method - a string whose title case is a class in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  *                  YabaCon\Paystack\Routes namespace implementing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  *                  YabaCon\Paystack\Contracts\RouteInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  * @param $args - arguments sent to the magic method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * @return the result of calling /{route}/get on the api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  * @see YabaCon\Paystack\Routes\Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function __call($method, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |    /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |    attempt to call getOne when the route is called directly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |   translates to /{root}/{get}/{id} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (in_array($method, $this->routes, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $route = new Router($method, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             if (count($args) === 1 && is_integer($args[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |            // no params, just one arg... the id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $args = [[], [ Router::ID_KEY => $args[0] ] ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 return $route->__call('getOne', $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } elseif (count($args) === 2 && is_integer($args[0]) && is_array($args[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |            // there are params, and just one arg... the id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $args = [$args[1], [ Router::ID_KEY => $args[0] ] ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 return $route->__call('getOne', $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |    // Should never get here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         throw new \InvalidArgumentException('Route "' . $method . '" only accepts an integer id and an optional array of paging arguments.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  * Call this function to register the custom auto loader for Paystack. * Required only if not using composer. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public static function registerAutoloader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         spl_autoload_register( /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             * $class_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             * Insert description here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             * @return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             * @access | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             * @see | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             * @since | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             function ($class_name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $file = dirname(__FILE__) . DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $file .= str_replace([ 'YabaCon\\', '\\' ], ['', DIRECTORY_SEPARATOR ], $class_name) . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 include_once $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  * __get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  * Insert description here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  * @return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  * @access | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  * @see | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  * @since | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function __get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if (in_array($name, $this->routes, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return new Router($name, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |