-
-
-
-
-
+
@@ -51,6 +102,58 @@
layui.use(['layer','form','upload','element'], function(){
var $ = layui.$;
+ // 添加商户按钮点击事件
+ $('#add-merchant').on('click', function() {
+ var index = $('.merchant-item').length;
+ var newItemHtml = `
+
+ `;
+ $('#merchants-container').append(newItemHtml);
+ });
+
+ // 删除商户按钮点击事件(使用事件委托)
+ $(document).on('click', '.remove-merchant', function() {
+ if ($('.merchant-item').length <= 1) {
+ layer.msg('至少保留一个商户信息', {icon: 2});
+ } else {
+ $(this).closest('.merchant-item').remove();
+ // 重新排序索引
+ $('.merchant-item').each(function(idx) {
+ $(this).find('input').each(function() {
+ var name = $(this).attr('name');
+ if (name) {
+ $(this).attr('name', name.replace(/merchants\[\d+\]/, 'merchants[' + idx + ']'));
+ }
+ });
+ });
+ }
+ });
});
function check(){
diff --git a/app/api/controller/AliNotify.php b/app/api/controller/AliNotify.php
index 96e670a..09fb732 100755
--- a/app/api/controller/AliNotify.php
+++ b/app/api/controller/AliNotify.php
@@ -40,10 +40,15 @@ class AliNotify extends Base
public function initialize()
{
+ // 获取微信支付配置
+ $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig();
+ $merchant = $wxpayConfig['merchant'];
- $secret_key = getConfig('weixinpay')['keys'];
+ // 使用随机商户的密钥
+ static::$secretKey = $merchant['keys'];
+
+ // 获取公众号密钥
static::$secretKeyAccount = getConfig('wechatofficialaccount')['keys'];
- static::$secretKey = $secret_key;//秘钥
}
public function ceshi()
diff --git a/app/api/controller/Notify.php b/app/api/controller/Notify.php
index 5d2c6a1..6fb5ac3 100755
--- a/app/api/controller/Notify.php
+++ b/app/api/controller/Notify.php
@@ -40,9 +40,15 @@ class Notify extends Base
public function initialize()
{
- $secret_key = getConfig('weixinpay')['keys'];
+ // 获取微信支付配置
+ $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig();
+ $merchant = $wxpayConfig['merchant'];
+
+ // 使用随机商户的密钥
+ static::$secretKey = $merchant['keys'];
+
+ // 获取公众号密钥
static::$secretKeyAccount = getConfig('wechatofficialaccount')['keys'];
- static::$secretKey = $secret_key;//秘钥
}
public function ceshi()
diff --git a/app/api/controller/Pay.php b/app/api/controller/Pay.php
index c3a8a97..f833770 100755
--- a/app/api/controller/Pay.php
+++ b/app/api/controller/Pay.php
@@ -6,6 +6,7 @@ namespace app\api\controller;
use app\common\model\Order as OrderModel;
use app\common\model\ProductOrder;
use app\common\model\UserRecharge;
+use app\common\helper\WxPayHelper;
use think\App;
use think\facade\Db;
@@ -22,23 +23,39 @@ class Pay extends Base
{
// 获取系统微信设置
$wechat_setting = getConfig('wechat_setting');
+ $wechatofficialaccount_setting = getConfig('wechatofficialaccount_setting');
if ($this->ish5()) {
+ // 公众号配置不变
$config = getConfig('wechatofficialaccount');
- // 公众号使用自己的appid
- $this->appid = $config['appid'];
- $this->merchant = $config['mch_id'];
- $this->secretKey = $config['keys'];
- } else {
- $config = getConfig('weixinpay');
- // 如果系统设置中存在微信配置,则优先使用
- if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) {
- $this->appid = $wechat_setting['appid'];
+ // 如果系统设置中存在微信公众号配置,则优先使用
+ if (!empty($wechatofficialaccount_setting) && !empty($wechatofficialaccount_setting['appid']) && !empty($wechatofficialaccount_setting['appSecret'])) {
+ $this->appid = $wechatofficialaccount_setting['appid'];
} else {
+ // 公众号使用自己的appid
$this->appid = $config['appid'];
}
$this->merchant = $config['mch_id'];
$this->secretKey = $config['keys'];
+ } else {
+ // 小程序使用随机商户
+ $wxpayConfig = WxPayHelper::getWxPayConfig();
+ if (!empty($wxpayConfig['merchant'])) {
+ $this->appid = $wxpayConfig['appid'];
+ $this->merchant = $wxpayConfig['merchant']['mch_id'];
+ $this->secretKey = $wxpayConfig['merchant']['keys'];
+ } else {
+ // 如果没有获取到商户信息,则使用旧方式
+ $config = getConfig('weixinpay');
+ // 如果系统设置中存在微信配置,则优先使用
+ if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) {
+ $this->appid = $wechat_setting['appid'];
+ } else {
+ $this->appid = $config['appid'];
+ }
+ $this->merchant = $config['mch_id'];
+ $this->secretKey = $config['keys'];
+ }
}
$this->noticeurl = request()->domain() . '/api/notify/order_notify';#订单回调URL
diff --git a/app/common/helper/WxPayHelper.php b/app/common/helper/WxPayHelper.php
new file mode 100644
index 0000000..c62e5c1
--- /dev/null
+++ b/app/common/helper/WxPayHelper.php
@@ -0,0 +1,88 @@
+ '默认商户',
+ 'mch_id' => $old_config['mch_id'],
+ 'keys' => $old_config['keys'],
+ 'weight' => 1
+ ];
+ }
+ }
+
+ // 获取微信AppID
+ $appid = '';
+ if (!empty($wechat_setting) && !empty($wechat_setting['appid'])) {
+ $appid = $wechat_setting['appid'];
+ } else if (!empty($old_config) && !empty($old_config['appid'])) {
+ $appid = $old_config['appid'];
+ }
+
+ return [
+ 'merchant' => $merchant,
+ 'appid' => $appid
+ ];
+ }
+}
\ No newline at end of file
diff --git a/app/common/server/WechatOfficialAccount.php b/app/common/server/WechatOfficialAccount.php
index d9dd62c..9c07479 100755
--- a/app/common/server/WechatOfficialAccount.php
+++ b/app/common/server/WechatOfficialAccount.php
@@ -15,8 +15,17 @@ class WechatOfficialAccount extends MyController
public function initialize()
{
$weixinpay = getConfig("wechatofficialaccount");
- self::$wx_appid = $weixinpay['appid'];
- self::$wx_secret = $weixinpay['appSecret'];
+ $wechatofficialaccount_setting = getConfig("wechatofficialaccount_setting");
+
+ // 如果系统设置中存在微信公众号配置,则优先使用
+ if (!empty($wechatofficialaccount_setting) && !empty($wechatofficialaccount_setting['appid']) && !empty($wechatofficialaccount_setting['appSecret'])) {
+ self::$wx_appid = $wechatofficialaccount_setting['appid'];
+ self::$wx_secret = $wechatofficialaccount_setting['appSecret'];
+ } else {
+ self::$wx_appid = $weixinpay['appid'];
+ self::$wx_secret = $weixinpay['appSecret'];
+ }
+
self::$mch = $weixinpay['mch_id'];
self::$key = $weixinpay['keys'];
}
diff --git a/app/common/server/WechatRefund.php b/app/common/server/WechatRefund.php
index a8716a5..db49f2f 100755
--- a/app/common/server/WechatRefund.php
+++ b/app/common/server/WechatRefund.php
@@ -3,6 +3,7 @@
namespace app\common\server;
use app\MyController;
+use app\common\helper\WxPayHelper;
class WechatRefund extends MyController
@@ -14,10 +15,11 @@ class WechatRefund extends MyController
*/
public function OrderRefund($info)
{
- $weixinpay = getConfig("weixinpay");
- $appid = $weixinpay['appid'];
- $merchant = $weixinpay['mch_id'];
-
+ // 使用WxPayHelper获取随机商户配置
+ $wxpayConfig = WxPayHelper::getWxPayConfig();
+ $appid = $wxpayConfig['appid'];
+ $merchant = $wxpayConfig['merchant']['mch_id'];
+
$params['appid'] = $appid;
$params['mch_id'] = $merchant;
$params['nonce_str'] = $this->genRandomString();
@@ -45,8 +47,10 @@ class WechatRefund extends MyController
*/
public function MakeSign($params)
{
- $weixinpay = getConfig("weixinpay");
- $secretKey = $weixinpay['keys'];
+ // 使用WxPayHelper获取随机商户配置
+ $wxpayConfig = WxPayHelper::getWxPayConfig();
+ $secretKey = $wxpayConfig['merchant']['keys'];
+
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params);
@@ -153,8 +157,19 @@ class WechatRefund extends MyController
private function postXmlCurl($xml, $url, $second = 30)
{
$path = app()->getRootPath();
- $ssl_cert = $path . 'app/common/ssl/apiclient_cert.pem';
- $ssl_key = $path . 'app/common/ssl/apiclient_key.pem';
+
+ // 获取商户配置
+ $wxpayConfig = WxPayHelper::getWxPayConfig();
+ $merchant = $wxpayConfig['merchant'];
+
+ // 优先使用商户配置中的证书路径,如果未设置则使用默认路径
+ $ssl_cert = isset($merchant['ssl_cert']) && !empty($merchant['ssl_cert'])
+ ? $merchant['ssl_cert']
+ : $path . 'app/common/ssl/apiclient_cert.pem';
+
+ $ssl_key = isset($merchant['ssl_key']) && !empty($merchant['ssl_key'])
+ ? $merchant['ssl_key']
+ : $path . 'app/common/ssl/apiclient_key.pem';
$ch = curl_init();
//设置超时
diff --git a/app/common/server/Wx.php b/app/common/server/Wx.php
index 220738f..b76253a 100755
--- a/app/common/server/Wx.php
+++ b/app/common/server/Wx.php
@@ -14,20 +14,26 @@ class Wx extends MyController
public function initialize()
{
- $weixinpay = getConfig("weixinpay");
$wechat_setting = getConfig("wechat_setting");
+ // 获取微信支付配置
+ $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig();
+
// 如果系统设置中存在微信配置,则优先使用
if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) {
self::$wx_appid = $wechat_setting['appid'];
self::$wx_secret = $wechat_setting['appSecret'];
} else {
- self::$wx_appid = $weixinpay['appid'];
- self::$wx_secret = $weixinpay['appSecret'];
+ self::$wx_appid = $wxpayConfig['appid'];
+ // 从旧配置或商户配置中获取appSecret
+ $weixinpay = getConfig("weixinpay");
+ self::$wx_secret = !empty($weixinpay['appSecret']) ? $weixinpay['appSecret'] : '';
}
- self::$mch = $weixinpay['mch_id'];
- self::$key = $weixinpay['keys'];
+ // 使用商户信息
+ $merchant = $wxpayConfig['merchant'];
+ self::$mch = $merchant['mch_id'];
+ self::$key = $merchant['keys'];
}
/**