load->library('session'); $this->load->_model('Model_fullorder','fullorder'); $this->load->_model('Model_fullordertt','fullordertt'); $this->load->_model('Model_fullorder_smt','fullordersmt'); $this->load->_model('Model_express','express'); $this->load->_model('Model_shop','shop'); $this->load->_model('Model_transact','transact'); $this->load->library('parser'); // $this->load->library('dispute_collect'); if(isset($_SESSION['api'])) { $user = $this->user->get_api($_SESSION['api']); $fgshop = []; $user = explode('|',trim($user['shop'],'|')); foreach ($user as $value) { if($value<=6){ $fgshop[]=$value; } } } $sid=$this->input->get('sid'); if(!$sid||!in_array($sid,$fgshop)){ $sid=$fgshop[0]; } $this->sid=$sid; } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'detail')//添加 { $this->_detail($arg_array); }elseif($arg == 'syns'){ $this->syns($arg_array); } else { $this->_index($arg_array); } } public function _index(){ $dt = 0; if(isset($_SESSION['api'])) { $user = $this->user->get_api($_SESSION['api']); $usp = $user; $fgshop = "";$sid = ""; $usersp = explode('|',trim($user['shop'],'|')); foreach ($usersp as $value) { $fgshop .= " shop = ".$value." or"; $sid .= " id = ".$value." or"; } if($user['vip'] == 1) { $vip = 1; } else { $vip = 0; } } else { $vip = 0; } $post = $this->input->post(NULL, TRUE); $where = "1=1 and (".rtrim($fgshop,'or').")"; if(isset($post['page'])) { $shop = $this->input->post('shop',true); $page = $this->input->post('page',true); $express = $this->input->post('express',true); $perpage = $this->input->post('perpage',true); $orderinfo = $this->input->post('orderinfo',true); $number = $this->input->post('number',true); $waybill = $this->input->post('waybill',true); //数据排序 $order_str = "id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } if($express) { $where .= " and carrier = '$express'"; } if($shop) { $where .= " and shop = '$shop'"; } if($orderinfo) { $where .= " and order_id = '$orderinfo'"; } if($number) { $where .= " and number = '$number'"; } if($waybill) { $where .= " and tracking_number = '$waybill'"; } //取得信息列表id,shop,type,source,user,orderinfo,number,name,state,buytime,clientremarks,review,print,library,libraryconfirm,express,waybill,orderremarks,black $info_list = $this->transact->find_all($where,'id,shop,order_id,number,transaction_id,carrier,tracking_number,is_transact',$order_str,$start,$perpage); foreach($info_list as $k=>$v){ $shop=$this->shop->read($v['shop']); $info_list[$k]['shop']=$shop['shopname']; $express=$this->express->read($v['carrier']); $info_list[$k]['carrier']=$express['iscode']; $info_list[$k]['order_id']='

'.$v['order_id'].'

