@@ -17,6 +17,7 @@ class ApiRequest implements ApiRequestInterface
1717 const REFUND_API = '/api/v4/payments/refund ' ;
1818 const STATUS_API = '/api/v4/payments/status ' ;
1919 const PAYOUT_CREATE_API = '/api/v4/payout ' ;
20+ const REPORTS_ORDERS_API = '/reports/orders ' ;
2021 const HOST = 'https://secure.ypmn.ru ' ;
2122 const SANDBOX_HOST = 'https://sandbox.ypmn.ru ' ;
2223 const LOCAL_HOST = 'http://localhost ' ;
@@ -40,25 +41,141 @@ public function __construct(MerchantInterface $merchant)
4041 $ this ->merchant = $ merchant ;
4142 }
4243
44+ public function getHost () : string
45+ {
46+ if ($ this ->localModeIsOn ) {
47+ return self ::LOCAL_HOST ;
48+ } else {
49+ return ($ this ->getSandboxMode () ? self ::SANDBOX_HOST : self ::HOST );
50+ }
51+ }
52+
53+ public function sendGetReportRequest (?string $ startDate = null , ?string $ endDate = null , ?array $ orderStatus = null ): string
54+ {
55+ //проверить даты
56+ if ($ startDate !== null ) {
57+ if (($ startDate = strtotime ($ startDate )) === false ) {
58+ throw new \Exception ('Неверная дата для формирования запроса ' );
59+ } else {
60+ $ startDate = date ('Y-m-d ' , $ startDate );
61+ }
62+ } else {
63+ $ startDate = date ('Y-m-d ' , strtotime ('today ' ));
64+ }
65+
66+ if ($ endDate !== null ) {
67+ if (($ endDate = strtotime ($ endDate )) === false ) {
68+ throw new \Exception ('Неверная дата для формирования запроса ' );
69+ } else {
70+ $ endDate = date ('Y-m-d ' , $ endDate );
71+ }
72+ } else {
73+ $ endDate = date ('Y-m-d ' , strtotime ('tomorrow ' ));
74+ }
75+
76+ $ merchant = $ this ->merchant ->getCode ();
77+ $ timeStamp = time ();
78+
79+ // $parameters = compact('merchant', 'startDate', 'endDate', 'orderStatus', 'timeStamp');
80+ $ parameters = compact ('merchant ' , 'startDate ' , 'endDate ' , 'timeStamp ' );
81+
82+
83+ //сформировать URL
84+ $ url = $ this ->getHost ()
85+ . $ this ::REPORTS_ORDERS_API
86+ . '? '
87+ . http_build_query ($ parameters )
88+ . '&signature= '
89+ . $ this ->reportsSign ($ parameters );
90+
91+
92+ if ($ this ->getDebugMode ()) {
93+ echo Std::alert ([
94+ 'text ' => $ url ,
95+ ]);
96+ }
97+
98+ // отправить запрос
99+ $ curl = curl_init ();
100+ $ requestHttpVerb = 'GET ' ;
101+
102+ $ date = (new DateTime ())->format (DateTimeInterface::ATOM );
103+ $ setopt_array = [
104+ CURLOPT_URL => $ url ,
105+ CURLOPT_RETURNTRANSFER => true ,
106+ CURLOPT_ENCODING => '' ,
107+ CURLOPT_MAXREDIRS => 10 ,
108+ CURLOPT_TIMEOUT => 30 ,
109+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
110+ CURLOPT_CUSTOMREQUEST => $ requestHttpVerb ,
111+ CURLOPT_HTTPHEADER => [
112+ 'Accept: application/json ' ,
113+ 'Content-Type: application/json ' ,
114+ 'X-Header-Date: ' . $ date ,
115+ // 'X-Header-Merchant: ' . $this->merchant->getCode()
116+ ]
117+ ];
118+
119+ curl_setopt_array ($ curl , $ setopt_array );
120+
121+ $ response = curl_exec ($ curl );
122+ $ err = curl_error ($ curl );
123+ curl_close ($ curl );
124+
125+ if ($ this ->getDebugMode ()) {
126+ $ this ->echoDebugMessage ('Ответ от ' . $ this ->getHost () . ': ' );
127+ $ this ->echoDebugMessage (Std::json_fix_cyr ($ response ));
128+
129+ if ($ err ) {
130+ $ this ->echoDebugMessage ('Ошибка: ' );
131+ $ this ->echoDebugMessage ($ err );
132+ }
133+ }
134+
135+ // вернуть результат
136+ return Std::json_fix_cyr ($ response );
137+ }
138+
139+ private function buildReportsSourceString ($ parameters )
140+ {
141+ $ hashString = '' ;
142+
143+ foreach ($ parameters as $ currentData ) {
144+ // if (is_array($currentData)) {
145+ // //TODO
146+ // $currentData = '';
147+ // }
148+
149+ if (strlen ($ currentData ) > 0 ) {
150+ $ hashString .= strlen ($ currentData );
151+ $ hashString .= $ currentData ;
152+ }
153+ }
154+
155+ return $ hashString ;
156+ }
157+
158+ private function reportsSign ($ parameters )
159+ {
160+ $ sourceString = $ this ->buildReportsSourceString ($ parameters );
161+
162+ return hash_hmac ('MD5 ' , $ sourceString , $ this ->merchant ->getSecret ());
163+ }
164+
43165 /**
44166 * Отправка GET-запроса
45167 * @param string $api адрес API (URI)
46168 * @return array ответ сервера Ypmn
169+ * @throws PaymentException
47170 */
48171 private function sendGetRequest (string $ api ): array
49172 {
50173 $ curl = curl_init ();
51174 $ date = (new DateTime ())->format (DateTimeInterface::ATOM );
52175 $ requestHttpVerb = 'GET ' ;
53176
54- if ($ this ->localModeIsOn ) {
55- $ urlToPostTo = self ::LOCAL_HOST . $ api ;
56- } else {
57- $ urlToPostTo = ($ this ->getSandboxMode () ? self ::SANDBOX_HOST : self ::HOST ) . $ api ;
58- }
59-
60177 $ setopt_array = [
61- CURLOPT_URL => $ urlToPostTo ,
178+ CURLOPT_URL => $ this -> getHost () . $ api ,
62179 CURLOPT_RETURNTRANSFER => true ,
63180 CURLOPT_ENCODING => '' ,
64181 CURLOPT_MAXREDIRS => 10 ,
@@ -73,7 +190,7 @@ private function sendGetRequest(string $api): array
73190 'X-Header-Signature: ' . $ this ->getSignature (
74191 $ this ->merchant ,
75192 $ date ,
76- $ urlToPostTo ,
193+ $ this -> getHost () . $ api ,
77194 $ requestHttpVerb ,
78195 md5 ('' ),
79196 )
@@ -88,7 +205,7 @@ private function sendGetRequest(string $api): array
88205
89206 if (true === $ this ->getDebugMode ()) {
90207 $ this ->echoDebugMessage ('GET-Запрос к серверу Ypmn: ' );
91- $ this ->echoDebugMessage ($ urlToPostTo );
208+ $ this ->echoDebugMessage ($ this -> getHost () . $ api );
92209 $ this ->echoDebugMessage ('Ответ от сервера Ypmn: ' );
93210 $ this ->echoDebugMessage (json_encode (json_decode ($ response ), JSON_PRETTY_PRINT ));
94211
@@ -149,14 +266,8 @@ private function sendPostRequest(JsonSerializable $data, string $api): array
149266 $ date = (new DateTime ())->format (DateTimeInterface::ATOM );
150267 $ requestHttpVerb = 'POST ' ;
151268
152- if ($ this ->localModeIsOn ) {
153- $ urlToPostTo = self ::LOCAL_HOST . $ api ;
154- } else {
155- $ urlToPostTo = ($ this ->getSandboxMode () ? self ::SANDBOX_HOST : self ::HOST ) . $ api ;
156- }
157-
158269 curl_setopt_array ($ curl , [
159- CURLOPT_URL => $ urlToPostTo ,
270+ CURLOPT_URL => $ this -> getHost () . $ api ,
160271 CURLOPT_RETURNTRANSFER => true ,
161272 CURLOPT_ENCODING => '' ,
162273 CURLOPT_MAXREDIRS => 10 ,
@@ -172,7 +283,7 @@ private function sendPostRequest(JsonSerializable $data, string $api): array
172283 'X-Header-Signature: ' . $ this ->getSignature (
173284 $ this ->merchant ,
174285 $ date ,
175- $ urlToPostTo ,
286+ $ this -> getHost () . $ api ,
176287 $ requestHttpVerb ,
177288 $ encodedJsonDataHash
178289 )
0 commit comments