chore(addon/huaweipay): mch_id -> merc_no

This commit is contained in:
2025-12-04 11:00:24 +08:00
parent 1793e4b2aa
commit 8e159edf1d
4 changed files with 41 additions and 39 deletions

View File

@@ -24,7 +24,7 @@ class Config extends BaseModel
// 加密字段 // 加密字段
private $encrypt_fields = [ private $encrypt_fields = [
// 'mch_id', // 'merc_no',
// 'app_id', // 'app_id',
// 'mch_auth_id', // 'mch_auth_id',
'private_key', 'private_key',

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* 华为支付模型 * 华为支付模型
*/ */
@@ -22,13 +23,13 @@ class Pay extends BaseModel
* 支付接口实例 * 支付接口实例
* @var * @var
*/ */
private $hwpay_client; private $hwpay_client;
/** /**
* 支付配置 * 支付配置
* @var array|mixed * @var array|mixed
*/ */
private $config = []; private $config = [];
/** /**
* 站点id * 站点id
@@ -49,16 +50,17 @@ class Pay extends BaseModel
if (empty($config_data)) { if (empty($config_data)) {
$config_info = (new Config())->getPayConfig($this->site_id)['data']['value']; $config_info = (new Config())->getPayConfig($this->site_id)['data']['value'];
$this->config = $config_info; $this->config = $config_info;
if (empty($this->config)) throw new ApiException(-1, "平台未配置华为支付"); if (empty($this->config))
throw new ApiException(-1, "平台未配置华为支付");
} else { } else {
$this->config = $config_data; $this->config = $config_data;
} }
// 添加站点ID // 添加站点ID
$this->config['site_id'] = $this->site_id; $this->config['site_id'] = $this->site_id;
// 初始化华为支付客户端 // 创建华为支付客户端
$this->hwpay_client = new HuaweiPayClient($this->config); $this->hwpay_client = new HuaweiPayClient($this->config);
} }
/** /**
@@ -94,7 +96,7 @@ class Pay extends BaseModel
$parameter = array( $parameter = array(
"out_trade_no" => $param["out_trade_no"], "out_trade_no" => $param["out_trade_no"],
"subject" => mb_substr($param["pay_body"], 0, 128, 'UTF-8'), // 限制长度,支持中文 "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'), // 限制长度,支持中文 "body" => mb_substr($param["pay_body"], 0, 512, 'UTF-8'), // 限制长度,支持中文
); );
@@ -102,7 +104,7 @@ class Pay extends BaseModel
if (!empty($param["client_ip"])) { if (!empty($param["client_ip"])) {
$parameter["client_ip"] = $param["client_ip"]; $parameter["client_ip"] = $param["client_ip"];
} }
// 添加附加数据(如果有) // 添加附加数据(如果有)
if (!empty($param["attach"])) { if (!empty($param["attach"])) {
$parameter["attach"] = mb_substr($param["attach"], 0, 128, 'UTF-8'); $parameter["attach"] = mb_substr($param["attach"], 0, 128, 'UTF-8');
@@ -120,10 +122,10 @@ class Pay extends BaseModel
case "weapp": case "weapp":
// 微信小程序支付 // 微信小程序支付
$result = $this->hwpay_client->weappPay($parameter, $param["notify_url"]); $result = $this->hwpay_client->weappPay($parameter, $param["notify_url"]);
// 记录响应日志 // 记录响应日志
Log::info('华为微信小程序支付响应: ' . json_encode($result)); Log::info('华为微信小程序支付响应: ' . json_encode($result));
if (isset($result['code']) && $result['code'] == '0') { if (isset($result['code']) && $result['code'] == '0') {
return $this->success([ return $this->success([
'type' => 'params', 'type' => 'params',
@@ -140,12 +142,12 @@ class Pay extends BaseModel
if (empty($param["return_url"])) { if (empty($param["return_url"])) {
throw new \Exception('H5支付缺少必要参数return_url'); throw new \Exception('H5支付缺少必要参数return_url');
} }
$result = $this->hwpay_client->h5Pay($parameter, $param["return_url"], $param["notify_url"]); $result = $this->hwpay_client->h5Pay($parameter, $param["return_url"], $param["notify_url"]);
// 记录响应日志 // 记录响应日志
Log::info('华为H5支付响应: ' . json_encode($result)); Log::info('华为H5支付响应: ' . json_encode($result));
if (isset($result['code']) && $result['code'] == '0') { if (isset($result['code']) && $result['code'] == '0') {
// 检查是否有pay_url // 检查是否有pay_url
if (isset($result['pay_url'])) { if (isset($result['pay_url'])) {
@@ -165,10 +167,10 @@ class Pay extends BaseModel
case "app": case "app":
// APP支付 // APP支付
$result = $this->hwpay_client->appPay($parameter, $param["notify_url"]); $result = $this->hwpay_client->appPay($parameter, $param["notify_url"]);
// 记录响应日志 // 记录响应日志
Log::info('华为APP支付响应: ' . json_encode($result)); Log::info('华为APP支付响应: ' . json_encode($result));
if (isset($result['code']) && $result['code'] == '0') { if (isset($result['code']) && $result['code'] == '0') {
return $this->success([ return $this->success([
'type' => 'params', 'type' => 'params',
@@ -201,10 +203,10 @@ class Pay extends BaseModel
$parameter = array( $parameter = array(
"out_trade_no" => $param["out_trade_no"] "out_trade_no" => $param["out_trade_no"]
); );
// 调用华为支付关闭订单API // 调用华为支付关闭订单API
$result = $this->hwpay_client->closeOrder($parameter); $result = $this->hwpay_client->closeOrder($parameter);
if ($result['code'] == '0') { if ($result['code'] == '0') {
return $this->success(); return $this->success();
} else { } else {
@@ -234,40 +236,40 @@ class Pay extends BaseModel
if (!isset($param["refund_fee"]) || $param["refund_fee"] <= 0) { if (!isset($param["refund_fee"]) || $param["refund_fee"] <= 0) {
throw new \Exception('缺少或无效的参数refund_fee'); throw new \Exception('缺少或无效的参数refund_fee');
} }
$pay_info = $param["pay_info"]; $pay_info = $param["pay_info"];
$refund_no = $param["refund_no"]; $refund_no = $param["refund_no"];
$out_trade_no = $pay_info["out_trade_no"] ?? ''; $out_trade_no = $pay_info["out_trade_no"] ?? '';
$trade_no = $pay_info["trade_no"] ?? ''; $trade_no = $pay_info["trade_no"] ?? '';
$refund_fee = $param["refund_fee"]; $refund_fee = $param["refund_fee"];
// 至少需要out_trade_no或trade_no中的一个 // 至少需要out_trade_no或trade_no中的一个
if (empty($out_trade_no) && empty($trade_no)) { if (empty($out_trade_no) && empty($trade_no)) {
throw new \Exception('退款请求必须提供out_trade_no或trade_no'); throw new \Exception('退款请求必须提供out_trade_no或trade_no');
} }
$parameter = array( $parameter = array(
'refund_amount' => number_format($refund_fee, 2, '.', ''), // 确保金额格式正确 'refund_amount' => number_format($refund_fee, 2, '.', ''), // 确保金额格式正确
'out_request_no' => $refund_no, 'out_request_no' => $refund_no,
'refund_reason' => isset($param["refund_desc"]) ? mb_substr($param["refund_desc"], 0, 512, 'UTF-8') : '用户退款', 'refund_reason' => isset($param["refund_desc"]) ? mb_substr($param["refund_desc"], 0, 512, 'UTF-8') : '用户退款',
); );
// 添加订单标识(二选一) // 添加订单标识(二选一)
if (!empty($out_trade_no)) { if (!empty($out_trade_no)) {
$parameter['out_trade_no'] = $out_trade_no; $parameter['out_trade_no'] = $out_trade_no;
} else { } else {
$parameter['trade_no'] = $trade_no; $parameter['trade_no'] = $trade_no;
} }
// 记录退款请求日志 // 记录退款请求日志
Log::info('华为支付退款请求参数: ' . json_encode($parameter)); Log::info('华为支付退款请求参数: ' . json_encode($parameter));
// 调用华为支付退款API // 调用华为支付退款API
$result = $this->hwpay_client->refund($parameter); $result = $this->hwpay_client->refund($parameter);
// 记录退款响应日志 // 记录退款响应日志
Log::info('华为支付退款响应: ' . json_encode($result)); Log::info('华为支付退款响应: ' . json_encode($result));
if (isset($result['code']) && $result['code'] == '0') { if (isset($result['code']) && $result['code'] == '0') {
return $this->success($result); return $this->success($result);
} else { } else {
@@ -308,28 +310,28 @@ class Pay extends BaseModel
try { try {
// 记录原始回调参数 // 记录原始回调参数
Log::info('华为支付回调参数: ' . json_encode($param)); Log::info('华为支付回调参数: ' . json_encode($param));
// 验证华为支付回调签名 // 验证华为支付回调签名
$is_valid = $this->hwpay_client->verifyNotify($param); $is_valid = $this->hwpay_client->verifyNotify($param);
if ($is_valid) { // 验证成功 if ($is_valid) { // 验证成功
// 获取订单号和交易状态 // 获取订单号和交易状态
$out_trade_no = $param['out_trade_no'] ?? ''; $out_trade_no = $param['out_trade_no'] ?? '';
$trade_no = $param['trade_no'] ?? ''; $trade_no = $param['trade_no'] ?? '';
$trade_status = $param['trade_status'] ?? ''; $trade_status = $param['trade_status'] ?? '';
// 验证必要参数 // 验证必要参数
if (empty($out_trade_no)) { if (empty($out_trade_no)) {
Log::error('华为支付回调缺少out_trade_no'); Log::error('华为支付回调缺少out_trade_no');
echo "fail"; echo "fail";
return; return;
} }
// 根据华为支付文档,成功状态可能是'SUCCESS'或'TRADE_SUCCESS' // 根据华为支付文档,成功状态可能是'SUCCESS'或'TRADE_SUCCESS'
if ($trade_status == "SUCCESS" || $trade_status == "TRADE_SUCCESS") { if ($trade_status == "SUCCESS" || $trade_status == "TRADE_SUCCESS") {
$pay_common = new PayCommon(); $pay_common = new PayCommon();
$retval = $pay_common->onlinePay($out_trade_no, "huaweipay", $trade_no, "huaweipay"); $retval = $pay_common->onlinePay($out_trade_no, "huaweipay", $trade_no, "huaweipay");
// 记录支付结果 // 记录支付结果
if ($retval['code'] >= 0) { if ($retval['code'] >= 0) {
Log::info('华为支付回调处理成功,订单号: ' . $out_trade_no); Log::info('华为支付回调处理成功,订单号: ' . $out_trade_no);
@@ -339,7 +341,7 @@ class Pay extends BaseModel
} else { } else {
Log::info('华为支付回调状态非成功: ' . $trade_status . ',订单号: ' . $out_trade_no); Log::info('华为支付回调状态非成功: ' . $trade_status . ',订单号: ' . $out_trade_no);
} }
// 无论支付是否成功只要签名验证通过就返回success // 无论支付是否成功只要签名验证通过就返回success
// 避免华为服务器重复推送 // 避免华为服务器重复推送
echo "success"; echo "success";
@@ -383,10 +385,10 @@ class Pay extends BaseModel
$parameter = array( $parameter = array(
"out_trade_no" => $param["out_trade_no"], "out_trade_no" => $param["out_trade_no"],
); );
// 调用华为支付查询订单API // 调用华为支付查询订单API
$result = $this->hwpay_client->queryOrder($parameter); $result = $this->hwpay_client->queryOrder($parameter);
if ($result['code'] == '0') { if ($result['code'] == '0') {
return $this->success($result['data']); return $this->success($result['data']);
} else { } else {

View File

@@ -19,7 +19,7 @@ class Pay extends BaseShop
$config_model = new ConfigModel(); $config_model = new ConfigModel();
if (request()->isJson()) { if (request()->isJson()) {
$app_id = input("app_id", "");//华为应用ID, // PETALPAY.APPID, 商户号关联的APPID $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 = input("private_key", "");//商户应用私钥, // PETALPAY.MERC_PRIVATE_KEY, 商户应用私钥
$private_key_text = input("private_key_text", "");//商户应用私钥文本, // PETALPAY.MERC_PRIVATE_KEY_TEXT, 商户应用私钥文本 $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 $mch_auth_id = input("mch_auth_id", "");//商户证书id, // PETALPAY.MERC_AUTH_ID, 商户证书id
@@ -36,7 +36,7 @@ class Pay extends BaseShop
$data = array ( $data = array (
"app_id" => $app_id, "app_id" => $app_id,
"mch_id" => $mch_id, "merc_no" => $merc_no,
"private_key" => $private_key, "private_key" => $private_key,
"private_key_text" => $private_key_text, "private_key_text" => $private_key_text,
"mch_auth_id" => $mch_auth_id, "mch_auth_id" => $mch_auth_id,

View File

@@ -15,9 +15,9 @@
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label" name="mch_id"><span class="required">*</span>商户号:</label> <label class="layui-form-label" name="merc_no"><span class="required">*</span>商户号:</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input name="mch_id" type="text" value="{$info.mch_id ?? ''}" class="layui-input len-long" lay-verify="required"> <input name="merc_no" type="text" value="{$info.merc_no ?? ''}" class="layui-input len-long" lay-verify="required">
</div> </div>
<div class="word-aux"><span>[MERC_NO]</span>华为支付商户号 <a href="https://developer.huawei.com/consumer/cn/doc/pay-docs/hwzf-shanghuhao-0000001725982508" target="_blank">查看指引</a></div> <div class="word-aux"><span>[MERC_NO]</span>华为支付商户号 <a href="https://developer.huawei.com/consumer/cn/doc/pay-docs/hwzf-shanghuhao-0000001725982508" target="_blank">查看指引</a></div>
</div> </div>