├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── examples ├── auth-with-token.php ├── create-charge.php ├── create-checkout.php ├── create-refund.php ├── create-user.php ├── get-charge.php ├── get-charges.php ├── get-refund.php ├── get-refunds.php ├── get-user.php ├── get-users.php ├── test-auth-with-bearer.php ├── test-auth-with-rsa.php └── update-charge.php ├── init.php └── lib ├── Amount.php ├── Api.php ├── Charge.php ├── Checkout.php ├── Refund.php ├── Request.php └── User.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /examples/authentication.json 3 | composer.lock 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-2019 Satispay 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Satispay Online API PHP SDK 2 | [![Packagist Version](https://img.shields.io/packagist/v/satispay/online-api-php-sdk.svg?style=flat-square)](https://packagist.org/packages/satispay/online-api-php-sdk) 3 | [![Packagist Downloads](https://img.shields.io/packagist/dt/satispay/online-api-php-sdk.svg?style=flat-square)](https://packagist.org/packages/satispay/online-api-php-sdk) 4 | 5 | ## Deprecated API 6 | For new integrations please use https://github.com/satispay/gbusiness-api-php-sdk. 7 | 8 | ## Installation 9 | Run the following command: 10 | 11 | ```bash 12 | composer require satispay/online-api-php-sdk 13 | ``` 14 | 15 | If you do not wish to use Composer, import the `init.php` file. 16 | 17 | ```php 18 | require_once("/path/init.php"); 19 | ``` 20 | 21 | ## Documentation 22 | https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html 23 | 24 | ## Authenticate with Bearer 25 | Sign in to your [Dashboard](https://business.satispay.com) at [business.satispay.com](https://business.satispay.com), click "Negozi Online", click on "Crea codice di attivazione" and select "Security bearer" on top menu. 26 | 27 | ```php 28 | \SatispayOnline\Api::setSecurityBearer("osh_..."); 29 | ``` 30 | 31 | ## Authenticate with RSA Signature 32 | Sign in to your [Dashboard](https://business.satispay.com) at [business.satispay.com](https://business.satispay.com), click "Negozi Online", and then click on "Genera un token di attivazione" to generate an activation token. 33 | 34 | Use the activation token with the `authenticateWithToken` function to generate and exchange a pair of RSA keys. 35 | 36 | Save the keys in your database or in a **safe place** not accesibile from your website. 37 | ```php 38 | // Authenticate and generate the keys 39 | $authentication = \SatispayOnline\Api::authenticateWithToken("XXXXXX"); 40 | 41 | // Export keys 42 | $publicKey = $authentication->publicKey; 43 | $privateKey = $authentication->privateKey; 44 | $keyId = $authentication->keyId; 45 | ``` 46 | 47 | Reuse the keys after authentication. 48 | ```php 49 | // Keys variables 50 | $publicKey = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhk..."; 51 | $privateKey = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBg..."; 52 | $keyId = "ldg9sbq283og7ua1abpj989kbbm2g60us6f18c1sciq..."; 53 | 54 | // Set keys 55 | \SatispayOnline\Api::setPublicKey($publicKey); 56 | \SatispayOnline\Api::setPrivateKey($privateKey); 57 | \SatispayOnline\Api::setKeyId($keyId); 58 | 59 | // Test the authentication 60 | \SatispayOnline\Api::testAuthentication(); 61 | ``` 62 | 63 | ## Enable Sandbox 64 | To enable sandbox use `setSandbox` function. 65 | ```php 66 | \SatispayOnline\Api::setSandbox(true); 67 | ``` 68 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "satispay/online-api-php-sdk", 3 | "description": "Satispay Online API PHP SDK", 4 | "license": "MIT", 5 | "keywords": [ 6 | "satispay", 7 | "online", 8 | "api", 9 | "php", 10 | "sdk" 11 | ], 12 | "homepage": "https://www.satispay.com", 13 | "version": "2.0.1", 14 | "authors": [ 15 | { 16 | "name": "Satispay", 17 | "homepage": "https://www.satispay.com" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=5.4.0", 22 | "ext-curl": "*", 23 | "ext-json": "*", 24 | "ext-mbstring": "*" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "SatispayOnline\\": "lib/" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /examples/auth-with-token.php: -------------------------------------------------------------------------------- 1 | publicKey; 10 | $privateKey = $authentication->privateKey; 11 | $keyId = $authentication->keyId; 12 | 13 | file_put_contents("authentication.json", json_encode(array( 14 | "public_key" => $publicKey, 15 | "private_key" => $privateKey, 16 | "key_id" => $keyId 17 | ), JSON_PRETTY_PRINT)); 18 | -------------------------------------------------------------------------------- /examples/create-charge.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $charge = \SatispayOnline\Charge::create(array( 14 | "user_id" => "c7cfe7ea-ad2b-40f6-8098-253bc701a2b3", 15 | "amount" => 199, 16 | "currency" => "EUR" 17 | )); 18 | 19 | var_dump($charge); 20 | -------------------------------------------------------------------------------- /examples/create-checkout.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $checkout = \SatispayOnline\Checkout::create(array( 14 | "phone_number" => "", 15 | "amount_unit" => 199, 16 | "currency" => "EUR", 17 | "description" => "", 18 | "redirect_url" => "" 19 | )); 20 | 21 | var_dump($checkout); 22 | -------------------------------------------------------------------------------- /examples/create-refund.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $refund = \SatispayOnline\Refund::create(array( 14 | "charge_id" => "6ae4cfd9-5f79-441e-9a94-3dd157708351", 15 | "amount" => 199, 16 | "currency" => "EUR" 17 | )); 18 | 19 | var_dump($refund); 20 | -------------------------------------------------------------------------------- /examples/create-user.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $user = \SatispayOnline\User::create(array( 14 | "phone_number" => "+390000000000" 15 | )); 16 | 17 | var_dump($user); 18 | -------------------------------------------------------------------------------- /examples/get-charge.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $charge = \SatispayOnline\Charge::get("6ae4cfd9-5f79-441e-9a94-3dd157708351"); 14 | 15 | var_dump($charge); 16 | -------------------------------------------------------------------------------- /examples/get-charges.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $charges = \SatispayOnline\Charge::all(); 14 | 15 | var_dump($charges); 16 | -------------------------------------------------------------------------------- /examples/get-refund.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $refund = \SatispayOnline\Refund::get("f37af3e1-a25c-4924-a8ea-e88a7bbbaea4"); 14 | 15 | var_dump($refund); 16 | -------------------------------------------------------------------------------- /examples/get-refunds.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $refunds = \SatispayOnline\Refund::all(); 14 | 15 | var_dump($refunds); 16 | -------------------------------------------------------------------------------- /examples/get-user.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $user = \SatispayOnline\User::get("c7cfe7ea-ad2b-40f6-8098-253bc701a2b3"); 14 | 15 | var_dump($user); 16 | -------------------------------------------------------------------------------- /examples/get-users.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $users = \SatispayOnline\User::all(); 14 | 15 | var_dump($users); 16 | -------------------------------------------------------------------------------- /examples/test-auth-with-bearer.php: -------------------------------------------------------------------------------- 1 | security_bearer); 10 | 11 | $result = \SatispayOnline\Api::testAuthentication(); 12 | 13 | var_dump($result); 14 | -------------------------------------------------------------------------------- /examples/test-auth-with-rsa.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $result = \SatispayOnline\Api::testAuthentication(); 14 | 15 | var_dump($result); 16 | -------------------------------------------------------------------------------- /examples/update-charge.php: -------------------------------------------------------------------------------- 1 | public_key); 10 | \SatispayOnline\Api::setPrivateKey($authData->private_key); 11 | \SatispayOnline\Api::setKeyId($authData->key_id); 12 | 13 | $charge = \SatispayOnline\Charge::create(array( 14 | "user_id" => "c7cfe7ea-ad2b-40f6-8098-253bc701a2b3", 15 | "amount" => 199, 16 | "currency" => "EUR" 17 | )); 18 | 19 | var_dump($charge); 20 | 21 | $ucharge = \SatispayOnline\Charge::update($charge->id, array( 22 | "charge_state" => "CANCELED" 23 | )); 24 | 25 | var_dump($ucharge); 26 | -------------------------------------------------------------------------------- /init.php: -------------------------------------------------------------------------------- 1 | true 17 | )); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/Api.php: -------------------------------------------------------------------------------- 1 | "sha256", 26 | "private_key_bits" => 2048 27 | )); 28 | 29 | openssl_pkey_export($pkeyResource, $generatedPrivateKey); 30 | 31 | $pkeyResourceDetails = openssl_pkey_get_details($pkeyResource); 32 | $generatedPublicKey = $pkeyResourceDetails["key"]; 33 | 34 | $requestResult = Request::post("/g_business/v1/authentication_keys", array( 35 | "body" => array( 36 | "public_key" => $generatedPublicKey, 37 | "token" => $token 38 | ) 39 | )); 40 | 41 | self::$privateKey = $generatedPrivateKey; 42 | self::$publicKey = $generatedPublicKey; 43 | self::$keyId = $requestResult->key_id; 44 | 45 | $returnClass = new ApiAuthentication(); 46 | $returnClass->privateKey = $generatedPrivateKey; 47 | $returnClass->publicKey = $generatedPublicKey; 48 | $returnClass->keyId = $requestResult->key_id; 49 | return $returnClass; 50 | } 51 | 52 | /** 53 | * Test authentication keys 54 | */ 55 | public static function testAuthentication() { 56 | $result = Request::get("/wally-services/protocol/tests/signature", array( 57 | "sign" => true 58 | )); 59 | 60 | if ($result->authentication_key->role !== "ONLINE_SHOP") { 61 | throw new \Exception("Invalid authentication"); 62 | } 63 | 64 | return $result; 65 | } 66 | 67 | /** 68 | * Get env 69 | * @return string 70 | */ 71 | public static function getEnv() { 72 | return self::$env; 73 | } 74 | /** 75 | * Set env 76 | * @param string $value 77 | */ 78 | public static function setEnv($value) { 79 | self::$env = $value; 80 | if ($value == "production") { 81 | self::$authservicesUrl = "https://authservices.satispay.com"; 82 | } else { 83 | self::$authservicesUrl = "https://".$value.".authservices.satispay.com"; 84 | } 85 | } 86 | 87 | /** 88 | * Get private key 89 | * @return string 90 | */ 91 | public static function getPrivateKey() { 92 | return self::$privateKey; 93 | } 94 | /** 95 | * Set private key 96 | * @param string $value 97 | */ 98 | public static function setPrivateKey($value) { 99 | self::$privateKey = $value; 100 | } 101 | 102 | /** 103 | * Get public key 104 | * @return string 105 | */ 106 | public static function getPublicKey() { 107 | return self::$publicKey; 108 | } 109 | /** 110 | * Set public key 111 | * @param string $value 112 | */ 113 | public static function setPublicKey($value) { 114 | self::$publicKey = $value; 115 | } 116 | 117 | /** 118 | * Get key id 119 | * @return string 120 | */ 121 | public static function getKeyId() { 122 | return self::$keyId; 123 | } 124 | /** 125 | * Set key id 126 | * @param string $value 127 | */ 128 | public static function setKeyId($value) { 129 | self::$keyId = $value; 130 | } 131 | 132 | /** 133 | * Get version 134 | * @return string 135 | */ 136 | public static function getVersion() { 137 | return self::$version; 138 | } 139 | 140 | /** 141 | * Get authservices url 142 | * @return string 143 | */ 144 | public static function getAuthservicesUrl() { 145 | return self::$authservicesUrl; 146 | } 147 | 148 | /** 149 | * Get security bearer 150 | * @return string 151 | */ 152 | public static function getSecurityBearer() { 153 | return self::$securityBearer; 154 | } 155 | /** 156 | * Set security bearer 157 | * @param string $securityBearer 158 | */ 159 | public static function setSecurityBearer($securityBearer) { 160 | self::$securityBearer = $securityBearer; 161 | } 162 | 163 | /** 164 | * Is sandbox enabled? 165 | * @return boolean 166 | */ 167 | public static function getSandbox() { 168 | if (self::$env == "staging") { 169 | return true; 170 | } else { 171 | return false; 172 | } 173 | } 174 | /** 175 | * Enable or disable sandbox 176 | * @param boolean $value 177 | */ 178 | public static function setSandbox($value) { 179 | if ($value == true) { 180 | self::setEnv("staging"); 181 | } else { 182 | self::setEnv("production"); 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /lib/Charge.php: -------------------------------------------------------------------------------- 1 | $body, 12 | "sign" => true 13 | )); 14 | } 15 | 16 | /** 17 | * Get charge 18 | * @param string $id 19 | */ 20 | public static function get($id) { 21 | return Request::get("/online/v1/charges/$id", array( 22 | "sign" => true 23 | )); 24 | } 25 | 26 | /** 27 | * Get charges list 28 | * @param array $options 29 | */ 30 | public static function all($options = array()) { 31 | $queryString = ""; 32 | if (!empty($options)) { 33 | $queryString .= "?"; 34 | $queryString .= http_build_query($options); 35 | } 36 | return Request::get("/online/v1/charges$queryString", array( 37 | "sign" => true 38 | )); 39 | } 40 | 41 | /** 42 | * Update charge 43 | * @param string $id 44 | * @param array $body 45 | */ 46 | public static function update($id, $body) { 47 | return Request::put("/online/v1/charges/$id", array( 48 | "body" => $body, 49 | "sign" => true 50 | )); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /lib/Checkout.php: -------------------------------------------------------------------------------- 1 | $body, 12 | "sign" => true 13 | )); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lib/Refund.php: -------------------------------------------------------------------------------- 1 | $body, 12 | "sign" => true 13 | )); 14 | } 15 | 16 | /** 17 | * Get refund 18 | * @param string $id 19 | */ 20 | public static function get($id) { 21 | return Request::get("/online/v1/refunds/$id", array( 22 | "sign" => true 23 | )); 24 | } 25 | 26 | /** 27 | * Get refunds list 28 | * @param array $options 29 | */ 30 | public static function all($options = array()) { 31 | $queryString = ""; 32 | if (!empty($options)) { 33 | $queryString .= "?"; 34 | $queryString .= http_build_query($options); 35 | } 36 | return Request::get("/online/v1/refunds$queryString", array( 37 | "sign" => true 38 | )); 39 | } 40 | 41 | /** 42 | * Update refund 43 | * @param string $id 44 | * @param array $body 45 | */ 46 | public static function update($id, $body) { 47 | return Request::put("/online/v1/refunds/$id", array( 48 | "body" => $body, 49 | "sign" => true 50 | )); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /lib/Request.php: -------------------------------------------------------------------------------- 1 | $path, 15 | "method" => "GET" 16 | ); 17 | 18 | if (!empty($options["sign"])) { 19 | $requestOptions["sign"] = $options["sign"]; 20 | } 21 | 22 | return self::request($requestOptions); 23 | } 24 | 25 | /** 26 | * POST request 27 | * @param string $path 28 | * @param array $options 29 | */ 30 | public static function post($path, $options = array()) { 31 | $requestOptions = array( 32 | "path" => $path, 33 | "method" => "POST", 34 | "body" => $options["body"] 35 | ); 36 | 37 | if (!empty($options["sign"])) { 38 | $requestOptions["sign"] = $options["sign"]; 39 | } 40 | 41 | return self::request($requestOptions); 42 | } 43 | 44 | /** 45 | * PUT request 46 | * @param string $path 47 | * @param array $options 48 | */ 49 | public static function put($path, $options = array()) { 50 | $requestOptions = array( 51 | "path" => $path, 52 | "method" => "PUT", 53 | "body" => $options["body"] 54 | ); 55 | 56 | if (!empty($options["sign"])) { 57 | $requestOptions["sign"] = $options["sign"]; 58 | } 59 | 60 | return self::request($requestOptions); 61 | } 62 | 63 | /** 64 | * PATCH request 65 | * @param string $path 66 | * @param array $options 67 | */ 68 | public static function patch($path, $options = array()) { 69 | $requestOptions = array( 70 | "path" => $path, 71 | "method" => "PATCH", 72 | "body" => $options["body"] 73 | ); 74 | 75 | if (!empty($options["sign"])) { 76 | $requestOptions["sign"] = $options["sign"]; 77 | } 78 | 79 | return self::request($requestOptions); 80 | } 81 | 82 | /** 83 | * Sign request 84 | * @param array $options 85 | */ 86 | private static function signRequest($options = array()) { 87 | $headers = array(); 88 | $authorizationHeader = ""; 89 | 90 | $privateKey = Api::getPrivateKey(); 91 | $keyId = Api::getKeyId(); 92 | $securityBearer = Api::getSecurityBearer(); 93 | 94 | if (!empty($privateKey) && !empty($keyId)) { 95 | $date = date("r"); 96 | array_push($headers, "Date: ".$date); 97 | 98 | $signature = "(request-target): ".strtolower($options["method"])." ".$options["path"]."\n"; 99 | $signature .= "host: ".str_replace("https://", "", Api::getAuthservicesUrl())."\n"; 100 | if (!empty($options["body"])) { 101 | $digest = base64_encode(hash("sha256", $options["body"], true)); 102 | array_push($headers, "Digest: SHA-256=".$digest); 103 | 104 | $signature .= "content-type: application/json\n"; 105 | $signature .= "content-length: ".strlen($options["body"])."\n"; 106 | $signature .= "digest: SHA-256=$digest\n"; 107 | } 108 | $signature .= "date: $date"; 109 | 110 | openssl_sign($signature, $signedSignature, $privateKey, OPENSSL_ALGO_SHA256); 111 | $base64SignedSignature = base64_encode($signedSignature); 112 | 113 | $signatureHeaders = "(request-target) host date"; 114 | if (!empty($options["body"])) { 115 | $signatureHeaders = "(request-target) host content-type content-length digest date"; 116 | } 117 | 118 | $authorizationHeader = "Signature keyId=\"$keyId\", algorithm=\"rsa-sha256\", headers=\"$signatureHeaders\", signature=\"$base64SignedSignature\""; 119 | } else if (!empty($securityBearer)) { 120 | $authorizationHeader = "Bearer $securityBearer"; 121 | } 122 | 123 | if (!empty($authorizationHeader)) { 124 | array_push($headers, "Authorization: $authorizationHeader"); 125 | } 126 | 127 | return array( 128 | "headers" => $headers 129 | ); 130 | } 131 | 132 | /** 133 | * Execute request 134 | * @param array $options 135 | */ 136 | private static function request($options = array()) { 137 | $body = ""; 138 | $headers = array( 139 | "Accept: application/json", 140 | "User-Agent: ".self::$userAgentName."/".Api::getVersion() 141 | ); 142 | $method = "GET"; 143 | 144 | if (!empty($options["method"])) { 145 | $method = $options["method"]; 146 | } 147 | 148 | if (!empty($options["body"])) { 149 | array_push($headers, "Content-Type: application/json"); 150 | $body = json_encode($options["body"]); 151 | array_push($headers, "Content-Length: ".strlen($body)); 152 | } 153 | 154 | $sign = false; 155 | if (!empty($options["sign"])) { 156 | $sign = $options["sign"]; 157 | } 158 | 159 | if ($sign) { 160 | $signResult = self::signRequest(array( 161 | "body" => $body, 162 | "method" => $method, 163 | "path" => $options["path"] 164 | )); 165 | $headers = array_merge($headers, $signResult["headers"]); 166 | } 167 | 168 | $curlResult = self::curl(array( 169 | "url" => Api::getAuthservicesUrl().$options["path"], 170 | "method" => $method, 171 | "body" => $body, 172 | "headers" => $headers 173 | )); 174 | 175 | if (!empty($curlResult["errorCode"]) && !empty($curlResult["errorMessage"])) { 176 | throw new \Exception($curlResult["errorMessage"], $curlResult["errorCode"]); 177 | } 178 | 179 | $isResponseOk = true; 180 | if ($curlResult["status"] < 200 || $curlResult["status"] > 299) { 181 | $isResponseOk = false; 182 | } 183 | 184 | $responseData = json_decode($curlResult["body"]); 185 | 186 | if (!$isResponseOk) { 187 | if (!empty($responseData->message) && !empty($responseData->code) && !empty($responseData->wlt)) { 188 | throw new \Exception($responseData->message.", request id: ".$responseData->wlt, $responseData->code); 189 | } else { 190 | throw new \Exception("HTTP status is not 2xx"); 191 | } 192 | } 193 | 194 | return $responseData; 195 | } 196 | 197 | /** 198 | * Curl request 199 | * @param array $options 200 | */ 201 | private static function curl($options = array()) { 202 | $curlOptions = array(); 203 | $curl = curl_init(); 204 | 205 | $curlOptions[CURLOPT_URL] = $options["url"]; 206 | $curlOptions[CURLOPT_RETURNTRANSFER] = true; 207 | 208 | if ($options["method"] != "GET") { 209 | if ($options["method"] != "POST") { 210 | $curlOptions[CURLOPT_CUSTOMREQUEST] = $options["method"]; 211 | } 212 | $curlOptions[CURLOPT_POSTFIELDS] = $options["body"]; 213 | $curlOptions[CURLOPT_POST] = true; 214 | } else { 215 | $curlOptions[CURLOPT_HTTPGET] = true; 216 | } 217 | 218 | if (Api::getEnv() == "test") { 219 | $curlOptions[CURLOPT_VERBOSE] = true; 220 | $curlOptions[CURLOPT_SSL_VERIFYHOST] = false; 221 | $curlOptions[CURLOPT_SSL_VERIFYPEER] = false; 222 | } 223 | 224 | $curlOptions[CURLOPT_HTTPHEADER] = $options["headers"]; 225 | curl_setopt_array($curl, $curlOptions); 226 | 227 | $responseJson = curl_exec($curl); 228 | $responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); 229 | $curlErrorCode = curl_errno($curl); 230 | $curlErrorMessage = curl_error($curl); 231 | curl_close($curl); 232 | 233 | return array( 234 | "body" => $responseJson, 235 | "status" => $responseStatus, 236 | "errorCode" => $curlErrorCode, 237 | "errorMessage" => $curlErrorMessage 238 | ); 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /lib/User.php: -------------------------------------------------------------------------------- 1 | $body, 12 | "sign" => true 13 | )); 14 | } 15 | 16 | /** 17 | * Get user 18 | * @param string $id 19 | */ 20 | public static function get($id) { 21 | return Request::get("/online/v1/users/$id", array( 22 | "sign" => true 23 | )); 24 | } 25 | 26 | /** 27 | * Get users list 28 | * @param array $options 29 | */ 30 | public static function all($options = array()) { 31 | $queryString = ""; 32 | if (!empty($options)) { 33 | $queryString .= "?"; 34 | $queryString .= http_build_query($options); 35 | } 36 | return Request::get("/online/v1/users$queryString", array( 37 | "sign" => true 38 | )); 39 | } 40 | } 41 | --------------------------------------------------------------------------------