2
0

PayMentService.php 9.0 KB

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