2
0

PayMentService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. $services = DB::table('agent.dbo.admin_configs')->where([
  99. 'new_pay_type' => $method,
  100. 'type' => 'pay',
  101. 'status' => 1,
  102. ])->whereRaw('pay_types&'.$pay_method.'='.$pay_method)
  103. ->where('min_amount', '<=', $payAmt)
  104. ->where('max_amount', '>=', $payAmt)
  105. ->get();
  106. if (!$services->count()) {
  107. Log::error('支付渠道配置异常', [
  108. 'method' => $method,
  109. ]);
  110. return false;
  111. }
  112. $matrix = [];
  113. foreach ($services as $v) {
  114. for ($i = 0; $i < $v->sort; $i++) {
  115. // if(Redis::exists("PayErro_".$v->config_key)) continue;
  116. if(!empty($v->remarks)&&$v->remarks!=null){
  117. $remarks=json_decode($v->remarks,true);
  118. // 按支付类型校验金额是否在允许范围内:不满足则跳过该渠道
  119. if(isset($remarks['limit'])){
  120. $typeKey='type_'.$pay_method;
  121. $limitConfig=$remarks['limit'][$typeKey]??null;
  122. if($limitConfig!==null && !self::isAmountInLimit($payAmt,$limitConfig)){
  123. continue;
  124. }
  125. }
  126. }
  127. $matrix[] = $v->config_key;
  128. }
  129. }
  130. if(count($matrix)==0) {
  131. foreach ($services as $v) {
  132. for ($i = 0; $i < $v->sort; $i++) {
  133. $matrix[] = $v->config_key;
  134. }
  135. }
  136. }
  137. $res = $matrix[mt_rand(0, count($matrix)-1)];
  138. return $res;
  139. }
  140. /**
  141. * 判断支付金额是否在 limit 配置的取值范围内
  142. * @param float $payAmt 支付金额
  143. * @param mixed $limitConfig 配置:数组为允许金额列表;['min'=>x,'max'=>y] 为区间;'all' 或 ['全部'] 为不限制
  144. * @return bool
  145. */
  146. public static function isAmountInLimit($payAmt, $limitConfig)
  147. {
  148. if ($limitConfig === 'all' || $limitConfig === '全部' || $limitConfig === ['全部']) {
  149. return true;
  150. }
  151. if (isset($limitConfig['min']) && isset($limitConfig['max'])) {
  152. $min = (float)$limitConfig['min'];
  153. $max = (float)$limitConfig['max'];
  154. $amt = (float)$payAmt;
  155. return $amt >= $min && $amt <= $max;
  156. }
  157. if (is_array($limitConfig)) {
  158. $payAmtRounded = round((float)$payAmt, 2);
  159. foreach ($limitConfig as $allowed) {
  160. if (round((float)$allowed, 2) === $payAmtRounded) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. return true;
  167. }
  168. /**
  169. * 根据支付方式随机获取充值渠道
  170. * @param $method
  171. * @param $channel
  172. * @return bool|mixed
  173. */
  174. public static function getServiceByPayCountry($method, $channel,$country,$except_channelName="")
  175. {
  176. $except_configKeys=[$except_channelName];
  177. $switch = Redis::get("recharge_config_switch_{$channel}");
  178. if ($channel && $switch) {
  179. $services = DB::connection('write')
  180. ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  181. ->join(
  182. 'agent.dbo.admin_configs',
  183. 'admin_configs.id',
  184. '=',
  185. 'ChannelOpenRecharge.ConfigID'
  186. )
  187. ->where('admin_configs.new_pay_type', $method)
  188. ->whereNotIn('admin_configs.config_key',$except_configKeys)
  189. ->where('admin_configs.country', $country)
  190. ->where('admin_configs.type', 'pay')
  191. ->where('admin_configs.status', 1)
  192. ->where('ChannelOpenRecharge.Status', 1)
  193. ->where('ChannelOpenRecharge.Channel', $channel)
  194. ->where('ChannelOpenRecharge.Sort', '>', 0)
  195. ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  196. ->get();
  197. }
  198. if (!isset($services) || !$services->count()) {
  199. $services = DB::table('agent.dbo.admin_configs')->where([
  200. 'new_pay_type' => $method,
  201. 'type' => 'pay',
  202. 'status' => 1,
  203. 'country' => $country,
  204. ])->get();
  205. if (!$services->count()) {
  206. Log::error('支付渠道配置异常', [
  207. 'method' => $method,
  208. ]);
  209. return false;
  210. }
  211. }
  212. $matrix = [];
  213. foreach ($services as $v) {
  214. for ($i = 0; $i < $v->sort; $i++) {
  215. if(Redis::exists("PayErro_".$v->config_key)) continue;
  216. $matrix[] = $v->config_key;
  217. }
  218. }
  219. if(count($matrix)==0) {
  220. foreach ($services as $v) {
  221. for ($i = 0; $i < $v->sort; $i++) {
  222. $matrix[] = $v->config_key;
  223. }
  224. }
  225. }
  226. $res = $matrix[mt_rand(0, count($matrix)-1)];
  227. return $res;
  228. }
  229. }