$rocket]); $config = get_provider_config('wechat', $rocket->getParams()); $payload = $rocket->getPayload(); $timestamp = time(); $random = Str::random(32); $signContent = $this->getSignatureContent($config, $payload, $timestamp, $random); $signature = $this->getSignature($config, $timestamp, $random, $signContent); $rocket->mergePayload(['_authorization' => $signature]); Logger::info('[Wechat][V3][AddPayloadSignaturePlugin] 插件装载完毕', ['rocket' => $rocket]); return $next($rocket); } /** * @throws InvalidParamsException */ protected function getSignatureContent(array $config, ?Collection $payload, int $timestamp, string $random): string { $url = get_wechat_url($config, $payload); $urlPath = parse_url($url, PHP_URL_PATH); $urlQuery = parse_url($url, PHP_URL_QUERY); return get_wechat_method($payload)."\n" .$urlPath.(empty($urlQuery) ? '' : '?'.$urlQuery)."\n" .$timestamp."\n" .$random."\n" .get_wechat_body($payload)."\n"; } /** * @throws InvalidConfigException */ protected function getSignature(array $config, int $timestamp, string $random, string $contents): string { $mchPublicCertPath = $config['mch_public_cert_path'] ?? null; if (empty($mchPublicCertPath)) { throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_public_cert_path]'); } $ssl = openssl_x509_parse(get_public_cert($mchPublicCertPath)); if (empty($ssl['serialNumberHex'])) { throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 解析微信配置 [mch_public_cert_path] 出错'); } $auth = sprintf( 'mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $config['mch_id'] ?? '', $random, $timestamp, $ssl['serialNumberHex'], get_wechat_sign($config, $contents), ); return 'WECHATPAY2-SHA256-RSA2048 '.$auth; } }