2
0

PayMentService.php 9.2 KB

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