$config['interfaceVersion'] = "1.0.0.0"; #接口版本
$config['tranType'] =0; #交易类别 0:B:C
$config['curType'] = 'CNY'; # 交易币种
$config['notifyType'] =1; #0=不通知 1=通知 2=抓取
$config['merURL'] = "http://www.111com.net /pay/notify"; # 主动通知url
$config['goodsURL' = 'http://www.111com.net /goods/1.html'; #取货url
$config['jumpSeconds'] =3; #跳转时间
$config['payBatchNo'] = ''; #商户批次号
$config['proxyMerName'] = ''; #代理商家名字
$config['proxyMerType'] = ''; #代理商类型
$config['proxyMerCredentials']= ''; #代理商家批次号
$config['netType'] = 0; #渠道编号
//以下是 新接口需要的参数
$config['socketUrl'] ="tcp://127.0.0.1:8891"; #socket url
$config['merID'] ='301310063009501'; #商户id 3013100630095012
2.2 Model
/**
* 交通银行支付类
*/
class Bocom extends CI_Model {
private $arrReturn=array();
private $socket;
public function __construct() {
parent::__construct ();
//加载交通银行配置文件
$this->config->load('bocom');
$this->socket=$this->config->item('socketUrl');
}
/**
* 支付方法
*
* @param unknown $arr_data=array(
* 'bill_no'=>
* )
*/
public function pay($arr_data){
//获得表单传过来的数据
$this->arrReturn['interfaceVersion'] = $this->config->item('interfaceVersion');
$this->arrReturn['merID'] = $this->config->item('merID'); //商户号为固定
$this->arrReturn['orderid'] = $arr_data['bill_no'];
$this->arrReturn['orderDate'] = $arr_data['bill_date'];
$this->arrReturn['orderTime'] = $arr_data['bill_time'];
$this->arrReturn['tranType'] = $this->config->item('tranType');
$this->arrReturn['amount'] = $arr_data['bill_fee'];
$this->arrReturn['curType'] = $this->config->item('curType');
$this->arrReturn['orderContent'] = isset($arr_data['bill_title'])?iconv('utf-8','gb2312',$arr_data["bill_title"]): ''; #订单内容
$this->arrReturn['orderMono'] = isset($arr_data['bill_mono'])? iconv('utf-8','gb2312',$arr_data['bill_mono']):''; #商家备注
$this->arrReturn['phdFlag'] = isset($arr_data['phpFlag'])?$arr_data['phpFlag']:'';
$this->arrReturn['notifyType'] = $this->config->item('notifyType');
$this->arrReturn['merURL'] = $this->config->item('merURL');
$this->arrReturn['goodsURL'] = $this->config->item('goodsURL');
$this->arrReturn['jumpSeconds'] = $this->config->item('jumpSeconds');
$this->arrReturn['payBatchNo'] = $this->config->item('payBatchNo');
$this->arrReturn['proxyMerName'] = $this->config->item('proxyMerName');
$this->arrReturn['proxyMerType'] = $this->config->item('proxyMerType');
$this->arrReturn['proxyMerCredentials']= $this->config->item('proxyMerCredentials');
$this->arrReturn['netType'] = $this->config->item('netType');
//以下参数 不参与签名
$this->arrReturn['issBankNo'] =isset($arr_data['code_id'])? trim($arr_data['code_id']):'';
$tranCode = "cb2200_sign";
$source='';
$len = count($this->arrReturn)-1;$j=1;
foreach($this->arrReturn as $v){
if($j<=$len){
$source.=$v."|";
}
$j++;
}
$source= substr($source, 0,strlen($source)-1);
$fp= stream_socket_client($this->socket,$errno, $errstr, 30);
$retMsg="";
if (!$fp) {
log_message("info","socket连接失败");
return false;
} else
{
$in = "";
$in .= "";
$in .= "".$tranCode."";
$in .= "".$source."";
$in .= "";
fwrite($fp, $in);
while (!feof($fp)) {
$retMsg =$retMsg.fgets($fp, 1024);
}
fclose($fp);
}
if(false!==$xml_arr=$this->xmlParse($retMsg)){
if(is_array($xml_arr)){
foreach($xml_arr as $k=>$v){
$this->arrReturn[$k]=$v;
}
}else{
return false;
}
}else{
return false;
}
return $this->arrReturn;
}
/**
* 解析XML
*/
public function xmlParse($retMsg){
$arr=array();
//解析返回xml
$dom = new DOMDocument;
$dom->loadXML($retMsg);
$retCode = $dom->getElementsByTagName('retCode');
$retCode_value = $retCode->item(0)->nodeValue;
$errMsg = $dom->getElementsByTagName('errMsg');
$errMsg_value = $errMsg->item(0)->nodeValue;
$signMsg = $dom->getElementsByTagName('signMsg');
$signMsg_value = $signMsg->item(0)->nodeValue;
$orderUrl = $dom->getElementsByTagName('orderUrl');
$orderUrl_value = $orderUrl->item(0)->nodeValue;
$MerchID = $dom->getElementsByTagName('MerchID');
$merID = $MerchID->item(0)->nodeValue;
if($retCode_value != "0"){
log_message("info","交易返回码:".$retCode_value);
log_message("info","交易错误信息:".$errMsg_value);
return false;
}
$arr['merSignMsg'] = $signMsg_value;
$arr['merID'] = $merID;
$arr['orderUrl'] = $orderUrl_value;
return $arr;
}
/**
* 交通银行 支付通知
* @return boolean|unknown
*/
public function notify(){
$tranCode = "cb2200_verify";
if(!isset($_REQUEST['notifyMsg'])){
log_message("error","网银支付通知·非法请求");
return false;
}
$notifyMsg = $_REQUEST["notifyMsg"];
log_message("error",$notifyMsg."回调....");
$lastIndex = strripos($notifyMsg,"|");
$signMsg = substr($notifyMsg,$lastIndex+1); //签名信息
$srcMsg = substr($notifyMsg,0,$lastIndex+1);//原文
$merID = $this->config->item('merID');
$fp = stream_socket_client($this->socket, $errno, $errstr, 30);
$retMsg="";
//
if (!$fp) {
echo "$errstr ($errno) \n";
log_message("error","$errstr ($errno) \n");
} else{
$in = "";
$in .= "";
$in .= "".$tranCode."";
$in .= "".$merID."";
$in .= "".$notifyMsg."";
$in .= "";
fwrite($fp, $in);
while (!feof($fp)) {
$retMsg =$retMsg.fgets($fp, 1024);
}
fclose($fp);
}
//解析返回xml
$dom = new DOMDocument;
$dom->loadXML($retMsg);
$retCode = $dom->getElementsByTagName('retCode');
$retCode_value = $retCode->item(0)->nodeValue;
$errMsg = $dom->getElementsByTagName('errMsg');
$errMsg_value = $errMsg->item(0)->nodeValue;
if($retCode_value != '0')
{
log_message("error","交易错误信息:" .$errMsg_value." ");
return false;
}else{
$arr = preg_split("/\|{1,}/",$srcMsg);
if($arr[9]=="1"){
return $this->updateBill($arr[1]);
}
log_message("error","交易失败:".$arr[13]." ");
return false;
}
}
private function updateBill($billNo){
// 更新 订单状态
}
//end class
}
2.3 控制器
$this->load->model("Bocom");
支付方法:
$this->arrData =$this->Bocom->pay($this->data);
通知:
$this->arrData =$this->Bocom->notify();
|