PayMentService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, $channel,$except_channelName="")
  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. if (!isset($services) || !$services->count()) {
  105. $services = DB::table('agent.dbo.admin_configs')->where([
  106. 'new_pay_type' => $method,
  107. 'type' => 'pay',
  108. 'status' => 1,
  109. ])->get();
  110. if (!$services->count()) {
  111. Log::error('支付渠道配置异常', [
  112. 'method' => $method,
  113. ]);
  114. return false;
  115. }
  116. }
  117. $matrix = [];
  118. foreach ($services as $v) {
  119. for ($i = 0; $i < $v->sort; $i++) {
  120. if(Redis::exists("PayErro_".$v->config_key)) continue;
  121. $matrix[] = $v->config_key;
  122. }
  123. }
  124. if(count($matrix)==0) {
  125. foreach ($services as $v) {
  126. for ($i = 0; $i < $v->sort; $i++) {
  127. $matrix[] = $v->config_key;
  128. }
  129. }
  130. }
  131. $res = $matrix[mt_rand(0, count($matrix)-1)];
  132. return $res;
  133. }
  134. /**
  135. * 根据支付方式随机获取充值渠道
  136. * @param $method
  137. * @param $channel
  138. * @return bool|mixed
  139. */
  140. public static function getServiceByPayCountry($method, $channel,$country,$except_channelName="")
  141. {
  142. $except_configKeys=[$except_channelName];
  143. $switch = Redis::get("recharge_config_switch_{$channel}");
  144. if ($channel && $switch) {
  145. $services = DB::connection('write')
  146. ->table('QPPlatformDB.dbo.ChannelOpenRecharge')
  147. ->join(
  148. 'agent.dbo.admin_configs',
  149. 'admin_configs.id',
  150. '=',
  151. 'ChannelOpenRecharge.ConfigID'
  152. )
  153. ->where('admin_configs.new_pay_type', $method)
  154. ->whereNotIn('admin_configs.config_key',$except_configKeys)
  155. ->where('admin_configs.country', $country)
  156. ->where('admin_configs.type', 'pay')
  157. ->where('admin_configs.status', 1)
  158. ->where('ChannelOpenRecharge.Status', 1)
  159. ->where('ChannelOpenRecharge.Channel', $channel)
  160. ->where('ChannelOpenRecharge.Sort', '>', 0)
  161. ->selectRaw('admin_configs.config_key, ChannelOpenRecharge.Sort as sort')
  162. ->get();
  163. }
  164. if (!isset($services) || !$services->count()) {
  165. $services = DB::table('agent.dbo.admin_configs')->where([
  166. 'new_pay_type' => $method,
  167. 'type' => 'pay',
  168. 'status' => 1,
  169. 'country' => $country,
  170. ])->get();
  171. if (!$services->count()) {
  172. Log::error('支付渠道配置异常', [
  173. 'method' => $method,
  174. ]);
  175. return false;
  176. }
  177. }
  178. $matrix = [];
  179. foreach ($services as $v) {
  180. for ($i = 0; $i < $v->sort; $i++) {
  181. if(Redis::exists("PayErro_".$v->config_key)) continue;
  182. $matrix[] = $v->config_key;
  183. }
  184. }
  185. if(count($matrix)==0) {
  186. foreach ($services as $v) {
  187. for ($i = 0; $i < $v->sort; $i++) {
  188. $matrix[] = $v->config_key;
  189. }
  190. }
  191. }
  192. $res = $matrix[mt_rand(0, count($matrix)-1)];
  193. return $res;
  194. }
  195. }