output->enable_profiler(TRUE);
$this->load->model('Paypal_model');
$this->load->model('Note_model');
}
public function index() {
$this->note_list();
}
//收款记录列表
public function index_old($date = 'all', $page = 1) {
$this->permission->is_admin(true);
$data = array();
$data['current_m'] = $date == 'all' ? date('Ym') : date('Ym', strtotime($date));
$data['current_d'] = $date == 'all' ? '' : date('Ymj', strtotime($date));
$data['userdata'] = $this->session->userdata('admin_chtcdn');
$date = $date == 'all' ? 'all' : date('Y-m-d', strtotime($date));
$list = $this->Paypal_model->get_mail_list(30, '', $date);
$pages['total'] = count($list); //数据总条数
$pages['pageSize'] = 30; //每页展示数量
$pages['url'] = site_url("apps/paypal/index/index/" . $date . '/'); //页码链接
$pages['current'] = $page; //当前页码
$data['pageinfo'] = show_page($pages);
$mail_id_str = 0;
$list = array_slice($list, $pages['pageSize'] * ($page - 1), $pages['pageSize']);
foreach ($list as $v) {
$mail_id_str.=',' . $v->M_SN;
}
//echo $mail_id_str;
$data['list'] = array();
!empty($list) && $data['list'] = $this->Paypal_model->get_mail_list(30, $mail_id_str);
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
}
//收款邮件详情
public function detail($mail_sn, $tpl = 0) {
$this->permission->is_admin(true);
$data = array();
$mailinfo = $list = $this->Paypal_model->get_mail_list(30, $mail_sn);
if (empty($mailinfo[0])) {
//$url=site_url('apps/paypal/index/error_paypal');
//echo '无详细信息,三秒后跳转......';
//echo '';
//die();
}
$mailinfo = $mailinfo[0];
$data['mail'] = $mailinfo;
if ($tpl == 0) {
$data['current_m'] = date('Ym', strtotime($mailinfo->M_AddTime));
$data['current_d'] = date('Ymj', strtotime($mailinfo->M_AddTime));
$data['detail_tpl'] = '1';
$data['userdata'] = $this->session->userdata('admin_chtcdn');
!empty($mailinfo->pm_orderid) && $data['orderinfo'] = $this->Paypal_model->get_order($mailinfo->pm_orderid, true);
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
} else {
$this->load->view('detail', $data);
}
}
//用于HT--交易详情
public function detail_ht($pm_transaction_id) {
$data = array();
//先查询原来的数据
$data['mailinfo'] = $list = $this->Paypal_model->get_paypal_msg($pm_transaction_id);
if (!empty($data['mailinfo'])) {
$data['mailinfo'] = $data['mailinfo'][0];
$this->load->view('detail_ht', $data);
} else {
$data = $this->Note_model->note($pm_transaction_id);
$this->load->view('mail_templete', $data);
}
}
//收款记录搜索
public function search() {
$data = array();
$data['current_m'] = date('Ym');
$data['current_d'] = '';
$data['userdata'] = $this->session->userdata('admin_chtcdn');
$data['list'] = array();
$mail_o_orderno = $this->input->post('search_key');
$data['search_key'] = $mail_o_orderno;
$data['list'] = $this->Paypal_model->get_search_mails($mail_o_orderno);
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
}
//我的收款
public function my_paypal() {
$userdata = $this->session->userdata('admin_chtcdn');
$data['current_m'] = date('Ym');
$data['current_d'] = '';
$data['userdata'] = $userdata;
$data['list'] = array();
if ($userdata != false && !empty($userdata['OPI_Email']) && !empty($userdata['OPI_FirstName'])) {
$data['list'] = $this->Paypal_model->get_my_order($userdata['OPI_Email'], $userdata['OPI_FirstName'], $userdata['OPI_Name']);
}
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
}
//从paypal接口获取交易记录列表
public function get_transactions_list() {
//查询条件 最后一次更新的时间
$last_paypal_msg = $this->Paypal_model->last_paypal_msg();
//修改为,查询最后一个交易记录时间,根据原始文本中的时间来查询,而不是数据库入库时间
$last_date = empty($last_paypal_msg) ? date('Y-m-d') . ' 00:00:00' : $last_paypal_msg[0]->pm_payment_date;
$last_date = str_replace(' ', 'T', $last_date) . 'Z';
$param['request'] = array(
'TRANSACTIONCLASS' => 'ALL',
'startdate' => $last_date
);
//访问接口,并对返回的数据转换格式
$sandbox = '0';
$base = $this->base($sandbox);
$transactions = $this->get_transactions($base, $param); //获取历史交易记录
$transactions = urldecode($transactions); //url格式
$result_count = substr_count($transactions, 'L_TIMESTAMP'); //交易记录条数
$list = array();
$temparray = explode('&', $transactions);
foreach ($temparray as $key => $value) {
$temp_arr = explode('=', $value);
if (isset($temp_arr[1])) {
$list[$temp_arr[0]] = $temp_arr[1];
}
}
//保存信息
for ($i = 0; $i < $result_count; $i++) {
//已经更新了的记录就不再保存,并停止处理剩下的数据
$has_exit = $this->Paypal_model->get_paypal_msg($list['L_TRANSACTIONID' . $i]);
if (!empty($has_exit)) {
die('更新了' . $i . '/' . $result_count . '条记录');
}
//只保存已经支付完成,且交易类型是付款和退款的交易记录,因为其他类型的交易记录获取不到交易详情
if ($list['L_STATUS' . $i] != 'Completed' || !in_array($list['L_TYPE' . $i], array('Payment', 'Refund'))) {
continue;
}
//保存交易信息
$pm_transaction_id = $list['L_TRANSACTIONID' . $i]; //交易号
$pm_orderid = ''; //订单号
$pm_item_name = ''; //交易备注说明
$pm_money = $list['L_AMT' . $i]; //交易金额
$pm_currency = $list['L_CURRENCYCODE' . $i]; //交易币种
$pm_payer = $list['L_NAME' . $i]; //客人名字
$pm_payer_email = ''; //客人邮箱
$pm_payer_status = ''; //是否paypal已认证的客人
$pm_memo = ''; //交易详情json数据集
$pm_pay_type = $list['L_TYPE' . $i]; //交易类型
$pm_payment_date = $list['L_TIMESTAMP' . $i]; //交易时间
$pm_payment_date = str_replace(array('T', 'Z'), array(' ', ''), $pm_payment_date);
$insert_res = $this->Paypal_model->save_paypal_msg($pm_transaction_id, $pm_orderid, $pm_item_name, $pm_money, $pm_currency, $pm_payer, $pm_payer_email, $pm_payer_status, $pm_memo, $pm_payment_date, $pm_pay_type);
}
echo ('更新了' . ($result_count) . '条记录');
}
public function get_transactions_detail_by_paymentToken()
{
$token = $this->input->post("t");
$base = $this->base(0);
// $post = "METHOD=GetTransactionDetails&VERSION=100.0&TRANSACTIONID=$token";
// $post = "METHOD=GetExpressCheckoutDetails&VERSION=124.0&TOKEN=$token";
$token = str_replace(' ', 'T', $token) . 'Z';
$post = "METHOD=TransactionSearch&VERSION=100.0&TRANSACTIONCLASS=ALL&STARTDATE=$token";
$detail = $this->call($base, $post);
var_dump($detail);
return;
}
//根据交易号获取交易详细信息
public function get_transactions_detail($redirect = 0) {
//从数据库提取一条还没有更新交易详情的记录获取交易号
$pay_data = $this->Paypal_model->get_paypal_msg();
if (empty($pay_data)) {
if ($redirect == 1) {
//重定向
redirect(site_url('apps/paypal'));
}
die('暂无更新');
}
$pay_data = $pay_data[0];
$transactionsID = $pay_data->pm_transaction_id;
//接口信息拼接
$sandbox = '0';
$base = $this->base($sandbox);
$post = "METHOD=GetTransactionDetails&VERSION=100.0&TRANSACTIONID=$transactionsID";
$detail = $this->call($base, $post);
//格式化接口返回数据
$detail = urldecode($detail);
$detail_list = array();
$temparray = explode('&', $detail);
foreach ($temparray as $key => $value) {
$temp_arr = explode('=', $value);
if (isset($temp_arr[1])) {
$detail_list[$temp_arr[0]] = $temp_arr[1];
}
}
//请求数据状态是成功且支付状态是完成的,更新交易记录信息
if (isset($detail_list['ACK']) && isset($detail_list['PAYMENTSTATUS']) && $detail_list['ACK'] == 'Success' && $detail_list['PAYMENTSTATUS'] == 'Completed') {
$pm_sn = $pay_data->pm_sn;
//$pm_orderid=(isset($detail_list['INVNUM']) && !empty($detail_list['INVNUM']))?$detail_list['INVNUM']:isset($detail_list['CUSTOM'])?$detail_list['CUSTOM']:isset($detail_list['L_NAME0'])?$detail_list['L_NAME0']:'';
//ycc 修改了获取订单号的优先级和优化逻辑判断,INVNUM>L_NUMBER0>CUSTOM>L_NAME0
$pm_orderid = '';
if (isset($detail_list['INVNUM']) && !empty($detail_list['INVNUM'])) {
$pm_orderid = $detail_list['INVNUM'];
} else {
if (isset($detail_list['L_NUMBER0']) && !empty($detail_list['L_NUMBER0'])) {
$pm_orderid = $detail_list['L_NUMBER0'];
} else {
if (isset($detail_list['CUSTOM']) && !empty($detail_list['CUSTOM'])) {
$pm_orderid = $detail_list['CUSTOM'];
} else {
if (isset($detail_list['L_NAME0']) && !empty($detail_list['L_NAME0'])) {
$pm_orderid = $detail_list['L_NAME0'];
}
}
}
}
$pm_item_name = $pm_orderid;
$orderinfo = $pm_orderid;
if (strpos($orderinfo, 'Tracking Code:') !== false) {
$orderinfo = explode('Tracking Code:', $orderinfo);
$orderinfo = explode('Travel Advisor', $orderinfo[1]);
$orderinfo = trim($orderinfo[0]);
}
//订单号过滤
$orderinfo = explode('--', $orderinfo);
$orderinfo = $orderinfo[0];
$orderinfo = explode('_', $orderinfo);
//$orderinfo=$orderinfo[0]; ycc:移到下面去了,在这里赋值的话后面用$orderinfo[1]是不存在的 --2016.02.19
$ordertype = 'N';
if (isset($orderinfo[1])) {
$ordertype_temp = trim($orderinfo[1]);
if (substr($ordertype_temp, 0, 1) == 'T') {
$ordertype = 'T';
} elseif (substr($ordertype_temp, 0, 1) == 'B') {
$ordertype = 'B';
}
}
$orderinfo = $orderinfo[0];
$orderinfo = explode(',', $orderinfo);
$orderid = trim($orderinfo[0]);
$temp_orderid = explode('-', $orderid);
if (isset($temp_orderid[1])) {
$orderid = trim($temp_orderid[1]);
}
$tempid = explode(' ', $orderid);
$pm_orderid = $tempid[0]; //订单号
$pm_payer_email = $detail_list['EMAIL']; //客人邮箱
$pm_payer_status = $detail_list['PAYERSTATUS']; //客人认证状态
$pm_memo = json_encode($detail_list); //序列化数据
$this->Paypal_model->update_paypal_msg($pm_sn, $pm_orderid, $pm_item_name, $pm_payer_email, $pm_payer_status, $pm_memo);
//查找外联信息
$M_State = 0;
if (!empty($pm_orderid)) {
$pm_invoice = $pm_orderid;
$advisor_info = $this->Paypal_model->get_order($pm_orderid, false, $ordertype);
$OPI_Email = isset($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : '';
$OPI_FirstName = isset($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : isset($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : '';
//有订单号,但是匹配不到外联(带收款进来的新订单)
if (empty($OPI_Email) && empty($OPI_FirstName)) {
$M_State = 2;
}
//匹配到了外联,但是外联没有邮箱
elseif (empty($OPI_Email) && !empty($OPI_FirstName)) {
$M_State = 3;
}
}
//没有订单号,转发给陆燕
else {
$OPI_Email = 'lussie@chinahighlights.net';
$OPI_FirstName = 'Lussie';
}
//给外联发送通知邮件
$fromName = $pay_data->pm_payer;
$fromEmail = $detail_list['EMAIL'];
$toName = $OPI_FirstName;
$toEmail = $OPI_Email;
$subject = '来自' . $pay_data->pm_payer . '的付款';
if (!empty($pm_orderid))
$subject = $pm_orderid . ' / ' . $pay_data->pm_money . $pay_data->pm_currency . ' / ' . $pay_data->pm_payer;
$data['mailinfo'] = $pay_data;
$data['mailinfo']->INSURANCEOPTIONSELECTED = isset($detail_list['INSURANCEOPTIONSELECTED']) ? $detail_list['INSURANCEOPTIONSELECTED'] : '';
$data['mailinfo']->SUBJECT = isset($detail_list['SUBJECT']) ? $detail_list['SUBJECT'] : '';
$data['mailinfo']->NOTE = isset($detail_list['NOTE']) ? $detail_list['NOTE'] : '';
$data['mailinfo']->pm_payer_email = $detail_list['EMAIL'];
$data['mailinfo']->pm_item_name = $pm_item_name;
$data['mailinfo']->pm_memo = $pm_memo;
$data['mailinfo']->pm_payer_status = $pm_payer_status;
$body = $this->load->view('mail_tpl', $data, true);
$M_RelatedInfo = $pm_sn;
$M_AddTime = $pay_data->pm_payment_date;
$this->Paypal_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime);
if (!empty($pm_orderid)) {
//更新还没有填的客邮和交易号de收款记录(商务订单)
if (isset($advisor_info->order_type) && $advisor_info->order_type == 0) {
$ht_memo = '交易号(自动录入):' . $transactionsID;
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
$this->Paypal_model->add_account_info($GAI_COLI_SN, $advisor_info->COLI_ID, $pay_data->pm_money, $pay_data->pm_payment_date, mb_strtoupper($pay_data->pm_currency), $pay_data->pm_payment_date, $pay_data->pm_payment_date, $pay_data->pm_payment_date, '', $detail_list['EMAIL'], $transactionsID, $ht_memo);
}
//更新还没有填的客邮和交易号de收款记录(传统订单)
elseif (isset($advisor_info->order_type) && $advisor_info->order_type == 1) {
//
}
}
}
if ($redirect == 1) {
//redirect(site_url('apps/paypal'));
}
echo $transactionsID . '更新成功';
}
//定时执行:重新设置新订单邮件的外联信息
public function set_owner() {
//新订单,无外联,每次更新最新的10条
$mail_info2 = $this->Paypal_model->get_unsend_list(10, 2);
if (!empty($mail_info2)) {
foreach ($mail_info2 as $key => $mail_info) {
$orderinfo = $mail_info->pm_item_name;
if (strpos($orderinfo, 'Tracking Code:') !== false) {
$orderinfo = explode('Tracking Code:', $orderinfo);
$orderinfo = explode('Travel Advisor', $orderinfo[1]);
$orderinfo = trim($orderinfo[0]);
}
$orderinfo = explode('--', $orderinfo);
$orderinfo = $orderinfo[0];
$orderinfo = explode('_', $orderinfo);
$ordertype = 'N';
if (isset($orderinfo[1])) {
$ordertype_temp = trim($orderinfo[1]);
if (substr($ordertype_temp, 0, 1) == 'T') {
$ordertype = 'T';
} elseif (substr($ordertype_temp, 0, 1) == 'B') {
$ordertype = 'B';
}
}
$advisor_info = $this->Paypal_model->get_order($mail_info->pm_orderid, false, $ordertype);
$OPI_Email = isset($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : '';
$OPI_FirstName = isset($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : isset($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : '';
//设置外联邮箱,并把邮件设置为未发送
if (!empty($OPI_Email)) {
$send_state = 0;
$this->Paypal_model->update_mail($mail_info->M_SN, $OPI_Email, $OPI_FirstName, $send_state);
echo '新订单外联信息有更新
';
}
}
} else {
echo '暂无“没有设置外联”的新订单
';
}
//外联无邮箱,每次更新最新的10条
$mail_info3 = $this->Paypal_model->get_unsend_list(10, 3);
if (!empty($mail_info3)) {
foreach ($mail_info3 as $key => $mail_info) {
$orderinfo = $mail_info->pm_item_name;
if (strpos($orderinfo, 'Tracking Code:') !== false) {
$orderinfo = explode('Tracking Code:', $orderinfo);
$orderinfo = explode('Travel Advisor', $orderinfo[1]);
$orderinfo = trim($orderinfo[0]);
}
$orderinfo = explode('--', $orderinfo);
$orderinfo = $orderinfo[0];
$orderinfo = explode('_', $orderinfo);
$ordertype = 'N';
if (isset($orderinfo[1])) {
$ordertype_temp = trim($orderinfo[1]);
if (substr($ordertype_temp, 0, 1) == 'T') {
$ordertype = 'T';
} elseif (substr($ordertype_temp, 0, 1) == 'B') {
$ordertype = 'B';
}
}
$advisor_info = $this->Paypal_model->get_order($mail_info->pm_orderid, false, $ordertype);
$OPI_Email = isset($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : '';
$OPI_FirstName = isset($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : isset($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : '';
//设置外联邮箱,并把邮件设置为未发送
if (!empty($OPI_Email)) {
$send_state = 0;
$this->Paypal_model->update_mail($mail_info->M_SN, $OPI_Email, $OPI_FirstName, $send_state);
echo '外联没有邮箱的邮件信息有更新
';
} else {
echo $OPI_FirstName . '没有外联邮箱
';
}
}
} else {
echo '暂无外联没有邮箱
';
}
echo 'ok
';
}
//设置授权
public function base($sandbox) {
if ($sandbox == '1') {
$base = array(
'url' => 'https://api-3t.sandbox.paypal.com/nvp',
'username' => 'pays_api1.chinahighlights.com',
'password' => 'HWJ7BFSZBZUETZ4K',
'signature' => 'AxfjDmuPaaxMccY-8ZHIWGDHQKxOAyYzBPToLDzU-cgnldnh6eZGKJMn'
);
}
if ($sandbox == '0') {
$base = array(
'url' => 'https://api-3t.paypal.com/nvp',
'username' => 'pays_api1.chinahighlights.com',
'password' => 'HWJ7BFSZBZUETZ4K',
'signature' => 'AxfjDmuPaaxMccY-8ZHIWGDHQKxOAyYzBPToLDzU-cgnldnh6eZGKJMn'
);
}
return $base;
}
//获取交易历史记录列表
public function get_transactions($base, $param) {
$post = 'METHOD=TransactionSearch&';
$post .= 'VERSION=100.0';
foreach ($param['request'] as $key => $value) {
$post .= '&' . strtoupper($key) . '=' . $value;
}
$transactions = $this->call($base, $post);
return $transactions;
}
//请求接口
public function call($base, $post) {
$post .= '&PWD=' . $base['password'];
$post .= '&USER=' . $base['username'];
$post .= '&SIGNATURE=' . $base['signature'];
//print_r($post);
// print_r($base);
//die();
//启动服务器代理
$ch = curl_init($base['url']); //初始化URL
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_HEADER, 0); //启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); //在完成交互以后强迫断开连接,不能重用
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); //强制获取一个新的连接,替代缓存中的连接。
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //数据全部通过post发送,Content-Type头将会被设置成multipart/form-data
$response = curl_exec($ch);
if (empty($response)) {
return 'No response received.';
} else {
return $response;
}
curl_close($ch);
}
//错误信息
public function error_paypal($page = 1) {
$userdata = $this->session->userdata('admin_chtcdn');
$data['current_m'] = date('Ym');
$data['current_d'] = '';
$data['userdata'] = $userdata;
//获取最近三个月的数据
$lastinfo = $this->Paypal_model->last_paypal_msg();
$lastdate = $lastinfo[0]->pm_create_date;
$lastdate = substr($lastdate, 0, 10);
$data['list'] = array();
$data['list'] = $this->Paypal_model->get_all_error_order(0, $lastdate);
$data['error'] = 1;
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
}
//订单不匹配处理
public function unmatch($type) {
$userdata = $this->session->userdata('admin_chtcdn');
$data['current_m'] = date('Ym');
$data['current_d'] = '';
$data['userdata'] = $userdata;
$data['list2'] = array();
//获取最近三个月的数据
$lastinfo = $this->Paypal_model->last_paypal_msg();
$lastdate = $lastinfo[0]->pm_create_date;
$lastdate = substr($lastdate, 0, 10);
if (isset($type)) {
$data['list2'] = $this->Paypal_model->get_unmatch_list($type, $lastdate);
} else {
redirect(site_url('apps/paypal'));
die();
}
$data['fg'] = 1; //@fg为订单不匹配时显示的标志
$this->load->view('n-header', $data);
$this->load->view('index');
$this->load->view('n-footer');
}
//存储paypal发送的消息
public function paypal_note() {
$pn_txn_id = $this->input->post('txn_id');
$pn_invoice = $this->input->post('invoice');
empty($pn_invoice) ? $pn_invoice = $this->input->post('transaction_subject') : false;
empty($pn_invoice) ? $pn_invoice = '' : false;
$pn_custom = $this->input->post('custom');
empty($pn_custom) ? $pn_custom = '' : false;
$pn_mc_gross = $this->input->post('mc_gross');
$pn_item_name = $this->input->post('item_name');
if (empty($pn_item_name)) {
$pn_item_name = $this->input->post('item_name1'); //APP用SDK支付的话会是item_name1
empty($pn_item_name) ? $pn_item_name = '' : false;
}
$pn_item_number = $this->input->post('item_number');
if (empty($pn_item_number)) {
$pn_item_number = $this->input->post('item_number1'); //APP用SDK支付的话会是item_number1
empty($pn_item_number) ? $pn_item_number = '' : false;
}
$pn_mc_currency = $this->input->post('mc_currency');
$pn_payment_status = $this->input->post('payment_status');
$pn_payment_date = $this->input->post('payment_date');
$pn_payer = $this->input->post('first_name') . ' ' . $this->input->post('last_name');
$pn_payer_email = $this->input->post('payer_email');
$pn_memo = json_encode($_POST);
if (!empty($pn_txn_id)) {
//把PDT时间转成GMT时间
$pn_payment_date = gmdate('Y-m-d H:i:s', strtotime($pn_payment_date));
$this->Note_model->save_paypal_note($pn_txn_id, $pn_invoice, $pn_custom, $pn_mc_gross, $pn_item_name, $pn_item_number, $pn_mc_currency, $pn_payment_status, $pn_payer, $pn_payer_email, $pn_payment_date, $pn_memo);
echo 'ok';
} else {
echo 'no';
}
}
//解析出订单号
public function analysis_orderid($note_invoice_string) {
$data = array();
//空字符串或者小于8位都属于不正确的订单号
if (empty($note_invoice_string) || strlen($note_invoice_string) < 8) {
return false;
}
//APP订单处理,如标题China Train Booking-160617462
if ((strpos($note_invoice_string, 'China Train Booking') !== false) || (strpos($note_invoice_string, 'ChinaTrainBooking') !== false)) {
$note_invoice_string = explode('-', $note_invoice_string);
if (isset($note_invoice_string[1])) {
$note_invoice_string = trim($note_invoice_string[1]);
}
return json_encode(array('orderid' => $note_invoice_string, 'ordertype' => 'A')); //APP订单,不需要处理交易记录和通知
}
//订单号例子 160420021_B--9608
//Tracking code:2016-05-06-JJ160319027 /Travel advisor:Fiona Jiang /Content:Shanghai, Beijing, Pingyao, Xian, Guilin, Yangshuo, S
if (strpos($note_invoice_string, 'Tracking Code:') !== false) {
$note_invoice_string = explode('Tracking Code:', $note_invoice_string);
$note_invoice_string = explode('Travel Advisor', $note_invoice_string[1]);
$note_invoice_string = trim($note_invoice_string[0]);
}
//订单号过滤
$note_invoice_string = explode('--', $note_invoice_string);
$note_invoice_string = $note_invoice_string[0];
$note_invoice_string = explode('_', $note_invoice_string);
//订单类型识别
$ordertype = 'N';
if (isset($note_invoice_string[1])) {
$ordertype_temp = trim($note_invoice_string[1]);
if (substr($ordertype_temp, 0, 1) == 'T') {
$ordertype = 'T';
} elseif (substr($ordertype_temp, 0, 1) == 'B') {
$ordertype = 'B';
}
}
// 2018.05.28 for Trippest, tourMaster的订单号是01开头
if (substr($note_invoice_string[0], 0, 2) == '01') {
$ordertype = 'TP';
}
//手机订单、机票订单都没有加标示,在这里帮加上,暂时的,今后还是要在网前设置好
if ($ordertype == 'N' && isset($note_invoice_string[0])) {
$orderid_temp = $note_invoice_string[0];
if (strlen($orderid_temp) == 9 && substr($orderid_temp, 0, 2) == '16') {
$ordertype = 'B';
}
}
//前台预付款订单,以45开头,如45117640类型的订单号,这个类型的订单会分配到某个新订单去,需要查找COLI_AddCode来找新订单号,然后再发送通知
if ($ordertype == 'N' && isset($note_invoice_string[0])) {
$orderid_temp = $note_invoice_string[0];
if (strlen($orderid_temp) == 8 && substr($orderid_temp, 0, 2) == '45') {
$ordertype = 'M';
}
}
//新的订单号14733661876255
if ($ordertype == 'N' && isset($note_invoice_string[0])) {
$orderid_temp = $note_invoice_string[0];
if (strlen($orderid_temp) == 14 && substr($orderid_temp, 0, 2) == '14') {
$ordertype = 'M';
}
}
if ($ordertype == 'N') {
return false; //没有编号的订单直接显示错误,由人工分配
}
$note_invoice_string = $note_invoice_string[0];
$note_invoice_string = explode(',', $note_invoice_string);
$orderid = trim($note_invoice_string[0]);
$temp_orderid = explode('-', $orderid);
if (isset($temp_orderid[1])) {
$orderid = trim($temp_orderid[1]);
}
$tempid = explode(' ', $orderid);
$pm_orderid = $tempid[0]; //订单号
if (empty($pm_orderid) || strlen($pm_orderid) < 8) {
return false;
}
return json_encode(array('orderid' => $pm_orderid, 'ordertype' => $ordertype));
}
/*
业务处理流程:
1. paypal_note函数是用来接收paypal提交过来的数据,每一笔交易记录都会自动post过来
2. 用定时程序访问send_note函数,查询未发送和发送错误的记录(paypal_note函数存储的记录)
3. analysis_orderid函数分析记录中的订单号,识别传统订单、商务订单等
4. Paypal_model->get_order根据订单号和类型找到对应外联信息,构造邮件发送给外联
5. 添加付款记录到订单信息中
*/
//找到没有发送的记录
public function send_note($pn_txn_id = false) {
$data = array();
//优先处理指定的交易号,用于修正交易号直接发送通知
if (!empty($pn_txn_id)) {
$data['unsend_list'] = array($this->Note_model->note($pn_txn_id));
}
//找到处理过的的记录
if (empty($data['unsend_list'])) {
$data['unsend_list'] = $this->Note_model->unsend(10);
}
//没有未处理的数据再查找处理失败的数据
if (empty($data['unsend_list'])) {
$data['unsend_list'] = $this->Note_model->failnote(20);
}
$show_index = 0;
foreach ($data['unsend_list'] as $item) {
//已经发送的不处理,防止重复发送
if ($item->pn_send == 'send' && empty($pn_txn_id)) {
continue;
}
//显示处理记录
if (empty($pn_txn_id)) {
echo ++$show_index . ' ' . $item->pn_txn_id . '
';
}
//退款状态默认为已经处理,陆燕在退款前手动通知外联了,系统跳过处理
if ($item->pn_payment_status == 'Refunded') {
$this->Note_model->update_send($item->pn_txn_id, 'send');
continue;
}
//只处理完成状态,其他状态由陆燕处理
if ($item->pn_payment_status !== 'Completed') {
$this->Note_model->update_send($item->pn_txn_id, 'sendfail');
continue;
}
//检测是否是APP订单,默认不处理
if ((strpos($item->pn_memo, 'China Train Booking') !== false) || (strpos($item->pn_memo, 'ChinaTrainBooking') !== false)) { //APP自动出票的订单不需要处理
$this->Note_model->update_send($item->pn_txn_id, 'send');
continue;
}
//根据note信息找到订单号
$orderid_info = $this->analysis_orderid($item->pn_invoice);
if (empty($orderid_info)) {
$orderid_info = $this->analysis_orderid($item->pn_custom);
}
if (empty($orderid_info)) {
$orderid_info = $this->analysis_orderid($item->pn_item_name);
}
if (empty($orderid_info)) {
$orderid_info = $this->analysis_orderid($item->pn_item_number);
}
//找不到订单号,设置为发送失败标示
if (empty($orderid_info)) {
$this->Note_model->update_send($item->pn_txn_id, 'sendfail');
continue;
}
//根据订单号查找外联信息
$orderid_info = json_decode($orderid_info);
$advisor_info = $this->Paypal_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype);
// for trippest tourMaster 2018.05.28
if ($orderid_info->ordertype == 'TP') {
$this->trippest_note($orderid_info, $item);
continue;
}
//查不到订单信息
if (empty($advisor_info)) {
$this->Note_model->update_send($item->pn_txn_id, 'sendfail');
continue;
}
//更新正确的订单信息到记录中,以这个为主
$this->Note_model->set_invoice($item->pn_txn_id, $orderid_info->orderid . '_' . $orderid_info->ordertype);
//检测是否是APP订单,默认不处理
if ($orderid_info->ordertype == 'A') { //APP自动出票的订单不需要处理
$this->Note_model->update_send($item->pn_txn_id, 'send');
continue;
}
//添加支付信息入库
//没有分配订单之前先添加付款记录,这个过程可能会执行多次,必须在添加记录前查找是否有数据
if (!empty($orderid_info)) {
//更新还没有填的客邮和交易号de收款记录(商务订单)
if (isset($advisor_info->order_type) && $advisor_info->order_type == 0) {
$ht_memo = '交易号(自动录入):' . $item->pn_txn_id;
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
//CHTAPP订单添加记录前判断是否有记录,以前的APP版本没有交易号,只能拿金额来判断
if (substr($advisor_info->COLI_WebCode, 0, 6) == 'CHTAPP') {//只判断前6位字符,CHTAPP-fr CHTAPP-jp等各语种都属于APP订单
$ssje = $this->Paypal_model->get_ssje($item->pn_mc_gross, '15002', mb_strtoupper($item->pn_mc_currency));
$this->Paypal_model->add_account_info_forAPP($GAI_COLI_SN, $advisor_info->COLI_ID, $item->pn_mc_gross, $item->pn_payment_date, mb_strtoupper($item->pn_mc_currency), $ssje, $item->pn_payment_date, $item->pn_payment_date, $item->pn_payment_date, '', $item->pn_payer_email, $item->pn_txn_id, $ht_memo);
if ($advisor_info->COLI_WebCode == 'CHTAPP' && $advisor_info->COLI_State == 11) { //只修改APP组的订单状态,并且订单进度是我的订单
$this->Paypal_model->update_biz_coli_state($GAI_COLI_SN, 8); //把订单状态改为已付款
}
} else {
$ssje = $this->Paypal_model->get_ssje($item->pn_mc_gross, '15002', mb_strtoupper($item->pn_mc_currency));
$this->Paypal_model->add_account_info($GAI_COLI_SN, $advisor_info->COLI_ID, $item->pn_mc_gross, $item->pn_payment_date, mb_strtoupper($item->pn_mc_currency), $ssje, $item->pn_payment_date, $item->pn_payment_date, $item->pn_payment_date, '', $item->pn_payer_email, $item->pn_txn_id, $ht_memo);
// 更新订单主表付款方式,防止没访问thankyou-train.asp
$this->Paypal_model->update_paymanner($GAI_COLI_SN, '15010');
// 把订单状态设置为13-新订单已支付
$this->Paypal_model->update_biz_coli_state($GAI_COLI_SN, 13);
}
}
//更新还没有填的客邮和交易号de收款记录(传统订单)
elseif (isset($advisor_info->order_type) && $advisor_info->order_type == 1) {
$ht_memo = '交易号(自动录入):' . $item->pn_txn_id;
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
$ssje = $this->Paypal_model->get_ssje($item->pn_mc_gross, '15002', mb_strtoupper($item->pn_mc_currency));
$gai_sn = $this->Paypal_model->add_tour_account_info($GAI_COLI_SN, $item->pn_mc_gross, $item->pn_payment_date, mb_strtoupper($item->pn_mc_currency), $ssje, $item->pn_payment_date, $item->pn_payment_date, $item->pn_payment_date, $item->pn_payer, $item->pn_payer_email, $item->pn_txn_id, $ht_memo);
//添加汉特的订单提醒
$this->Paypal_model->update_coli_introduction($GAI_COLI_SN, '已支付 ' . mb_strtoupper($item->pn_mc_currency) . $item->pn_mc_gross);
// 添加HT任务
$this->Paypal_model->exec_addToTask($gai_sn);
}
}
$opi_email = !empty($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : ''; //lussie@chinahighlights.net
$opi_firstname = !empty($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : !empty($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : ''; //lussie
//没有外联信息表示订单未分配
if (empty($opi_email) || empty($opi_firstname)) {
$this->Note_model->update_send($item->pn_txn_id, 'sendfail');
continue;
}
//添加邮件发送记录
//给外联发送通知邮件
$fromName = !empty($item->pn_payer) ? $item->pn_payer : '';
$fromEmail = !empty($item->pn_payer_email) ? $item->pn_payer_email : '';
$toName = !empty($opi_firstname) ? $opi_firstname : '';
$toEmail = !empty($opi_email) ? $opi_email : '';
$subject = $orderid_info->orderid . '_' . $orderid_info->ordertype . ' / ' . $item->pn_mc_gross . $item->pn_mc_currency . ' / ' . $fromName;
$body = $this->load->view('mail_templete', $item, true); //$item->pn_memo;
$M_RelatedInfo = $item->pn_sn;
$M_AddTime = $item->pn_payment_date;
$M_State = 0;
$this->Paypal_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime, 'paypal note');
//添加邮件发送记录 end
$this->Note_model->update_send($item->pn_txn_id, 'send');
}
//echo 'done!';
}
public function trippest_note($orderid_info, $paypal_msg)
{
$opi_firstname = "David";
$opi_email = "david@trippest.com";
$orderid_info->orderid = $orderid_info->orderid . "#" . substr($orderid_info->orderid, 10);
$fromName = !empty($paypal_msg->pn_payer) ? $paypal_msg->pn_payer : '';
$fromEmail = !empty($paypal_msg->pn_payer_email) ? $paypal_msg->pn_payer_email : '';
$toName = !empty($opi_firstname) ? $opi_firstname : '';
$toEmail = !empty($opi_email) ? $opi_email : '';
$subject = $orderid_info->orderid . '_' . $orderid_info->ordertype . ' / ' . $paypal_msg->pn_mc_gross . $paypal_msg->pn_mc_currency . ' / ' . $fromName;
$body = $this->load->view('mail_templete', $paypal_msg, true); //$paypal_msg->pn_memo;
$M_RelatedInfo = $paypal_msg->pn_sn;
$M_AddTime = $paypal_msg->pn_payment_date;
$M_State = 0;
$this->Paypal_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime, 'paypal note');
$this->Note_model->update_send($paypal_msg->pn_txn_id, 'send');
}
//所有记录列表
public function note_list() {
$data = array();
//有关键词则不限制日期
$data['search_key'] = $this->input->post('search_key');
$data['date'] = $this->input->get('date');
empty($data['date']) ? $data['date'] = date('Y-m-d') : false;
if (!empty($data['search_key'])) {
$data['notelist'] = $this->Note_model->search_key($data['search_key']);
} else {
$data['notelist'] = $this->Note_model->search_date($data['date']);
}
$this->load->view('n-header', $data);
$this->load->view('note_list');
$this->load->view('n-footer');
}
//所有记录列表
public function note_faillist() {
$data = array();
//有关键词则不限制日期
$data['search_key'] = $this->input->post('search_key');
$data['date'] = $this->input->get('date');
empty($data['date']) ? $data['date'] = date('Y-m-d') : false;
$data['notelist'] = $this->Note_model->failnote(100);
$this->load->view('n-header', $data);
$this->load->view('note_list');
$this->load->view('n-footer');
}
//获取note详情,修改各项数据
public function note_modal($pn_txn_id, $pn_invoice = false) {
$data = array();
$data['pn_invoice'] = $pn_invoice;
if (!empty($pn_txn_id)) {
$data['note'] = $this->Note_model->note($pn_txn_id);
if (!empty($data['note'])) {
if (!empty($pn_invoice)) {
$orderid_info = $this->analysis_orderid($pn_invoice);
} else {
$orderid_info = $this->analysis_orderid($data['note']->pn_invoice);
}
if (!empty($orderid_info)) {
$orderid_info = json_decode($orderid_info);
$data['order_info'] = $this->Paypal_model->get_order($orderid_info->orderid, true, $orderid_info->ordertype);
}
echo json_encode($this->load->view('note_setting', $data, true));
return true;
}
}
echo json_encode('没找到数据!');
}
//修改订单名
public function note_modal_save() {
$this->gai_modal_save();
// $data = array();
// $pn_txn_id = $this->input->post('pn_txn_id');
// $pn_invoice = $this->input->post('pn_invoice');
// if (!empty($pn_txn_id) && !empty($pn_invoice)) {
// $orderid_info = $this->analysis_orderid($pn_invoice);
// if (!empty($orderid_info)) {
// $orderid_info = json_decode($orderid_info);
// $advisor_info = $this->Paypal_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype);
// if (!empty($advisor_info)) {
// $this->Note_model->set_invoice($pn_txn_id, $pn_invoice);
// $this->send_note($pn_txn_id);
// echo json_encode('修改成功!');
// return true;
// }
// }
// }
// echo json_encode('没找到数据!');
}
//关闭note通知,用于手动处理通知后
public function close_note($pn_txn_id) {
$data = array();
$data['note'] = $this->Note_model->note($pn_txn_id);
if (!empty($data['note'])) {
$this->Note_model->update_send($pn_txn_id, 'send');
echo json_encode('通知已经关闭!');
return true;
}
echo json_encode('没找到数据!');
}
public function export_list()
{
$from_date = $this->input->post("from_date");
$to_date = $this->input->post("to_date");
$currency = $this->input->post("currency");
$export_list = $this->Note_model->date_range($from_date, $to_date, $currency);
if ($export_list == false) {
echo "Not found any records for export.";
return false;
}
$this->load->library('PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
//set width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
// 对齐
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// 表标题行
$objPHPExcel->getActiveSheet()
->SetCellValue('A1', '#')
->SetCellValue('B1', '团号')
->SetCellValue('C1', '金额')
->SetCellValue('D1', '付款人')
->SetCellValue('E1', '付款人邮箱')
->SetCellValue('F1', '交易号')
->SetCellValue('G1', '收单时间');
$currency_sum = array();
bcscale(2);
$rowCount = 2;
foreach ($export_list as $key => $row) {
if( trim($row->pn_mc_currency) != "") {
$currency_sum[trim($row->pn_mc_currency)] = @ bcadd($currency_sum[trim($row->pn_mc_currency)], $row->pn_mc_gross);
}
$payer = $row->pn_payer ? ($row->pn_payer . "<" . $row->pn_payer_email . ">") : "退款";
$orderid = $row->pn_invoice ? $row->pn_invoice : $row->pn_item_number;
$objPHPExcel->getActiveSheet()
->SetCellValue('A'.$rowCount, ($rowCount-1))
->setCellValueExplicit('B'.$rowCount, $orderid,PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('C'.$rowCount, trim($row->pn_mc_currency) . number_format($row->pn_mc_gross, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING)
->SetCellValue('D'.$rowCount, $row->pn_payer)
->SetCellValue('E'.$rowCount, $row->pn_payer_email)
->setCellValueExplicit('F'.$rowCount, $row->pn_txn_id,PHPExcel_Cell_DataType::TYPE_STRING)
->SetCellValue('G'.$rowCount, $row->pn_datetime);
$payer = $orderid = "";
$rowCount++;
}
$rowCount++; // 隔一行
// 汇总行
$objPHPExcel->getActiveSheet()
->SetCellValue('A' . $rowCount, '合计');
foreach ($currency_sum as $kc => $cur) {
$objPHPExcel->getActiveSheet()
->SetCellValue('B' . $rowCount, $kc)
->setCellValueExplicit('C' . $rowCount, number_format($cur, 2, ".", ""),PHPExcel_Cell_DataType::TYPE_STRING);
$rowCount++;
}
$filename = "export_paypal_" . 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');
}
/** 查看收款记录的是否已录入到订单 */
public function gai_modal($pn_txn_id=null, $pn_id = null, $neworder=null)
{
$data = array();
$data['note'] = $this->Note_model->note($pn_txn_id, $pn_id);
$orderid_info = $this->analysis_orderid($data['note']->pn_invoice);
if (!empty($orderid_info)) {
$orderid_info = json_decode($orderid_info);
if ($orderid_info->ordertype === 'T') {
$data['gai_info'] = $this->Paypal_model->get_money_t($pn_txn_id);
} elseif ($orderid_info->ordertype === 'B') {
$data['gai_info'] = $this->Paypal_model->get_money_b($pn_txn_id);
}
}
$data['old_order'] = $data['note']->pn_invoice;
$data['new_order'] = $neworder;
$data['order_info'] = null;
if ($neworder !== null) {
$neworder_id = $this->analysis_orderid($neworder);
$neworder_id = json_decode($neworder_id);
if ( ! empty($neworder_id)) {
$data['order_info'] = $this->Paypal_model->get_order($neworder_id->orderid, true, $neworder_id->ordertype);
}
}
echo json_encode($this->load->view('gai_setting', $data, true));
}
public function gai_modal_save()
{
$data = array();
$pn_txn_id = $this->input->post('pn_txn_id');
$pn_id = $this->input->post('pn_id');
$neworder = $this->input->post('pn_invoice');
$data['note'] = $this->Note_model->note($pn_txn_id, $pn_id);
$orderid_info = $this->analysis_orderid($data['note']->pn_invoice);
if (!empty($orderid_info)) {
$orderid_info = json_decode($orderid_info);
if ($orderid_info->ordertype === 'T') {
$data['gai_info'] = $this->Paypal_model->get_money_t($pn_txn_id);
$this->Paypal_model->delete_money_t($pn_txn_id);
} elseif ($orderid_info->ordertype === 'B') {
$data['gai_info'] = $this->Paypal_model->get_money_b($pn_txn_id);
$this->Paypal_model->delete_money_b($pn_txn_id);
}
}
if (!empty($pn_txn_id) && !empty($neworder)) {
$orderid_info = $this->analysis_orderid($neworder);
if (!empty($orderid_info)) {
$orderid_info = json_decode($orderid_info);
$advisor_info = $this->Paypal_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype);
if (!empty($advisor_info)) {
$this->Note_model->set_invoice($pn_txn_id, $neworder);
$this->send_note($pn_txn_id);
echo json_encode('修改成功!');
return true;
}
}
}
echo json_encode('没找到数据!');
return;
}
public function closeGai($pn_txn_id)
{
$data = array();
$data['note'] = $this->Note_model->note($pn_txn_id);
if (!empty($data['note'])) {
$this->Note_model->update_send($pn_txn_id, 'closeRecord');
echo json_encode('该收款记录已经忽略!');
return true;
}
echo json_encode('没找到数据!');
return;
}
}