PayMentService.php 7.8 KB

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