| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace bSecure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use bSecure\Helpers\Constant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class bSecure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class bSecure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** @var string The base URL to be used for requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public static $apiBase = Constant::AUTH_SERVER_URL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var string The login URL to be used for requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public static $loginBase = Constant::LOGIN_REDIRECT_URL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string The bSecure auth token to be used for Connect requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static $authToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var string The bSecure client id to be used for Connect requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private static $clientId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var string The bSecure client secret to be used for Connect requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private static $clientSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string The bSecure application environment to be used for Connect requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public static $appEnv = Constant::DEFAULT_APP_ENVIRONMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string The bSecure application environment to be used for Connect requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public static $authTokenEnv = Constant::DEFAULT_APP_ENVIRONMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var null|string The version of the Stripe API to use for requests. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public static $apiVersion = Constant::API_VERSION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var array The application's information (name, version, URL) */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public static $appInfo = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public static $initialize = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Sets the client_id to be used for Connect requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $clientId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public static function initialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         self::$clientId = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self::$initialize = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self::$clientSecret = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         self::$authToken = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         self::$authTokenEnv = Constant::DEFAULT_APP_ENVIRONMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         self::$appEnv = Constant::DEFAULT_APP_ENVIRONMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Sets the client_id to be used for Connect requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $clientId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public static function setClientId($clientId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         self::$authToken = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         self::$clientId = $clientId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return string the client_id used for Connect requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public static function getClientId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return self::$clientId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Sets the clientSecret to be used for Connect requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param string $clientSecret | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public static function setClientSecret($clientSecret) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         self::$authToken = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self::$clientSecret = $clientSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return string the client_id used for Connect requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public static function getClientSecret() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return self::$clientSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Sets the app_environment to be used for Connect requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $appEnv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public static function setAppEnvironment($appEnv) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $val = in_array($appEnv, Constant::APP_ENVIRONMENT) ? $appEnv : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         self::$appEnv = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         self::$authTokenEnv = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return string the Auth Token used for requests | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     public static function getAuthToken() | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         if(self::$authToken == null || gettype(self::$authToken) != "string"){ | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             (array) $token =  self::setAuthToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             $tokenBody = $token->body; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             if(array_key_exists('access_token',$tokenBody)){ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 self::$authTokenEnv = array_key_exists('environment',$tokenBody) ? $tokenBody['environment'] :null; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 self::$authToken = array_key_exists('access_token',$tokenBody) ? $tokenBody['access_token'] :null; | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             }else{ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 return $token; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         if(self::$appEnv == self::$authTokenEnv) | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             return self::$authToken; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         else | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |             throw new Exception\UnexpectedValueException('Selected environment keys are invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Sets the authToken to be used for Connect requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $authToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public static function setAuthToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return OAuth::token(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return array | null The application's environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public static function getAppInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         self::setAppInfo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return self::$appInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return string The application's api version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     public static function getApiVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return self::$apiVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param string $authToken The application's authentication token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param null|string $environment The application's environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @param null|string $version The application's API version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     public static function setAppInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         self::$appInfo = self::$appInfo ?: []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         self::$appInfo['client_id'] = self::$clientId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         self::$appInfo['client_secret'] = self::$clientSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         self::$appInfo['environment'] = self::$appEnv; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  | } |