银商支付宝h5支付,以及回调

2022-11-10  本文已影响0人  lhx很好

银商支付宝h5支付

<?php
$appId = 'appId';
$appKey = 'appKey';
$mid = 'mid';//商户号(mid)
$tid = 'tid ';//终端号(tid)
//业务内容
$time = time();
$content = [
    'requestTimestamp' =>date('Y-m-d H:i:s', $time),//报文请求时间
    'merOrderId' =>'merOrderId',//商户订单号
    'merOderId'=>'merOderId',//测试上送
    'mid' => $mid,//商户号
    'tid' => $tid,//终端号
    'instMid' => 'H5DEFAULT',//业务类型
    'totalAmount' =>'1',//支付总金额
    'expireTime' => date('Y-m-d H:i:s', strtotime('+1 day', $time)),//过期时间
    'notifyUrl' =>'https://xxxx.com/notifyUrl.php',//支付通知地址
    'returnUrl' =>'https://xxxx.com/returnUrl.php'//网页跳转地址
];
$timestamp = date('YmdHis', $time);
//随机数
$str = md5(uniqid(mt_rand(), true));
$uuid = substr($str, 0, 8) . '-';
$uuid .= substr($str, 8, 4) . '-';
$uuid .= substr($str, 12, 4) . '-';
$uuid .= substr($str, 16, 4) . '-';
$uuid .= substr($str, 20, 12);
$nonce = $uuid;
//签名
$hash = bin2hex(hash('sha256', json_encode($content), true));
$hashStr = $appId . $timestamp . $nonce . $hash;
$signature = base64_encode((hash_hmac('sha256', $hashStr, $appKey, true))); //$appKey银联商户H5支付产品的AppKey
$data = [
    'timestamp' => $timestamp,//时间戳
    'authorization' => 'OPEN-FORM-PARAM',//认证方式
    'appId' => $appId,//APPID
    'nonce' => $nonce,//随机数
    'content' => urlencode(json_encode($content)),//业务内容
    'signature' => urlencode($signature),//签名
];
//接口返回信息
$options = '';
foreach ($data as $key => $value) {
    $options .= $key . '=' . $value .'&';
}
$options = rtrim($options, '&');
//存在转义字符,那么去掉转义
if(get_magic_quotes_gpc()){
    $options = stripslashes($options);
}
$url = 'https://test-api-open.chinaums.com/v1/netpay/trade/h5-pay?'.$options; //支付宝
//$url = 'https://test-api-open.chinaums.com/v1/netpay/uac/order?'.$options; //云闪付
//$url = 'https://test-api-open.chinaums.com/v1/netpay/wxpay/h5-pay?'.$options; //微信h5
//$url = 'https://test-api-open.chinaums.com/v1/netpay/wxpay/h5-to-minipay?'.$options; //微信小程序
//echo "地址:https://test-api-open.chinaums.com/v1/netpay/trade/h5-pay<br>";
//echo "参数:".$options."<br>";
//echo "签名:".urlencode($signature)."<br>";
echo "请求:".$url;
exit;

回调验签

$inputurl = file_get_contents("php://input");
$md5key="md5key";//md5密钥
parse_str($inputurl,$input);
$sign=$input['sign'];
unset($input['sign']);
if ($sign=sign($input,$md5key)) {
    if ($input['status']=='TRADE_SUCCESS') {
        $amount=$input['buyerPayAmount']*0.01;
        $merOrderId=$input['merOrderId'];
        var_dump($amount);exit;
    }
    echo "SUCCESS";
}else{
    echo "FAILED";
}

function sign($input,$md5key){
    ksort($input);
    $options = '';
    foreach ($input as $key => $value) {
        $options .= $key . '=' . $value .'&';
    }
    $options = rtrim($options, '&');
    $key=$options.$md5key;
    return strtoupper(hash('sha256', $key ));

}

退款

