123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Http\Controllers\Admin;
- 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 RefundController extends Controller
- {
- public function __construct()
- {
- }
- public function info(Request $request){
- $input=$request->all();
- $data= DB::table("charge")->where('uid',$input['id'])->orderBy('month','desc')->get();
- return response()->json([
- 'data'=>$data,
- ]);
- }
- public function refund(Request $request)
- {
- $input=$request->all();
- $where['status']=1;
- $where['id']=$input['id'];
- $aaid=Auth::guard('admin')->user()->id;
- $order_pay=DB::table("charge")->where($where)->first();
- if(!$order_pay){
- return response()->json([
- 'error_code'=>410015,
- 'msg'=>"订单信息有误"
- ]);
- }
- if($order_pay->out_refund_no){
- $refund_no=$order_pay->out_refund_no;
- }else{
- $refund_no='T'.date("YmdHis").substr(md5(mt_rand(1000,9999)),10,12);
- }
- if($order_pay->type==2){
- $row=DB::table("charge")->where("id",$input['id'])->update([
- 'status'=>3,
- 'refund_id'=>0,
- 'out_refund_no'=>$refund_no,
- 'refund_fee'=>$order_pay->fee,
- 'refund_at'=>date("Y-m-d H:i:s"),
- 'refund_status'=>1
- ]);
- if($row){
- Log::info('管理员'.$aaid.'操作订单id'.$input['id'].'退款成功');
- return response()->json([
- 'error_code'=>200,
- 'msg'=>"退款成功"
- ]);
- }else{
- return response()->json([
- 'error_code'=>410015,
- 'msg'=>"退款失败"
- ]);
- }
- }
- $time=strtotime("-1 years");
- $paytime=strtotime($order_pay->pays_at);
- if($time>=$paytime){
- return response()->json([
- 'error_code'=>410015,
- 'msg'=>"订单超过一年,不能退款"
- ]);
- }
- $app = Factory::payment(config('wechat.defaults.payment'));
- $total_money = $order_pay->fee * 100;
- $refund_money = $order_pay->fee * 100;
- $out_trade_no=$order_pay->out_trade_no;
- $result = $app->refund->byOutTradeNumber(
- $out_trade_no,//商户订单号
- $refund_no,//商户退款单号
- $total_money,//订单金额
- $refund_money,//退款金额
- [
- 'refund_desc' => '退款',
- 'notify_url' => 'http://'. $request->server('SERVER_NAME').'/api/refund/refundsuccess',
- 'out_refund_no' => $refund_no
- ]
- );
- if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- DB::table('charge')->where($where)->update([
- 'status'=>2,
- ]);
- Log::info('管理员'.$aaid.'操作订单号'.$out_trade_no.'退款申请请求成功');
- return response()->json([
- 'error_code'=>200,
- 'msg'=>'退款申请请求成功'
- ]);
- }else{
- return response()->json([
- 'error_code'=>410015,
- 'msg'=>'退款申请请求失败',
- // 'data'=>$result
- ]);
- }
- }
- // public function req_info_decrypt($str){
- // $key = md5(config('wechat.defaults.payment.key'));
- // $str = base64_decode($str);
- // $str=openssl_decrypt($str, 'aes-256-ecb',$key,1,substr( 0, 16));
- // $len = strlen($str);
- // $pad = ord($str[$len - 1]);
- // $xml = substr($str, 0, strlen($str) - $pad);
- // $ss=substr($str,0,35);
- // DB::table('charge')->where('id','230')->update([
- // 'out_refund_no'=>$ss
- // ]);
- // libxml_disable_entity_loader(true);
- // return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- // }
- public function refundSuccess()
- {
- $app = Factory::payment(config('wechat.defaults.payment'));
- $response = $app->handleRefundedNotify(function ($message,$reqinfo, $fail) {
- if($message['appid'] != config('wechat.defaults.payment.app_id')){
- //不是商户本身的请求,直接略过
- return true;
- }
- Log::info($message['req_info']);
- $order= DB::table("charge")->where('out_trade_no',$reqinfo['out_trade_no'])->first();
- if ($order->status == '3') {
- return true;
- }
- // $data = $this->req_info_decrypt($message['req_info']);
- if($reqinfo['refund_status'] === 'SUCCESS') {
- // Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
- $row=DB::table('charge')
- ->where('out_trade_no',$reqinfo['out_trade_no'])
- ->update([
- 'status'=>3,
- 'refund_id'=>$reqinfo['refund_id'],
- 'out_refund_no'=>$reqinfo['out_refund_no'],
- 'refund_fee'=>$reqinfo['settlement_refund_fee']/100,
- 'refund_at'=>$reqinfo['success_time'],
- 'refund_status'=>1
- ]);
- if($row){
- Log::info('微信退款成功 out_refund_no:'.$reqinfo['out_refund_no']);
- return true;
- }
- }
- });
- return $response;
- }
- public function refundListInfo(Request $request){
- $where=[];
- $adminInfo=Auth::guard('admin')->user();
- if($adminInfo){
- $atis=$adminInfo->is_team;
- if($atis==2){
- $where['charge.team_id']=$adminInfo->team_id;
- }
- }
- $search_name = $request->search_name;
- $page_size = $request->page_size;
- $page_index = $request->page_index;
- $num = ($page_index - 1) * $page_size;
- // $where['charge.status']=3;
- if(!empty($request->month)){
- $where['charge.month'] = date("Y-m",strtotime($request->month));
- }
- if(!empty($request->team_id)){
- $where['charge.team_id'] = $request->team_id;
- }
- $count = DB::table('charge')
- ->join('users', 'users.id', '=', 'charge.uid')
- ->leftJoin('team','team.id','=','charge.team_id')
- ->where(function ($query) use ($search_name) {
- $query->where('users.name', 'like', '%' . $search_name . '%')
- ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
- ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
- })
- ->where(function ($query) use ($search_name) {
- $query->where('charge.status', '=', '2')
- ->orWhere('charge.status', '=', '3');
- })
- ->where($where)
- ->count();
- if ($count > 0) {
- $data = DB::table('charge')
- ->join('users', 'users.id', '=', 'charge.uid')
- ->leftJoin('team','team.id','=','charge.team_id')
- ->where(function ($query) use ($search_name) {
- $query->where('users.name', 'like', '%' . $search_name . '%')
- ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
- ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
- })
- ->where(function ($query) use ($search_name) {
- $query->where('charge.status', '=', '2')
- ->orWhere('charge.status', '=', '3');
- })
- ->where($where)
- ->select('charge.id','users.name','users.telphone','users.cre_num','team.name as tname','team.id as tid','charge.fee', 'charge.status', 'charge.month', 'charge.pays_at','charge.type','charge.refund_at','charge.refund_status')
- ->orderBy('charge.refund_at','desc')
- ->skip($num)->take($page_size)->get();
- if ($data) {
- $data=json_decode($data,true);
- foreach ($data as $key => $val) {
- $data[$key]['month'] = substr($val['month'], 0, 7);
- }
- 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' => '没有缴费信息',
- 'list' => [],
- 'count' => $count
- ]);
- }
- }
- }
|