Skip to content

Commit

Permalink
MAC_ADVANCED / MAC_EXTENDED switch methods!
Browse files Browse the repository at this point in the history
  • Loading branch information
veneliniliev committed Apr 2, 2021
1 parent 43c384d commit 9c2ac2d
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 91 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ openssl req -new -key development.key -out VNNNNNNN_YYYYMMDD_D.csr

## Usage

**IMPORTANT**: Switch signing schema MAC_EXTENDED / MAC_ADVANCED with methods:
````php
$saleRequest->setSigningSchemaMacExtended(); // use MAC_EXTENDED
$saleRequest->setSigningSchemaMacAdvanced(); // use MAC_ADVANCED
````
Default signing schema is **MAC_ADVANCED**!

### Sale request

````php
Expand All @@ -61,6 +68,8 @@ $saleRequest = (new SaleRequest())
->setTerminalID('<TID - V*******>')
->setMerchantId('<MID - 15 chars>')
->setPrivateKey('\<path to certificate.key>', '<password / or use method from bottom>')
//->setSigningSchemaMacExtended(); // use MAC_EXTENDED
//->setSigningSchemaMacAdvanced(); // use MAC_ADVANCED
->setPrivateKeyPassword('test');

$formHtml = $saleRequest->generateForm(); // only generate hidden html form with filled inputs
Expand Down
129 changes: 94 additions & 35 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@
abstract class Base
{

const SIGNING_SCHEMA_MAC_ADVANCED = 'MAC_ADVANCED';
const SIGNING_SCHEMA_MAC_EXTENDED = 'MAC_EXTENDED';

/**
* Default signing schema
*
* @var string
*/
protected $signingSchema = self::SIGNING_SCHEMA_MAC_ADVANCED;

/**
* @var string
*/
protected $merchantId;

/**
* @var string
*/
protected $publicKey;

/**
* @var string
*/
Expand All @@ -44,15 +59,11 @@ abstract class Base

/**
* In develop mode of application
*
* @var string
*/
private $environment = 'production';

/**
* @var string
*/
protected $publicKey;

/**
* @return boolean
*/
Expand Down Expand Up @@ -82,6 +93,7 @@ public function getEnvironmentUrl()

/**
* Switch environment to development/production
*
* @param boolean $production True - production / false - development.
*
* @return Base
Expand All @@ -98,6 +110,7 @@ public function setEnvironment($production = true)

/**
* Switch to production mode
*
* @return Base
*/
public function inProduction()
Expand All @@ -108,6 +121,7 @@ public function inProduction()

/**
* Switch to development mode
*
* @return Base
*/
public function inDevelopment()
Expand All @@ -118,6 +132,7 @@ public function inDevelopment()

/**
* Get terminal ID
*
* @return mixed
*/
public function getTerminalID()
Expand All @@ -127,7 +142,9 @@ public function getTerminalID()

/**
* Set terminal ID
*
* @param string $terminalID Terminal ID.
*
* @return Base
* @throws ParameterValidationException
*/
Expand Down Expand Up @@ -167,9 +184,71 @@ public function setMerchantId($merchantId)
return $this;
}

/**
* Switch signing schema to MAC_ADVANCED
*
* @return Base
*/
public function setSigningSchemaMacAdvanced()
{
$this->signingSchema = self::SIGNING_SCHEMA_MAC_ADVANCED;
return $this;
}

/**
* Switch signing schema to MAC_EXTENDED
*
* @return Base
*/
public function setSigningSchemaMacExtended()
{
$this->signingSchema = self::SIGNING_SCHEMA_MAC_EXTENDED;
return $this;
}

/**
* Get public key
*
* @return string
* @throws ParameterValidationException
*/
public function getPublicKey()
{
if (empty($this->publicKey)) {
throw new ParameterValidationException('Please set public key first!');
}

return $this->publicKey;
}

/**
* Set public key
*
* @param string $publicKey Public key path.
*
* @return Base
*/
public function setPublicKey($publicKey)
{
$this->publicKey = $publicKey;
return $this;
}

/**
* Is MAC_ADVANCE signing schema?
*
* @return boolean
*/
protected function isSigningSchemaMacAdvanced()
{
return $this->signingSchema == self::SIGNING_SCHEMA_MAC_ADVANCED;
}

/**
* Generate signature of data with private key
*
* @param array $data Данни върху които да генерира подписа.
*
* @return string
* @throws SignatureException
*/
Expand All @@ -193,10 +272,10 @@ protected function getPrivateSignature(array $data)
if (PHP_MAJOR_VERSION < 8) {
/**
* @deprecated in PHP 8.0
* @note The openssl_pkey_free() function is deprecated and no longer has an effect,
* @note The openssl_pkey_free() function is deprecated and no longer has an effect,
* instead the OpenSSLAsymmetricKey instance is automatically destroyed if it is no
* longer referenced.
* @see https://github.com/php/php-src/blob/master/UPGRADING#L397
* @see https://github.com/php/php-src/blob/master/UPGRADING#L397
*/
openssl_pkey_free($privateKey);
}
Expand All @@ -206,8 +285,10 @@ protected function getPrivateSignature(array $data)

/**
* Generate signature source
*
* @param array $data Data of signature.
* @param boolean $isResponse Generate signature from response.
*
* @return string
*/
protected function getSignatureSource(array $data, $isResponse = false)
Expand All @@ -225,6 +306,7 @@ protected function getSignatureSource(array $data, $isResponse = false)

/**
* Get private key
*
* @return string
*/
public function getPrivateKey()
Expand All @@ -234,8 +316,10 @@ public function getPrivateKey()

/**
* Set private key
*
* @param string $privateKeyPath Път до файла на частният ключ.
* @param string|null $password Парола на частният ключ.
*
* @return Base
*/
public function setPrivateKey($privateKeyPath, $password = null)
Expand All @@ -251,6 +335,7 @@ public function setPrivateKey($privateKeyPath, $password = null)

/**
* Get private key password
*
* @return string|null
*/
public function getPrivateKeyPassword()
Expand All @@ -260,40 +345,14 @@ public function getPrivateKeyPassword()

/**
* Set private key password
*
* @param string|null $privateKeyPassword Парола на частният ключ.
*
* @return Base
*/
public function setPrivateKeyPassword($privateKeyPassword)
{
$this->privateKeyPassword = $privateKeyPassword;
return $this;
}

/**
* Get public key
*
* @return string
* @throws ParameterValidationException
*/
public function getPublicKey()
{
if (empty($this->publicKey)) {
throw new ParameterValidationException('Please set public key first!');
}

return $this->publicKey;
}

/**
* Set public key
*
* @param string $publicKey Public key path.
*
* @return Base
*/
public function setPublicKey($publicKey)
{
$this->publicKey = $publicKey;
return $this;
}
}
Loading

0 comments on commit 9c2ac2d

Please sign in to comment.