| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GloBee\PaymentApi\Connectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GloBee\PaymentApi\PaymentApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class GloBeeCurlConnector extends Connector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $baseUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var CurlWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $platforms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * GloBeeCurlConnector constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string           $apiKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string           $baseUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param array            $platforms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param CurlWrapper|null $curlConnector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 9 |  |     public function __construct($apiKey, $baseUrl = null, array $platforms, CurlWrapper $curlConnector = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |         $this->apiKey = $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  |         $this->baseUrl = $baseUrl ?: 'https://globee.com/payment-api'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |         $this->platforms = $platforms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 9 |  |         $this->client = $curlConnector ?: new CurlWrapper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 8 |  |     public function getJson($uri) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 8 |  |         return $this->send('GET', $uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |     public function postJson($uri, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $json = json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         return $this->send('POST', $uri, $json, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             'Content-Type' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param array  $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 9 |  |     protected function send($method, $uri, $body = null, array $headers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 9 |  |         $headers = $this->compileHeaders($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |         $this->client->setOptions([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 9 |  |             CURLOPT_RETURNTRANSFER => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |             CURLOPT_URL => $this->baseUrl.'/'.$uri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 9 |  |             CURLOPT_ACCEPT_ENCODING => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |             CURLOPT_CUSTOMREQUEST => $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |             CURLOPT_HTTPHEADER => $headers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 9 |  |             CURLOPT_USERAGENT => $this->getUserAgentString(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 9 |  |         if ($method !== 'GET' && $body) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             $this->client->setOption(CURLOPT_POSTFIELDS, $body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 9 |  |         $response = $this->client->exec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 9 |  |         $httpcode = $this->client->getInfo(CURLINFO_HTTP_CODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 9 |  |         if ($httpcode >= 400) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 6 |  |             $this->handleErrors($httpcode, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |         return json_decode($response, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param array $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 9 |  |     protected function compileHeaders(array $headers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 9 |  |                 'X-AUTH-KEY' => $this->apiKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 9 |  |             ] + $headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 9 |  |         $return = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 9 |  |         foreach ($headers as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 9 |  |             $return[] = $key.': '.$value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 9 |  |         return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 9 |  |     protected function getUserAgentString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |         $platforms = $this->platforms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 9 |  |         array_walk($platforms, function (&$version, $platform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             $version = $platform.'/'.$version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 9 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 9 |  |         $platforms = implode(' ', $platforms); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 9 |  |         $userAgent = sprintf('GloBeePaymentSdk/%s (%s) PHP/%s', PaymentApi::VERSION, PHP_OS, PHP_VERSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 9 |  |         return trim($userAgent.' '.$platforms); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  |