PayMentService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\AiPayController;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class PayMentService
  16. {
  17. public static function pay_order($transport = null)
  18. {
  19. switch ($transport) {
  20. case 'WiwiPay':
  21. return new WiwiPayController();
  22. case 'WDPay':
  23. return new WDPayController();
  24. case 'AiPay':
  25. return new AiPayController();
  26. case 'apple':
  27. return new AppleStorePayController();
  28. case 'google':
  29. return new GooglePayController();
  30. default:
  31. throw new \Exception('unknown pay channel:'.$transport);
  32. }
  33. }
  34. /**
  35. * 根据支付方式随机获取充值渠道
  36. * @param $method
  37. * @param $channel
  38. * @return bool|mixed
  39. */
  40. /*public static function getServiceByPayMethod3($method, $channel)
  41. {
  42. // $switch = Redis::get("recharge_config_switch_{$channel}");
  43. // if ($channel && $switch) {
  44. // $services = DB::connection('write')
  45. // ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  46. // ->join(
  47. // 'agent.dbo.admin_configs',
  48. // 'admin_configs.id',
  49. // '=',
  50. // 'ChannelOpenRecharge.ConfigID'
  51. // )
  52. // ->where('admin_configs.new_pay_type', $method)
  53. // ->where('admin_configs.type', 'pay')
  54. // ->where('admin_configs.status', 1)
  55. // ->where('ChannelOpenRecharge.Status', 1)
  56. // ->where('ChannelOpenRecharge.Channel', $channel)
  57. // ->where('ChannelOpenRecharge.Sort', '>', 0)
  58. // ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  59. // ->get();
  60. // }
  61. $services = DB::table('agent.dbo.admin_configs')->where([
  62. 'new_pay_type' => $method,
  63. 'type' => 'pay',
  64. 'status' => 1,
  65. ])->get();
  66. if (!$services->count()) {
  67. Log::error('支付渠道配置异常', [
  68. 'method' => $method,
  69. ]);
  70. return false;
  71. }
  72. $matrix = [];
  73. foreach ($services as $v) {
  74. for ($i = 0; $i < $v->sort; $i++) {
  75. $matrix[] = $v->config_key;
  76. }
  77. }
  78. $res = $matrix[mt_rand(0, count($matrix)-1)];
  79. return $res;
  80. }*/
  81. /**
  82. * 根据支付方式随机获取充值渠道
  83. * @param $method
  84. * @param $channel
  85. * @return bool|mixed
  86. */
  87. public static function getServiceByPayMethod($method, $payAmt,$pay_method)
  88. {
  89. // $except_configKeys=[$except_channelName];
  90. // $switch = Redis::get("recharge_config_switch_{$channel}");
  91. // if ($channel && $switch) {
  92. // $services = DB::connection('write')
  93. // ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  94. // ->join(
  95. // 'agent.dbo.admin_configs',
  96. // 'admin_configs.id',
  97. // '=',
  98. // 'ChannelOpenRecharge.ConfigID'
  99. // )
  100. // ->where('admin_configs.new_pay_type', $method)
  101. // ->whereNotIn('admin_configs.config_key',$except_configKeys)
  102. // ->where('admin_configs.type', 'pay')
  103. // ->where('admin_configs.status', 1)
  104. // ->where('ChannelOpenRecharge.Status', 1)
  105. // ->where('ChannelOpenRecharge.Channel', $channel)
  106. // ->where('ChannelOpenRecharge.Sort', '>', 0)
  107. // ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  108. // ->get();
  109. // }
  110. $services = DB::table('agent.dbo.admin_configs')->where([
  111. 'new_pay_type' => $method,
  112. 'type' => 'pay',
  113. 'status' => 1,
  114. ])->whereRaw('pay_types&'.$pay_method.'='.$pay_method)
  115. ->where('min_amount', '<=', $payAmt)
  116. ->where('max_amount', '>=', $payAmt)
  117. ->get();
  118. if (!$services->count()) {
  119. Log::error('支付渠道配置异常', [
  120. 'method' => $method,
  121. ]);
  122. return false;
  123. }
  124. $matrix = [];
  125. foreach ($services as $v) {
  126. for ($i = 0; $i < $v->sort; $i++) {
  127. if(Redis::exists("PayErro_".$v->config_key)) continue;
  128. $matrix[] = $v->config_key;
  129. }
  130. }
  131. if(count($matrix)==0) {
  132. foreach ($services as $v) {
  133. for ($i = 0; $i < $v->sort; $i++) {
  134. $matrix[] = $v->config_key;
  135. }
  136. }
  137. }
  138. $res = $matrix[mt_rand(0, count($matrix)-1)];
  139. return $res;
  140. }
  141. /**
  142. * 根据支付方式随机获取充值渠道
  143. * @param $method
  144. * @param $channel
  145. * @return bool|mixed
  146. */
  147. public static function getServiceByPayCountry($method, $channel,$country,$except_channelName="")
  148. {
  149. $except_configKeys=[$except_channelName];
  150. $switch = Redis::get("recharge_config_switch_{$channel}");
  151. if ($channel && $switch) {
  152. $services = DB::connection('write')
  153. ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  154. ->join(
  155. 'agent.dbo.admin_configs',
  156. 'admin_configs.id',
  157. '=',
  158. 'ChannelOpenRecharge.ConfigID'
  159. )
  160. ->where('admin_configs.new_pay_type', $method)
  161. ->whereNotIn('admin_configs.config_key',$except_configKeys)
  162. ->where('admin_configs.country', $country)
  163. ->where('admin_configs.type', 'pay')
  164. ->where('admin_configs.status', 1)
  165. ->where('ChannelOpenRecharge.Status', 1)
  166. ->where('ChannelOpenRecharge.Channel', $channel)
  167. ->where('ChannelOpenRecharge.Sort', '>', 0)
  168. ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  169. ->get();
  170. }
  171. if (!isset($services) || !$services->count()) {
  172. $services = DB::table('agent.dbo.admin_configs')->where([
  173. 'new_pay_type' => $method,
  174. 'type' => 'pay',
  175. 'status' => 1,
  176. 'country' => $country,
  177. ])->get();
  178. if (!$services->count()) {
  179. Log::error('支付渠道配置异常', [
  180. 'method' => $method,
  181. ]);
  182. return false;
  183. }
  184. }
  185. $matrix = [];
  186. foreach ($services as $v) {
  187. for ($i = 0; $i < $v->sort; $i++) {
  188. if(Redis::exists("PayErro_".$v->config_key)) continue;
  189. $matrix[] = $v->config_key;
  190. }
  191. }
  192. if(count($matrix)==0) {
  193. foreach ($services as $v) {
  194. for ($i = 0; $i < $v->sort; $i++) {
  195. $matrix[] = $v->config_key;
  196. }
  197. }
  198. }
  199. $res = $matrix[mt_rand(0, count($matrix)-1)];
  200. return $res;
  201. }
  202. }