PayMentService.php 10 KB

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