validated(); $oid = $validated['oid']; $price = (int) $validated['price']; $timestamp = $validated['timestamp']; $rateLimitKey = $this->rateLimitKey($request, $oid); if (RateLimiter::tooManyAttempts($rateLimitKey, 20)) { return response()->json([ 'success' => false, 'message' => 'Too many KG Inicis signature requests. Please try again later.', ], 429); } RateLimiter::hit($rateLimitKey, 60); if (! $this->isTimestampFresh($timestamp)) { return response()->json([ 'success' => false, 'message' => 'Timestamp is stale or invalid (signature replay protection).', ], 422); } $contextError = $this->validateOrderContext($request, $oid, $price); if ($contextError instanceof JsonResponse) { return $contextError; } if (! $this->apiService->hasStandardPaymentCredentials()) { return response()->json([ 'success' => false, 'message' => 'KG Inicis standard payment credentials are not configured.', ], 422); } $signature = $this->apiService->generateSignature($oid, $price, $timestamp); $verification = $this->apiService->generateVerification($oid, $price, $timestamp); $mKey = $this->apiService->getMKey(); return response()->json([ 'data' => [ 'signature' => $signature, 'verification' => $verification, 'mKey' => $mKey, ], ]); } private function validateOrderContext(SignatureRequest $request, string $oid, int $price): ?JsonResponse { $order = $this->orderService->findByOrderNumber($oid); if (! $order) { return response()->json([ 'success' => false, 'message' => 'Order not found.', ], 404); } if (! $order->order_status->isBeforePayment()) { return response()->json([ 'success' => false, 'message' => 'Order is not payable.', ], 422); } if (strtoupper((string) $order->currency) !== 'KRW') { return response()->json([ 'success' => false, 'message' => 'Standard KG Inicis signature is only available for KRW orders.', ], 422); } if (! $this->requestMatchesOrderBuyer($request, $order)) { return response()->json([ 'success' => false, 'message' => 'Order buyer verification failed.', ], 403); } if ($price !== $this->expectedPaymentPrice($order)) { return response()->json([ 'success' => false, 'message' => 'Payment amount does not match the order amount.', ], 422); } return null; } private function rateLimitKey(SignatureRequest $request, string $oid): string { return 'sirsoft-pay_kginicis:signature:' . sha1($request->ip() . '|' . $oid); } }