微信退款php
经过整理,看完了以后
直接上代码:
public function refund()
{
header(“Content-type:text/html;charset=utf-8”);
$nId = I(‘get.id’, ‘’);
$cOrders = D(‘orders’);
$cWxLog = D(‘wxpayLog’);
if ('' == $nId || NULL == $nId) {
$this->error('订单错误');
}
$arrOrderInfo = $cOrders->getOrder($nId);
if (!$arrOrderInfo)
{
$this->error('订单获取失败');
}
if ( $arrOrderInfo['status'] != 201 )
{
$this->error('订单状态错误');
}
if ( empty($arrOrderInfo['sn']) )
{
$this->error('订单标示获取失败');
}
$sOrderTrade = $this->getOrderTradeNum( $arrOrderInfo['sn'] );
$arrData = array(
'appid' => C("APP_ID"),
'mch_id' => C("WXPAY_CONFIG")['mch_id'],
'nonce_str' => C('WXPAY_CONFIG')['trade_key'],
'out_trade_no' => $arrOrderInfo['sn'],
'total_fee' => $arrOrderInfo['actual_should_pay']*100,
'refund_fee' => $arrOrderInfo['actual_should_pay']*100,
'out_refund_no' => $sOrderTrade->transaction_id,
);
$sSign = $this->makeWXpaySign( $arrData );
$arrData['sign']= $sSign;
$xmlData = $this->makeXML($arrData);
$sUrl = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$ret = curl_post_xml_with_wxCA($sUrl,$xmlData);
$objData = xml2obj($ret);
$arrRet = object_array($objData);
$retInfo = json_encode($arrRet);
$nSid = $arrOrderInfo['s_id'];
if ( $arrRet['result_code'] == 'SUCCESS')
{
$nRetStatus = $cOrders->saveStatus($nId,3);
if ( $nRetStatus == $cOrders::EDU_MODEL_ORDERSMODEL_SAVESTATUS_SAVE_SUCCESS )
{
$cWxLog->wxLog($arrOrderInfo['u_id'],$nId,$arrOrderInfo['actual_should_pay'],$retInfo,1,$nSid);
$this->success('退款成功');
}
else
{
$cWxLog->wxLog($arrOrderInfo['u_id'],$nId,$arrOrderInfo['actual_should_pay'],$retInfo,-2,$nSid);
$this->error('状态修改失败');
}
}
else
{
$cWxLog->wxLog($arrOrderInfo['u_id'],$nId,$arrOrderInfo['actual_should_pay'],$retInfo,-1,$nSid);
$this->error('退款失败');
}
}
/***
* @author lifang
* @desc 获取订单交易号
/
public function getOrderTradeNum( KaTeX parse error: Expected '}', got 'EOF' at end of input: … if ( !sSn )
{
$this->error(‘订单标示不为空’);
}
$arrData = array(
‘appid’ => C(“APP_ID”), //小程序id
‘mch_id’ => C(“WXPAY_CONFIG”)[‘mch_id’], //商户id
‘nonce_str’ => C(‘WXPAY_CONFIG’)[‘trade_key’], //支付key
//‘total_tee’ => 10, //支付金额
‘out_trade_no’ => $sSn, //订单唯一标示
//‘refund_fee’ => 0.1100, //退款金额
);
$sSign = $this->makeWXpaySign( $arrData ); //获取签名
//var_dump($sSign);die;
$arrData['sign'] = $sSign;
$xmlData= $this->makeXML( $arrData ); //获取xml格式数据
$sUrl = "https://api.mch.weixin.qq.com/pay/orderquery";//微信统一退款请求接口
$xmlRet = $this->CurlPostXmlStr($xmlData,$sUrl); //获取微信xml返回数据
$objData= xml2obj( $xmlRet );
return $objData;
//var_dump($objData);die;
}
/***
* @param $arrData
* @author lifang
* @desc 生成订单签名
* @return $sStringD string 签名
*/
protected function makeWXpaySign( $arrData )
{
$sTradeKey = C(‘WXPAY_CONFIG’)[‘trade_key’];
ksort($arrData ); //按照ASCII从小到大排列
reset($arrData); //指针重置
//重组数组
foreach ( $arrData as $key => $value )
{
if ( $key != 'sign' && $key != 'sign_type' )
{
$arrInfo[] = $key.'='.$value;
}
}
$sStringA = implode('&',$arrInfo); //用&符分割数组的值,组成字符串
$sStringB = $sStringA.'&key='.$sTradeKey;//拼接支付key
$sStringC = MD5($sStringB); //加密
$sStringD = strtoupper( $sStringC ); //所有字符转大写
return $sStringD;
}
/***
* @param $arrData
* @return string
* @author lifang
* @desc 返回微信所需要的xml格式数据
*/
protected function makeXML( $arrData )
{
$sString = “”;
foreach ( $arrData as $key => $value )
{
$sString .= '<'.$key.'>'.$value.'</'.$key.'>';
}
$sString .= "</XML>";
return $sString;
}
/***
* @param $xmlData
* @param $sUrl
* @return bool|mixed 返回xml数据
* @author lifang
* @desc 请求微信统一接口,
*/
protected function CurlPostXmlStr( $xmlData,$sUrl )
{
if ( !is_string($xmlData) )
{
$this->error('请求参数必须为字符串');
}
$httph = curl_init($sUrl);
curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($httph, CURLOPT_SSLVERSION, 1);
curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($httph, CURLOPT_POST, true);//设置为POST方式
curl_setopt($httph, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
curl_setopt($httph, CURLOPT_HEADER,0);//返回不包含header
$rst = curl_exec($httph);
$stat = curl_getinfo($httph);
curl_close($httph);
//dump($stat);die;
if ( 200 == $stat['http_code'] )
{
return $rst;
}
else
{
return false;
}
}
/**
-
微信企业付款到零钱 curl方法
-
@param {String} $url 微信企业付款到零钱 api
-
@param {String} s t r x m l 格 式 的 参 数 字 符 串 ∗ / f u n c t i o n c u r l p o s t x m l w i t h w x C A ( str xml格式的参数字符串 */ function curl_post_xml_with_wxCA( strxml格式的参数字符串∗/functioncurlpostxmlwithwxCA(url, KaTeX parse error: Expected '}', got 'EOF' at end of input: … if(!is_string(str)){
throw new Exception(“参数必须为string”);
}
c e r t P a t h = ′ / w e b / w x a p p c e r t / a p i c l i e n t c e r t . p e m ′ ; / / v a r d u m p ( certPath = '/web/wxappcert/apiclient_cert.pem'; //var_dump( certPath=′/web/wxappcert/apiclientcert.pem′;//vardump(certPath);die;
$keyPath = ‘/web/wxappcert/apiclient_key.pem’;c h = c u r l i n i t ( ) ; c u r l s e t o p t ( ch = curl_init(); curl_setopt( ch=curlinit();curlsetopt(ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt( c h , C U R L O P T S S L V E R I F Y H O S T , f a l s e ) ; c u r l s e t o p t ( ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt( ch,CURLOPTSSLVERIFYHOST,false);curlsetopt(ch, CURLOPT_URL, u r l ) ; c u r l s e t o p t ( url); curl_setopt( url);curlsetopt(ch,CURLOPT_SSLCERTTYPE,‘PEM’);
curl_setopt( c h , C U R L O P T S S L C E R T , ch, CURLOPT_SSLCERT, ch,CURLOPTSSLCERT,certPath);
curl_setopt( c h , C U R L O P T S S L K E Y T Y P E , ′ P E M ′ ) ; c u r l s e t o p t ( ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt( ch,CURLOPTSSLKEYTYPE,′PEM′);curlsetopt(ch, CURLOPT_SSLKEY,$keyPath);curl_setopt( c h , C U R L O P T R E T U R N T R A N S F E R , 1 ) ; c u r l s e t o p t ( ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt( ch,CURLOPTRETURNTRANSFER,1);curlsetopt(ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt( c h , C U R L O P T P O S T , 1 ) ; c u r l s e t o p t ( ch, CURLOPT_POST, 1); curl_setopt( ch,CURLOPTPOST,1);curlsetopt(ch, CURLOPT_POSTFIELDS, s t r ) ; / / 因 为 微 信 红 包 在 使 用 过 程 中 需 要 验 证 服 务 器 和 域 名 , 故 需 要 设 置 下 面 两 行 c u r l s e t o p t ( str); //因为微信红包在使用过程中需要验证服务器和域名,故需要设置下面两行 curl_setopt( str);//因为微信红包在使用过程中需要验证服务器和域名,故需要设置下面两行curlsetopt(ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
curl_setopt( c h , C U R L O P T S S L V E R I F Y H O S T , 2 ) ; / / 检 查 证 书 中 是 否 设 置 域 名 , 并 且 是 否 与 提 供 的 主 机 名 匹 配 c u r l s e t o p t ( ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配 curl_setopt( ch,CURLOPTSSLVERIFYHOST,2);//检查证书中是否设置域名,并且是否与提供的主机名匹配curlsetopt(ch, CURLOPT_HEADER, 0);// curl_setopt($ch, CURLOPT_CAINFO, ‘/web/wxhongbaocert/rootca.pem’); // CA根证书(用来验证的网站证书是否是CA颁布)
r s t = c u r l e x e c ( rst = curl_exec( rst=curlexec(ch);
s t a t = c u r l g e t i n f o ( stat = curl_getinfo( stat=curlgetinfo(ch);
curl_close( c h ) ; / / d u m p ( ch); //dump( ch);//dump(stat);die;
if(intval($stat[“http_code”])==200){
return $rst;
}else{
return false;
}
}