config->load('paypal'); $this->load->helper('payment_helper'); $this->load->model('Report_model'); } /*! * 差集: * @author LYT * @date 2018-03-26 * @param POST 时间区间 */ public function unstore_statement($begin_m=NULL, $end_m=NULL) { /*! 对账单 > HT收款记录 */ $ret = $this->Report_model->ipaylinks_abnormal_in_HT(); $no_dealId = $this->Report_model->HT_no_dealId(); if (empty($ret) && empty($no_dealId)) { echo "Not Found records."; return; } $this->load->library('PHPExcel'); $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); //set width $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('k')->setWidth(30); $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20); // 对齐 $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); /** HT中没有找到相应交易号的记录, 对账单中存在 */ $objPHPExcel->getActiveSheet() ->SetCellValue('A1', 'HT中没有找到相应交易号的记录, 对账单中存在'); // 表标题行 $objPHPExcel->getActiveSheet() ->SetCellValue('A2', '#') ->SetCellValue('B2', '渠道') ->SetCellValue('C2', '对账类型') ->SetCellValue('D2', '订单号') ->SetCellValue('E2', '申请币种') ->SetCellValue('F2', '申请金额') ->SetCellValue('G2', '清算汇率') ->SetCellValue('H2', '入账币种') ->SetCellValue('I2', '入账金额') ->SetCellValue('J2', '手续费') ->SetCellValue('K2', '交易号') ->SetCellValue('L2', '交易日期') ->SetCellValue('M2', '备注'); $rowCount = 3; foreach ($ret as $key => $row) { $objPHPExcel->getActiveSheet() ->SetCellValue('A'.$rowCount, ($rowCount-1)) ->setCellValueExplicit('B'.$rowCount, "iPaylinks",PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('C'.$rowCount, $row->PR_dealType,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('D'.$rowCount, $row->PR_orderId,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('E'.$rowCount, $row->PR_currency,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('F'.$rowCount, number_format($row->PR_amount, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('G'.$rowCount, number_format($row->PR_rate, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('H'.$rowCount, $row->PR_entryCurrency,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('I'.$rowCount, number_format($row->PR_entryAmount, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('J'.$rowCount, number_format($row->PR_fee, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('K'.$rowCount, $row->PR_AccreditNo,PHPExcel_Cell_DataType::TYPE_STRING) ->SetCellValue('L'.$rowCount, $row->PR_paymentTime); $rowCount++; } $rowCount++; // 隔一行 /** HT手动录入的 */ $objPHPExcel->getActiveSheet() ->SetCellValue('A'.$rowCount, 'HT系统中缺少交易号的,手动录入的') ->SetCellValue('M'.$rowCount, '备注'); $rowCount++; foreach ($no_dealId as $key2 => $row2) { $objPHPExcel->getActiveSheet() ->SetCellValue('A'.$rowCount, ($key2+1)) ->setCellValueExplicit('B'.$rowCount, "iPaylinks",PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('C'.$rowCount, "",PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('D'.$rowCount, $row2->COLI_ID,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('E'.$rowCount, $row2->GAI_SQJECurrency,PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit('F'.$rowCount, number_format($row2->GAI_SQJE, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('G'.$rowCount, number_format($row2->PR_rate, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('H'.$rowCount, $row2->PR_entryCurrency,PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('I'.$rowCount, number_format($row2->PR_entryAmount, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('J'.$rowCount, number_format($row2->PR_fee, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING) // ->setCellValueExplicit('K'.$rowCount, $row2->PR_AccreditNo,PHPExcel_Cell_DataType::TYPE_STRING) ->SetCellValue('L'.$rowCount, $row2->GAI_AccountDate) ->SetCellValue('M'.$rowCount, $row2->GAI_Memo); $rowCount++; } $filename = "export_ipaylinks_abnormal_" . date('Y-m-d'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); } /*! * 转换字符集编码 * @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) { if ( ! in_array(strrchr($fe, '.'), array(".xlsx", ".xls"))) { continue; } $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->ipaylinks_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 ipaylinks_noStore_dealType() { return array( "归还保证金" ,"记账" ,"冻结" ,"解冻" ,"提现" ,"提现手续费" ); } public function paypal_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','paypal excel POST: ' . $target_folder . $this->input->get_post("fjson")); $statement_folder = FCPATH.'download_statement\paypal_activities\\' . $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) { if ( ! in_array(strrchr($fe, '.'), array(".xlsx", ".xls"))) { continue; } $file_path = $statement_folder . '\\' . $fe; if ( ! file_exists($file_path)) { continue; } $settlement_record = $this->paypal_read_excel($file_path); if (empty($settlement_record)) { continue; } var_dump($settlement_record); foreach ($settlement_record as $settle) { $orderid_info = analysis_orderid(trim($settle['Invoice Number'])); // todo $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['Transaction ID'])); } else if (strcasecmp($orderid_info->ordertype, "T") === 0){ // $old_info = $this->IPayLinks_model->get_money_t(trim($settle['Transaction ID'])); } } $warrant["PR_AccreditNo"] = trim($settle['Transaction ID']); $warrant["PR_payType"] = 15002; $warrant["PR_entryCurrency"] = trim($settle['Currency']); $warrant["PR_entryAmount"] = number_format(trim($settle['Net']), 2, ".", ""); $warrant["PR_COLI_SN"] = !empty($old_info) ? $old_info[0]->GAI_COLI_SN : NULL; $warrant["PR_currency"] = trim($settle['Currency']); $warrant["PR_amount"] = number_format(trim($settle['Gross']), 2, ".", ""); $warrant["PR_orderType"] = $orderid_info ? $orderid_info->ordertype : NULL; $warrant["PR_fee"] = number_format(trim($settle['Fee']), 2, ".", ""); $warrant["PR_paymentTime"] = trim($settle['Date']) . " " . trim($settle['Time']); $warrant["PR_dealType"] = trim($settle['Type']); $warrant["PR_orderId"] = trim($settle['Invoice Number']); // todo $warrant["PR_rate"] = "1.00"; $warrant["PR_status"] = trim($settle['Status']); $warrant["PR_buyerName"] = trim($settle['Name']); $warrant["PR_buyerEmail"] = trim($settle['From Email Address']); $this->Report_model->new_report($warrant); $warrant = NULL; $update_cnt++; } $settlement_record = NULL; } echo $update_cnt; } public function paypal_read_excel($filePath) { $tarr1=array(); $this->load->library('PHPExcel'); $PHPExcel = new PHPExcel(); $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); foreach($currentSheet->getRowIterator() as $key1 => $row) { $row_tmp = array(); foreach($row->getCellIterator() as $key => $cell) { if ($key1 === 1) { $col_titles[] = $cell->getCalculatedValue(); } elseif ($key === 1) { $row_tmp[$col_titles[$key]] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'H:i:s'); } else { $row_tmp[$col_titles[$key]] = $cell->getCalculatedValue(); } } $row_tmp ? $tarr1[] = $row_tmp : NULL; } return $tarr1; } 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=NULL) { $url = $this->config->item("payment_url") . "?start_time=2018-03-28T11:00:00Z&end_time=2018-03-30T11:00:00Z"; // $url = $this->config->item("sale_url") . $payment_id; $c = $this->call_paypal($url); echo (json_encode($c)); return $c; } public function call_paypal($url, $body = false) { $token_info = $this->paypal_get_token(); // 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 } }