From abe3c6d0bde5d8c2acfb929215c23ee16f114a15 Mon Sep 17 00:00:00 2001 From: DerCoder Date: Thu, 10 Mar 2016 18:08:52 +0100 Subject: [PATCH] Test mode feature added --- src/Gateway.php | 68 +++++++++++++++--- src/Message/AbstractRequest.php | 69 +++++++++++++++---- src/Message/CompletePurchaseRequest.php | 6 +- src/Message/CompletePurchaseResponse.php | 10 ++- src/Message/OldCompletePurchaseRequest.php | 8 +-- src/Message/OldPurchaseRequest.php | 2 +- src/Message/PurchaseRequest.php | 6 +- tests/unit/GatewayTest.php | 11 +-- .../Message/CompletePurchaseRequestTest.php | 38 +++++++++- .../Message/CompletePurchaseResponseTest.php | 11 +-- .../OldCompletePurchaseRequestTest.php | 3 +- .../OldCompletePurchaseResponseTest.php | 3 +- tests/unit/Message/OldPurchaseRequestTest.php | 4 +- .../unit/Message/OldPurchaseResponseTest.php | 8 ++- tests/unit/Message/PurchaseRequestTest.php | 8 ++- tests/unit/Message/PurchaseResponseTest.php | 8 ++- 16 files changed, 209 insertions(+), 54 deletions(-) diff --git a/src/Gateway.php b/src/Gateway.php index 13f38c8..94b70ec 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -33,9 +33,11 @@ public function getName() public function getDefaultParameters() { return [ - 'purse' => '', - 'secret' => '', - 'testMode' => false, + 'checkoutId' => '', + 'signAlgorithm' => 'sha256', + 'signKey' => '', + 'testKey' => '', + 'testMode' => false, ]; } @@ -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); } /** diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 1d5f54a..669f243 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -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); } /** @@ -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)); } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 77b02ba..8e68438 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -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) { diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index 03abe32..1179d07 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -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') { @@ -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(); } /** diff --git a/src/Message/OldCompletePurchaseRequest.php b/src/Message/OldCompletePurchaseRequest.php index 5b0c618..8055174 100644 --- a/src/Message/OldCompletePurchaseRequest.php +++ b/src/Message/OldCompletePurchaseRequest.php @@ -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)); } } diff --git a/src/Message/OldPurchaseRequest.php b/src/Message/OldPurchaseRequest.php index 540df14..abf274a 100644 --- a/src/Message/OldPurchaseRequest.php +++ b/src/Message/OldPurchaseRequest.php @@ -22,7 +22,7 @@ class OldPurchaseRequest extends AbstractRequest */ public function getBaggageFields() { - return $this->getCurrency() . ' ' . $this->username; + return $this->getCurrency(); } /** diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 13f5a4b..1619238 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -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(), @@ -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; diff --git a/tests/unit/GatewayTest.php b/tests/unit/GatewayTest.php index 43f05ba..a1c0f0b 100644 --- a/tests/unit/GatewayTest.php +++ b/tests/unit/GatewayTest.php @@ -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'; @@ -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() @@ -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()); } diff --git a/tests/unit/Message/CompletePurchaseRequestTest.php b/tests/unit/Message/CompletePurchaseRequestTest.php index 7a4c2b0..d7ee97f 100644 --- a/tests/unit/Message/CompletePurchaseRequestTest.php +++ b/tests/unit/Message/CompletePurchaseRequestTest.php @@ -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'; @@ -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, ]); } @@ -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']); + } } diff --git a/tests/unit/Message/CompletePurchaseResponseTest.php b/tests/unit/Message/CompletePurchaseResponseTest.php index c20da7d..eb06cb0 100644 --- a/tests/unit/Message/CompletePurchaseResponseTest.php +++ b/tests/unit/Message/CompletePurchaseResponseTest.php @@ -19,7 +19,9 @@ class CompletePurchaseResponseTest extends TestCase { protected $purse = '887ac1234c1eeee1488b156b'; - protected $secret = 'Zp2zfdSJzbS61L32'; + protected $signAlgorithm = 'sha256'; + protected $signKey = 'Zp2zfdSJzbS61L32'; + protected $testKey = 'W0b98idvHeKY2h3w'; protected $payment_no = '1235151'; protected $description = 'Test Transaction long description'; protected $payway = 'visa_liqpay_merchant_usd'; @@ -30,6 +32,7 @@ class CompletePurchaseResponseTest extends TestCase protected $state = 'success'; protected $sign = 'CwbLEwwevJc/5TyOTfIPDXMfIfXP5tPjWkUDX98bAug='; protected $time = '2015-12-17 17:36:13'; + protected $timestamp = 1450362973; /** * @param array $options @@ -52,7 +55,8 @@ public function createRequest($options = []) $request = new CompletePurchaseRequest($this->getHttpClient(), $httpRequest); $request->initialize([ - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, ]); return $request; @@ -74,14 +78,13 @@ public function testSuccess() { /** @var CompletePurchaseResponse $response */ $response = $this->createRequest()->send(); - $this->assertTrue($response->isSuccessful()); $this->assertSame($this->purse, $response->getCheckoutId()); $this->assertSame($this->payment_no, $response->getTransactionId()); $this->assertSame($this->transactionId, $response->getTransactionReference()); $this->assertSame($this->amount, $response->getAmount()); $this->assertSame($this->currency, $response->getCurrency()); - $this->assertSame(strtotime($this->time . ' Europe/Moscow'), $response->getTime()); + $this->assertSame($this->timestamp, $response->getTime()); $this->assertSame($this->payway, $response->getPayer()); $this->assertSame($this->state, $response->getState()); $this->assertSame($this->sign, $response->getSign()); diff --git a/tests/unit/Message/OldCompletePurchaseRequestTest.php b/tests/unit/Message/OldCompletePurchaseRequestTest.php index 2d03312..109210c 100644 --- a/tests/unit/Message/OldCompletePurchaseRequestTest.php +++ b/tests/unit/Message/OldCompletePurchaseRequestTest.php @@ -41,7 +41,8 @@ public function setUp() $this->request = new OldCompletePurchaseRequest($this->getHttpClient(), $httpRequest); $this->request->initialize([ 'purse' => $this->purse, - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, ]); } diff --git a/tests/unit/Message/OldCompletePurchaseResponseTest.php b/tests/unit/Message/OldCompletePurchaseResponseTest.php index 0a6d71f..42be422 100644 --- a/tests/unit/Message/OldCompletePurchaseResponseTest.php +++ b/tests/unit/Message/OldCompletePurchaseResponseTest.php @@ -47,7 +47,8 @@ public function createRequest($options = []) $request = new OldCompletePurchaseRequest($this->getHttpClient(), $httpRequest); $request->initialize([ - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, ]); return $request; diff --git a/tests/unit/Message/OldPurchaseRequestTest.php b/tests/unit/Message/OldPurchaseRequestTest.php index 2721796..2befeb3 100644 --- a/tests/unit/Message/OldPurchaseRequestTest.php +++ b/tests/unit/Message/OldPurchaseRequestTest.php @@ -29,7 +29,9 @@ public function setUp() $this->request = new OldPurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize([ 'purse' => $this->purse, - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, + 'testKey' => $this->testKey, 'returnUrl' => $this->returnUrl, 'cancelUrl' => $this->cancelUrl, 'notifyUrl' => $this->notifyUrl, diff --git a/tests/unit/Message/OldPurchaseResponseTest.php b/tests/unit/Message/OldPurchaseResponseTest.php index 1d97c9f..7f55c27 100644 --- a/tests/unit/Message/OldPurchaseResponseTest.php +++ b/tests/unit/Message/OldPurchaseResponseTest.php @@ -13,6 +13,7 @@ use Omnipay\InterKassa\Message\OldPurchaseRequest; use Omnipay\InterKassa\Message\PurchaseRequest; +use Omnipay\InterKassa\Message\PurchaseResponse; class OldPurchaseResponseTest extends PurchaseResponseTest { @@ -22,7 +23,8 @@ class OldPurchaseResponseTest extends PurchaseResponseTest protected $request; protected $purse = '887ac1234c1eeee1488b156b'; - protected $secret = 'Zp2zfdSJzbS61L32'; + protected $signKey = 'Zp2zfdSJzbS61L32'; + protected $testKey = 'W0b98idvHeKY2h3w'; protected $returnUrl = 'https://www.example.com/success'; protected $cancelUrl = 'https://www.example.com/failure'; protected $notifyUrl = 'https://www.example.com/notify'; @@ -39,7 +41,9 @@ public function setUp() $this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize([ 'purse' => $this->purse, - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, + 'testKey' => $this->testKey, 'returnUrl' => $this->returnUrl, 'cancelUrl' => $this->cancelUrl, 'notifyUrl' => $this->notifyUrl, diff --git a/tests/unit/Message/PurchaseRequestTest.php b/tests/unit/Message/PurchaseRequestTest.php index bd00833..03d0c02 100644 --- a/tests/unit/Message/PurchaseRequestTest.php +++ b/tests/unit/Message/PurchaseRequestTest.php @@ -22,7 +22,9 @@ class PurchaseRequestTest extends TestCase protected $request; protected $purse = '887ac1234c1eeee1488b156b'; - protected $secret = 'Zp2zfdSJzbS61L32'; + protected $signAlgorithm = 'sha256'; + protected $signKey = 'Zp2zfdSJzbS61L32'; + protected $testKey = 'W0b98idvHeKY2h3w'; protected $returnUrl = 'https://www.example.com/success'; protected $cancelUrl = 'https://www.example.com/failure'; protected $notifyUrl = 'https://www.example.com/notify'; @@ -38,7 +40,9 @@ public function setUp() $this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize([ 'purse' => $this->purse, - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, + 'testKey' => $this->testKey, 'returnUrl' => $this->returnUrl, 'cancelUrl' => $this->cancelUrl, 'notifyUrl' => $this->notifyUrl, diff --git a/tests/unit/Message/PurchaseResponseTest.php b/tests/unit/Message/PurchaseResponseTest.php index 6947a91..63e4a4f 100644 --- a/tests/unit/Message/PurchaseResponseTest.php +++ b/tests/unit/Message/PurchaseResponseTest.php @@ -23,7 +23,9 @@ class PurchaseResponseTest extends TestCase protected $request; protected $purse = '887ac1234c1eeee1488b156b'; - protected $secret = 'Zp2zfdSJzbS61L32'; + protected $signAlgorithm = 'sha256'; + protected $signKey = 'Zp2zfdSJzbS61L32'; + protected $testKey = 'W0b98idvHeKY2h3w'; protected $returnUrl = 'https://www.example.com/success'; protected $cancelUrl = 'https://www.example.com/failure'; protected $notifyUrl = 'https://www.example.com/notify'; @@ -45,7 +47,9 @@ public function setUp() $this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize([ 'purse' => $this->purse, - 'secret' => $this->secret, + 'signAlgorithm' => $this->signAlgorithm, + 'signKey' => $this->signKey, + 'testKey' => $this->testKey, 'returnUrl' => $this->returnUrl, 'cancelUrl' => $this->cancelUrl, 'notifyUrl' => $this->notifyUrl,