'; $info_list[$k]['is_transact']=$v['is_transact']?"是":"否"; // $info_list[$k]['black']=[]; } $total = $this->transact->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list)); echo json_encode($rows);exit; } // $this->data= $wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or')); $this->data['express'] = $this->express->find_all(); $this->data['wlshop'] = $wlshop; $this->_Template('transact',$this->data); } public function _detail($arg_array){ $id=$arg_array[0]; $transact=$this->transact->read($id); if(!$transact){ echo json_encode(array('msg'=>'数据不存在','success'=>false));exit; } // if(isset($transact['info'])&&!empty($transact['info'])){ // $info=json_decode($transact['info'],true); // // $this->data['info']=$transact['info']; // }else{ $shop=$this->shop->read($transact['shop']); $info=$this->transaction($shop['shopname'],$transact['transaction_id']); if(isset($info['debug_id'])&&!empty($info['debug_id'])){ echo json_encode(array('msg'=>$info['message'],'success'=>false));exit; } // $this->transact->save(['info'=>json_encode($info,true)]); // } if(!empty($info['transaction_details'])){ $detail=$info['transaction_details'][0]; }else{ $detail=[]; } $this->data['info']=$detail; $this->_Template('transact_info',$this->data); } public function syns(){ $shops=[ 1=>'supernovahair', 2=>'asteriahair', 3=>'alipearlhair', 4=>'westkiss', 5=>'yolissahair', 6=>'wigginshair', ]; $shops=[ 1=>'supernovahair']; //step1 获取最新订单数据 $start_time=strtotime(date('Y-m-d',time()-24*3600)); // $end_time=strtotime(date('Y-m-d',time())); $end_time=time(); $dlz = $this->fullorder->paypal($start_time,$end_time); $smt = $this->fullordersmt->paypal($start_time,$end_time); $data = array_merge($dlz,$smt); $keys=['number','order_id','transaction_id','tracking_number','carrier','shop','created_at','updated_at']; $list=[]; try { $this->db->trans_begin(); for($i=1;$i<=count($data);$i++){ $item=[]; $v=$data[$i-1]; $item['number']=$v['number']; $item['order_id']=$v['orderinfo']; $item['transaction_id']=$v['paypal']; $item['tracking_number']=$v['waybill']; $item['carrier']=$v['express']; $item['shop']=$v['shop']; $item['created_at']=time(); $item['updated_at']=time(); $list[]=$item; if($i % 10==0||$i==count($data)){ $res=$this->transact->insert_batch($keys,$list); if(!$res){ throw new Exception("数据库异常"); } $list=[]; } } if ($this->db->trans_status() === FALSE){ throw new Exception("数据库异常"); } $this->db->trans_commit(); } catch (\Throwable $e) { $this->db->trans_rollback(); } //step2 更新track $where="1=1 and is_transact=0"; $where.=" and created_at > ".strtotime(date('Y-m-d',time()-24*3600*7)); $transact_data=$this->transact->find_all($where); $express_list=$this->express->find_all(); $express_list=array_combine(array_column($express_list,'id'),$express_list); $list=[]; foreach($shops as $k=>$shop){ $shopdata=array_filter($transact_data,function($item)use($k){ return $item['shop']==$k; }); $shopdata=array_values($shopdata); try { $this->db->trans_begin(); for($i=1;$i<=count($shopdata);$i++){ $item=[]; $v=$shopdata[$i-1]; if(!empty($express_list[$v['carrier']])){ $expressName=$express_list[$v['carrier']]['iscode']; $allow_express=['FEDEX','TOLL','DHL','USPS','UPS','TNT','ARAMEX','ROYAL_MAIL']; if(!in_array($expressName,$allow_express)){ $item['carrier_name_other']=$expressName; $expressName="OTHER"; } }else{ throw new \Exception("快递不存在"); } if(empty($v['tracking_number'])){ continue; } if(substr_count($v['transaction_id'],'-')>=2){ continue; } $item['transaction_id']=$v['transaction_id']; $item['tracking_number']=$v['tracking_number']; $item['carrier']=$expressName; $item['status']="SHIPPED"; $list[]=$item; //20 为固定数值 api单次更新限制 if($i % 20==0||$i==count($shopdata)){ $info=$this->setTrack($shop,$list); if(isset($info['errors'])&&!empty($info['errors'])){ throw new Exception("请求paypal异常"); // echo json_encode(array('msg'=>$info['message'],'success'=>false));exit; } $res=$this->db ->set('is_transact',1) ->where_in('transaction_id',array_column($list,'transaction_id')) ->update("transact"); //更新数据库; if(!$res){ throw new Exception("数据库异常"); } $list=[]; } } if ($this->db->trans_status() === FALSE){ throw new Exception("数据库异常"); } $this->db->trans_commit(); } catch (\Throwable $e) { $this->db->trans_rollback(); p($e->getMessage()); } } //step3 // foreach($shops as $k=>$shop){ // $list=$this->transaction($shop,'',7); // p($list); // } return true; } private function setTrack($dp,$data){ $url = $this->baseurl.'/v1/shipping/trackers-batch'; $header[] = 'Content-Type: application/json'; $header[] = 'Authorization: Bearer '.$this->getToken($dp); $param=['trackers'=>$data]; $info = $this->_curl($param,$url,3000,'POST',"json",$header); $info = json_decode($info,true); return $info; } private function transaction($dp,$transaction_id="",$day=30){ $url = $this->baseurl.'/v1/reporting/transactions?'; $header[] = 'Content-Type: application/json'; $header[] = 'Authorization: Bearer '.$this->getToken($dp); $start_date=date("Y-m-d\TH:i:sO",time()-24*3600*$day); $end_date=date("Y-m-d\TH:i:sO",time()); $param=[ 'start_date'=>$start_date, 'end_date'=>$end_date, 'fields'=>"all", ]; if($transaction_id){ $param['transaction_id']=$transaction_id; } $data = http_build_query($param); $url=$url.$data; $info = $this->_curl([],$url,3000,'GET',"http_build_query",$header); $info = json_decode($info,true); return $info; } private function getToken($dp) { $access_arr=[]; $url = $this->baseurl.'/v1/oauth2/token'; $header[] = 'Content-Type: application/json'; $header[] = 'Accept-Language: en_US'; $header[] = 'Accept: */*'; $data = array('grant_type' => 'client_credentials'); $method = 'POST'; $hf_path = $_SERVER["DOCUMENT_ROOT"] . '/data/hf_access_token.txt'; $hf_access_token = ''; if(file_exists($hf_path)) { $file_read = fopen($hf_path, 'r'); $access_str = fgets($file_read); fclose($file_read); $access_arr = unserialize($access_str); if(isset($access_arr[$dp])&&is_array($access_arr[$dp])){ $dp_access_arr=$access_arr[$dp]; if (time() < intval($dp_access_arr['expires_at'])) { $hf_access_token = isset($dp_access_arr['access_token']) ? $dp_access_arr['access_token'] : ''; } } } if(!$hf_access_token) { // $config=$this->getDpConfig(); // $config=[ // 'account'=>'paypal-facilitator@supernovahair.com', // 'client_id'=>'Ae5ZECTwT-JY-GrHW2-XW234yJ4tYT-7RAt3s1mY8GtW1rX470Kr8weXkCH3GMaO-V7mnmnCTsxFvsiy', // 'secret'=>'EOiZjIwRaiK3pvbJgMURKcGy6ULt5YCGLkqN7WngwG-r34brYrfVwS6ECI8cig7l8lOObvF-ukeZxB-3', // ]; $config=[ 'account'=>'paypal@supernovahair.com', 'client_id'=>'AYn5qSfZKh9ApN_IQvwuRq-3t07XflNIbczH6pfz-GccZ1bPTiYk3RzJVTJPQkMmoQM-PWmZX1hkUB18', 'secret'=>'EBd_oR-c1s4SVhznh1832_ua0WumbAqFamSM5uKJSHbpRv8qkI1Z9D2Tqcizaz5DRrgmQ7Mkl-Cq-TgL', ]; $file = fopen($hf_path, 'w'); $hf_account = $config['account']; $client_id = $config['client_id']; $secret_id = $config['secret']; $userpwd = $client_id . ':' . $secret_id; $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd); $result = json_decode($info,true); // $access_new = (time() + $result['expires_in']) . ':' . $result['access_token']; $access_arr[$dp]=[ 'expires_at'=>time() + $result['expires_in'], 'access_token'=>$result['access_token'], ]; fwrite($file, serialize($access_arr)); fclose($file); $hf_access_token = $result['access_token']; } return $hf_access_token; } private function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='') { if ($date_type == 'http_build_query') { $data = http_build_query($data); } else if ($date_type == 'json') { $data = json_encode($data); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); switch ($httptype) { case "GET": curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); break; case "POST": curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; } $isSecure = strpos($url, "https://"); if ($isSecure === 0) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } if(!empty($header)) { curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION curl_setopt($ch,CURLOPT_HTTPHEADER,$header); } if(!empty($userpwd)) { curl_setopt($ch,CURLOPT_USERPWD,$userpwd); } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $result = curl_exec($ch); curl_close($ch); return $result; } }