config->load('paypal'); $this->load->helper('payment_helper'); $this->load->model('Report_model'); } /*! * 差集: 对账单 > HT收款记录 * @author LYT * @date 2018-03-26 * @param POST 时间区间 */ public function unstore_statement($begin_m=NULL, $end_m=NULL) { } /*! * 转换字符集编码 * @param $data * @param $targetCharset * @return string */ protected function characet($data, $targetCharset) { if (!empty($data)) { $fileType = "UTF-8"; if (strcasecmp($fileType, $targetCharset) != 0) { $data = mb_convert_encoding($data, $targetCharset, $fileType); // $data = iconv($fileType, $targetCharset.'//IGNORE', $data); } } return $data; } /*! * 检验非空 * @author LYT * @date 2017-08-17 * @param [type] $value [description] * @return boolean true-空;false-非空 */ protected function checkEmpty($value) { if (!isset($value)) return true; if ($value === null) return true; if (trim($value) === "") return true; return false; } protected function create_guid() { return strtolower(md5(uniqid(mt_rand(), true))); } /*! * 解析ipaylinks对账单excel, 存入数据库 * @author LYT * @date 2018-03-26 * @param POST f={target_folder}&fjson={files_list_JSON} */ public function ipaylinks_excel() { $this->load->model('IPayLinks_model'); set_time_limit(0); // 解析excel $target_folder = $this->input->get_post("f"); $files = json_decode($this->input->get_post("fjson")); log_message('error','ipaylinks excel POST: ' . $target_folder . $this->input->get_post("fjson")); $statement_folder = FCPATH.'download_statement\statement_files\\' . $target_folder; if ( ! is_dir($statement_folder)) { return; } $files = $files ? $files : array_values(array_diff(scandir($statement_folder), array('.', '..'))); if (empty($files)) { echo "none excel."; return; } $settle_cnt = 0; $update_cnt = 0; $file_path_output = ""; bcscale(4); foreach ($files as $k => $fe) { $file_path = $statement_folder . '\\' . $fe; if ( ! file_exists($file_path)) { continue; } $settlement_record = $this->read_ipaylinks_excel($file_path); if (empty($settlement_record)) { continue; } foreach ($settlement_record as $settle) { $orderid_info = analysis_orderid(trim($settle['orderid'])); $orderid_info = json_decode($orderid_info); if ( ! empty($orderid_info)) { if (strcasecmp($orderid_info->ordertype, "B") === 0) { $old_info = $this->IPayLinks_model->get_money_b(trim($settle['pn_invoice'])); } else if (strcasecmp($orderid_info->ordertype, "T") === 0){ $old_info = $this->IPayLinks_model->get_money_t(trim($settle['pn_invoice'])); } } $warrant["PR_AccreditNo"] = trim($settle['pn_invoice']); $warrant["PR_payType"] = 15018; $warrant["PR_entryCurrency"] = trim($settle['entry_currency']); $warrant["PR_entryAmount"] = bcadd(floatval($settle['entry_security']), floatval($settle['entry_basic'])); $warrant["PR_COLI_SN"] = !empty($old_info) ? $old_info[0]->GAI_COLI_SN : NULL; $warrant["PR_currency"] = trim($settle['currency']); $warrant["PR_amount"] = trim($settle['order_amount']); $warrant["PR_orderType"] = $orderid_info ? $orderid_info->ordertype : NULL; $warrant["PR_fee"] = bcadd(trim($settle['service_fee']), trim($settle['transaction_fee'])); $warrant["PR_paymentTime"] = trim($settle['complete_date']); $warrant["PR_dealType"] = trim($settle['data_type']); $warrant["PR_orderId"] = trim($settle['orderid']); $warrant["PR_rate"] = trim($settle['settlement_rate']); $warrant["PR_status"] = ""; $warrant["PR_buyerName"] = ""; $warrant["PR_buyerEmail"] = ""; $this->Report_model->new_report($warrant); $warrant = NULL; $update_cnt++; } $settlement_record = null; // reset $settle_cnt++; $file_path_output .= "\r\n" . $file_path; } $result = "read ipaylinks reports result: " . date('Y-m-d H:i:s') . " ------ \r\n"; $result .= "Found $settle_cnt Excels; Store $update_cnt records;" . $file_path_output; log_message('error', $result); echo $result; return; } public function read_ipaylinks_excel($filePath) { $tarr1=array(); $this->load->library('PHPExcel'); $PHPExcel = new PHPExcel(); /**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/ $PHPReader = new PHPExcel_Reader_Excel2007(); if(!$PHPReader->canRead($filePath)){ $PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($filePath)){ echo 'no Excel'; return ; } } $PHPExcel = $PHPReader->load($filePath); /**读取excel文件中的第一个工作表*/ $currentSheet = $PHPExcel->getSheet(0); /**取得最大的列号*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); $col_titles = $this->ipaylinks_col_title(); /**从第二行开始输出,因为excel表中第一行为列名*/ for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ $row_tmp = array(); /**从第A列开始输出*/ for($currentColumn= 'A';$currentColumn<= "N"; $currentColumn++){ /**ord()将字符转为十进制数*/ $val = trim($currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue()); $col_mean = $col_titles[$currentColumn]; if($col_mean == 'data_type' && in_array($val, $this->noStore_dealType()) ) // strcasecmp('清算', $val) !== 0 { break; } if ($col_mean == 'orderid' && strcasecmp('-', $val) === 0) { break; } $row_tmp[$col_mean] = $val; } if (isset($row_tmp['settlement_amount'])) { $tarr1[] = $row_tmp; } } return $tarr1; } public function ipaylinks_col_title() { return array( "A" => "complete_date", "B" => "pn_invoice", "C" => "orderid", "D" => "data_type", "E" => "currency", "F" => "order_amount", "G" => "settlement_rate", "H" => "settlement_amount", "I" => "service_fee", "J" => "transaction_fee", "K" => "mark_memo", "L" => "entry_currency", "M" => "entry_security", "N" => "entry_basic" ); } public function noStore_dealType() { return array( "归还保证金" ,"记账" ,"冻结" ,"解冻" ,"提现" ,"提现手续费" ); } public function paypal_get_token() { $header = array( "Accept: application/json", "Accept-Language: en_US", ); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $this->config->item('token_url')); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSLVERSION, 6); $clientId = $this->config->item('client_id'); $secret = $this->config->item('secret'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret); curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); $reponse = curl_exec($ch); if (curl_errno($ch)) { log_message('error', "paypal token curl error code: ".curl_error($ch)); } else { $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (200 !== $httpStatusCode) { log_message('error', "paypal token Request html Status Code: ".$httpStatusCode); } } curl_close($ch); return json_decode($reponse); } public function paypal_get_payment($payment_id) { // $url = $this->config->item("payment_url") . "/" . $payment_id; // $url = $this->config->item("sale_url") . "/" . $payment_id; $url = $this->config->item("activities_url") . "?page_size=50&sort_by=create_time&sort_order=desc&start_time=2018-03-25T11:00:00Z&end_time=2018-03-28T11:00:00Z"; $c = $this->call_paypal($url); var_dump($c); return $c; } public function call_paypal($url, $body = false) { $token_info = $this->paypal_get_token();var_dump($token_info); // todo if no token $access_token = $token_info->access_token; $token_type = $token_info->token_type; $header = array( "Content-Type:application/json", "Authorization: $token_type $access_token" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($ch, CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 Available since PHP 5.5.19 and 5.6.3 $param_str = ""; if (is_array($body) && 0 < count($body)) { $param_str = $this->toJSON($body); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $param_str); } $reponse = curl_exec($ch); if (curl_errno($ch)) { log_message('error', "paypal curl REST API error code: " . curl_error($ch) . "; post: ".$param_str); } else { $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (200 !== $httpStatusCode) { log_message('error', "paypal REST API Request html Status Code: ".$httpStatusCode); } } curl_close($ch); return json_decode($reponse); } public function toJSON($arr, $options = 0) { if (version_compare(phpversion(), '5.4.0', '>=') === true) { return json_encode($arr, $options | 64); } return str_replace('\\/', '/', json_encode($arr, $options)); // ~ php 5.3 } }