<?php
//退款
$time= time();
$APPID="APPID";
$APPKEY="APPKEY";
$mid="mid";
$tid="tid";
$merOrderId="merOrderId";//商户订单号
$authorization="OPEN-BODY-SIG";
$YmdHis=date("YmdHis",$time);
$nonce=md5(uniqid(microtime(true),true));
$instMid="H5DEFAULT";
$sendUrl="https://test-api-open.chinaums.com/v1/netpay/refund";
$tmpArray=array();
$tmpArray['requestTimestamp']=date("Y-m-d H:i:s",$time);//报文请求时间 字符串是格式yyyy-MM-dd HH:mm:ss
$tmpArray['mid']=$mid;//商户号
$tmpArray['tid']=$tid;//终端号
$tmpArray['instMid']=$instMid;//业务类型
$tmpArray['refundAmount']=1;
$tmpArray['refundDesc']='测试';//退款说明;
$tmpArray['refundOrderId']=101720221108120002;//可以生成退款订单号
$tmpArray['merOrderId']=$merOrderId;//商户订单号
$tmpArrayContentJson=json_encode($tmpArray);
$tmpArrayContent=bin2hex(hash('sha256', $tmpArrayContentJson, true));
$Sign= base64_encode(hash_hmac('sha256',$APPID.$YmdHis.$nonce.$tmpArrayContent, $APPKEY, true));
$Authorization="OPEN-BODY-SIG AppId=\"".$APPID."\", Timestamp=\"".$YmdHis."\", Nonce=\"".$nonce."\", Signature=\"".$Sign."\"";
$postdata=json_encode($tmpArray);
$header=array();
$header[]='AUTHORIZATION:'.$Authorization;
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;charset=utf-8';
$header[] ='Content-Length:'.strlen($postdata);
$curl = curl_init();  //初始化
curl_setopt($curl, CURLOPT_URL,$sendUrl);  //设置url
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 设为0表示不检查证书 设为1表示检查证书中是否有CN(common name)字段 设为2表示在1的基础上校验当前的域名是否与CN匹配
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$ret = curl_exec($curl);
var_dump($ret);exit;
if($ret === false){
    exit();
}
$retArray=json_decode($ret,true);
curl_close($curl);

退款查询

$time= time();
$APPID="APPID";
$APPKEY="APPKEY";
$mid="mid";
$tid="tid";
$merOrderId="merOrderId";//退款商户订单号

$authorization="OPEN-BODY-SIG";
$YmdHis=date("YmdHis",$time);
$nonce=md5(uniqid(microtime(true),true));


$instMid="H5DEFAULT";
$sendUrl="https://test-api-open.chinaums.com/v1/netpay/refund-query";
$tmpArray=array();
$tmpArray['requestTimestamp']=date("Y-m-d H:i:s",$time);//报文请求时间 字符串是格式yyyy-MM-dd HH:mm:ss
$tmpArray['mid']=$mid;//商户号
$tmpArray['tid']=$tid;//终端号
$tmpArray['instMid']=$instMid;//业务类型
$tmpArray['refundAmount']=1;
$tmpArray['merOrderId']=$merOrderId;//商户订单号
$tmpArrayContentJson=json_encode($tmpArray);
$tmpArrayContent=bin2hex(hash('sha256', $tmpArrayContentJson, true));
$Sign= base64_encode(hash_hmac('sha256',$APPID.$YmdHis.$nonce.$tmpArrayContent, $APPKEY, true));
$Authorization="OPEN-BODY-SIG AppId=\"".$APPID."\", Timestamp=\"".$YmdHis."\", Nonce=\"".$nonce."\", Signature=\"".$Sign."\"";
$postdata=json_encode($tmpArray);
$header=array();
$header[]='AUTHORIZATION:'.$Authorization;
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;charset=utf-8';
$header[] ='Content-Length:'.strlen($postdata);
$curl = curl_init();  //初始化
curl_setopt($curl, CURLOPT_URL,$sendUrl);  //设置url
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 设为0表示不检查证书 设为1表示检查证书中是否有CN(common name)字段 设为2表示在1的基础上校验当前的域名是否与CN匹配
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$ret = curl_exec($curl);
var_dump($ret);exit;
if($ret === false){
    exit();
}
$retArray=json_decode($ret,true);
curl_close($curl);
上一篇下一篇

猜你喜欢

热点阅读