123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- namespace App\Http\Controllers;
- use function EasyWeChat\Kernel\Support\str_random;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use EasyWeChat\Factory;
- class PayController extends Controller
- {
- public function __construct()
- {
- }
- public function lists(Request $request)
- {
- $user = Auth::user();
- $input=$request->all();
- $month=$input['month'];
- $page_size=$input['page_size'];
- $page_index=$input['page_index'];
- $num=($page_index-1)*$page_size;
- $where['uid']=$user->id;
- if($month){
- $where['month']=date("Y-m",strtotime($month));
- }
- $where['status']=1;
- $count=DB::table('charge')
- ->where($where)
- ->count();
- if($count>0){
- $data=DB::table('charge')
- ->where($where)
- ->orderBy('month','desc')
- ->skip($num)->take($page_size)->get();
- if($data){
- return response()->json([
- 'error_code'=>200,
- 'msg'=>'获取信息成功',
- 'data'=>$data,
- 'count'=>$count
- ]);
- }else{
- return response()->json([
- 'error_code'=>0,
- 'msg'=>'获取信息失败',
- ]);
- }
- }else{
- return response()->json([
- 'error_code'=>200,
- 'msg'=>'没有数据',
- 'data'=>[],
- 'count'=>$count
- ]);
- }
- }
- public function payOrders(Request $request){
- $input=$request->all();
- $rules=[
- 'month'=>'required',
- 'fee'=>'required',
- ];
- $messages=[
- 'month.required'=>'月份不能为空.',
- 'fee.required'=>'金额不能为空.',
- ];
- $validator = Validator::make($input, $rules, $messages);
- if($validator->fails()){
- return response()->json([
- 'error_code'=>'41113',
- 'msg'=>$validator->errors()->first()
- ]);
- }
- $teamId=Auth::user()->team_id;
- if(empty($teamId) || $teamId==0){
- return response()->json([
- 'error_code'=>'0',
- 'msg'=>'无团队,不能缴费'
- ]);
- }
- $uid=Auth::user()->id;
- $count=DB::table('upload_commit_log')->where('uid',$uid)->count();
- if($count==0){
- return response()->json([
- 'error_code'=>'0',
- 'msg'=>'未上传承诺书'
- ]);
- }
- $is_v=$input['is_v'];
- $income=floatval($input['fee']);
- $data['income']=ceil($income*100)/100;
- $month=date("Y-m",strtotime($input['month']));
- if(intval(str_replace('-','',$month))>201909){
- if($income<1900) {
- return response()->json([
- 'error_code' => '0',
- 'msg' => '最低工资标准不能低于1900元'
- ]);
- }
- }elseif(intval(str_replace('-','',$month))>201901){
- if($income<1000) {
- return response()->json([
- 'error_code' => '0',
- 'msg' => '最低工资标准不能低于1000元'
- ]);
- }
- }
- if($income>100000000){
- return response()->json([
- 'error_code'=>'0',
- 'msg'=>'交易金额过大,请联系管理员确认缴费'
- ]);
- }
- // $data['income']=ceil($income*100)/100;
- // $month=date("Y-m",strtotime($input['month']));
- if(intval(str_replace('-','',$month))>201909){
- if($income>10000){
- $income_fee=ceil($income*2)/100;
- }elseif($income>5000){
- $income_fee=ceil($income*1.5)/100;
- }elseif($income>3000){
- $income_fee=ceil($income)/100;
- }elseif($income>1900){
- $income_fee=ceil($income*0.5)/100;
- }else{
- $income_fee=9.5;
- }
- }elseif(intval(str_replace('-','',$month))<201901){
- return response()->json([
- 'error_code'=>'0',
- 'msg'=>'暂不支持缴纳2019年之前党费'
- ]);
- }else{
- $income_fee=5;
- }
- if($uid=='795' || $uid=='896'){
- $income_fee=0.01;
- }
- $f_fee=floatval($income_fee);
- if($is_v==1){
- if(($f_fee*6/1000) < 0.01){
- $v_fee=0.01;
- }else{
- $v_fee=ceil($f_fee*6/10)/100;
- };
- $data['is_v']=1;
- $data['v_fee']=$v_fee;
- $fee=$f_fee+$v_fee;
- }else{
- $data['is_v']=3;
- $data['v_fee']=0;
- $fee=$f_fee;
- }
- $where['month']=$data['month']=$month;
- $where['uid']=$data['uid']=$uid;
- // $where['status']=1;
- $data['fee']=$fee;
- $data['f_fee']=$f_fee;
- $data['type']=1;
- $data['added_on']=date("Y-m-d H:i:s");
- $options = [
- 'app_id' => env('WECHAT_APPID'),
- 'mch_id' => env('MCH_ID'),
- 'key' => env('MCH_KEY'),
- 'notify_url' => 'http://' . $request->server('SERVER_NAME').'/api/pay/notify'
- ];
- // $code=$input['code'];
- // $config = config('wechat.defaults.mini_program.default');
- // $mini = Factory::miniProgram($config);
- // $info = $mini->auth->session($code);
- // if (isset($info['errcode'])) {
- // return response()->json([
- // 'error_code'=>'411111',
- // 'msg'=>'code 不正确'
- // ]);
- // }
- $openid=$input['openid'];
- $data['openid']= $openid;
- $payinfo=DB::table('charge')
- ->where($where)
- ->where(function ($query) {
- $query->where('status', '=', '1')
- ->orWhere('status', '=', '2');
- })
- ->first();
- if($payinfo){
- return response()->json([
- 'error_code'=>'411112',
- 'msg'=>'该月已缴费'
- ]);
- }else{
- $dd['month']=$month;
- $dd['uid']=$uid;
- $dd['fee']=$fee;
- $dd['status']=0;
- $payin=DB::table('charge')
- ->where($dd)->first();
- if($payin){
- if($payin->out_trade_no){
- $out_trade_no=$payin->out_trade_no;
- }
- }else{
- $out_trade_no=date("YmdHis").mt_rand(1000,9999).substr(md5(mt_rand(1000,9999)),10,12);
- $data['out_trade_no']=$out_trade_no;
- $data['team_id']=Auth::user()->team_id;
- $data['added_on']=date("Y-m-d H:i:s");
- $row=DB::table('charge')->insert($data);
- if(!$row){
- return response()->json([
- 'error_code'=>'411112',
- 'msg'=>'订单上传失败'
- ]);
- }
- }
- }
- $payment = Factory::payment($options);
- $jssdk = $payment->jssdk;
- $attributes = [
- 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI
- 'body' => '缴费', // 订单说明
- 'out_trade_no' => $out_trade_no, // 自定义订单号
- 'total_fee' => $fee * 100, // 单位:分
- 'openid' => $openid // 当前用户的openId
- ];
- $result = $payment->order->unify($attributes);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $prepayId = $result['prepay_id'];
- $config = $jssdk->sdkConfig($prepayId);
- return response()->json([
- 'error_code'=>200,
- 'msg'=>'成功',
- 'data'=>$config
- ]);
- }
- if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) {
- Log::info($uid.'/'.$out_trade_no.'/'.$fee.'/'.$result['return_msg']);
- return response()->json([
- 'error_code'=>401,
- 'msg'=> $result['return_msg'],
- ]);
- }
- Log::info($uid.'/'.$out_trade_no.'/'.$fee.'/'.$result['err_code_des']);
- return response()->json([
- 'error_code'=>401,
- 'msg'=> $result['err_code_des'],
- ]);
- }
- public function notify(Request $request)
- {
- $options = [
- 'app_id' => env('WECHAT_APPID'),
- 'mch_id' => env('MCH_ID'),
- 'key' => env('MCH_KEY'),
- 'notify_url' => 'http://' . $request->server('SERVER_NAME').'/api/pay/notify'
- ];
- $payment = Factory::payment($options);
- $response = $payment->handlePaidNotify(function ($message, $fail)
- {
- // 根据返回的订单号查询订单数据
- $order=DB::table('charge')->where('out_trade_no',$message['out_trade_no'])->first();
- if (!$order) {
- Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code'].'/'.'Order not exist');
- $fail('Order not exist.');
- }
- if ($order->status == '1') {
- return true;
- }
- // 支付成功后的业务逻辑
- if($message['result_code'] === 'SUCCESS')
- {
- $teamId=DB::table('users')->where('id',$order->uid)->value('team_id');
- Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
- DB::table('charge')
- ->where('out_trade_no',$message['out_trade_no'])
- ->update([
- 'status'=>'1',
- 'team_id'=>$teamId,
- 'pays_at'=>$message['time_end'],
- 'wxpay_trade_no'=>$message['transaction_id'],
- ]);
- }
- return true;
- });
- return $response;
- }
- }
|