Skip to content

Commit

Permalink
Test mode feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
dercoder committed Mar 10, 2016
1 parent 1368089 commit abe3c6d
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 54 deletions.
68 changes: 57 additions & 11 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public function getName()
public function getDefaultParameters()
{
return [
'purse' => '',
'secret' => '',
'testMode' => false,
'checkoutId' => '',
'signAlgorithm' => 'sha256',
'signKey' => '',
'testKey' => '',
'testMode' => false,
];
}

Expand Down Expand Up @@ -83,25 +85,69 @@ public function setCheckoutId($value)
}

/**
* Get the secret key.
* Get the sign algorithm.
*
* @return string secret
* @return string sign algorithm
*/
public function getSecret()
public function getSignAlgorithm()
{
return $this->getParameter('secret');
return $this->getParameter('signAlgorithm');
}

/**
* Set the secret.
* Set the sign algorithm.
*
* @param string $value secret
* @param string $value sign algorithm
*
* @return self
*/
public function setSecret($value)
public function setSignAlgorithm($value)
{
return $this->setParameter('secret', $value);
return $this->setParameter('signAlgorithm', $value);
}

/**
* Get the sign key.
*
* @return string sign key
*/
public function getSignKey()
{
return $this->getParameter('signKey');
}

/**
* Set the sign key.
*
* @param string $value sign key
*
* @return self
*/
public function setSignKey($value)
{
return $this->setParameter('signKey', $value);
}

/**
* Get the test key.
*
* @return string test key
*/
public function getTestKey()
{
return $this->getParameter('testKey');
}

/**
* Set the test key.
*
* @param string $value test key
*
* @return self
*/
public function setTestKey($value)
{
return $this->setParameter('testKey', $value);
}

/**
Expand Down
69 changes: 57 additions & 12 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,69 @@ public function setCheckoutId($purse)
}

/**
* Get the secret.
* Get the sign algorithm.
*
* @return string secret key
* @return string sign algorithm
*/
public function getSecret()
public function getSignAlgorithm()
{
return $this->getParameter('secret');
return strtolower($this->getParameter('signAlgorithm'));
}

/**
* Set the secret.
* Set the sign algorithm.
*
* @param string $key secret key
* @param string $value sign algorithm
*
* @return self
*/
public function setSecret($key)
public function setSignAlgorithm($value)
{
return $this->setParameter('secret', $key);
return $this->setParameter('signAlgorithm', $value);
}

/**
* Get the sign key.
*
* @return string sign key
*/
public function getSignKey()
{
return $this->getParameter('signKey');
}

/**
* Set the sign key.
*
* @param string $value sign key
*
* @return self
*/
public function setSignKey($value)
{
return $this->setParameter('signKey', $value);
}

/**
* Get the test key.
*
* @return string test key
*/
public function getTestKey()
{
return $this->getParameter('testKey');
}

/**
* Set the test key.
*
* @param string $value test key
*
* @return self
*/
public function setTestKey($value)
{
return $this->setParameter('testKey', $value);
}

