123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?php
- namespace App\Http\Controllers;
- use App\Events\AuthRefundTip;
- use App\Models\Address;
- use App\Models\DwbsUser;
- use App\Models\Goods;
- use App\Models\Order;
- use App\Models\OrderCancel;
- use App\Models\OrderRefund;
- use App\Models\OrderDetail;
- use App\Models\Store;
- use App\Models\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- class RefundController extends Controller
- {
- //获取退款订单原始信息
- public function getOriginOrderInfo(Request $request)
- {
- $order_no=$request->input('order_no');
- $data=Order::with(['address','cancel'=>function($query){$query->where('using',1);},'store:id,name,img,phone']);
- $order=$data->where('order_no',$order_no)->first()->toArray();
- if(empty($order)){
- return $this->error('450001','订单不存在');
- }
- $goods_ids=OrderDetail::where('order_no',$order['order_no'])->groupBy('goods_id')->pluck('goods_id');
- $goods=[];
- foreach($goods_ids as $key=>$val){
- $goods[$val]=Goods::select('id','name','img','main_attr')->where('id',$val)->first()->toArray();
- $order_details=OrderDetail::where('order_no',$order['order_no'])->where('goods_id',$val)->get()->toArray();
- if(count($order_details)>0){
- $sku=[];
- foreach($order_details as $k=>$v){
- $sku[$v['sku_id']]=$v;
- }
- $goods[$val]['sku']=$sku;
- $goods[$val]['price']=$order_details[0]['price'];
- }
- }
- $order['goods']=$goods;
- $refund_nos=OrderRefund::where('order_no',$order_no)->whereIn('status',[0,1,3])->pluck('refund_no');
- $refund_goods_ids=OrderDetail::whereIn('order_no',$refund_nos)->groupBy('goods_id')->pluck('goods_id');
- $refunds=[];
- foreach($refund_goods_ids as $key=>$val){
- $refunds[$val]=Goods::select('id','name','img','main_attr')->where('id',$val)->first()->toArray();
- $refunds_details=OrderDetail::select('goods_id','sku_id','type','size',DB::raw('sum(num) as num'))
- ->whereIn('order_no',$refund_nos)->where('goods_id',$val)->groupBy('sku_id')->get()->toArray();
- if(count($refunds_details)>0){
- $sku=[];
- foreach($refunds_details as $k=>$v){
- $sku[$v['sku_id']]=$v;
- }
- $refunds[$val]['sku']=$sku;
- }
- }
- $order['refunds']=$refunds;
- $products=[];
- foreach($goods as $key=>$val){
- $products[$key]= $goods[$key];
- $sku=[];
- foreach($val['sku'] as $k=>$v){
- if(empty($refunds[$key]['sku'][$k])){
- unset($products[$key]['sku']);
- $sku[$k]= $goods[$key]['sku'][$k];
- }else{
- if($goods[$key]['sku'][$k]['num'] > $refunds[$key]['sku'][$k]['num']){
- unset($products[$key]['sku']);
- $sku[$k] = $goods[$key]['sku'][$k];
- $sku[$k]['num']=$goods[$key]['sku'][$k]['num'] - $refunds[$key]['sku'][$k]['num'];
- }else{
- unset($products[$key]['sku']);
- }
- }
- }
- $products[$key]['sku']= $sku;
- if(empty($products[$key]['sku'])){
- unset($products[$key]);
- }
- }
- $order['products']=$products;
- return $this->success($order);
- }
- //提交退货订单
- public function submitRefundOrder(Request $request)
- {
- $sku = $request->input('sku');
- $reason = $request->input('reason');
- $reason_all = $request->input('reason_all');
- $img = $request->input('img');
- $origin_order_no=$request->input('origin_order_no');
- $origin_order=Order::where('order_no',$origin_order_no)->first();
- if($origin_order->is_refund==1){
- return $this->error('450001','该订单存在正在审核中的退货订单');
- }
- if($origin_order->is_refund==3){
- return $this->error('450001','该订单已全部退货');
- }
- $refund_no=Order::order_no('th');
- $account = $total = 0;
- $list = $sku_ids = [];
- foreach($sku as $key=>$val){
- if($val[1] > 0){
- $order_detail=OrderDetail::with(['goods'])->where('id',$val[0])->first();
- if($order_detail->num < $val[1]){
- $num = $order_detail->num;
- }else{
- $num = $val[1];
- }
- $list[$key]['num']=$num;
- $list[$key]['price']=$order_detail->price;
- $list[$key]['account']=$order_detail->price * $num;
- $list[$key]['order_no']=$refund_no;
- $list[$key]['goods_id']=$order_detail->goods_id;
- $list[$key]['sku_id']=$order_detail->sku_id;
- $list[$key]['size']=$order_detail->size;
- $list[$key]['type']=$order_detail->type;
- $list[$key]['style']=2;
- $list[$key]['created_at']=date("Y-m-d H:i:s");
- $list[$key]['updated_at']=date("Y-m-d H:i:s");
- $total += $num;
- $account += $order_detail->price * $num;
- }
- }
- $data=[
- 'refund_no'=>$refund_no,
- 'order_no'=>$origin_order_no,
- 'store_id'=>$origin_order->store_id,
- 'user_id'=>$origin_order->user_id,
- 'reason'=>$reason,
- 'reason_all'=>$reason_all,
- 'total'=>$total,
- 'account'=>$account,
- 'img'=>json_encode($img),
- 'status'=>0,
- 'op_name'=>Auth::user()->nickname,
- 'op_type'=>1,
- 'remark'=>null,
- ];
- try{
- DB::transaction(function ()use($list,$data,$origin_order_no){
- OrderDetail::insert($list);
- OrderRefund::create($data);
- Order::where('order_no',$origin_order_no)->update(['is_refund'=>1]);
- },5);
- //申请退货提醒
- $store=Store::where('id',$origin_order->store_id)->first();
- $user=DwbsUser::withTrashed()->where('id',$store->user_id)->first();
- if($user->openid){
- $data['openid']=$user->openid;
- $data['data']=[
- 'first' => '您的客户提交了新的退货信息',
- 'keyword1' => $origin_order->order_no,
- 'keyword2' => '客户退货',
- ];
- event(new AuthRefundTip($data));
- }
- return $this->success($refund_no);
- }catch(\Exception $e){
- return $this->error('450001',$e->getMessage());
- }
- }
- //取消退货审核
- public function cancelRefundOrder(Request $request){
- $refund_id=$request->input('refund_id');
- $refund=OrderRefund::where('id',$refund_id)->first();
- if(empty($refund)){
- return $this->error('450001','退货单不存在');
- }
- if($refund->status==1){
- return $this->error('450001','已成功退货,不能取消');
- }
- if($refund->status==2){
- return $this->error('450001','退货申请已被驳回,不能取消');
- }
- try{
- DB::transaction(function()use($refund){
- OrderRefund::where('id',$refund->id)->delete();
- $auth_count=OrderRefund::where('order_no',$refund->order_no)->where('status',0)->count();
- if($auth_count>=1){
- Order::where('order_no',$refund->order_no)->update([
- 'is_refund'=>1
- ]);
- }else{
- $count=OrderRefund::where('order_no',$refund->order_no)->where('status',1)->count();
- if($count>=1){
- Order::where('order_no',$refund->order_no)->update([
- 'is_refund'=>2
- ]);
- }else{
- Order::where('order_no',$refund->order_no)->update([
- 'is_refund'=>0
- ]);
- }
- }
- },5);
- return $this->success([]);
- }catch(\Exception $e){
- return $this->error('450001',$e->getMessage());
- }
- }
- //获取用户端所有退货订单
- public function getAllRefundOrder(Request $request)
- {
- $user_id=Auth::user()->id;
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $refund_no=$request->input('refund_no');
- $data=OrderRefund::with('store:id,name,phone')->where('user_id',$user_id);
- if($refund_no){
- $data->where('refund_no','like','%'.$refund_no.'%');
- }
- $total= $data->count();
- $refunds = $data->orderBy('id','desc')->skip($num)->take($page_size)->get();
- foreach($refunds as $key=>$val){
- $goods=[];
- $goods_ids=OrderDetail::where('order_no',$val->refund_no)->groupBy('goods_id')->pluck('goods_id');
- foreach($goods_ids as $k=>$v){
- $goods[$k]=Goods::where('id',$v)->first();
- $goods[$k]->sku=OrderDetail::where('order_no',$val->refund_no)->where('goods_id',$v)->get();
- }
- $refunds[$key]->goods=$goods;
- }
- return $this->success_list($refunds,'成功',$total);
- }
- //获取用户端退货订单详情
- public function getRefundOrderDetail(Request $request)
- {
- $refund_id = $request->input('refund_id');
- //获取订单信息(id)
- $order = OrderRefund::with(['store:id,name,img,phone'])->where('id', $refund_id)->first();
- $goods_ids = OrderDetail::where('order_no', $order->refund_no)->groupBy('goods_id')->pluck('goods_id');
- $goods = [];
- foreach ($goods_ids as $key => $val) {
- $goods[$key] = Goods::where('id', $val)->first();
- $goods[$key]->sku = OrderDetail::where('order_no', $order->refund_no)->where('goods_id', $val)->get();
- }
- $order->goods = $goods;
- return $this->success($order);
- }
- //删除订单
- public function deleteOriginOrder(Request $request){
- $order_no=$request->input('order_no');
- $res=Order::where('order_no',$order_no)->delete();
- if($res){
- return $this->success([]);
- }else{
- return $this->error();
- }
- }
- //上传图片
- public function uploadRefundOrderImg(Request $request){
- $path_url = 'public/refund';
- $path = $request->file('img')->store($path_url);
- if($path){
- $url = env('APP_URL').Storage::url($path);
- return $this->success($url);
- }
- return $this->error();
- }
- function createNoncestr($length =32)
- {
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
- function unicode() {
- $str = uniqid(mt_rand(),1);
- $str=sha1($str);
- return md5($str);
- }
- function arraytoxml($data){
- $str='<xml>';
- foreach($data as $k=>$v) {
- $str.='<'.$k.'>'.$v.'</'.$k.'>';
- }
- $str.='</xml>';
- return $str;
- }
- function xmltoarray($xml) {
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- $val = json_decode(json_encode($xmlstring),true);
- return $val;
- }
- function curl($param="",$url) {
- $postUrl = $url;
- $curlPost = $param;
- $ch = curl_init(); //初始化curl
- curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页
- curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 增加 HTTP Header(头)里的字段
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch,CURLOPT_SSLCERT,config('wechat.payment.default.cert_path')); //这个是证书的位置绝对路径cert_path
- curl_setopt($ch,CURLOPT_SSLKEY,config('wechat.payment.default.key_path')); //这个也是证书的位置绝对路径
- $data = curl_exec($ch); //运行curl
- curl_close($ch);
- return $data;
- }
- /*
- $amount 发送的金额(分)目前发送金额不能少于1元
- $re_openid, 发送人的 openid
- $desc // 企业付款描述信息 (必填)
- $check_name 收款用户姓名 (选填)
- */
- public function sendMoney($desc='测试',$check_name=''){
- $data=array(
- 'mch_appid'=>config('wechat.payment.default.app_id'),//商户账号appid
- 'mchid'=> "1604585534",//商户号
- 'nonce_str'=>$this->createNoncestr(),//随机字符串
- // 'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
- 'partner_trade_no'=> 'CP202101290000000000001',//商户订单号
- 'openid'=> 'oJn4Fv3GoK8rMqftML5kSU8FoDUc',//用户openid
- 'check_name'=>'NO_CHECK',//校验用户姓名选项,
- // 're_user_name'=> $check_name,//收款用户姓名
- 'amount'=> 1,//金额
- 'desc'=> $desc,//企业付款描述信息
- 'spbill_create_ip'=> $_SERVER['REMOTE_ADDR'],//Ip地址
- );
- $secrect_key=config('wechat.payment.default.key');///这个就是个API密码。MD5 32位。
- $data=array_filter($data);
- ksort($data);
- $str='';
- foreach($data as $k=>$v) {
- $str.=$k.'='.$v.'&';
- }
- $str.='key='.$secrect_key;
- $data['sign']=md5($str);
- $xml=$this->arraytoxml($data);
- $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
- $res=$this->curl($xml,$url);
- $return=$this->xmltoarray($res);
- return $return;
- print_r($return);
- //返回来的结果
- // [return_code] => SUCCESS [return_msg] => Array ( ) [mch_appid] => wxd44b890e61f72c63 [mchid] => 1493475512 [nonce_str] => 616615516 [result_code] => SUCCESS [partner_trade_no] => 20186505080216815
- // [payment_no] => 1000018361251805057502564679 [payment_time] => 2018-05-15 15:29:50
- $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
- echo $res= $responseObj->return_code; //SUCCESS 如果返回来SUCCESS,则发生成功,处理自己的逻辑
- return $res;
- }
- }
|