Skip to content

Commit

Permalink
*fix bug callback signature verify, change verifying action
Browse files Browse the repository at this point in the history
*refactoring signature (callback)
  • Loading branch information
gitkv committed Jul 20, 2018
1 parent 4ef7931 commit c7a9edc
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Receive incoming parameters from gateway and verifying signature.

```php
<?php
if (! $uniteller->getSignaturePayment()->verify('signature_from_post_params', ['all_parameters_from_post'])) {
if (! $uniteller->verifyCallbackRequest(['all_parameters_from_post_with_signature'])) {
return 'invalid_signature';
}
```
Expand Down
4 changes: 2 additions & 2 deletions README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ var_dump($results);

### Callback

Приём данных от шлюза и проверка сигнатуры.
Проверка сигнатуры при приёме данных от шлюза.

```php
<?php
if (! $uniteller->getSignaturePayment()->verify('signature_from_post_params', ['all_parameters_from_post'])) {
if (! $uniteller->verifyCallbackRequest(['all_parameters_from_post_with_signature'])) {
return 'invalid_signature';
}
```
Expand Down
42 changes: 42 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Tmconsulting\Uniteller\Recurrent\RecurrentRequest;
use Tmconsulting\Uniteller\Request\RequestInterface;
use Tmconsulting\Uniteller\Results\ResultsRequest;
use Tmconsulting\Uniteller\Signature\SignatureCallback;
use Tmconsulting\Uniteller\Signature\SignatureInterface;
use Tmconsulting\Uniteller\Signature\SignaturePayment;
use Tmconsulting\Uniteller\Signature\SignatureRecurrent;
Expand Down Expand Up @@ -50,6 +51,11 @@ class Client implements ClientInterface
*/
protected $signatureRecurrent;

/**
* @var SignatureInterface
*/
protected $signatureCallback;

/**
* @var RequestInterface
*/
Expand Down Expand Up @@ -81,6 +87,7 @@ public function __construct()
$this->registerRecurrentRequest(new RecurrentRequest());
$this->registerSignaturePayment(new SignaturePayment());
$this->registerSignatureRecurrent(new SignatureRecurrent());
$this->registerSignatureCallback(new SignatureCallback());
}

/**
Expand Down Expand Up @@ -215,6 +222,17 @@ public function registerSignatureRecurrent(SignatureInterface $signature)
return $this;
}

/**
* @param \Tmconsulting\Uniteller\Signature\SignatureInterface $signature
* @return $this
*/
public function registerSignatureCallback(SignatureInterface $signature)
{
$this->signatureCallback = $signature;

return $this;
}

/**
* @return array
*/
Expand Down Expand Up @@ -317,6 +335,14 @@ public function getSignatureRecurrent()
return $this->signatureRecurrent;
}

/**
* @return \Tmconsulting\Uniteller\Signature\SignatureInterface
*/
public function getSignatureCallback()
{
return $this->signatureCallback;
}

/**
* @return \Tmconsulting\Uniteller\Http\HttpManagerInterface
*/
Expand Down Expand Up @@ -458,4 +484,20 @@ private function getParameters($parameters)

return $parameters;
}

/**
* Verify signature when Client will be send callback request.
*
* @param array $params
* @return bool
*/
public function verifyCallbackRequest(array $params)
{
return $this->signatureCallback
->setOrderId(array_get($params, 'Order_ID'))
->setStatus(array_get($params, 'Status'))
->setFields(array_except($params, ['Order_ID', 'Status', 'Signature']))
->setPassword($this->getPassword())
->verify(array_get($params, 'Signature'));
}
}
10 changes: 9 additions & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ public function confirm($parameters);
* @return mixed
*/
public function card($parameters);
}

/**
* Verify signature when Client will be send callback request.
*
* @param array $params
* @return bool
*/
public function verifyCallbackRequest(array $params);
}
11 changes: 5 additions & 6 deletions src/Signature/AbstractSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class AbstractSignature implements SignatureInterface, ArraybleInterfac
{

/**
* Create signature for send payment request.
* Create signature
*
* @return string
*/
Expand All @@ -33,14 +33,13 @@ public function create()
}

