PayMentService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\StarPayController;
  10. use App\Http\Controllers\Api\WiwiPayController;
  11. use App\Http\Controllers\Api\WDPayController;
  12. use App\Http\Controllers\Api\CoinPayController;
  13. use App\Http\Controllers\Api\AiPayController;
  14. use App\Http\Controllers\Api\PagYeepPayController;
  15. use App\Http\Controllers\Api\AiNewPayController;
  16. use App\Http\Controllers\Api\SupefinaSpeiController;
  17. use App\Http\Controllers\Api\NewSupefinaSpeiController;
  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 'SupefinaSpei':
  39. return new SupefinaSpeiController();
  40. case 'NewSupefinaSpei':
  41. return new NewSupefinaSpeiController();
  42. case 'StarPay':
  43. return new StarPayController();
  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. for ($i = 0; $i < $v->sort; $i++) {
  124. // if(Redis::exists("PayErro_".$v->config_key)) continue;
  125. if(!empty($v->remarks)&&$v->remarks!=null){
  126. $remarks=json_decode($v->remarks,true);
  127. // 按支付类型校验金额是否在允许范围内:不满足则跳过该渠道
  128. if(isset($remarks['limit'])){
  129. $typeKey='type_'.$pay_method;
  130. $limitConfig=$remarks['limit'][$typeKey]??null;
  131. if($limitConfig!==null && !self::isAmountInLimit($payAmt,$limitConfig)){
  132. continue;
  133. }
  134. }
  135. }
  136. $matrix[] = $v->config_key;
  137. }
  138. }
  139. if(count($matrix)==0) {
  140. foreach ($services as $v) {
  141. for ($i = 0; $i < $v->sort; $i++) {
  142. $matrix[] = $v->config_key;
  143. }
  144. }
  145. }
  146. $res = $matrix[mt_rand(0, count($matrix)-1)];
  147. return $res;
  148. }
  149. /**
  150. * 判断支付金额是否在 limit 配置的取值范围内
  151. * @param float $payAmt 支付金额
  152. * @param mixed $limitConfig 配置:数组为允许金额列表;['min'=>x,'max'=>y] 为区间;'all' 或 ['全部'] 为不限制
  153. * @return bool
  154. */
  155. public static function isAmountInLimit($payAmt, $limitConfig)
  156. {
  157. if ($limitConfig === 'all' || $limitConfig === '全部' || $limitConfig === ['全部']) {
  158. return true;
  159. }
  160. if (isset($limitConfig['min']) && isset($limitConfig['max'])) {
  161. $min = (float)$limitConfig['min'];
  162. $max = (float)$limitConfig['max'];
  163. $amt = (float)$payAmt;
  164. return $amt >= $min && $amt <= $max;
  165. }
  166. if (is_array($limitConfig)) {
  167. $payAmtRounded = round((float)$payAmt, 2);
  168. foreach ($limitConfig as $allowed) {
  169. if (round((float)$allowed, 2) === $payAmtRounded) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * 根据支付方式随机获取充值渠道
  179. * @param $method
  180. * @param $channel
  181. * @return bool|mixed
  182. */
  183. public static function getServiceByPayCountry($method, $channel,$country,$except_channelName="")
  184. {
  185. $except_configKeys=[$except_channelName];
  186. $switch = Redis::get("recharge_config_switch_{$channel}");
  187. if ($channel && $switch) {
  188. $services = DB::connection('write')
  189. ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  190. ->join(
  191. 'agent.dbo.admin_configs',
  192. 'admin_configs.id',
  193. '=',
  194. 'ChannelOpenRecharge.ConfigID'
  195. )
  196. ->where('admin_configs.new_pay_type', $method)
  197. ->whereNotIn('admin_configs.config_key',$except_configKeys)
  198. ->where('admin_configs.country', $country)
  199. ->where('admin_configs.type', 'pay')
  200. ->where('admin_configs.status', 1)
  201. ->where('ChannelOpenRecharge.Status', 1)
  202. ->where('ChannelOpenRecharge.Channel', $channel)
  203. ->where('ChannelOpenRecharge.Sort', '>', 0)
  204. ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  205. ->get();
  206. }
  207. if (!isset($services) || !$services->count()) {
  208. $services = DB::table('agent.dbo.admin_configs')->where([
  209. 'new_pay_type' => $method,
  210. 'type' => 'pay',
  211. 'status' => 1,
  212. 'country' => $country,
  213. ])->get();
  214. if (!$services->count()) {
  215. Log::error('支付渠道配置异常', [
  216. 'method' => $method,
  217. ]);
  218. return false;
  219. }
  220. }
  221. $matrix = [];
  222. foreach ($services as $v) {
  223. for ($i = 0; $i < $v->sort; $i++) {
  224. if(Redis::exists("PayErro_".$v->config_key)) continue;
  225. $matrix[] = $v->config_key;
  226. }
  227. }
  228. if(count($matrix)==0) {
  229. foreach ($services as $v) {
  230. for ($i = 0; $i < $v->sort; $i++) {
  231. $matrix[] = $v->config_key;
  232. }
  233. }
  234. }
  235. $res = $matrix[mt_rand(0, count($matrix)-1)];
  236. return $res;
  237. }
  238. }