PayMentService.php 10 KB

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