From 17311ca34d0f999b328e98f74fa54bf98d8a39a0 Mon Sep 17 00:00:00 2001 From: Novikov Andrey Date: Mon, 16 Apr 2018 16:04:37 +0300 Subject: [PATCH] Fix data before send to Authorize --- src/Message/AbstractRequest.php | 5 ++-- src/Message/AuthorizeRequest.php | 35 ++++++++++++++++++++++++++ src/Message/BindCardRequest.php | 4 +-- tests/Message/AuthorizeRequestTest.php | 11 +++++++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index ac1e1ef..1590c33 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -2,6 +2,7 @@ namespace Omnipay\Sberbank\Message; +use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Exception\RuntimeException; use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest; @@ -165,11 +166,11 @@ public function sendData($data) ], $data) ); - $responseClassName = str_replace('Request', 'Response', get_class($this)); + $responseClassName = str_replace('Request', 'Response', \get_class($this)); $reflection = new \ReflectionClass($responseClassName); if (!$reflection->isInstantiable()) { throw new RuntimeException( - 'Class ' . str_replace('Request', 'Response', get_class($this)) . ' not found' + 'Class ' . str_replace('Request', 'Response', \get_class($this)) . ' not found' ); } diff --git a/src/Message/AuthorizeRequest.php b/src/Message/AuthorizeRequest.php index ef94211..6125f81 100644 --- a/src/Message/AuthorizeRequest.php +++ b/src/Message/AuthorizeRequest.php @@ -2,6 +2,8 @@ namespace Omnipay\Sberbank\Message; +use Omnipay\Common\Exception\RuntimeException; + /** * Class AuthorizeRequest * @package Omnipay\Sberbank\Message @@ -226,4 +228,37 @@ public function setBindingId($value) { return $this->setParameter('bindingId', $value); } + + /** + * @param mixed $data + * @return object|\Omnipay\Common\Message\ResponseInterface + * @throws \ReflectionException + * @throws \Omnipay\Common\Exception\InvalidRequestException + */ + public function sendData($data) + { + $url = $this->getEndPoint() . $this->getMethod(); + $this->validate('userName', 'password'); + $data['currency'] = $this->getCurrencyNumeric(); + $data['amount'] = $this->getAmountInteger(); + $httpResponse = $this->httpClient->send( + $this->getHttpMethod(), + $url, + $this->getHeaders(), + array_merge([ + 'userName' => $this->getUserName(), + 'password' => $this->getPassword() + ], $data) + ); + + $responseClassName = str_replace('Request', 'Response', \get_class($this)); + $reflection = new \ReflectionClass($responseClassName); + if (!$reflection->isInstantiable()) { + throw new RuntimeException( + 'Class ' . str_replace('Request', 'Response', \get_class($this)) . ' not found' + ); + } + + return $reflection->newInstance($this, json_decode($httpResponse->getBody(true), true)); + } } diff --git a/src/Message/BindCardRequest.php b/src/Message/BindCardRequest.php index 54e42e2..efcfa64 100644 --- a/src/Message/BindCardRequest.php +++ b/src/Message/BindCardRequest.php @@ -24,11 +24,9 @@ public function getData() { $this->validate('bindingId'); - $data = [ + return [ 'bindingId' => $this->getBindingId() ]; - - return $data; } /** diff --git a/tests/Message/AuthorizeRequestTest.php b/tests/Message/AuthorizeRequestTest.php index b907c40..36d1ddf 100644 --- a/tests/Message/AuthorizeRequestTest.php +++ b/tests/Message/AuthorizeRequestTest.php @@ -32,6 +32,13 @@ class AuthorizeRequestTest extends AbstractRequestTest */ protected $orderNumber; + /** + * Currency + * + * @var string + */ + protected $currency; + /** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. @@ -41,6 +48,7 @@ public function setUp() $this->amount = mt_rand(1, 100); $this->returnUrl = 'https://test.com/' . uniqid('', true); $this->orderNumber = uniqid('order_number_', true); + $this->currency = 'RUB'; parent::setUp(); } @@ -77,7 +85,8 @@ protected function getRequestParameters() return [ 'orderNumber' => $this->orderNumber, 'amount' => $this->amount, - 'returnUrl' => $this->returnUrl + 'returnUrl' => $this->returnUrl, + 'currency' => $this->currency ]; }