151 lines
4.7 KiB
PHP
151 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace app\common\helper;
|
||
|
||
class WxPayHelper
|
||
{
|
||
/**
|
||
* 根据权重随机获取一个商户
|
||
*
|
||
* @param array $merchants 商户数组
|
||
* @return array|null 随机选择的商户信息
|
||
*/
|
||
public static function getRandomMerchant(array $merchants)
|
||
{
|
||
if (empty($merchants)) {
|
||
return null;
|
||
}
|
||
|
||
// 只有一个商户,直接返回
|
||
if (count($merchants) === 1) {
|
||
return $merchants[0];
|
||
}
|
||
|
||
// 计算总权重
|
||
$totalWeight = 0;
|
||
foreach ($merchants as $merchant) {
|
||
$totalWeight += isset($merchant['weight']) ? (int)$merchant['weight'] : 1;
|
||
}
|
||
|
||
// 生成随机数
|
||
$randomWeight = mt_rand(1, $totalWeight);
|
||
|
||
// 根据权重选择商户
|
||
$currentWeight = 0;
|
||
foreach ($merchants as $merchant) {
|
||
$weight = isset($merchant['weight']) ? (int)$merchant['weight'] : 1;
|
||
$currentWeight += $weight;
|
||
|
||
if ($randomWeight <= $currentWeight) {
|
||
return $merchant;
|
||
}
|
||
}
|
||
|
||
// 默认返回第一个商户
|
||
return $merchants[0];
|
||
}
|
||
|
||
/**
|
||
* 获取微信支付配置
|
||
*
|
||
* @return array 包含随机选择的商户信息和appid
|
||
*/
|
||
public static function getWxPayConfig()
|
||
{
|
||
$wechat_setting = getConfig('wechat_setting');
|
||
$weixinpay_setting = getConfig('weixinpay_setting');
|
||
|
||
// 选择一个随机商户
|
||
$merchant = null;
|
||
if (!empty($weixinpay_setting) && !empty($weixinpay_setting['merchants'])) {
|
||
$merchant = self::getRandomMerchant($weixinpay_setting['merchants']);
|
||
} else {
|
||
// 兼容旧配置
|
||
$old_config = getConfig('weixinpay');
|
||
if (!empty($old_config)) {
|
||
$merchant = [
|
||
'name' => '默认商户',
|
||
'mch_id' => $old_config['mch_id'],
|
||
'keys' => $old_config['keys'],
|
||
'order_prefix' => $old_config['order_prefix'] ?? 'MYH',
|
||
'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
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 获取固定的微信支付配置(基于订单前缀)
|
||
*
|
||
* @param string $order_prefix 订单前缀,如果提供则返回对应前缀的商户信息
|
||
* @return array 包含固定商户信息和appid
|
||
*/
|
||
public static function getFixedWxPayConfig($order_prefix = '')
|
||
{
|
||
$wechat_setting = getConfig('wechat_setting');
|
||
$weixinpay_setting = getConfig('weixinpay_setting');
|
||
|
||
// 尝试查找与订单前缀匹配的商户
|
||
$merchant = null;
|
||
if (!empty($order_prefix) && !empty($weixinpay_setting) && !empty($weixinpay_setting['merchants'])) {
|
||
foreach ($weixinpay_setting['merchants'] as $m) {
|
||
if (!empty($m['order_prefix']) && $m['order_prefix'] === $order_prefix) {
|
||
$merchant = $m;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 如果没有找到匹配的商户,则使用随机选择
|
||
if (empty($merchant)) {
|
||
return self::getWxPayConfig();
|
||
}
|
||
|
||
// 获取微信AppID
|
||
$appid = '';
|
||
if (!empty($wechat_setting) && !empty($wechat_setting['appid'])) {
|
||
$appid = $wechat_setting['appid'];
|
||
} else {
|
||
$old_config = getConfig('weixinpay');
|
||
if (!empty($old_config) && !empty($old_config['appid'])) {
|
||
$appid = $old_config['appid'];
|
||
}
|
||
}
|
||
|
||
return [
|
||
'merchant' => $merchant,
|
||
'appid' => $appid
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 从订单号中提取商户前缀
|
||
*
|
||
* @param string $order_no 订单号(格式如 MH_ABC20240529...)
|
||
* @return string|null 商户前缀,如果无法提取则返回null
|
||
*/
|
||
public static function extractOrderPrefix($order_no)
|
||
{
|
||
if (strpos($order_no, 'MH_') === 0) {
|
||
// 尝试提取MH_后的3个字符作为商户前缀
|
||
$prefix = substr($order_no, 3, 3);
|
||
if (!empty($prefix) && strlen($prefix) === 3) {
|
||
return $prefix;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
}
|