From 8ccdea99c8c843d45a2e9201d75368264205779a Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Mon, 2 Nov 2026 11:21:36 +0330 Subject: [PATCH] . --- .../abshode-domain-lookup/learned-notes.md | 2 +- .../Controllers/Admin/ShayeganController.php | 12 +-- app/Services/ShayeganClient.php | 73 ++++++++++++++++--- app/Services/ShayeganSyncService.php | 14 ++-- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/.cursor/skills/abshode-domain-lookup/learned-notes.md b/.cursor/skills/abshode-domain-lookup/learned-notes.md index c77ab52..1a3ad60 100644 --- a/.cursor/skills/abshode-domain-lookup/learned-notes.md +++ b/.cursor/skills/abshode-domain-lookup/learned-notes.md @@ -3,7 +3,7 @@ فرمت هر خط: - YYYY-MM-DD | موضوع | مسیر فایل(ها) | نکتهٔ ۱ خطی -- 2026-11-02 | PDF وب‌سرویس شایگان | `docs/shayegan/shayegan.pdf` صفحه ارسال حساب | نمونهٔ رسمی `AccountNumber` نمی‌فرستد؛ `MoinNumber` + `UpdateKind=0` + `AccountTypeId=1`. فرمول `12101530` در PDF نیست +- 2026-11-02 | دیباگ تبادل شایگان | `ShayeganClient::request` + `registerCustomer` / health / retry | پاسخ ادمین `client_request` و `server_response` دارد؛ رمز DB و Auth برابر `***` - 2026-11-02 | سه سطح شایگان | `ShayeganSyncService::registerCustomer` + migration `2026_11_02_000001` | کل 121، معین `12101`، تفصیلی = معین+کد کاربری (`12101530`). `1210101` غلط است - 2026-11-01 | پیامک تبدیل | `CopperConvertRequestService::notifyCustomer` | بعد از تأیید یا رد، پیامک جدا به موبایل مشتری؛ خطا پیامک سفارش را برنمی‌گرداند - 2026-11-01 | درخواست تبدیل مس | `CopperConvertRequestService` + `copper_convert_requests` + `POST customer/copper-convert-request` + `admin/copper-convert-request/{id}/approve` | تأیید = فروش دارایی و خرید حلقه با quote_unit_price؛ مهاجرت `2026_11_01_000001` diff --git a/app/Http/Controllers/Admin/ShayeganController.php b/app/Http/Controllers/Admin/ShayeganController.php index d6e4f92..71b0828 100644 --- a/app/Http/Controllers/Admin/ShayeganController.php +++ b/app/Http/Controllers/Admin/ShayeganController.php @@ -28,14 +28,13 @@ class ShayeganController extends Controller $result = $this->client->healthCheck(); $status = ! empty($result['ok']) ? 200 : 422; - return response()->json([ + return response()->json(array_merge([ 'ok' => ! empty($result['ok']), 'message' => $result['message'] ?? null, 'http_status' => $result['http_status'] ?? null, 'timeout' => ! empty($result['timeout']), - // never echo Config passwords 'body' => $this->sanitizeBodyForAdmin($result['body'] ?? null), - ], $status); + ], $this->client->debugExchange($result)), $status); } /** @@ -136,13 +135,14 @@ class ShayeganController extends Controller } $result = $this->client->put($endpoint, $put); + $exchange = $this->client->debugExchange($result); if (! empty($result['timeout'])) { $row->sync_status = ShayeganSyncService::STATUS_TIMEOUT; $row->sync_log = (string) ($result['message'] ?? 'timeout'); $row->res = json_encode(['timeout' => true], JSON_UNESCAPED_UNICODE); $row->save(); - return response()->json(['ok' => false, 'message' => 'timeout', 'row' => $row], 422); + return response()->json(array_merge(['ok' => false, 'message' => 'timeout', 'row' => $row], $exchange), 422); } if (! empty($result['ok'])) { @@ -153,7 +153,7 @@ class ShayeganController extends Controller $row->res = json_encode($result['body'] ?? [], JSON_UNESCAPED_UNICODE); $row->save(); - return response()->json(['ok' => true, 'row' => $row]); + return response()->json(array_merge(['ok' => true, 'row' => $row], $exchange)); } $row->sync_status = ShayeganSyncService::STATUS_FAILED; @@ -161,7 +161,7 @@ class ShayeganController extends Controller $row->res = json_encode($result['body'] ?? [], JSON_UNESCAPED_UNICODE); $row->save(); - return response()->json(['ok' => false, 'message' => $row->sync_log, 'row' => $row], 422); + return response()->json(array_merge(['ok' => false, 'message' => $row->sync_log, 'row' => $row], $exchange), 422); } /** diff --git a/app/Services/ShayeganClient.php b/app/Services/ShayeganClient.php index 50d3a54..bed57aa 100644 --- a/app/Services/ShayeganClient.php +++ b/app/Services/ShayeganClient.php @@ -125,12 +125,19 @@ class ShayeganClient $url = $this->endpointUrl($endpoint, $query); $timeout = max(5, $this->config->getInt('shayegan_timeout_seconds', 30)); + $clientRequest = [ + 'endpoint' => $endpoint, + 'url' => $url, + 'query' => $query, + 'payload' => $this->redactPayload($payload), + ]; + $this->logTraffic('request', [ 'style' => $style, 'endpoint' => $endpoint, 'url' => $url, 'timeout_seconds' => $timeout, - 'payload' => $this->redactPayload($payload), + 'payload' => $clientRequest['payload'], ]); try { @@ -147,13 +154,14 @@ class ShayeganClient } $error = $this->extractErrorMessage($json); + $serverResponse = $this->serverResponse($status, $error !== '' ? $error : 'OK', $json); if ($status >= 400) { - $result = [ + $result = $this->withExchange([ 'ok' => false, 'message' => $error !== '' ? $error : ('HTTP ' . $status), 'http_status' => $status, 'body' => $json, - ]; + ], $clientRequest, $serverResponse); $this->logTraffic('response', [ 'style' => $style, 'endpoint' => $endpoint, @@ -168,12 +176,12 @@ class ShayeganClient } if ($error !== '') { - $result = [ + $result = $this->withExchange([ 'ok' => false, 'message' => $error, 'http_status' => $status, 'body' => $json, - ]; + ], $clientRequest, $serverResponse); $this->logTraffic('response', [ 'style' => $style, 'endpoint' => $endpoint, @@ -187,12 +195,12 @@ class ShayeganClient return $result; } - $result = [ + $result = $this->withExchange([ 'ok' => true, 'message' => 'OK', 'http_status' => $status, 'body' => $json, - ]; + ], $clientRequest, $serverResponse); $this->logTraffic('response', [ 'style' => $style, 'endpoint' => $endpoint, @@ -213,13 +221,13 @@ class ShayeganClient 'payload' => $this->redactPayload($payload), ]); - return [ + return $this->withExchange([ 'ok' => false, 'message' => 'timeout/connection: ' . $e->getMessage(), 'http_status' => null, 'body' => null, 'timeout' => true, - ]; + ], $clientRequest, $this->serverResponse(null, 'timeout/connection: ' . $e->getMessage(), null)); } catch (\Throwable $e) { Log::warning('shayegan.request_failed', [ 'endpoint' => $endpoint, @@ -229,15 +237,58 @@ class ShayeganClient 'payload' => $this->redactPayload($payload), ]); - return [ + return $this->withExchange([ 'ok' => false, 'message' => $e->getMessage(), 'http_status' => null, 'body' => null, - ]; + ], $clientRequest, $this->serverResponse(null, $e->getMessage(), null)); } } + /** + * @param array $result + * @return array{client_request:mixed,server_response:mixed} + */ + public function debugExchange(array $result): array + { + return [ + 'client_request' => $result['client_request'] ?? null, + 'server_response' => $result['server_response'] ?? null, + ]; + } + + /** + * @param array $result + * @param array $clientRequest + * @param array $serverResponse + * @return array + */ + private function withExchange(array $result, array $clientRequest, array $serverResponse): array + { + $result['client_request'] = $clientRequest; + $result['server_response'] = $serverResponse; + + return $result; + } + + /** + * @param mixed $body + * @return array + */ + private function serverResponse(?int $status, ?string $message, $body): array + { + $response = [ + 'message' => $message, + 'http_status' => $status, + ]; + if (is_array($body)) { + $response['body'] = $body; + } + + return $response; + } + /** * @param array $query */ diff --git a/app/Services/ShayeganSyncService.php b/app/Services/ShayeganSyncService.php index 7cab2e2..8c5fc59 100644 --- a/app/Services/ShayeganSyncService.php +++ b/app/Services/ShayeganSyncService.php @@ -138,16 +138,20 @@ class ShayeganSyncService $result = $this->client->put('Account/Put', $put); $accountNumber = trim((string) ($result['number'] ?? '')); + $exchange = $this->client->debugExchange($result); if (! empty($result['timeout'])) { - return ['ok' => false, 'message' => (string) ($result['message'] ?? 'timeout')]; + return array_merge([ + 'ok' => false, + 'message' => (string) ($result['message'] ?? 'timeout'), + ], $exchange); } if (empty($result['ok']) || $accountNumber === '') { - return [ + return array_merge([ 'ok' => false, 'message' => (string) ($result['message'] ?? 'ثبت حساب مشتری در شایگان ناموفق بود'), - ]; + ], $exchange); } $customer->moshtari_code = $accountNumber; @@ -174,11 +178,11 @@ class ShayeganSyncService $row->res = json_encode($result['body'] ?? [], JSON_UNESCAPED_UNICODE); $row->save(); - return [ + return array_merge([ 'ok' => true, 'message' => 'حساب تفصیلی مشتری در شایگان ایجاد شد', 'moshtari_code' => $accountNumber, - ]; + ], $exchange); } /**