PayController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace App\Http\Controllers;
  3. use function EasyWeChat\Kernel\Support\str_random;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Validator;
  9. use EasyWeChat\Factory;
  10. class PayController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. }
  15. public function lists(Request $request)
  16. {
  17. $user = Auth::user();
  18. $input=$request->all();
  19. $month=$input['month'];
  20. $page_size=$input['page_size'];
  21. $page_index=$input['page_index'];
  22. $num=($page_index-1)*$page_size;
  23. $where['uid']=$user->id;
  24. if($month){
  25. $where['month']=date("Y-m",strtotime($month));
  26. }
  27. $where['status']=1;
  28. $count=DB::table('charge')
  29. ->where($where)
  30. ->count();
  31. if($count>0){
  32. $data=DB::table('charge')
  33. ->where($where)
  34. ->orderBy('month','desc')
  35. ->skip($num)->take($page_size)->get();
  36. if($data){
  37. return response()->json([
  38. 'error_code'=>200,
  39. 'msg'=>'获取信息成功',
  40. 'data'=>$data,
  41. 'count'=>$count
  42. ]);
  43. }else{
  44. return response()->json([
  45. 'error_code'=>0,
  46. 'msg'=>'获取信息失败',
  47. ]);
  48. }
  49. }else{
  50. return response()->json([
  51. 'error_code'=>200,
  52. 'msg'=>'没有数据',
  53. 'data'=>[],
  54. 'count'=>$count
  55. ]);
  56. }
  57. }
  58. public function payOrders(Request $request){
  59. $input=$request->all();
  60. $rules=[
  61. 'month'=>'required',
  62. 'fee'=>'required',
  63. ];
  64. $messages=[
  65. 'month.required'=>'月份不能为空.',
  66. 'fee.required'=>'金额不能为空.',
  67. ];
  68. $validator = Validator::make($input, $rules, $messages);
  69. if($validator->fails()){
  70. return response()->json([
  71. 'error_code'=>'41113',
  72. 'msg'=>$validator->errors()->first()
  73. ]);
  74. }
  75. $teamId=Auth::user()->team_id;
  76. if(empty($teamId) || $teamId==0){
  77. return response()->json([
  78. 'error_code'=>'0',
  79. 'msg'=>'无团队,不能缴费'
  80. ]);
  81. }
  82. $uid=Auth::user()->id;
  83. $count=DB::table('upload_commit_log')->where('uid',$uid)->count();
  84. if($count==0){
  85. return response()->json([
  86. 'error_code'=>'0',
  87. 'msg'=>'未上传承诺书'
  88. ]);
  89. }
  90. $is_v=$input['is_v'];
  91. $income=floatval($input['fee']);
  92. $data['income']=ceil($income*100)/100;
  93. $month=date("Y-m",strtotime($input['month']));
  94. if(intval(str_replace('-','',$month))>201909){
  95. if($income<1900) {
  96. return response()->json([
  97. 'error_code' => '0',
  98. 'msg' => '最低工资标准不能低于1900元'
  99. ]);
  100. }
  101. }elseif(intval(str_replace('-','',$month))>201901){
  102. if($income<1000) {
  103. return response()->json([
  104. 'error_code' => '0',
  105. 'msg' => '最低工资标准不能低于1000元'
  106. ]);
  107. }
  108. }
  109. if($income>100000000){
  110. return response()->json([
  111. 'error_code'=>'0',
  112. 'msg'=>'交易金额过大,请联系管理员确认缴费'
  113. ]);
  114. }
  115. // $data['income']=ceil($income*100)/100;
  116. // $month=date("Y-m",strtotime($input['month']));
  117. if(intval(str_replace('-','',$month))>201909){
  118. if($income>10000){
  119. $income_fee=ceil($income*2)/100;
  120. }elseif($income>5000){
  121. $income_fee=ceil($income*1.5)/100;
  122. }elseif($income>3000){
  123. $income_fee=ceil($income)/100;
  124. }elseif($income>1900){
  125. $income_fee=ceil($income*0.5)/100;
  126. }else{
  127. $income_fee=9.5;
  128. }
  129. }elseif(intval(str_replace('-','',$month))<201901){
  130. return response()->json([
  131. 'error_code'=>'0',
  132. 'msg'=>'暂不支持缴纳2019年之前党费'
  133. ]);
  134. }else{
  135. $income_fee=5;
  136. }
  137. if($uid=='795' || $uid=='896'){
  138. $income_fee=0.01;
  139. }
  140. $f_fee=floatval($income_fee);
  141. if($is_v==1){
  142. if(($f_fee*6/1000) < 0.01){
  143. $v_fee=0.01;
  144. }else{
  145. $v_fee=ceil($f_fee*6/10)/100;
  146. };
  147. $data['is_v']=1;
  148. $data['v_fee']=$v_fee;
  149. $fee=$f_fee+$v_fee;
  150. }else{
  151. $data['is_v']=3;
  152. $data['v_fee']=0;
  153. $fee=$f_fee;
  154. }
  155. $where['month']=$data['month']=$month;
  156. $where['uid']=$data['uid']=$uid;
  157. // $where['status']=1;
  158. $data['fee']=$fee;
  159. $data['f_fee']=$f_fee;
  160. $data['type']=1;
  161. $data['added_on']=date("Y-m-d H:i:s");
  162. $options = [
  163. 'app_id' => env('WECHAT_APPID'),
  164. 'mch_id' => env('MCH_ID'),
  165. 'key' => env('MCH_KEY'),
  166. 'notify_url' => 'http://' . $request->server('SERVER_NAME').'/api/pay/notify'
  167. ];
  168. // $code=$input['code'];
  169. // $config = config('wechat.defaults.mini_program.default');
  170. // $mini = Factory::miniProgram($config);
  171. // $info = $mini->auth->session($code);
  172. // if (isset($info['errcode'])) {
  173. // return response()->json([
  174. // 'error_code'=>'411111',
  175. // 'msg'=>'code 不正确'
  176. // ]);
  177. // }
  178. $openid=$input['openid'];
  179. $data['openid']= $openid;
  180. $payinfo=DB::table('charge')
  181. ->where($where)
  182. ->where(function ($query) {
  183. $query->where('status', '=', '1')
  184. ->orWhere('status', '=', '2');
  185. })
  186. ->first();
  187. if($payinfo){
  188. return response()->json([
  189. 'error_code'=>'411112',
  190. 'msg'=>'该月已缴费'
  191. ]);
  192. }else{
  193. $dd['month']=$month;
  194. $dd['uid']=$uid;
  195. $dd['fee']=$fee;
  196. $dd['status']=0;
  197. $payin=DB::table('charge')
  198. ->where($dd)->first();
  199. if($payin){
  200. if($payin->out_trade_no){
  201. $out_trade_no=$payin->out_trade_no;
  202. }
  203. }else{
  204. $out_trade_no=date("YmdHis").mt_rand(1000,9999).substr(md5(mt_rand(1000,9999)),10,12);
  205. $data['out_trade_no']=$out_trade_no;
  206. $data['team_id']=Auth::user()->team_id;
  207. $data['added_on']=date("Y-m-d H:i:s");
  208. $row=DB::table('charge')->insert($data);
  209. if(!$row){
  210. return response()->json([
  211. 'error_code'=>'411112',
  212. 'msg'=>'订单上传失败'
  213. ]);
  214. }
  215. }
  216. }
  217. $payment = Factory::payment($options);
  218. $jssdk = $payment->jssdk;
  219. $attributes = [
  220. 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI
  221. 'body' => '缴费', // 订单说明
  222. 'out_trade_no' => $out_trade_no, // 自定义订单号
  223. 'total_fee' => $fee * 100, // 单位:分
  224. 'openid' => $openid // 当前用户的openId
  225. ];
  226. $result = $payment->order->unify($attributes);
  227. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  228. $prepayId = $result['prepay_id'];
  229. $config = $jssdk->sdkConfig($prepayId);
  230. return response()->json([
  231. 'error_code'=>200,
  232. 'msg'=>'成功',
  233. 'data'=>$config
  234. ]);
  235. }
  236. if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) {
  237. Log::info($uid.'/'.$out_trade_no.'/'.$fee.'/'.$result['return_msg']);
  238. return response()->json([
  239. 'error_code'=>401,
  240. 'msg'=> $result['return_msg'],
  241. ]);
  242. }
  243. Log::info($uid.'/'.$out_trade_no.'/'.$fee.'/'.$result['err_code_des']);
  244. return response()->json([
  245. 'error_code'=>401,
  246. 'msg'=> $result['err_code_des'],
  247. ]);
  248. }
  249. public function notify(Request $request)
  250. {
  251. $options = [
  252. 'app_id' => env('WECHAT_APPID'),
  253. 'mch_id' => env('MCH_ID'),
  254. 'key' => env('MCH_KEY'),
  255. 'notify_url' => 'http://' . $request->server('SERVER_NAME').'/api/pay/notify'
  256. ];
  257. $payment = Factory::payment($options);
  258. $response = $payment->handlePaidNotify(function ($message, $fail)
  259. {
  260. // 根据返回的订单号查询订单数据
  261. $order=DB::table('charge')->where('out_trade_no',$message['out_trade_no'])->first();
  262. if (!$order) {
  263. Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code'].'/'.'Order not exist');
  264. $fail('Order not exist.');
  265. }
  266. if ($order->status == '1') {
  267. return true;
  268. }
  269. // 支付成功后的业务逻辑
  270. if($message['result_code'] === 'SUCCESS')
  271. {
  272. $teamId=DB::table('users')->where('id',$order->uid)->value('team_id');
  273. Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
  274. DB::table('charge')
  275. ->where('out_trade_no',$message['out_trade_no'])
  276. ->update([
  277. 'status'=>'1',
  278. 'team_id'=>$teamId,
  279. 'pays_at'=>$message['time_end'],
  280. 'wxpay_trade_no'=>$message['transaction_id'],
  281. ]);
  282. }
  283. return true;
  284. });
  285. return $response;
  286. }
  287. }