Skip to content

Commit

Permalink
Merge pull request #3 from sergeynezbritskiy/dev
Browse files Browse the repository at this point in the history
RC-1.0.0
  • Loading branch information
sergeynezbritskiy authored Mar 17, 2018
2 parents 71feec4 + e75a68a commit 79f344b
Show file tree
Hide file tree
Showing 65 changed files with 3,120 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.phar
/vendor/
/build/
.idea
phpunit.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Library for connecting your PHP application with PrivatBank API
## Installation
The easiest way to install module is using Composer
```
composer require sergeynezbritskiy/privatbank-api:"dev-master"
composer require sergeynezbritskiy/privatbank-api:"^1.0.0"
```

## Notes
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"guzzlehttp/guzzle": "~6.0",
"sergeynezbritskiy/xml-io": "^4.0.0"
"sergeynezbritskiy/xml-io": "^5.0.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@
<log type="coverage-html" target="build/coverage/html" title="phpDox" charset="UTF-8" highlight="true" lowUpperBound="60" highLowerBound="90"/>
</logging>

<php>
<env name="cardNumber" value=""/>
<env name="merchantId" value=""/>
<env name="merchantSecret" value=""/>
<env name="startDate" value=""/>
<env name="endDate" value=""/>
</php>

</phpunit>
24 changes: 24 additions & 0 deletions src/Api/AuthorizedRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace SergeyNezbritskiy\PrivatBank\Api;

use SergeyNezbritskiy\PrivatBank\Merchant;

/**
* Interface RequiresAuthenticationInterface
* @package SergeyNezbritskiy\PrivatBank\Api
*/
interface AuthorizedRequestInterface extends RequestInterface
{

/**
* @param Merchant $merchant
* @return void
*/
public function setMerchant(Merchant $merchant);

/**
* @return Merchant
*/
public function getMerchant(): Merchant;
}
27 changes: 27 additions & 0 deletions src/Api/HttpResponseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace SergeyNezbritskiy\PrivatBank\Api;

/**
* Interface HttpResponseInterface
* @package SergeyNezbritskiy\PrivatBank\Api
*/
interface HttpResponseInterface
{

/**
* @return string
*/
public function getContent(): string;

/**
* @return int
*/
public function getStatusCode(): int;

/**
* @return string
*/
public function getReasonPhrase(): string;

}
3 changes: 3 additions & 0 deletions src/Api/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SergeyNezbritskiy\PrivatBank\Api;

use SergeyNezbritskiy\PrivatBank\Base\PrivatBankApiException;

/**
* Interface RequestInterface
* @package SergeyNezbritskiy\PrivatBank\Api
Expand All @@ -12,6 +14,7 @@ interface RequestInterface
/**
* @param array $params
* @return ResponseInterface
* @throws PrivatBankApiException
*/
public function execute(array $params = array()): ResponseInterface;

Expand Down
11 changes: 0 additions & 11 deletions src/Api/RequiresAuthenticationInterface.php

This file was deleted.

93 changes: 93 additions & 0 deletions src/Base/AbstractAuthorizedRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

namespace SergeyNezbritskiy\PrivatBank\Base;

use SergeyNezbritskiy\PrivatBank\Api\AuthorizedRequestInterface;
use SergeyNezbritskiy\XmlIo\XmlWriter;

