Skip to content

Commit 6eb6285

Browse files
authored
Merge pull request #4 from yourpayments/YP-291__sdelat-zanovo-grafiki-v-cpanel
added methods for general and chart reports
2 parents a3ed03c + f919430 commit 6eb6285

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

src/ApiRequest.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ApiRequest implements ApiRequestInterface
1919
const PAYOUT_CREATE_API = '/api/v4/payout';
2020
const REPORTS_ORDERS_API = '/reports/orders';
2121
const SESSION_API = '/api/v4/payments/sessions';
22+
const REPORT_CHART_API = '/api/v4/reports/chart';
23+
const REPORT_GENERAL_API = '/api/v4/reports/general';
2224
const HOST = 'https://secure.ypmn.ru';
2325
const SANDBOX_HOST = 'https://sandbox.ypmn.ru';
2426
const LOCAL_HOST = 'http://localhost';
@@ -239,14 +241,6 @@ private function sendGetRequest(string $api): array
239241
}
240242
}
241243

242-
if (mb_strlen($err) > 0) {
243-
throw new PaymentException($err);
244-
}
245-
246-
if ($response == null || strlen($response) === 0) {
247-
throw new PaymentException('Вы можете попробовать другой способ оплаты, либо свяжитесь с продавцом.');
248-
}
249-
250244
return ['response' => $response, 'error' => $err];
251245
}
252246

@@ -372,7 +366,17 @@ public function sendRefundRequest(RefundInterface $refund): array
372366
/** @inheritdoc */
373367
public function sendStatusRequest(string $merchantPaymentReference): array
374368
{
375-
return $this->sendGetRequest(self::STATUS_API . '/' . $merchantPaymentReference);
369+
$responseData = $this->sendGetRequest(self::STATUS_API . '/' . $merchantPaymentReference);
370+
371+
if (mb_strlen($responseData['error']) > 0) {
372+
throw new PaymentException($responseData['error']);
373+
}
374+
375+
if ($responseData['response'] == null || strlen($responseData['response']) === 0) {
376+
throw new PaymentException('Вы можете попробовать другой способ оплаты, либо свяжитесь с продавцом.');
377+
}
378+
379+
return $responseData;
376380
}
377381

378382
/** @inheritdoc */
@@ -393,6 +397,35 @@ public function sendPayoutCreateRequest(PayoutInterface $payout)
393397
return $this->sendPostRequest($payout, self::PAYOUT_CREATE_API);
394398
}
395399

400+
/** @inheritdoc */
401+
public function sendReportChartRequest(array $params): array
402+
{
403+
return $this->sendGetRequest(self::REPORT_CHART_API . '/?' . http_build_query($params));
404+
}
405+
406+
/** @inheritdoc */
407+
public function sendReportChartUpdateRequest(array $params): array
408+
{
409+
$getParams = [
410+
'startDate' => $_GET['startDate'],
411+
'endDate' => $_GET['endDate'],
412+
'status' => $_GET['status'],
413+
'type' => $_GET['type'],
414+
'periodLength' => $_GET['periodLength'],
415+
'jsonForUpdate' => 'true'
416+
];
417+
418+
$params = array_merge($getParams, $params);
419+
420+
return $this->sendGetRequest(self::REPORT_CHART_API . '/?' . http_build_query($params));
421+
}
422+
423+
/** @inheritdoc */
424+
public function sendReportGeneralRequest(array $params): array
425+
{
426+
return $this->sendGetRequest(self::REPORT_GENERAL_API . '/?' . http_build_query($params));
427+
}
428+
396429
/**
397430
* Подпись запроса
398431
* @param MerchantInterface $merchant Мерчант

src/ApiRequestInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,22 @@ public function sendTokenCreationRequest(PaymentReference $payuPaymentReference)
8787
* @return array
8888
*/
8989
public function sendTokenPaymentRequest(MerchantToken $tokenHash): array;
90+
91+
/**
92+
* Отправить запрос для получения графика
93+
* @param array $params
94+
*/
95+
public function sendReportChartRequest(array $params);
96+
97+
/**
98+
* Отправить запрос для получения JSON для обновления графика
99+
* @param array $params
100+
*/
101+
public function sendReportChartUpdateRequest(array $params);
102+
103+
/**
104+
* Отправить запрос для получения JSON данных отчета
105+
* @param array $params
106+
*/
107+
public function sendReportGeneralRequest(array $params);
90108
}

src/Examples/getReportChart.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ypmn\ApiRequest;
6+
7+
// Подключим файл, в котором заданы параметры мерчанта
8+
include_once 'start.php';
9+
10+
// Получение графика отчета
11+
12+
// Создадим HTTP-запрос к API
13+
$apiRequest = new ApiRequest($merchant);
14+
// Включить режим отладки (закомментируйте или удалите в рабочей программе!)
15+
$apiRequest->setDebugMode();
16+
// Переключиться на тестовый сервер (закомментируйте или удалите в рабочей программе!)
17+
$apiRequest->setSandboxMode();
18+
19+
// Подготовим диапазон дат для отчета
20+
$endDate = (new DateTime('now'))->format("Y-m-d");
21+
22+
$startDate = (new DateTime($endDate))
23+
->modify('-14 day')
24+
->format("Y-m-d");
25+
26+
// Отправим запрос
27+
$responseData = $apiRequest->sendReportChartRequest([
28+
'startDate' => $startDate,
29+
'endDate' => $endDate,
30+
'periodLength' => 'day'
31+
]);

src/Examples/getReportGeneral.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ypmn\ApiRequest;
6+
7+
// Подключим файл, в котором заданы параметры мерчанта
8+
include_once 'start.php';
9+
10+
// Получение отчета в формате JSON
11+
12+
// Создадим HTTP-запрос к API
13+
$apiRequest = new ApiRequest($merchant);
14+
// Включить режим отладки (закомментируйте или удалите в рабочей программе!)
15+
$apiRequest->setDebugMode();
16+
// Переключиться на тестовый сервер (закомментируйте или удалите в рабочей программе!)
17+
$apiRequest->setSandboxMode();
18+
19+
// Подготовим диапазон дат для отчета
20+
$endDate = (new DateTime('now'))->format("Y-m-d");
21+
22+
$startDate = (new DateTime($endDate))
23+
->modify('-14 day')
24+
->format("Y-m-d");
25+
26+
// Отправим запрос
27+
$responseData = $apiRequest->sendReportGeneralRequest([
28+
'startDate' => $startDate,
29+
'endDate' => $endDate,
30+
'periodLength' => 'day'
31+
]);

0 commit comments

Comments
 (0)