1 开发前准备 先和银联手机支付签约,他们会给你测试的证书已经手机客户端的控件
2 将包里的文件(TESTMERCHANT.p12)经过openssl生成的密钥文件TESTMERCHANT.pm 默认密码是 1
3 PHP端程序
下订单程序
/*
* 银联支付
* */
//wap下订单接口(需要验证签名)
public function wapsendAction() {
header("Content-Type:text/html; charset=utf-8");
$request = $this->getRequest();
if(isset($_SESSION['uid'])){
if($request->isPost()){
$uid=intval($_SESSION['uid']);
$money = intval($request->getPost(money));//金额以元为单位
$beans = $money;
$arraymoney=array("1","10","30","100");
if(!in_array($money,$arraymoney)){
echo '1';//金额不对
exit();
}
}else{
echo '2';//不是POST请求
exit();
}
$payaction = new PayAction();
$rid = $payaction->insertRecharge($uid, 36, $money, $beans);
if($rid) {
$orderTime=date('YmdHis'); //交易开始日期时间
//$rid='00000123';//订单
$rid=str_pad($rid,'8','0',STR_PAD_LEFT);
//$money='000000000030';//金额str_pad(string,length,pad_string,pad_type)
$money= str_pad($money*100,'12','0',STR_PAD_LEFT);
//$bz='156';//币种
$terminal='00000001';//pos机
$shanghu='100011000110195';//商户代码
$shanghumc='我在找你';//商户名称
$string=$orderTime.$rid.$money.$terminal.$shanghu.$shanghumc;
* 银联支付
* */
//wap下订单接口(需要验证签名)
public function wapsendAction() {
header("Content-Type:text/html; charset=utf-8");
$request = $this->getRequest();
if(isset($_SESSION['uid'])){
if($request->isPost()){
$uid=intval($_SESSION['uid']);
$money = intval($request->getPost(money));//金额以元为单位
$beans = $money;
$arraymoney=array("1","10","30","100");
if(!in_array($money,$arraymoney)){
echo '1';//金额不对
exit();
}
}else{
echo '2';//不是POST请求
exit();
}
$payaction = new PayAction();
$rid = $payaction->insertRecharge($uid, 36, $money, $beans);
if($rid) {
$orderTime=date('YmdHis'); //交易开始日期时间
//$rid='00000123';//订单
$rid=str_pad($rid,'8','0',STR_PAD_LEFT);
//$money='000000000030';//金额str_pad(string,length,pad_string,pad_type)
$money= str_pad($money*100,'12','0',STR_PAD_LEFT);
//$bz='156';//币种
$terminal='00000001';//pos机
$shanghu='100011000110195';//商户代码
$shanghumc='我在找你';//商户名称
$string=$orderTime.$rid.$money.$terminal.$shanghu.$shanghumc;
$fp = fopen(ROOT_ACTION.'quickpay/TESTMERCHANT.pem', "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
openssl_sign($string, $signature, $pkeyid);
$len = strlen($signature);
$hexArray = "0123456789abcdef";
$str = $signature;
$result = "";
for($i=0;$i<$len;$i++) {
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
$byte = $byte & 0xff;
$result .= $hexArray[$byte >> 4];
$result .= $hexArray[$byte & 0xf];
}
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><cupMobile version="1.01" application="All"><transaction type="Purchase.MARsp"><submitTime>'.$orderTime.'</submitTime><order id="'.$rid.'">'.$beans.'块钱订单</order><transAmount>'.$money.'</transAmount><terminal id="'.$terminal.'"/><merchant name="我在找你" id="'.$shanghu.'"/></transaction><senderSignature>'.$result.'</senderSignature></cupMobile>';
}else{
$payaction = new PayAction();
$str = '银联wap支付下订单失败uid='.$uid.'money='.$money.'rid='.$rid;
$payaction->Insert_errorpay($str,1,0);//1为银联1 为下订单失败
echo '3';//订单验证签名失败
exit;
}
}else{
echo '4';//登录后再下订单
exit();
}
}
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
openssl_sign($string, $signature, $pkeyid);
$len = strlen($signature);
$hexArray = "0123456789abcdef";
$str = $signature;
$result = "";
for($i=0;$i<$len;$i++) {
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
$byte = $byte & 0xff;
$result .= $hexArray[$byte >> 4];
$result .= $hexArray[$byte & 0xf];
}
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><cupMobile version="1.01" application="All"><transaction type="Purchase.MARsp"><submitTime>'.$orderTime.'</submitTime><order id="'.$rid.'">'.$beans.'块钱订单</order><transAmount>'.$money.'</transAmount><terminal id="'.$terminal.'"/><merchant name="我在找你" id="'.$shanghu.'"/></transaction><senderSignature>'.$result.'</senderSignature></cupMobile>';
}else{
$payaction = new PayAction();
$str = '银联wap支付下订单失败uid='.$uid.'money='.$money.'rid='.$rid;
$payaction->Insert_errorpay($str,1,0);//1为银联1 为下订单失败
echo '3';//订单验证签名失败
exit;
}
}else{
echo '4';//登录后再下订单
exit();
}
}
现在再提供一个回调的函数就可以了。
public function wapback_notifyAction() {
$request = $this->getRequest();
if($request->isPost()){
$arr_ret = file_get_contents("php://input");
$payaction = new PayAction();
$xml = new SimpleXMLElement($arr_ret);
$orderid=$this->getAttr($arr_ret,'order') ;//获取订单id
$submitTime=$this->getDataForXML($arr_ret,'/cupMobile/transaction/submitTime');//获取交易时间
$moeney=$this->getDataForXML($arr_ret,'/cupMobile/transaction/billAmount');//获取金额
$accountNumber1=$this->getDataForXML($arr_ret,'/cupMobile/transaction/accountNumber1');//获取支付卡
$settleDate=date('Ymd'); //交易开始日期时间
$payaction->insertPayQQ(1,1,$arr_ret.'orderid'.$orderid['id']);
if($orderid['id']>0){
$orderNumbe=substr($orderid['id'],'2');
$payaction->updateRechargeStatus($orderNumbe);
echo '<?xml version="1.0" encoding="UTF-8"?><cupMobile application="UPNoCards" version="1.01"><transaction type="PurchaseAdvice.MPRsp"><submitTime>'.$submitTime.'</submitTime><order id="'.$orderid['id'].'"><generateTime>'.$submitTime.'</generateTime></order><transAmount currency="156">'.$moeney.'</transAmount><terminal id="000001"/><merchant id="100011000110195"/><accountNumber1>'.$accountNumber1.'</accountNumber1><responseCode>000</responseCode><transSerialNumber>000001</transSerialNumber><billAmount currency="156">'.$moeney.'</billAmount><settleDate>'.$settleDate.'</settleDate></transaction></cupMobile>';
}
}
}
$request = $this->getRequest();
if($request->isPost()){
$arr_ret = file_get_contents("php://input");
$payaction = new PayAction();
$xml = new SimpleXMLElement($arr_ret);
$orderid=$this->getAttr($arr_ret,'order') ;//获取订单id
$submitTime=$this->getDataForXML($arr_ret,'/cupMobile/transaction/submitTime');//获取交易时间
$moeney=$this->getDataForXML($arr_ret,'/cupMobile/transaction/billAmount');//获取金额
$accountNumber1=$this->getDataForXML($arr_ret,'/cupMobile/transaction/accountNumber1');//获取支付卡
$settleDate=date('Ymd'); //交易开始日期时间
$payaction->insertPayQQ(1,1,$arr_ret.'orderid'.$orderid['id']);
if($orderid['id']>0){
$orderNumbe=substr($orderid['id'],'2');
$payaction->updateRechargeStatus($orderNumbe);
echo '<?xml version="1.0" encoding="UTF-8"?><cupMobile application="UPNoCards" version="1.01"><transaction type="PurchaseAdvice.MPRsp"><submitTime>'.$submitTime.'</submitTime><order id="'.$orderid['id'].'"><generateTime>'.$submitTime.'</generateTime></order><transAmount currency="156">'.$moeney.'</transAmount><terminal id="000001"/><merchant id="100011000110195"/><accountNumber1>'.$accountNumber1.'</accountNumber1><responseCode>000</responseCode><transSerialNumber>000001</transSerialNumber><billAmount currency="156">'.$moeney.'</billAmount><settleDate>'.$settleDate.'</settleDate></transaction></cupMobile>';
}
}
}
这样就好了 另外附带2个xml操作函数
//xml获取属性值
function getAttr($str , $tag) {
function getAttr($str , $tag) {
preg_match_all("/(?:<${tag})([^>]*)>/im" , $str , $ary);
if(is_array($ary[1])) {
preg_match_all('/([a-zA_Z0-9]+?)\s*?(?:=)\s*?(?:[\'"]?)([a-zA_Z0-9_\-\.]*?)(?:[\'"])/im' , $ary[1][0] , $a);
for($i = 0 , $j = count($a[1]) ; $i < $j ; ++$i) {
$v[$a[1][$i]] = $a[2][$i];
}
$v[$a[1][$i]] = $a[2][$i];
}
return $v;
}
}
//xml获取指定节点的元素值
function getDataForXML($res_data,$node)
{
$xml = simplexml_load_string($res_data);
$result = $xml->xpath($node);
}
}
//xml获取指定节点的元素值
function getDataForXML($res_data,$node)
{
$xml = simplexml_load_string($res_data);
$result = $xml->xpath($node);
while(list( , $node) = each($result))
{
return $node;
}
}
{
return $node;
}
}
转载于:https://blog.51cto.com/tanglulu/1093098