php 广发银行 聚合支付接口--微信支付
时间 17 : 00 , May 09,2020
pay.php
<?php
/*
*广发银行支付测试
*/
//加载phpcrypto.so
if(!extension_loaded('phpcrypto')) {
dl('phpcrypto.so');
}
$module = 'phpcrypto';
$functions = get_extension_funcs($module);
//引用动态库
$php_func = 'php_crypto_init';
$path = "/wwwdata/wwwroot/xxxxxxxxxxxx.com/src_crypt/libcryptAPIsm_lnx64.so";//国密动态库路径
$recode = $php_func($path);
if($recode!=0){
echo "加载失败";exit;
}
//公钥
$pubkey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//私钥
$privkey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
/********************报文 header**********************/
$header['appId']="01202003xxxxxxxxxxxxxx"; //商户应用编号
$header['encryptType']="SM2"; //加密类型
$header['instId']="012020033xxxxxxxxx"; //商户号
$header['productCode']="payService"; //产品编号
$header['requestTime']=date("Ymdhis"); //报文发起时间
$header['senderSN']=getRandCode(); //请求流水号
$header['tradeCode']="unifiedorderJH"; //接口编号
$header['version']="1.0.0"; //版本号
/********************生成订单 报文body**********************/
$body['service']="gcb.pay.wxPayAndXcx.order.unifiedorder"; //接口类型
$body['is_raw']=1; //原生JS 为1
$body['is_minipg']=0; //是否小程序支付,1为小程序 0为公众号
$body['mch_id']="3067xxxxxxxxxxxx"; //商户号
$body['sub_is_subscribe']=0; //是否要关注公众号
$body['out_trade_no']="test".date("YmdHis")."r".rand(10000,99999); //订单号
$body['body']="testpay"; //商品描述
$body['sub_openid']="xxxxxxxxxxxxxxxxxxxxxk"; //用户openid
$body['sub_appid']="xxxxxxxxxxxxxxxxxxxxx9e"; //公众号appid
$body['attach']="name-test123|phone-13000000000"; //附加信息
$body['total_fee']="1"; //总金额
$body['spbill_create_ip']=getip(); //APP 和网页支付提交用户端 ip,Native 支付填调用微信支付 API 的机器 IP。
$body['device_info']="web"; //设备号,PC网页或公众号内支付请传"WEB"
$body['notify_url']="http://xxxxxxxxxxxxxxxxx.com/pay_test/notify.php"; //通知地址
$body['nonce_str']=getRandCode(); //随机字符串
$body['mch_create_ip']="139.9.xxx.xxx"; //商户真实的发起交易的终端出网IP
$sign=md5(ToUrlParams($body)); //签名
$body['sign']=$sign;
$data['Header']=$header;
$data['Body']=$body;
php_HextoAsc($pubkey,$dataAsc);
$headers['certId']=strtoupper(MD5($dataAsc));
$headers['Content-Type']="application/json;charset=UTF-8"; //报文类型
$headers['signType']="SM2"; //签名方法(SM2)
//签名步骤一:按字典序排序参数
$json=decodeUnicode(json_encode($data));
php_HextoAsc($privkey,$k1);
php_HextoAsc($pubkey,$k2);
//生成签名
$recode = php_SM2Sign($json, $redata,$k1,$k2);
$headers['signature']=base64_encode($redata);
//encryptkey
//广发的公钥 注意和个人公钥不同,是广发单独发来的公钥
$gf_pubkey="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
php_HextoAsc($gf_pubkey,$gf_k);
$mw_key="1234567890123456";
php_SM2Encrypt($mw_key,$encryptkey,$gf_k); //加密
$headers['encryptKey']=base64_encode($encryptkey);
$head=array();
$head[]="certId:".$headers['certId'];
$head[]="Content-Type:application/json;charset=UTF-8";
$head[]="signType:SM2";
$head[]="signature:".$headers['signature'];
$head[]="encryptType:SM4";
$head[]="encryptKey:".$headers['encryptKey'];
/*--------------------------发送post申请--------------------------*/
$url="http://218.13.4.200:18090/gateway/API/payService/unifiedorderJH/1.0.0";
var_dump($head);
echo "-----报头-----<hr/>";
echo "<hr/>";
var_dump($json);
echo "-----报文-----<hr/>";
echo "<hr/>";
//加密应答报文直接输出
$key_en="";
$en_str="";
php_CryptKey(0,$mw_key,$key_en);
/*echo "-----php_CryptKey-----<hr/>";
echo "--hex--<hr/>";*/
//SM4加密
php_SM4CBCCrypt(0,$json,$back_json,$key_en,$mw_key);
$back_json=base64_encode($back_json);
var_dump($back_json);
echo "-----加密后-----<hr/>";
echo "<hr/>";
php_SM4CBCCrypt(1,base64_decode($back_json),$jm,$key_en,$mw_key);
var_dump($jm);
echo "-----解密后-----<hr/>";
echo "<hr/>";
//报文长度
$headers['Content-Length']=strlen($back_json); //报文长度
$head[]="Content-Length:".$headers['Content-Length'];
$res=curl_get_contents($url,1,$back_json,$head); //发送请求
$back=explode("\r\n",$res);
var_dump($back);
echo "-----返回报头和报文-----<hr/>";
//获取头部信息
foreach($back as $fk=>$fv){
if(strstr($fv,"certId")!==false){
$back_certId=explode(":",$fv);
$back_certId=trim($back_certId[1]);
}
if(strstr($fv,"signature")!==false){
$back_signature=explode(":",$fv);
$back_signature=trim($back_signature[1]);
}
if(strstr($fv,"encryptKey")!==false){
$back_encryptKey=explode(":",$fv);
$back_encryptKey=trim($back_encryptKey[1]);
}
}
var_dump($back_certId);
echo "---返回back_certId---<hr/>";
var_dump($back_signature);
echo "---返回signature---<hr/>";
var_dump($back_encryptKey);
//返回报文
echo "---返回encryptKey---<hr/>";
$back_body=trim(end($back));
var_dump($back_body);
echo "---返回报文---<hr/>";
//转DER编码
php_SM2FormatConvert(101,base64_decode($back_encryptKey),$c1c3c2);
//私钥解密
php_HextoAsc($privkey,$k1);
php_SM2Decrypt($c1c3c2, $plain,$k1);
php_CryptKey(0, $plain,$key);
php_SM4CBCCrypt(1,base64_decode($back_body),$dedata,$key,$plain);
var_dump($dedata);
echo "---解密后---<hr/>";
exit;
/*----------------------以下为公用函数------------------------*/
function string2hex($string){
$hex = '';
for($i = 0;$i < strlen($string); $i++){
$ch = dechex(ord($string[$i]));
if(strlen($ch)==1){
$ch = "0".$ch;
}
$hex .=$ch;
}
return $hex;
}
function hex2string($hex){
//echo "$hex \n";
$string = '';
for($i = 0;$i < strlen($hex);$i++){
$string .= chr(hexdec($hex[$i].$hex[$i++]));
}
return $string;
}
/**
* 格式化参数格式化成url参数
*/
function ToUrlParams($arr)
{
$buff = "";
foreach ($arr as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
//获取ip地址
function getip() {
static $realip;
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$realip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('HTTP_CLIENT_IP')) {
$realip = getenv('HTTP_CLIENT_IP');
} else {
$realip = getenv('REMOTE_ADDR');
}
}
return $realip;
}
/**
* 获取随机字符串
* @param int $length 获取的随机字符串长度,默认为16
* @return string $noncestr 随机字符串
*/
function getRandCode($length=16){
$code="qazxswedcrfvbgtyhnmjuiklop0123456789QWERTYUIOPLKMNJHBVGFCXDSZA";
$noncestr='';
$max_index=strlen($code)-1;
for($i=0;$i<$length;$i++){
$index=rand(0,$max_index);
$noncestr.=$code[$index];
}
return $noncestr;
}
/**
* curl_get_contents
* @param mixed $url 请求url
* @param mixed $ispost 是否是post请求
* @param mixed $post_data post请求数据
* @return mixed $output 返回数据
* @author winter
* 2017年6月15日09:48:52
*/
function curl_get_contents($url = '', $ispost = 0, $post_data = array(),$headers)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible;MSIE 5.01;Windows NT 5.0)"); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 1); // 查询显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
if ($ispost)
{
curl_setopt($ch, CURLOPT_POST, $ispost);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$output = curl_exec($ch);
$head=curl_getinfo($ch, CURLINFO_HEADER_OUT);
//echo "<hr/>";
curl_close($ch);
return $output;
}
function curl_send($url, $headers,$method = 'get', $data = '') {
//var_dump($headers);
$ch = curl_init(); //初始化
//$headers = array('Content-Type:application/json;charset=UTF-8','certId:2c6050953c1bb1a5d1371e881fd85f26','signType:SM2','');
//设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
var_dump($headers);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE 5.01;Windows NT 5.0)'); //头的字符串
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //提交数值
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //是否输出到屏幕上,true不直接输出
curl_setopt($ch, CURLOPT_HEADER, true);
$temp = curl_exec($ch); //执行并获取结果
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
echo "<hr/>------响应头------<br/>";
// 获得响应结果里的:头大小
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
$header = substr($sContent, 0, $headerSize);
print_r($header);
echo "<hr/>";
curl_close($ch);
return $temp; //return 返回值
}
function decodeUnicode($str)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
),
$str);
}
?>notify.php
<?php
//拿取数据
$json=file_get_contents('php://input');
//记录数据到本地
if(file_exists('data/notify'.date("Ymd").'.txt'))
{
$arr['head']=$_SERVER;
$arr['body']=$json;
//"当前目录中,文件存在",追加
$myfile = fopen("data/notify".date("Ymd").".txt", "a") or die("Unable to open file!");
$txt = "\n【".date('Y-m-d H:i:s',time())."】---"."成功回调--- \n".(json_encode($arr));
fwrite($myfile, $txt);
//记得关闭流
fclose($myfile);
}
else
{
//"当前目录中,文件不存在",新写入
$myfile = fopen("data/notify".date("Ymd").".txt", "w") or die("Unable to open file!");
$txt = "【".date('Y-m-d H:i:s',time())."】---"."成功回调--- \n".(json_encode($arr));
fwrite($myfile, $txt);
//记得关闭流
fclose($myfile);
}
//验签
//加载phpcrypto.so
if(!extension_loaded('phpcrypto')) {
dl('phpcrypto.so');
}
$module = 'phpcrypto';
$functions = get_extension_funcs($module);
//引用动态库
$php_func = 'php_crypto_init';
$path = "/wwwdata/wwwroot/xxxxxxxxxxxxxxxxxxxx.com/src_crypt/libcryptAPIsm_lnx64.so";//国密动态库路径
$recode = $php_func($path);
if($recode!=0){
echo "加载失败";exit;
}
//验签
$signature=$_SERVER['HTTP_SIGNATURE'];
$certid=$_SERVER['HTTP_CERTID'];
$http_encryptkey=$_SERVER['HTTP_ENCRYPTKEY'];
$json=base64_decode($json);
//公钥
$pubkey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//私钥
$privkey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//转DER编码
php_SM2FormatConvert(101,base64_decode($http_encryptkey),$c1c3c2);
//私钥解密
php_HextoAsc($privkey,$k1);
php_SM2Decrypt($c1c3c2, $plain,$k1);
php_CryptKey(0, $plain,$key);
php_SM4CBCCrypt(1,$json,$dedata,$key,$plain);
//记录银行返回数据
$dedata=json_decode($dedata,1);
//var_dump($dedata);
//响应报文
/********************报文 header**********************/
$header['appId']="012020033xxxxxxxxxx"; //商户应用编号
$header['encryptType']="SM2"; //加密类型
$header['instId']="012020033xxxxxxxxxx"; //商户号
$header['productCode']="payService"; //产品编号
$header['requestTime']=date("Ymdhis"); //报文发起时间
$header['senderSN']=getRandCode(); //请求流水号
//$header['tradeCode']="unifiedorderJH"; //微信支付 //接口编号
$header['tradeCode']="notifyJH"; // 回调响应
$header['version']="1.0.0"; //版本号
/********************生成订单 报文body**********************/
// var_dump($dedata);
if($dedata['Body']['orderStatus']=="S"){
//验证订单
$order=explode("-",$dedata["out_trade_no"]); //订单号
$order_number=$order[0];
$total_fee=$dedata['Body']["total_fee"]; //金额
if(!empty($dedata['Body']["attach"])){
$attach=explode("|",$dedata['Body']["attach"]); //附加信息
$namearr=explode("-",$attach[0]);
$name=$namearr[0]; //姓名
$phonearr=explode("-",$attach[0]);
$phone=$phonearr[0]; //手机
$order_numberarr=explode("-",$attach[0]);
$check_order_number=$order_numberarr[0]; //附加信息-订单号
}
$time_end=$dedata['Body']['time_end'];
//修改订单支付状态
$body['success']="success"; //接口类型
}else{
$body['fail']="fail"; //接口类型
}
$data['Header']=$header;
$data['Body']=$body;
//生产报头
php_HextoAsc($pubkey,$dataAsc);
$headers['certId']=MD5($dataAsc);
//签名步骤一:按字典序排序参数
$res_json=decodeUnicode(json_encode($data));
//报文长度
php_HextoAsc($privkey,$k11);
php_HextoAsc($pubkey,$k22);
header("certId:".$headers['certId']);
header("Content-Type:application/json;charset=UTF-8");
header("signType:SM2");
header("encrypttype:SM4");
$recode = php_SM2Sign($res_json, $redata1,$k11,$k22);
//$recode = php_SM2Sign("123", $redata1,$k11,$k22);
$headers['signature']=base64_encode($redata1);
//延签
//print(php_SM2Verify("123",$redata1,$k22));
//echo "/n";
//广发的公钥 注意和个人公钥不同,是广发单独发来的公钥
$gf_pubkey="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
php_HextoAsc($gf_pubkey,$gf_k);
//encryptkey
$mw_key="1234567890123456";
php_SM2Encrypt($mw_key,$encryptkey,$gf_k); //加密
$encryptkey=base64_encode($encryptkey);
/*var_dump($encryptkey);
echo "<hr/>";*/
header("encryptkey:".$encryptkey);
//加密应答报文直接输出
$key_en="";
php_CryptKey(0,$mw_key,$key_en);
/*var_dump($key_en);
echo "<hr/>";*/
//SM4加密
php_SM4CBCCrypt(0,$res_json,$back_json,$key_en,$mw_key);
//php_SM4CBCCrypt(0,"123",$back_json,$key_en,$mw_key);
//var_dump($res_json);
$back_json=base64_encode($back_json);
//echo "<hr/>";var_dump($headers['signature']);
header("signature:".$headers['signature']);
$headers['Content-Length']=strlen($back_json);
header("Content-Length:".$headers['Content-Length']);
echo $back_json;
//php_SM4CBCCrypt(1,base64_decode($back_json),$res_json,$key_en,$mw_key);
//var_dump($res_json);
/*---------------------------------------以下为公用函数---------------------------------------*/
/**
* 获取随机字符串
* @param int $length 获取的随机字符串长度,默认为16
* @return string $noncestr 随机字符串
*/
function getRandCode($length=16){
$code="qazxswedcrfvbgtyhnmjuiklop0123456789QWERTYUIOPLKMNJHBVGFCXDSZA";
$noncestr='';
$max_index=strlen($code)-1;
for($i=0;$i<$length;$i++){
$index=rand(0,$max_index);
$noncestr.=$code[$index];
}
return $noncestr;
}
function decodeUnicode($str)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
),
$str);
}
?>