PayMentService.php 10 KB

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