/**
* Verify signature when Client will be send callback request.
* Verify signature
*
* @param $signature
* @param array $params
* @param string $signature
* @return bool
*/
public function verify($signature, array $params)
public function verify($signature)
{
return strtoupper(md5(join('', $params))) === $signature;
return $this->create() === $signature;
}
}
137 changes: 137 additions & 0 deletions src/Signature/SignatureCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
/**
* Created by gitkv.
* E-mail: [email protected]
* GitHub: gitkv
*/

namespace Tmconsulting\Uniteller\Signature;


/**
* Class SignatureCallback
* @package Tmconsulting\Uniteller\Signature
*/
final class SignatureCallback extends AbstractSignature
{

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

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

/**
* @var array
*/
protected $fields = [];

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

/**
* @param $orderId
* @return SignatureCallback
*/
public function setOrderId($orderId)
{
$this->orderId = $orderId;

return $this;
}

/**
* @param $status
* @return SignatureCallback
*/
public function setStatus($status)
{
$this->status = $status;

return $this;
}

/**
* @param array $fields
* @return SignatureCallback
*/
public function setFields($fields)
{
$this->fields = $fields;

return $this;
}

/**
* @param string $password
* @return SignatureCallback
*/
public function setPassword($password)
{
$this->password = $password;

return $this;
}

/**
* @return string
*/
public function getOrderId()
{
return $this->orderId;
}

/**
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* @return array
*/
public function getFields()
{
return $this->fields;
}

/**
* @return string
*/
public function getPassword()
{
return $this->password;
}

/**
* @return array
*/
public function toArray()
{
$array = [];
$array['Order_ID'] = $this->getOrderId();
$array['Status'] = $this->getStatus();
$array = array_merge($array, $this->getFields());
$array['Password'] = $this->getPassword();

return $array;
}

/**
* Create signature
*
* @return string
*/
public function create()
{
return strtoupper(md5(join('', $this->toArray())));
}
}
16 changes: 11 additions & 5 deletions src/Signature/SignatureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
interface SignatureInterface
{
/**
* Create signature for send payment request.
* Create signature
*
* @return string
*/
public function create();

/**
* Verify signature when Client will be send callback request.
* Array params signature
*
* @param $signature
* @param array $params
* @return array
*/
public function toArray();

/**
* Verify signature
*
* @param string $signature
* @return bool
*/
public function verify($signature, array $params);
public function verify($signature);
}
14 changes: 14 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ function csv_to_array($string, $isFlat = false)
}

return $data;
}

/**
* @param $array
* @param array $excludeKeys
* @return mixed
*/
function array_except($array, array $excludeKeys)
{
foreach ($excludeKeys as $key) {
unset($array[$key]);
}

return $array;
}
27 changes: 27 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,33 @@ public function testShouldBeActionsAcceptClassesWhichImplementArraybleInterface(
$client->{$methodName}($arrayble);
}

public function testCallbackSignatureVerifying()
{
$params = [
'Order_ID' => 'FOO',
'Status' => 'paid',
'Signature' => '3F728AA479E50F5B10EE6C20258BFF88',
];
$client = new Client();
$client->setPassword('LONG-PWD');
$this->assertTrue($client->verifyCallbackRequest($params));
}

public function testCallbackSignatureVerifyingWithFields()
{
$params = [
'Order_ID' => 'FOO',
'Status' => 'paid',
'AcquirerID' => 'fOO',
'ApprovalCode' => 'BaR',
'BillNumber' => 'baz',
'Signature' => '1F4E3B63AE408D0BE1E33965E6697236',
];
$client = new Client();
$client->setPassword('LONG-PWD');
$this->assertTrue($client->verifyCallbackRequest($params));
}

}

class HttpManagerStub implements HttpManagerInterface {
Expand Down
Loading

0 comments on commit c7a9edc

Please sign in to comment.