/**
* Class AbstractAuthorizedRequest
* @package SergeyNezbritskiy\PrivatBank\Base
*/
abstract class AbstractAuthorizedRequest extends AbstractRequest implements AuthorizedRequestInterface
{

use HasMerchantTrait;

/**
* @return array
*/
abstract protected function getBodyMap(): array;

/**
* @return string
*/
protected function getMethod(): string
{
return 'POST';
}

/**
* @param array $params
* @return array
*/
protected function getBodyParams(array $params = []): array
{
return [
'oper' => 'cmt',
'wait' => $this->getClient()->getWaitTimeout(),
'test' => $this->getClient()->isTestMode(),
];
}

/**
* @param array $params
* @return array
*/
protected function getQueryParams(array $params = []): array
{
return [];
}

/**
* @param $params
* @return string
*/
protected function getBody(array $params = []): string
{
$xmlWriter = new XmlWriter();
$dataXml = $xmlWriter->toXml($params, $this->getBodyMap());
$dataContent = $this->getDataInnerXmlAsString($dataXml);
$signature = $this->getMerchant()->calculateSignature($dataContent);
$merchantId = $this->getMerchant()->getId();

$body = <<<XML
<request version="1.0">
<merchant>
<id>$merchantId</id>
<signature>$signature</signature>
</merchant>
<data>
$dataContent
</data>
</request>
XML;

return $body;

}

/**
* @param \DOMDocument $xml
* @return string
*/
private function getDataInnerXmlAsString($xml)
{
$innerXml = '';
foreach ($xml->childNodes as $node) {
$innerXml .= $node->ownerDocument->saveXML($node, LIBXML_NOEMPTYTAG);
}
return $innerXml;
}

}
29 changes: 29 additions & 0 deletions src/Base/AbstractPublicRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace SergeyNezbritskiy\PrivatBank\Base;

/**
* Class AbstractPublicRequest
* @package SergeyNezbritskiy\PrivatBank\Base
*/
abstract class AbstractPublicRequest extends AbstractRequest
{

/**
* @return string
*/
protected function getMethod(): string
{
return 'GET';
}

/**
* @param array $params
* @return array
*/
protected function getBodyParams(array $params = array()): array
{
return [];
}

}
65 changes: 52 additions & 13 deletions src/Base/AbstractRequest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
<?php declare(strict_types=1);

namespace SergeyNezbritskiy\PrivatBank\Base;

use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface as HttpResponseInterface;
use SergeyNezbritskiy\PrivatBank\Api\HttpResponseInterface;
use SergeyNezbritskiy\PrivatBank\Api\RequestInterface;
use SergeyNezbritskiy\PrivatBank\Api\ResponseInterface;
use SergeyNezbritskiy\PrivatBank\Client;

/**
* Class AbstractRequest
Expand All @@ -15,40 +15,79 @@ abstract class AbstractRequest implements RequestInterface
{

/**
* @var string
* @return string
*/
protected $url = 'https://api.privatbank.ua/p24api/';
abstract protected function getMethod(): string;

/**
* @return string
*/
abstract protected function getRoute(): string;

/**
* @param HttpResponseInterface $httpResponse
* @return ResponseInterface
*/
abstract protected function getResponse(HttpResponseInterface $httpResponse): ResponseInterface;

/**
* @param array $params
* @return array
*/
abstract protected function getQueryParams(array $params = []): array;

/**
* @param HttpResponseInterface $httpResponse
* @return ResponseInterface
* @param array $params
* @return array
*/
abstract protected function getResponse(HttpResponseInterface $httpResponse): ResponseInterface;
abstract protected function getBodyParams(array $params = []): array;

/**
* @var Client
*/
private $client;

/**
* AbstractPublicRequest constructor.
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @param array $params
* @return ResponseInterface
* @throws PrivatBankApiException
*/
public function execute(array $params = array()): ResponseInterface
{
$client = new Client();

$requestUri = $this->url . $this->getRoute();
$response = $client->request('GET', $requestUri, [
'query' => $this->getQueryParams($params)
$response = $this->client->request($this->getRoute(), [
'method' => $this->getMethod(),
'query' => $this->getQueryParams($params),
'body' => $this->getBody($this->getBodyParams($params)),
]);
return $this->getResponse($response);

}

/**
* @param array $params
* @return string
*/
protected function getBody(/** @noinspection PhpUnusedParameterInspection */
array $params = []): string
{
return '';
}

/**
* @return Client
*/
protected function getClient(): Client
{
return $this->client;
}

}
Loading

0 comments on commit 79f344b

Please sign in to comment.