Skip to content

Commit

Permalink
Merge pull request #19 from openpesa/feat/remove-phpseclib
Browse files Browse the repository at this point in the history
⚡️ remove phpseclib
  • Loading branch information
alphaolomi authored Oct 26, 2021
2 parents 513a8fa + 6dbb843 commit 4a2328c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand Down
31 changes: 14 additions & 17 deletions src/Pesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use phpseclib\Crypt\RSA;
use InvalidArgumentException;

/**
* @package Openpesa\SDK
Expand All @@ -23,11 +23,7 @@ class Pesa
* @access private
*/
private $client;
/**
* @var null|RSA
* @access private
*/
private $rsa;


/**
* BASE DOMAIN
Expand Down Expand Up @@ -93,7 +89,6 @@ class Pesa
];



/**
* Pesa constructor.
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/PesaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down

0 comments on commit 4a2328c

Please sign in to comment.