PayMentService.php 10 KB

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