PayMentService.php 8.0 KB

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