load->library('alipay/AopSdk'); // real URL $this->config->load('alipay'); // test URL // $this->config->load('dev_alipay'); $this->load->model('AlipayTradePagePayContentBuilder'); $this->load->model('AlipayTradeWapPayContentBuilder'); $this->load->model('AlipayTradeQueryContentBuilder'); $this->load->model('Alipay_note_model'); $this->load->model('Alipay_model'); $this->load->helper('payment'); $this->gateway_url = $this->config->item('gatewayUrl'); $this->appid = $this->config->item('app_id'); $this->seller_id = $this->config->item('seller_id'); $this->private_key = $this->config->item('merchant_private_key'); $this->alipay_public_key = $this->config->item('alipay_public_key'); $this->charset = $this->config->item('charset'); $this->signtype = $this->config->item('sign_type'); $this->timeout_express = $this->config->item('timeout_express'); $this->return_url = $this->config->item('return_url'); $this->notify_url = $this->config->item('notify_url'); if(empty($this->appid)||trim($this->appid)==""){ log_message('error','Alipay ERROR appid should not be NULL!'); } if(empty($this->private_key)||trim($this->private_key)==""){ log_message('error','Alipay ERROR private_key should not be NULL!'); } if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){ log_message('error','Alipay ERROR alipay_public_key should not be NULL!'); } if(empty($this->charset)||trim($this->charset)==""){ log_message('error','Alipay ERROR charset should not be NULL!'); } if(empty($this->gateway_url)||trim($this->gateway_url)==""){ log_message('error','Alipay ERROR gateway_url should not be NULL!'); } } public function index() { $this->note_list(); } /*! * 异步通知 * 必须返回"success"给支付系统 * @author LYT * @date 2017-09-13 * @return [type] [description] */ public function alipay_notice() { $resp_arr = $this->input->post(); $asyns_resp = $this->check($resp_arr); // 未得到结果 if (empty($asyns_resp->data->out_trade_no)) { echo "failed"; return; } if (true === $this->if_note_exists($asyns_resp->data->trade_no)) { echo "success"; return; } $code = isset($asyns_resp->data->code) ? strval($asyns_resp->data->code) : NULL ; $buyer = isset($asyns_resp->data->buyer_logon_id) ? strval($asyns_resp->data->buyer_logon_id) : NULL ; if (strcmp(strval($asyns_resp->data->trade_status), "TRADE_SUCCESS") == 0) { $this->Alipay_note_model->save_alipay( strval($asyns_resp->data->trade_no) ,strval($asyns_resp->data->out_trade_no) ,"CNY" ,strval($asyns_resp->data->total_amount) ,NULL ,NULL ,strval($asyns_resp->data->gmt_create) ,strval($asyns_resp->data->gmt_payment) ,json_encode($asyns_resp->data) ,strval("pay") ,$code ,strval($asyns_resp->data->trade_status) ,NULL ,$buyer ); // 查询payer // $this->AlipayTradeQueryContentBuilder->setTradeNo($asyns_resp->data->trade_no); // if ($asyns_resp->data->out_trade_no) { // $this->AlipayTradeQueryContentBuilder->setOutTradeNo($asyns_resp->data->out_trade_no); // } // $response = $this->Query($this->AlipayTradeQueryContentBuilder); // $resp_arr = (Array) $response; // $query_resp = $this->check($resp_arr); // if (strcmp(strval($response->trade_status), "TRADE_SUCCESS") == 0) { // $this->Alipay_note_model->update_query($response->trade_no,$response->buyer_logon_id); // } } // 返回状态码200 echo "success"; return; } public function if_note_exists($dealId) { return $this->Alipay_note_model->note_exists($dealId) ? true : false; } public function request_faild() { $data["code"] = 1; $data["msg"] = "Unknow request"; $data["error_code"] = "Unknow"; $url_query = ""; foreach ($data as $key => $v) { $url_query .= $key . "=" . urlencode($v) ."&"; } redirect("https://www.chinahighlights.com/secureipay/load_return" . "?" . substr($url_query, 0, -1)); } public function get_url_string() { $param = ($this->input->get_post('param')); return base64_decode($param); } // cht public function pay_fun() { $param_string = $this->get_url_string(); @parse_str($param_string, $param_arr); if ( ! $param_string || empty($param_arr['order_id'])) { $this->request_faild(); return false; } //商户订单号,商户网站订单系统中唯一订单号,必填 $out_trade_no = trim($param_arr['order_id']); //订单名称,必填 $subject = trim($param_arr['subject']); //付款金额,必填 $total_amount = trim($param_arr['total_amount']); //商品描述,可空 $body = str_replace("\n", "
", trim($param_arr['body'])); $post_return = $param_arr["return_url"] ? trim($param_arr["return_url"]) : false; $post_notify = $param_arr["notify_url"] ? trim($param_arr["notify_url"]) : false; $this->return_url = !empty($post_return) ? ($post_return) : $this->return_url; $this->notify_url = !empty($post_notify) ? ($post_notify) : $this->notify_url; $detect = new Mobile_Detect; if ($detect->isMobile() && !$detect->isTablet()) { // mobile page $this->AlipayTradeWapPayContentBuilder->setBody($body); $this->AlipayTradeWapPayContentBuilder->setSubject($subject); $this->AlipayTradeWapPayContentBuilder->setOutTradeNo($out_trade_no); $this->AlipayTradeWapPayContentBuilder->setTotalAmount($total_amount); $this->AlipayTradeWapPayContentBuilder->setTimeExpress($timeout_express); $result = $this->wapPay( $this->AlipayTradeWapPayContentBuilder, $this->return_url, $this->notify_url ); } else { // PC page $this->AlipayTradePagePayContentBuilder->setBody($body); $this->AlipayTradePagePayContentBuilder->setSubject($subject); $this->AlipayTradePagePayContentBuilder->setTotalAmount($total_amount); $this->AlipayTradePagePayContentBuilder->setOutTradeNo($out_trade_no); $url = $this->pagePay( $this->AlipayTradePagePayContentBuilder, $this->return_url, $this->notify_url ); } return; } /** * alipay.trade.page.pay * @param $builder 业务参数,使用buildmodel中的对象生成。 * @param $return_url 同步跳转地址,公网可以访问 * @param $notify_url 异步通知地址,公网可以访问 * @return $response 支付宝返回的信息 */ function pagePay($builder,$return_url,$notify_url) { $biz_content=$builder->getBizContent(); $request = new AlipayTradePagePayRequest(); $request->setNotifyUrl($notify_url); $request->setReturnUrl($return_url); $request->setBizContent ( $biz_content ); // 首先调用支付api $response = $this->aopclientRequestExecute ($request,true); return $response; } /** * alipay.trade.wap.pay * @param $builder 业务参数,使用buildmodel中的对象生成。 * @param $return_url 同步跳转地址,公网可访问 * @param $notify_url 异步通知地址,公网可以访问 * @return $response 支付宝返回的信息 */ function wapPay($builder,$return_url,$notify_url) { $biz_content=$builder->getBizContent(); $request = new AlipayTradeWapPayRequest(); $request->setNotifyUrl($notify_url); $request->setReturnUrl($return_url); $request->setBizContent ( $biz_content ); // 首先调用支付api $response = $this->aopclientRequestExecute ($request,true); // $response = $response->alipay_trade_wap_pay_response; return $response; } /** * sdkClient * @param $request 接口请求参数对象。 * @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。 * @return $response 支付宝返回的信息 */ function aopclientRequestExecute($request,$ispage=false) { $aop = new AopClient (); $aop->gatewayUrl = $this->gateway_url; $aop->appId = $this->appid; $aop->rsaPrivateKey = $this->private_key; $aop->alipayrsaPublicKey = $this->alipay_public_key; $aop->apiVersion = "1.0"; $aop->postCharset = $this->charset; $aop->format = $this->format; $aop->signType = $this->signtype; // 开启页面信息输出 $aop->debugInfo = true; $result = null; if($ispage) { $result = $aop->pageExecute($request,"post"); echo $result; } else { $result = $aop->Execute($request); } return $result; } /*! * 定时执行 * 处理异步通知: 解析订单号, 邮件外联/客人; 插入付款记录 * @author LYT */ public function send_alipay($pn_txn_id = false) { $data = array(); $int = 0; //优先处理指定的交易号,用于修正交易号直接发送通知 if ( ! empty($pn_txn_id)) { $data['unsend_list'] = array($this->Alipay_note_model->note($pn_txn_id)); } // 待处理的 if (empty($data['unsend_list'])) { $data['unsend_list'] = $this->Alipay_note_model->unsend(10); } //没有未处理的数据再查找处理失败的数据 if (empty($data['unsend_list'])) { $data['unsend_list'] = $this->Alipay_note_model->failnote(20); } $show_index = 0; foreach ($data['unsend_list'] as $item) { //已经发送的不处理,防止重复发送 if ($item->ALI_sent == 'send') { continue; } //退款状态默认为已经处理,陆燕在退款前手动通知外联了,系统跳过处理 if ($item->ALI_payType == 'Refunded') { $this->Alipay_note_model->update_send($item->ALI_dealId, 'send'); continue; } //只处理完成状态,其他状态由陆燕处理 if (strcmp(trim($item->ALI_resultMsg), "TRADE_SUCCESS")) { $this->Alipay_note_model->update_send($item->ALI_dealId, 'send'); continue; } //检测是否是APP订单,默认不处理 // if ((strpos($item->pn_memo, 'China Train Booking') !== false) || (strpos($item->pn_memo, 'ChinaTrainBooking') !== false)) { //APP自动出票的订单不需要处理 // $this->Alipay_note_model->update_send($item->ALI_dealId, 'send'); // continue; // } //根据note信息找到订单号 $orderid_info = analysis_orderid($item->ALI_orderId); //找不到订单号,设置为发送失败标示 if (empty($orderid_info)) { $this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail'); continue; } //根据订单号查找外联信息 $orderid_info = json_decode($orderid_info); $advisor_info = $this->Alipay_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype); //查不到订单信息 if (empty($advisor_info)) { $this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail'); continue; } //更新正确的订单信息到记录中,以这个为主 $this->Alipay_note_model->set_invoice($item->ALI_dealId, $orderid_info->orderid . '_' . $orderid_info->ordertype); //检测是否是APP订单,默认不处理 if ($orderid_info->ordertype == 'A') { //APP自动出票的订单不需要处理 $this->Alipay_note_model->update_send($item->ALI_dealId, 'send'); continue; } //添加支付信息入库 //没有分配订单之前先添加付款记录,这个过程可能会执行多次,必须在添加记录前查找是否有数据 if (!empty($orderid_info)) { $currencyCode = str_replace("CNY", "RMB", trim(mb_strtoupper($item->ALI_currencyCode))); $USD_amount = $this->Alipay_model->get_USD($item->ALI_orderAmount, $currencyCode); //更新还没有填的客邮和交易号de收款记录(商务订单) if (isset($advisor_info->order_type) && $advisor_info->order_type == 0) { $ht_memo = '交易号(自动录入):' . $item->ALI_dealId; $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订单 // $this->Alipay_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), $item->pn_payment_date, $item->pn_payment_date, $item->pn_payment_date, '', $item->pn_payer_email, $item->ALI_dealId, $ht_memo); // if ($advisor_info->COLI_WebCode == 'CHTAPP' && $advisor_info->COLI_State == 11) { //只修改APP组的订单状态,并且订单进度是我的订单 // $this->Alipay_model->update_biz_coli_state($GAI_COLI_SN, 8); //把订单状态改为已付款 // } } else { $this->Alipay_model->add_account_info( $GAI_COLI_SN, $advisor_info->COLI_ID, $item->ALI_orderAmount, $item->ALI_completeTime, $currencyCode, $USD_amount, $item->ALI_completeTime, $item->ALI_completeTime, $item->ALI_acquiringTime, $item->ALI_payerName, $item->ALI_payerEmail, $item->ALI_dealId, $ht_memo ); if ($advisor_info->COLI_Department == 10) { // 更新订单主表付款方式,防止没访问thankyou-train.asp $this->Alipay_model->update_paymanner($GAI_COLI_SN); // 把订单状态设置为13-新订单已支付 $this->Alipay_model->update_biz_coli_state($GAI_COLI_SN, 13); } } } //更新还没有填的客邮和交易号de收款记录(传统订单) elseif (isset($advisor_info->order_type) && $advisor_info->order_type == 1) { $ht_memo = '交易号(自动录入):' . $item->ALI_dealId; $GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0; $this->Alipay_model->add_tour_account_info( $GAI_COLI_SN, $item->ALI_orderAmount, $item->ALI_acquiringTime, $currencyCode, $item->ALI_completeTime, $item->ALI_completeTime, $item->ALI_acquiringTime, $item->ALI_payerName, $item->ALI_payerEmail, $item->ALI_dealId, $ht_memo ); //添加汉特的订单提醒 $this->Alipay_model->update_coli_introduction($GAI_COLI_SN, '已支付 ' . mb_strtoupper($item->ALI_currencyCode) . $item->ALI_orderAmount); } } $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->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail'); continue; } //添加邮件发送记录 //给外联发送通知邮件 $fromName = 'Alipay'; $fromEmail = ''; $toName = !empty($opi_firstname) ? $opi_firstname : ''; $toEmail = !empty($opi_email) ? $opi_email : ''; $subject = $orderid_info->orderid . '_' . $orderid_info->ordertype . ' / ' . $item->ALI_orderAmount . $item->ALI_currencyCode . ' / ' . $fromName; $body = $this->load->view('alipay_receipt_mail', $item, true); $M_RelatedInfo = $item->ALI_sn; $M_AddTime = $item->ALI_completeTime; $M_State = 0; $this->Alipay_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime, 'Alipay note'); //添加邮件发送记录 end $this->Alipay_note_model->update_send($item->ALI_dealId, 'send'); $int++; } // 批量结果 if (empty($pn_txn_id)) { echo "done. recorde count:".$int; } return; } /** * alipay.trade.query (统一收单线下交易查询) * @param $builder 业务参数,使用buildmodel中的对象生成。 * @return Array object $response 支付宝返回的信息 */ function Query($builder){ $biz_content=$builder->getBizContent(); $request = new AlipayTradeQueryRequest(); $request->setBizContent ( $biz_content ); $response = $this->aopclientRequestExecute ($request,true); $response = $response->alipay_trade_query_response; return $response; } public function query_pay($dealId,$orderId=NULL) { $this->AlipayTradeQueryContentBuilder->setTradeNo($dealId); if ($orderId) { $this->AlipayTradeQueryContentBuilder->setOutTradeNo($orderId); } $response = $this->Query($this->AlipayTradeQueryContentBuilder); if ( strcmp(trim($response->code), "10000")) { log_message('error',"Alipay query failed! error code:".$response->code."; result Msg: ".$response->msg.'; orderId:'.$response->out_trade_no.'; dealId:'.$response->trade_no."; "); } $html = ''; foreach ($response as $key => $value) { $html .= ""; } $html .= '
$key$value
'; echo $html; return; } /*! * 对账单 * 流程: * * 获取对账单下载地址 * * 下载对账单 * * 解压,获取明细表 * * 分析明细表,入库 * @author LYT * @date 2017-10-10 * @param [type] $date 按天yyyy-MM-dd;按月yyyy-MM */ public function get_billfile($date=NULL) { $request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); $request->setBizContent("{" . "\"bill_type\":\"trade\"," . "\"bill_date\":\"2017-09\"" . "}"); $response = $this->aopclientRequestExecute ($request); // var_dump($response); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; var_dump($response->$responseNode); // $resultCode = $result->$responseNode->code; // if(!empty($resultCode)&&$resultCode == 10000){ // } } /** * 验签方法 * @param $arr 验签支付宝返回的信息,使用支付宝公钥。 * @return boolean */ function check($arr){ $ret = new ArrayObject(); $ret->check = false; $ret->data = NULL; $ret->data = $arr_obj = (object) $arr; $aop = new AopClient(); $aop->alipayrsaPublicKey = $this->alipay_public_key; $ret->check = $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype); if ($result === false) { log_message('error','Alipay sign ERROR ! orderId:'.$arr_obj->out_trade_no.'; dealId:'.$arr_obj->trade_no . "; Original return:".json_encode($arr)."; "); return $ret; } // cht // 1.app_id if ($arr['app_id'] !== $this->appid) { log_message('error','Alipay ERROR APPID verify failed '.$arr_obj->app_id." !== ".$this->appid."; orderId:".$arr_obj->out_trade_no."; dealId:".$arr_obj->trade_no); return $ret; } // 2.seller_id if ($arr['seller_id'] !== $this->seller_id) { log_message('error','Alipay ERROR SELLERID verify failed '.$arr_obj->seller_id." !== ".$this->seller_id."; orderId:".$arr_obj->out_trade_no."; dealId:".$arr_obj->trade_no); return $ret; } // 3.pay status if (strcmp(strval($arr_obj->trade_status), "TRADE_SUCCESS")) { log_message('error',"Alipay payment failed! error code:".$arr_obj->trade_status."; result Msg: ".$arr_obj->trade_status.'; orderId:'.$arr_obj->out_trade_no.'; dealId:'.$arr_obj->trade_no."; "); return $ret; } $ret->check = true; return $ret; } /** *利用google api生成二维码图片 * $content:二维码内容参数 * $size:生成二维码的尺寸,宽度和高度的值 * $lev:可选参数,纠错等级 * $margin:生成的二维码离边框的距离 */ function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') { $content = urlencode($content); $image = ''; // http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http://www.chinahighlights.com/ return $image; } public function note_list() { $data = array(); $data["paytext"] = $this->payment_status(); $data["keywords"] = $this->input->get_post("keywords"); $data["date"] = $this->input->get_post("date"); empty($data['date']) ? $data['date'] = date('Y-m-d') : false; if (!empty($data['keywords'])) { $data['notelist'] = $this->Alipay_note_model->search_key($data['keywords']); } else { $data['notelist'] = $this->Alipay_note_model->search_date($data['date']); } $this->load->view("alipay_list",$data); return; } //失败记录列表 public function note_faillist() { $data = array(); $data["paytext"] = $this->payment_status(); //有关键词则不限制日期 $data['search_key'] = $this->input->post('keywords'); $data['date'] = $this->input->get_post('date'); empty($data['date']) ? $data['date'] = date('Y-m-d') : false; $data['notelist'] = $this->Alipay_note_model->failnote(100); $this->load->view("alipay_list",$data); } //用于HT--交易详情 public function receipt($pm_transaction_id) { if ( ! $pm_transaction_id) { return false; } $data = array(); $data = $this->Alipay_note_model->note($pm_transaction_id); $this->load->view('alipay_receipt_mail', $data); } //获取note详情,以便后续修改各项数据 public function note_modal($pn_txn_id = false, $pn_invoice = false ,$notice_time = false) { $data = array(); $data['IPL_orderId'] = $pn_invoice; if (!empty($pn_txn_id)) { $data['note'] = $this->Alipay_note_model->note($pn_txn_id); if (!empty($data['note'])) { if (!empty($pn_invoice)) { $orderid_info = analysis_orderid($pn_invoice); } else { $orderid_info = analysis_orderid($data['note']->IPL_orderId); } if (!empty($orderid_info)) { $orderid_info = json_decode($orderid_info); $data['order_info'] = $this->Alipay_model->get_order($orderid_info->orderid, true, $orderid_info->ordertype); } $data["paytext"] = $this->payment_status(); echo json_encode($this->load->view('alipay_note_setting', $data, true)); return true; } } echo json_encode('没找到数据!'); return; } public function note_order_modal($old_order,$pn_invoice = false ,$notice_time = false) { $data = array(); $data['IPL_orderId'] = $pn_invoice; if (!empty($pn_invoice)) { $data['note'] = $this->Alipay_note_model->note_order($old_order,$notice_time); if (!empty($data['note'])) { if (!empty($pn_invoice)) { $orderid_info = analysis_orderid($pn_invoice); } else { $orderid_info = analysis_orderid($data['note']->IPL_orderId); } if (!empty($orderid_info)) { $orderid_info = json_decode($orderid_info); $data['order_info'] = $this->Alipay_model->get_order($orderid_info->orderid, true, $orderid_info->ordertype); } $data["paytext"] = $this->payment_status(); echo json_encode($this->load->view('alipay_note_setting', $data, true)); return true; } } echo json_encode('没找到数据!'); return; } //关闭note通知,用于手动处理通知后 public function close_note($pn_txn_id) { $data = array(); $data['note'] = $this->Alipay_note_model->note($pn_txn_id); if (!empty($data['note'])) { $this->Alipay_note_model->update_send($pn_txn_id, 'send'); echo json_encode('通知已经关闭!'); return true; } echo json_encode('没找到数据!'); return; } //修改订单名 public function note_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 = analysis_orderid($pn_invoice); if (!empty($orderid_info)) { $orderid_info = json_decode($orderid_info); $advisor_info = $this->Alipay_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype); if (!empty($advisor_info)) { $this->Alipay_note_model->set_invoice($pn_txn_id, $pn_invoice); $this->send_alipay($pn_txn_id); echo json_encode('修改成功!'); return true; } } } echo json_encode('没找到数据!'); return; } public function payment_status() { return array( "WAIT_BUYER_PAY" => "Pending", "TRADE_SUCCESS" => "Payment success", "TRADE_FINISHED" => "Payment success" ); } }