From 8e159edf1dc56b7b132e0d87843e0d365a44d23d Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Thu, 4 Dec 2025 11:00:24 +0800 Subject: [PATCH] chore(addon/huaweipay): mch_id -> merc_no --- src/addon/huaweipay/model/Config.php | 2 +- src/addon/huaweipay/model/Pay.php | 70 ++++++++++--------- src/addon/huaweipay/shop/controller/Pay.php | 4 +- src/addon/huaweipay/shop/view/pay/config.html | 4 +- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/addon/huaweipay/model/Config.php b/src/addon/huaweipay/model/Config.php index 1834b9bf3..66e21b69d 100644 --- a/src/addon/huaweipay/model/Config.php +++ b/src/addon/huaweipay/model/Config.php @@ -24,7 +24,7 @@ class Config extends BaseModel // 加密字段 private $encrypt_fields = [ - // 'mch_id', + // 'merc_no', // 'app_id', // 'mch_auth_id', 'private_key', diff --git a/src/addon/huaweipay/model/Pay.php b/src/addon/huaweipay/model/Pay.php index 87f3a83ca..c650c29d2 100644 --- a/src/addon/huaweipay/model/Pay.php +++ b/src/addon/huaweipay/model/Pay.php @@ -1,4 +1,5 @@ getPayConfig($this->site_id)['data']['value']; $this->config = $config_info; - if (empty($this->config)) throw new ApiException(-1, "平台未配置华为支付"); + if (empty($this->config)) + throw new ApiException(-1, "平台未配置华为支付"); } else { $this->config = $config_data; } // 添加站点ID - $this->config['site_id'] = $this->site_id; - - // 初始化华为支付客户端 - $this->hwpay_client = new HuaweiPayClient($this->config); + $this->config['site_id'] = $this->site_id; + + // 创建华为支付客户端 + $this->hwpay_client = new HuaweiPayClient($this->config); } /** @@ -94,7 +96,7 @@ class Pay extends BaseModel $parameter = array( "out_trade_no" => $param["out_trade_no"], "subject" => mb_substr($param["pay_body"], 0, 128, 'UTF-8'), // 限制长度,支持中文 - "total_amount" => number_format((float)$param["pay_money"], 2, '.', ''), // 确保金额格式正确 + "total_amount" => number_format((float) $param["pay_money"], 2, '.', ''), // 确保金额格式正确 "body" => mb_substr($param["pay_body"], 0, 512, 'UTF-8'), // 限制长度,支持中文 ); @@ -102,7 +104,7 @@ class Pay extends BaseModel if (!empty($param["client_ip"])) { $parameter["client_ip"] = $param["client_ip"]; } - + // 添加附加数据(如果有) if (!empty($param["attach"])) { $parameter["attach"] = mb_substr($param["attach"], 0, 128, 'UTF-8'); @@ -120,10 +122,10 @@ class Pay extends BaseModel case "weapp": // 微信小程序支付 $result = $this->hwpay_client->weappPay($parameter, $param["notify_url"]); - + // 记录响应日志 Log::info('华为微信小程序支付响应: ' . json_encode($result)); - + if (isset($result['code']) && $result['code'] == '0') { return $this->success([ 'type' => 'params', @@ -140,12 +142,12 @@ class Pay extends BaseModel if (empty($param["return_url"])) { throw new \Exception('H5支付缺少必要参数:return_url'); } - + $result = $this->hwpay_client->h5Pay($parameter, $param["return_url"], $param["notify_url"]); - + // 记录响应日志 Log::info('华为H5支付响应: ' . json_encode($result)); - + if (isset($result['code']) && $result['code'] == '0') { // 检查是否有pay_url if (isset($result['pay_url'])) { @@ -165,10 +167,10 @@ class Pay extends BaseModel case "app": // APP支付 $result = $this->hwpay_client->appPay($parameter, $param["notify_url"]); - + // 记录响应日志 Log::info('华为APP支付响应: ' . json_encode($result)); - + if (isset($result['code']) && $result['code'] == '0') { return $this->success([ 'type' => 'params', @@ -201,10 +203,10 @@ class Pay extends BaseModel $parameter = array( "out_trade_no" => $param["out_trade_no"] ); - + // 调用华为支付关闭订单API $result = $this->hwpay_client->closeOrder($parameter); - + if ($result['code'] == '0') { return $this->success(); } else { @@ -234,40 +236,40 @@ class Pay extends BaseModel if (!isset($param["refund_fee"]) || $param["refund_fee"] <= 0) { throw new \Exception('缺少或无效的参数:refund_fee'); } - + $pay_info = $param["pay_info"]; $refund_no = $param["refund_no"]; $out_trade_no = $pay_info["out_trade_no"] ?? ''; $trade_no = $pay_info["trade_no"] ?? ''; $refund_fee = $param["refund_fee"]; - + // 至少需要out_trade_no或trade_no中的一个 if (empty($out_trade_no) && empty($trade_no)) { throw new \Exception('退款请求必须提供out_trade_no或trade_no'); } - + $parameter = array( 'refund_amount' => number_format($refund_fee, 2, '.', ''), // 确保金额格式正确 'out_request_no' => $refund_no, 'refund_reason' => isset($param["refund_desc"]) ? mb_substr($param["refund_desc"], 0, 512, 'UTF-8') : '用户退款', ); - + // 添加订单标识(二选一) if (!empty($out_trade_no)) { $parameter['out_trade_no'] = $out_trade_no; } else { $parameter['trade_no'] = $trade_no; } - + // 记录退款请求日志 Log::info('华为支付退款请求参数: ' . json_encode($parameter)); - + // 调用华为支付退款API $result = $this->hwpay_client->refund($parameter); - + // 记录退款响应日志 Log::info('华为支付退款响应: ' . json_encode($result)); - + if (isset($result['code']) && $result['code'] == '0') { return $this->success($result); } else { @@ -308,28 +310,28 @@ class Pay extends BaseModel try { // 记录原始回调参数 Log::info('华为支付回调参数: ' . json_encode($param)); - + // 验证华为支付回调签名 $is_valid = $this->hwpay_client->verifyNotify($param); - + if ($is_valid) { // 验证成功 // 获取订单号和交易状态 $out_trade_no = $param['out_trade_no'] ?? ''; $trade_no = $param['trade_no'] ?? ''; $trade_status = $param['trade_status'] ?? ''; - + // 验证必要参数 if (empty($out_trade_no)) { Log::error('华为支付回调缺少out_trade_no'); echo "fail"; return; } - + // 根据华为支付文档,成功状态可能是'SUCCESS'或'TRADE_SUCCESS' if ($trade_status == "SUCCESS" || $trade_status == "TRADE_SUCCESS") { $pay_common = new PayCommon(); $retval = $pay_common->onlinePay($out_trade_no, "huaweipay", $trade_no, "huaweipay"); - + // 记录支付结果 if ($retval['code'] >= 0) { Log::info('华为支付回调处理成功,订单号: ' . $out_trade_no); @@ -339,7 +341,7 @@ class Pay extends BaseModel } else { Log::info('华为支付回调状态非成功: ' . $trade_status . ',订单号: ' . $out_trade_no); } - + // 无论支付是否成功,只要签名验证通过就返回success // 避免华为服务器重复推送 echo "success"; @@ -383,10 +385,10 @@ class Pay extends BaseModel $parameter = array( "out_trade_no" => $param["out_trade_no"], ); - + // 调用华为支付查询订单API $result = $this->hwpay_client->queryOrder($parameter); - + if ($result['code'] == '0') { return $this->success($result['data']); } else { diff --git a/src/addon/huaweipay/shop/controller/Pay.php b/src/addon/huaweipay/shop/controller/Pay.php index 9425ed03c..4560470cc 100644 --- a/src/addon/huaweipay/shop/controller/Pay.php +++ b/src/addon/huaweipay/shop/controller/Pay.php @@ -19,7 +19,7 @@ class Pay extends BaseShop $config_model = new ConfigModel(); if (request()->isJson()) { $app_id = input("app_id", "");//华为应用ID, // PETALPAY.APPID, 商户号关联的APPID - $mch_id = input("mch_id", "");//商户号, // PETALPAY.MERC_NO, 商户号 + $merc_no = input("merc_no", "");//商户号, // PETALPAY.MERC_NO, 商户号 $private_key = input("private_key", "");//商户应用私钥, // PETALPAY.MERC_PRIVATE_KEY, 商户应用私钥 $private_key_text = input("private_key_text", "");//商户应用私钥文本, // PETALPAY.MERC_PRIVATE_KEY_TEXT, 商户应用私钥文本 $mch_auth_id = input("mch_auth_id", "");//商户证书id, // PETALPAY.MERC_AUTH_ID, 商户证书id @@ -36,7 +36,7 @@ class Pay extends BaseShop $data = array ( "app_id" => $app_id, - "mch_id" => $mch_id, + "merc_no" => $merc_no, "private_key" => $private_key, "private_key_text" => $private_key_text, "mch_auth_id" => $mch_auth_id, diff --git a/src/addon/huaweipay/shop/view/pay/config.html b/src/addon/huaweipay/shop/view/pay/config.html index 5fe1f1786..d99f8dcc8 100644 --- a/src/addon/huaweipay/shop/view/pay/config.html +++ b/src/addon/huaweipay/shop/view/pay/config.html @@ -15,9 +15,9 @@