diff --git a/composer.json b/composer.json index 820338f..27d5341 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php-http/client-implementation": "^1", "php-http/message": "^1.5", "php-http/discovery": "^1.14", - "symfony/http-foundation": "^6" + "symfony/http-foundation": "^6", + "guzzlehttp/psr7": "^2.3" }, "require-dev": { "php-http/mock-client": "^1", diff --git a/composer.lock b/composer.lock index b9621cb..46ee41f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "841c259ae4952442e7eaa53f1410765c", + "content-hash": "c176a4d7f47087c07fb8b86b76044123", "packages": [ { "name": "clue/stream-filter", diff --git a/lib/Adapter.php b/lib/Adapter.php index 451e7b4..f1cac2a 100644 --- a/lib/Adapter.php +++ b/lib/Adapter.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Payeer; +use Http\Discovery\HttpClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use GuzzleHttp\Psr7\Utils; + class Adapter { private array $arParams = array(); @@ -19,37 +23,28 @@ class Adapter protected function request(array $req = array()) { + $client = HttpClientDiscovery::find(); + $messageFactory = Psr17FactoryDiscovery::findRequestFactory(); $msec = round(microtime(true) * 1000); + $req['post']['ts'] = $msec; $post = json_encode($req['post']); $sign = hash_hmac('sha256', $req['method'] . $post, $this->arParams['key']); - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, "https://payeer.com/api/trade/" . $req['method']); + $request = $messageFactory->createRequest('POST', 'https://payeer.com/api/trade/' . $req['method']) + ->withHeader('Content-Type', 'application/json') + ->withHeader('API-ID', $this->arParams['id']) + ->withHeader('API-SIGN', $sign) + ->withBody(Utils::streamFor($post)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HEADER, false); - //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $response = $client->sendRequest($request); + if ($response->getStatusCode() !== 200) { + throw new \Exception('Server returned ' . $response->getStatusCode() . ':' . $response->getReasonPhrase()); + } - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - - curl_setopt( - $ch, - CURLOPT_HTTPHEADER, - array( - "Content-Type: application/json", - "API-ID: " . $this->arParams['id'], - "API-SIGN: " . $sign - ) - ); - - $response = curl_exec($ch); - curl_close($ch); - - $arResponse = json_decode($response, true); + $arResponse = json_decode((string) $response->getBody(), true); if ($arResponse['success'] !== true) { $this->arError = $arResponse['error'];