Skip to content

Commit

Permalink
php 5.6 adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stierlitz committed May 5, 2023
1 parent 5c43296 commit a8a61e9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^7.0",
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.5.x-dev",
"starkbank/ecdsa": "0.0.5"
},
"autoload": {
Expand Down
22 changes: 11 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

class Client extends RequestBuilder
{
private ?string $merchantId;
private ?string $merchantName;
public string $apiEndpoint = 'https://api.monobank.ua/';
private \GuzzleHttp\Client $httpClient;
private $merchantId;
private $merchantName;
public $apiEndpoint = 'https://api.monobank.ua/';
private $httpClient;

/**
* Створює клієнт з ключем для запитів до серверу Mono і отримує дані про мерчант
* @param string $token Токен з особистого кабінету https://fop.monobank.ua/ або тестовий токен з https://api.monobank.ua/
* @throws \GuzzleHttp\Exception\GuzzleException
* @link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1details/get Так отримуються деталі мерчанту
*/
public function __construct(string $token)
public function __construct($token='')
{
$this->httpClient = new \GuzzleHttp\Client([
'base_uri' => $this->apiEndpoint,
Expand All @@ -37,21 +37,21 @@ public function __construct(string $token)
throw new \Exception('Cannot decode json response from Mono', 500);
}
} else {
throw new \Exception($data['errorDescription'] ?? 'Unknown error response: ' . $json, $response->getStatusCode());
throw new \Exception(isset($data['errorDescription']) ? $data['errorDescription'] : 'Unknown error response: ' . $json, $response->getStatusCode());
}
}

public function getMerchantId(): string
public function getMerchantId()
{
return $this->merchantId;
}

public function getMerchantName(): string
public function getMerchantName()
{
return $this->merchantName;
}

public function getClient(): \GuzzleHttp\Client
public function getClient()
{
return $this->httpClient;
}
Expand All @@ -63,7 +63,7 @@ public function getClient(): \GuzzleHttp\Client
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function getPublicKey(): string
public function getPublicKey()
{
$response = $this->getClient()->request('GET','/api/merchant/pubkey');
$data = $this->getDataFromGuzzleResponse($response);
Expand All @@ -80,7 +80,7 @@ public function getPublicKey(): string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function getMerchant(): array
public function getMerchant()
{
$response = $this->getClient()->request('GET','/api/merchant/details');
return $this->getDataFromGuzzleResponse($response);
Expand Down
21 changes: 11 additions & 10 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Payment extends RequestBuilder
{
private \MonoPay\Client $client;
private $client;

public function __construct(\MonoPay\Client $client)
{
Expand All @@ -20,9 +20,10 @@ public function __construct(\MonoPay\Client $client)
* @param array $options Додаткові параметри (Див. посилання)
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1invoice~1create/post
*/
public function create(int $amount, array $options=[]): array
public function create( $amount, $options=[])
{
if($amount < 1){
throw new \Exception('Amount must be a natural number',500);
Expand All @@ -43,7 +44,7 @@ public function create(int $amount, array $options=[]): array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function info(string $invoiceId): array
public function info($invoiceId)
{
$response = $this->client->getClient()->request('GET','/api/merchant/invoice/status',[
\GuzzleHttp\RequestOptions::QUERY => [
Expand All @@ -63,7 +64,7 @@ public function info(string $invoiceId): array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function refund(string $invoiceId, array $options=[]): array
public function refund( $invoiceId, array $options=[])
{
$options['invoiceId'] = $invoiceId;

Expand All @@ -78,10 +79,10 @@ public function refund(string $invoiceId, array $options=[]): array
* Інвалідація рахунку
* Інвалідація рахунку, якщо за ним ще не було здіснено оплати
* @param string $invoiceId ID рахунку
* @throws \GuzzleHttp\Exception\GuzzleException*@throws \Exception
* @link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1invoice~1remove/post
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function cancel(string $invoiceId): array
public function cancel($invoiceId)
{
$response = $this->client->getClient()->request('POST','/api/merchant/invoice/remove',[
\GuzzleHttp\RequestOptions::JSON => [
Expand All @@ -99,7 +100,7 @@ public function cancel(string $invoiceId): array
* @throws \Exception
*@link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1invoice~1payment-info?invoiceId=%7BinvoiceId%7D/get
*/
public function successDetails(string $invoiceId): array
public function successDetails($invoiceId)
{
$response = $this->client->getClient()->request('GET','/api/merchant/invoice/payment-info',[
\GuzzleHttp\RequestOptions::QUERY => [
Expand All @@ -120,7 +121,7 @@ public function successDetails(string $invoiceId): array
* @throws \Exception
* @link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1invoice~1finalize/post
*/
public function captureHold(string $invoiceId, int $amount = null): array
public function captureHold( $invoiceId, $amount = null)
{
$body = [
'invoiceId' => $invoiceId
Expand All @@ -145,7 +146,7 @@ public function captureHold(string $invoiceId, int $amount = null): array
* @throws \Exception
* @link https://api.monobank.ua/docs/acquiring.html#/paths/~1api~1merchant~1statement/get
*/
public function items(int $fromTimestamp, int $toTimestamp=null): array
public function items( $fromTimestamp, $toTimestamp=null)
{
$query = [
'from' => $fromTimestamp
Expand All @@ -158,7 +159,7 @@ public function items(int $fromTimestamp, int $toTimestamp=null): array
]);

$data = $this->getDataFromGuzzleResponse($response);
return $data['list']??[];
return isset($data['list']) ? $data['list'] : [];
}

}
2 changes: 1 addition & 1 deletion src/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class RequestBuilder
{
protected function getDataFromGuzzleResponse(\Psr\Http\Message\ResponseInterface $response): array
protected function getDataFromGuzzleResponse(\Psr\Http\Message\ResponseInterface $response)
{
$json = $response->getBody()->getContents();
if(!$json){
Expand Down
8 changes: 4 additions & 4 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class Webhook
{
private string $publicKeyBase64;
private string $xSignBase64;
private $publicKeyBase64;
private $xSignBase64;

/**
* Класс для верифікації даних з вебхука
Expand All @@ -17,7 +17,7 @@ class Webhook
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
public function __construct(\MonoPay\Client $client, string $publicKeyBase64=null, string $xSignBase64=null)
public function __construct(\MonoPay\Client $client, $publicKeyBase64=null, $xSignBase64=null)
{
if(!$publicKeyBase64){
$publicKeyBase64 = $client->getPublicKey();
Expand All @@ -40,7 +40,7 @@ public function __construct(\MonoPay\Client $client, string $publicKeyBase64=nul
* @param string|null $requestBody Тіло запиту. Зазвичай це json body вхідного запиту який можна отримати через функцію file_get_contents('php://input')
* @return bool Чи коректні вхідні дані
*/
public function verify(string $requestBody=null): bool
public function verify( $requestBody=null)
{
if(empty($requestBody)){
$requestBody = file_get_contents('php://input');
Expand Down

0 comments on commit a8a61e9

Please sign in to comment.