/**
Expand Down Expand Up @@ -158,15 +202,16 @@ public function setNotifyMethod($notifyMethod)
* Calculates sign for the $data.
*
* @param array $data
* @param string $signKey
* @return string
*/
public function calculateSign($data)
public function calculateSign($data, $signKey)
{
unset($data['ik_sign']);
ksort($data, SORT_STRING);
array_push($data, $this->getSecret());
array_push($data, $signKey);
$signAlgorithm = $this->getSignAlgorithm();
$signString = implode(':', $data);
$sign = base64_encode(hash('sha256', $signString, true));
return $sign;
return base64_encode(hash($signAlgorithm, $signString, true));
}
}
6 changes: 5 additions & 1 deletion src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class CompletePurchaseRequest extends AbstractRequest
*/
public function getData()
{
$this->validate('secret');
if ($this->getTestMode()) {
$this->validate('testKey');
} else {
$this->validate('signKey');
}

$result = [];
foreach ($this->httpRequest->request->all() as $key => $parameter) {
Expand Down
10 changes: 7 additions & 3 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);

if ($this->getSign() !== $this->request->calculateSign($this->data)) {
throw new InvalidResponseException('Failed to validate signature: ' . $this->request->calculateSign($this->data));
$signKey = $this->request->getTestMode() ? $this->request->getTestKey() : $this->request->getSignKey();
$signExpected = $this->request->calculateSign($this->data, $signKey);

if ($this->getSign() !== $signExpected) {
throw new InvalidResponseException('Failed to validate signature: ' . $signExpected);
}

if ($this->getState() !== 'success') {
Expand Down Expand Up @@ -115,7 +118,8 @@ public function getCurrency()
*/
public function getTime()
{
return strtotime($this->data['ik_inv_prc'] . ' Europe/Moscow');
$date = new \DateTime($this->data['ik_inv_prc'], new \DateTimeZone('Europe/Moscow'));
return $date->getTimestamp();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Message/OldCompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function sendData($data)
/**
* {@inheritdoc}
*/
public function calculateSign($data)
public function calculateSign($data, $signKey)
{
unset($data['ik_sign_hash']);
ksort($data, SORT_STRING);
array_push($data, $this->getSecret());
array_push($data, $signKey);
$signAlgorithm = $this->getSignAlgorithm();
$signString = implode(':', $data);
$sign = base64_encode(hash('sha256', $signString, true));
return $sign;
return base64_encode(hash($signAlgorithm, $signString, true));
}
}
2 changes: 1 addition & 1 deletion src/Message/OldPurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OldPurchaseRequest extends AbstractRequest
*/
public function getBaggageFields()
{
return $this->getCurrency() . ' ' . $this->username;
return $this->getCurrency();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PurchaseRequest extends AbstractRequest
*/
public function getData()
{
$this->validate('checkoutId', 'amount', 'currency', 'description', 'transactionId');
$this->validate('checkoutId', 'signKey', 'amount', 'currency', 'description', 'transactionId');

$return = [
'ik_co_id' => $this->getCheckoutId(),
Expand Down Expand Up @@ -63,8 +63,8 @@ public function getData()
}
}

if ($this->getSecret()) {
$return['ik_sign'] = $this->calculateSign($return);
if ($signKey = $this->getSignKey()) {
$return['ik_sign'] = $this->calculateSign($return, $signKey);
}

return $return;
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class GatewayTest extends GatewayTestCase
public $gateway;

protected $purse = '887ac1234c1eeee1488b156b';
protected $secret = 'Zp2zfdSJzbS61L32';
protected $signKey = 'Zp2zfdSJzbS61L32';
protected $testKey = 'W0b98idvHeKY2h3w';
protected $transactionId = 'ID_123456';
protected $description = 'Test completePurchase description';
protected $currency = 'USD';
Expand All @@ -34,13 +35,15 @@ public function setUp()

$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
$this->gateway->setPurse($this->purse);
$this->gateway->setSecret($this->secret);
$this->gateway->setSignKey($this->signKey);
$this->gateway->setTestKey($this->testKey);
}

public function testGateway()
{
$this->assertSame($this->purse, $this->gateway->getPurse());
$this->assertSame($this->secret, $this->gateway->getSecret());
$this->assertSame($this->signKey, $this->gateway->getSignKey());
$this->assertSame($this->testKey, $this->gateway->getTestKey());
}

public function testCompletePurchase()
Expand All @@ -50,7 +53,7 @@ public function testCompletePurchase()
]);

$this->assertSame($this->purse, $request->getPurse());
$this->assertSame($this->secret, $request->getSecret());
$this->assertSame($this->signKey, $request->getSignKey());
$this->assertSame($this->transactionId, $request->getTransactionId());
}

Expand Down
38 changes: 36 additions & 2 deletions tests/unit/Message/CompletePurchaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class CompletePurchaseRequestTest extends TestCase
protected $request;

protected $purse = '887ac1234c1eeee1488b156b';
protected $secret = 'Zp2zfdSJzbS61L32';
protected $signAlgorithm = 'sha256';
protected $signKey = 'Zp2zfdSJzbS61L32';
protected $testKey = 'W0b98idvHeKY2h3w';
protected $description = 'Test Transaction long description';
protected $transactionId = 'ID_123456';
protected $amount = '1465.01';
Expand All @@ -50,7 +52,8 @@ public function setUp()
$this->request = new CompletePurchaseRequest($this->getHttpClient(), $httpRequest);
$this->request->initialize([
'purse' => $this->purse,
'secret' => $this->secret,
'signAlgorithm' => $this->signAlgorithm,
'signKey' => $this->signKey,
]);
}

Expand All @@ -72,4 +75,35 @@ public function testSendData()
$response = $this->request->sendData($data);
$this->assertSame('Omnipay\InterKassa\Message\CompletePurchaseResponse', get_class($response));
}

public function testTestMode()
{
$httpRequest = new HttpRequest([], [
'ik_co_id' => $this->purse,
'ik_trn_id' => $this->transactionId,
'ik_desc' => $this->description,
'ik_am' => $this->amount,
'ik_cur' => $this->currency,
'ik_inv_prc' => $this->time,
'ik_sign' => $this->sign,
'ik_inv_st' => $this->state,
]);

$request = new CompletePurchaseRequest($this->getHttpClient(), $httpRequest);
$request->initialize([
'testMode' => true,
'purse' => $this->purse,
'signAlgorithm' => $this->signAlgorithm,
'testKey' => $this->testKey,
]);

$data = $request->getData();

$this->assertSame($this->purse, $data['ik_co_id']);
$this->assertSame($this->transactionId, $data['ik_trn_id']);
$this->assertSame($this->description, $data['ik_desc']);
$this->assertSame($this->amount, $data['ik_am']);
$this->assertSame($this->currency, $data['ik_cur']);
$this->assertSame($this->time, $data['ik_inv_prc']);
}
}
Loading

0 comments on commit abe3c6d

Please sign in to comment.