diff --git a/composer.json b/composer.json index 0f09cb9..c28716a 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,7 @@ ], "require": { "php": "^7.2|^8.0", - "guzzlehttp/guzzle": "^6.0|^7.4", - "phpseclib/phpseclib": "^3.0", + "guzzlehttp/guzzle": "^6.0|^7.4", "ext-json": "*", "ext-openssl": "*" }, diff --git a/src/Pesa.php b/src/Pesa.php index 5da1c9b..a15c95c 100644 --- a/src/Pesa.php +++ b/src/Pesa.php @@ -5,7 +5,7 @@ use Exception; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; -use phpseclib\Crypt\RSA; +use InvalidArgumentException; /** * @package Openpesa\SDK @@ -23,11 +23,7 @@ class Pesa * @access private */ private $client; - /** - * @var null|RSA - * @access private - */ - private $rsa; + /** * BASE DOMAIN @@ -93,7 +89,6 @@ class Pesa ]; - /** * Pesa constructor. * @@ -102,22 +97,20 @@ class Pesa * @param null $client * @param null $rsa */ - public function __construct(array $options, $client = null, $rsa = null) + public function __construct(array $options, $client = null,) { - $options['client_options'] = $options['client_options'] ?? array(); + if (!key_exists('api_key', $options)) throw new InvalidArgumentException("api_key is required"); + if (!key_exists('public_key', $options)) throw new InvalidArgumentException("public_key is required"); + + $options['client_options'] = $options['client_options'] ?? []; $options['persistent_session'] = $options['persistent_session'] ?? false; $options['service_provider_code'] = $options['service_provider_code'] ?? null; $options['country'] = $options['country'] ?? null; $options['currency'] = $options['currency'] ?? null; - if ($options['api_key'] ?? null) throw new Exception("api_key is required"); - if ($options['public_key'] ?? null) throw new Exception("public_key is required"); - $this->options = $options; $this->client = $this->makeClient($options, $client); - - $this->rsa = ($rsa instanceof RSA) ? $rsa : new RSA(); } private function makeClient($options, $client = null): Client @@ -151,9 +144,13 @@ private function makeClient($options, $client = null): Client */ private function encryptKey($key): string { - $this->rsa->loadKey($this->options['public_key']); - $this->rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); - return base64_encode($this->rsa->encrypt($key)); + $publicKey = openssl_pkey_get_public("-----BEGIN PUBLIC KEY-----\n" . $this->options['public_key'] . "\n-----END PUBLIC KEY-----"); + if ($publicKey === false) { + throw new Exception("Invalid public key"); + } + + openssl_public_encrypt($key, $encrypted, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); + return base64_encode($encrypted); } /** diff --git a/tests/PesaTest.php b/tests/PesaTest.php index 081d48c..bb6bab9 100644 --- a/tests/PesaTest.php +++ b/tests/PesaTest.php @@ -88,7 +88,6 @@ public function pesa_has_these_attributes() { $this->assertClassHasAttribute('options', get_class($this->pesa)); $this->assertClassHasAttribute('client', get_class($this->pesa)); - $this->assertClassHasAttribute('rsa', get_class($this->pesa)); }