private function getWxPayInstance($appConfig) {
// 引入微信支付SDK
require_once('vendor/autoload.php');
// 商户号
$merchantId = $appConfig['sp_mchid'];
// 从本地文件中加载「商户API私钥」
$merchantPrivateKeyFilePath = 'file:///data/wwwroot/aasaas/user/Controllers/apiclient_key.pem';
if (!file_exists($merchantPrivateKeyFilePath)) {
throw new \Exception('商户API私钥文件不存在: ' . $merchantPrivateKeyFilePath);
}
$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
// 「商户API证书」的「证书序列号」
$merchantCertificateSerial = '291DB9DD6DF32D6C067F274B8AF8B1FE1A3967EA';
if (empty($merchantCertificateSerial)) {
throw new \Exception('商户API证书序列号未配置');
}
// 从本地文件中加载「微信支付公钥」
$platformPublicKeyFilePath = 'file:///data/wwwroot/aasaas/user/Controllers/pub_key.pem';
if (!file_exists($platformPublicKeyFilePath)) {
throw new \Exception('微信支付公钥文件不存在: ' . $platformPublicKeyFilePath);
}
$platformPublicKeyInstance = Rsa::from($platformPublicKeyFilePath, Rsa::KEY_TYPE_PUBLIC);
// 「微信支付公钥」的「微信支付公钥ID」
// 注意:这个ID需要从微信支付平台获取,不是证书序列号
$platformPublicKeyId = 'PUB_KEY_ID_0117179612632025101500192044001600'; // 请替换为正确的公钥ID
// 构造一个 APIv3 客户端实例
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformPublicKeyId => $platformPublicKeyInstance,
],
]);
// 自动获取并设置平台证书
//error_log('自动获取并设置平台证书: ' . json_encode($instance, JSON_UNESCAPED_UNICODE));
//$this->loadPlatformCertificates($instance);
return $instance;
}
$instance = $this->getWxPayInstance($appConfig);
// 构建Native支付参数
$params = [
'appid' => $appConfig['wx_app_id'],
'mchid' => $appConfig['wx_pay_mchid'],
'description' => '现场扫码支付',
'out_trade_no' => $order['order_no'],
'notify_url' => $GLOBALS['config']['notify_url'] ,
'amount' => [
'total' => intval($amount * 100),
'currency' => 'CNY'
],
'headers' => [
'Wechatpay-Serial' => 'PUB_KEY_ID_0117179612632025101500192044001600',
],
];
// 记录调试信息
error_log('Native支付参数: ' . json_encode($params, JSON_UNESCAPED_UNICODE));
// 调用微信支付Native下单接口
$resp = $instance->chain('/v3/pay/transactions/native')->post(['json' => $params]);
$response = json_decode((string) $resp->getBody(), true);
- code: 1001
- data: []
- msg: "生成二维码失败:Client error: `POST https://api.mch.weixin.qq.com/v3/pay/transactions/native` resulted in a `400 Bad Request` response"
- ret: 0

{"appid":"wxc837c789523eXXXX","mchid":"171796XXXX","description":"现场扫码支付","out_trade_no":"VB20251025220133478","notify_url":"https:\/\/s.aasaas.vip\/api\/pay\/notify","amount":{"total":5000,"currency":"CNY"},"headers":{"Wechatpay-Serial":"PUB_KEY_ID_011717961263202510150019204400XXXX"}}
Client error: `POST https://api.mch.weixin.qq.com/v3/pay/transactions/native` resulted in a `400 Bad Request` response | wx_app_id: wxc837c789523eXXXX | wx_pay_mchid: 171796XXXX"
PHP message: Native支付失败: Client error: `POST https://api.mch.weixin.qq.com/v3/pay/transactions/native` resulted in a `401 Unauthorized` response:
{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_ (truncated...)