PayMentService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Controllers\Api\AegPayController;
  4. use App\Http\Controllers\Api\AppleStorePayController;
  5. use App\Http\Controllers\Api\CryptoController;
  6. use App\Http\Controllers\Api\GooglePayController;
  7. use App\Http\Controllers\Api\GoopagoController;
  8. use App\Http\Controllers\Api\SitoBankController;
  9. use App\Http\Controllers\Api\WiwiPayController;
  10. use App\Http\Controllers\Api\WDPayController;
  11. use App\Http\Controllers\Api\CoinPayController;
  12. use App\Http\Controllers\Api\AiPayController;
  13. use App\Http\Controllers\Api\PagYeepPayController;
  14. use App\Http\Controllers\Api\SafePayController;
  15. use App\Http\Controllers\Api\AiNewPayController;
  16. use App\Http\Controllers\Api\BotImPayController;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. class PayMentService
  21. {
  22. public static function pay_order($transport = null)
  23. {
  24. switch ($transport) {
  25. case 'WiwiPay':
  26. return new WiwiPayController();
  27. case 'WDPay':
  28. return new WDPayController();
  29. case 'CoinPay':
  30. return new CoinPayController();
  31. case 'AiPay':
  32. return new AiPayController();
  33. case 'PagYeepPay':
  34. return new PagYeepPayController();
  35. case 'AiNewPay':
  36. return new AiNewPayController();
  37. case 'SafePay':
  38. return new SafePayController();
  39. case 'BotImPay':
  40. return new BotImPayController();
  41. case 'apple':
  42. return new AppleStorePayController();
  43. case 'google':
  44. return new GooglePayController();
  45. default:
  46. throw new \Exception('unknown pay channel:'.$transport);
  47. }
  48. }
  49. /**
  50. * 根据支付方式随机获取充值渠道
  51. * @param $method
  52. * @param $channel
  53. * @return bool|mixed
  54. */
  55. /*public static function getServiceByPayMethod3($method, $channel)
  56. {
  57. // $switch = Redis::get("recharge_config_switch_{$channel}");
  58. // if ($channel && $switch) {
  59. // $services = DB::connection('write')
  60. // ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  61. // ->join(
  62. // 'agent.dbo.admin_configs',
  63. // 'admin_configs.id',
  64. // '=',
  65. // 'ChannelOpenRecharge.ConfigID'
  66. // )
  67. // ->where('admin_configs.new_pay_type', $method)
  68. // ->where('admin_configs.type', 'pay')
  69. // ->where('admin_configs.status', 1)
  70. // ->where('ChannelOpenRecharge.Status', 1)
  71. // ->where('ChannelOpenRecharge.Channel', $channel)
  72. // ->where('ChannelOpenRecharge.Sort', '>', 0)
  73. // ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  74. // ->get();
  75. // }
  76. $services = DB::table('agent.dbo.admin_configs')->where([
  77. 'new_pay_type' => $method,
  78. 'type' => 'pay',
  79. 'status' => 1,
  80. ])->get();
  81. if (!$services->count()) {
  82. Log::error('支付渠道配置异常', [
  83. 'method' => $method,
  84. ]);
  85. return false;
  86. }
  87. $matrix = [];
  88. foreach ($services as $v) {
  89. for ($i = 0; $i < $v->sort; $i++) {
  90. $matrix[] = $v->config_key;
  91. }
  92. }
  93. $res = $matrix[mt_rand(0, count($matrix)-1)];
  94. return $res;
  95. }*/
  96. /**
  97. * 根据支付方式随机获取充值渠道
  98. * @param $method
  99. * @param $channel
  100. * @return bool|mixed
  101. */
  102. public static function getServiceByPayMethod($method, $payAmt,$pay_method)
  103. {
  104. $services = DB::table('agent.dbo.admin_configs')->where([
  105. 'new_pay_type' => $method,
  106. 'type' => 'pay',
  107. 'status' => 1,
  108. ])->whereRaw('pay_types&'.$pay_method.'='.$pay_method)
  109. ->where('min_amount', '<=', $payAmt)
  110. ->where('max_amount', '>=', $payAmt)
  111. ->get();
  112. if (!$services->count()) {
  113. Log::error('支付渠道配置异常', [
  114. 'method' => $method,
  115. ]);
  116. return false;
  117. }
  118. $matrix = [];
  119. foreach ($services as $v) {
  120. $remarks = [];
  121. if (!empty($v->remarks)) {
  122. $decoded = json_decode($v->remarks, true);
  123. if (is_array($decoded)) {
  124. $remarks = $decoded;
  125. }
  126. }
  127. // 按支付类型校验金额是否在允许范围内:不满足则跳过该渠道
  128. if (isset($remarks['limit'])) {
  129. $typeKey = 'type_' . $pay_method;
  130. $limitConfig = $remarks['limit'][$typeKey] ?? null;
  131. if ($limitConfig !== null && !self::isAmountInLimit($payAmt, $limitConfig)) {
  132. continue;
  133. }
  134. }
  135. // 新逻辑:优先按支付方式独立权重分配渠道;未配置时回退旧 sort 逻辑
  136. $weight = self::resolveMethodWeight($remarks, $pay_method, (int)$v->sort);
  137. if ($weight <= 0) {
  138. continue;
  139. }
  140. for ($i = 0; $i < $weight; $i++) {
  141. $matrix[] = $v->config_key;
  142. }
  143. }
  144. if(count($matrix)==0) {
  145. foreach ($services as $v) {
  146. for ($i = 0; $i < $v->sort; $i++) {
  147. $matrix[] = $v->config_key;
  148. }
  149. }
  150. }
  151. $res = $matrix[mt_rand(0, count($matrix)-1)];
  152. return $res;
  153. }
  154. /**
  155. * 解析指定支付方式的渠道权重(从 remarks 读取),未配置时回退默认权重
  156. * remarks 支持:
  157. * {"weight":{"type_1":30,"type_2":20,"type_4":10,"type_8":40}}
  158. * {"weights":{"type_1":30,"type_2":20}}
  159. * @param array $remarks
  160. * @param int $payMethod
  161. * @param int $defaultWeight
  162. * @return int
  163. */
  164. public static function resolveMethodWeight(array $remarks, $payMethod, $defaultWeight = 0)
  165. {
  166. $typeKey = 'type_' . (int)$payMethod;
  167. $weightConfig = $remarks['weight'] ?? ($remarks['weights'] ?? null);
  168. if (is_array($weightConfig) && array_key_exists($typeKey, $weightConfig)) {
  169. return max(0, (int)$weightConfig[$typeKey]);
  170. }
  171. return max(0, (int)$defaultWeight);
  172. }
  173. /**
  174. * 判断支付金额是否在 limit 配置的取值范围内
  175. * @param float $payAmt 支付金额
  176. * @param mixed $limitConfig 配置:数组为允许金额列表;['min'=>x,'max'=>y] 为区间;'all' 或 ['全部'] 为不限制
  177. * @return bool
  178. */
  179. public static function isAmountInLimit($payAmt, $limitConfig)
  180. {
  181. if ($limitConfig === 'all' || $limitConfig === '全部' || $limitConfig === ['全部']) {
  182. return true;
  183. }
  184. if (isset($limitConfig['min']) && isset($limitConfig['max'])) {
  185. $min = (float)$limitConfig['min'];
  186. $max = (float)$limitConfig['max'];
  187. $amt = (float)$payAmt;
  188. return $amt >= $min && $amt <= $max;
  189. }
  190. if (is_array($limitConfig)) {
  191. $payAmtRounded = round((float)$payAmt, 2);
  192. foreach ($limitConfig as $allowed) {
  193. if (round((float)$allowed, 2) === $payAmtRounded) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. return true;
  200. }
  201. /**
  202. * 根据支付方式随机获取充值渠道
  203. * @param $method
  204. * @param $channel
  205. * @return bool|mixed
  206. */
  207. public static function getServiceByPayCountry($method, $channel,$country,$except_channelName="")
  208. {
  209. $except_configKeys=[$except_channelName];
  210. $switch = Redis::get("recharge_config_switch_{$channel}");
  211. if ($channel && $switch) {
  212. $services = DB::connection('write')
  213. ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  214. ->join(
  215. 'agent.dbo.admin_configs',
  216. 'admin_configs.id',
  217. '=',
  218. 'ChannelOpenRecharge.ConfigID'
  219. )
  220. ->where('admin_configs.new_pay_type', $method)
  221. ->whereNotIn('admin_configs.config_key',$except_configKeys)
  222. ->where('admin_configs.country', $country)
  223. ->where('admin_configs.type', 'pay')
  224. ->where('admin_configs.status', 1)
  225. ->where('ChannelOpenRecharge.Status', 1)
  226. ->where('ChannelOpenRecharge.Channel', $channel)
  227. ->where('ChannelOpenRecharge.Sort', '>', 0)
  228. ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  229. ->get();
  230. }
  231. if (!isset($services) || !$services->count()) {
  232. $services = DB::table('agent.dbo.admin_configs')->where([
  233. 'new_pay_type' => $method,
  234. 'type' => 'pay',
  235. 'status' => 1,
  236. 'country' => $country,
  237. ])->get();
  238. if (!$services->count()) {
  239. Log::error('支付渠道配置异常', [
  240. 'method' => $method,
  241. ]);
  242. return false;
  243. }
  244. }
  245. $matrix = [];
  246. foreach ($services as $v) {
  247. for ($i = 0; $i < $v->sort; $i++) {
  248. if(Redis::exists("PayErro_".$v->config_key)) continue;
  249. $matrix[] = $v->config_key;
  250. }
  251. }
  252. if(count($matrix)==0) {
  253. foreach ($services as $v) {
  254. for ($i = 0; $i < $v->sort; $i++) {
  255. $matrix[] = $v->config_key;
  256. }
  257. }
  258. }
  259. $res = $matrix[mt_rand(0, count($matrix)-1)];
  260. return $res;
  261. }
  262. }