16 * 1024) { throw new RuntimeException('CheJiu request exceeds the size limit'); } $form = [ 'app_id' => $this->appId, 'app_key' => $this->appKey, 'operation' => $operation, 'payload' => rtrim(strtr(base64_encode($json), '+/', '-_'), '='), ]; $form['sign'] = Str::generateSignature($form, $this->appKey); try { $response = $this->http->request('POST', $this->origin . '/shared/chejiu/v1', [ 'form_params' => $form, 'allow_redirects' => false, 'http_errors' => false, 'verify' => true, 'connect_timeout' => 5, 'timeout' => 15, 'stream' => true, 'read_timeout' => 10, ]); $body = $response->getBody(); $raw = ''; $limit = 4 * 1024 * 1024; while (!$body->eof() && strlen($raw) <= $limit) { $chunk = $body->read(min(8192, $limit + 1 - strlen($raw))); if ($chunk === '' && !$body->eof()) { throw new RuntimeException('CheJiu response ended early'); } $raw .= $chunk; } if (strlen($raw) > $limit) { throw new RuntimeException('CheJiu response exceeds the size limit'); } $data = json_decode($raw, true, 64, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING); if (!is_array($data)) { throw new RuntimeException('Invalid CheJiu response'); } return ['status' => $response->getStatusCode(), 'data' => $data]; } catch (\Throwable) { // Do not retain transport exceptions that may include the signed form. throw new RuntimeException('CheJiu request outcome unavailable; keep the original order reference'); } finally { if (isset($body)) { $body->close(); } } } /** Verify the notification before looking up the canonical, account-owned order. */ public function queryAfterCallback( string $apiKey, string $timestamp, string $signature, string $rawBody, string $expectedReference, ): array { if (!hash_equals($this->appId, $apiKey) || !preg_match('/\A[1-9][0-9]{9,10}\z/D', $timestamp) || abs(time() - (int)$timestamp) > 60 || strlen($rawBody) > 4 * 1024 * 1024 || !preg_match('/\A[a-f0-9]{64}\z/D', $signature)) { throw new RuntimeException('Invalid CheJiu callback'); } $canonical = implode("\n", ['POST', self::SIGNATURE_PATH, $timestamp, md5($rawBody)]); $expected = hash_hmac('sha256', $canonical, $this->appKey); if (!hash_equals($expected, $signature)) { throw new RuntimeException('Invalid CheJiu callback'); } $notice = json_decode($rawBody, true, 64, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING); if (!is_array($notice) || !preg_match('/\A[A-Za-z0-9_.:-]{1,64}\z/D', $expectedReference) || ($notice['downstream_order_no'] ?? null) !== $expectedReference || (string)($notice['timestamp'] ?? '') !== $timestamp || !self::orderId($notice['order_id'] ?? null)) { throw new RuntimeException('Unexpected CheJiu callback order'); } $query = $this->call('query', ['downstream_order_no' => $expectedReference]); $order = $query['data']; if ($query['status'] !== 200 || ($order['ok'] ?? null) !== true || ($order['downstream_order_no'] ?? null) !== $expectedReference || !self::orderId($order['order_id'] ?? null) || (string)$order['order_id'] !== (string)$notice['order_id']) { throw new RuntimeException('CheJiu order status could not be confirmed'); } return $order; } private static function orderId(mixed $value): bool { return (is_int($value) || is_string($value)) && preg_match('/\A[1-9][0-9]{0,19}\z/D', (string)$value) === 1; } }