<?php
class WeiXinRefund{
protected $mchid;
protected $appid;
protected $key;
protected $sslcertPath;
protected $sslkeyPath;
protected $openid;
protected $totalFee;
protected $outRefundNo;
protected $outTradeNo;
protected $refundFee;
protected $amount;
protected $desc;
public function __construct($mchid, $appid, $key, $sslcertPath, $sslkeyPath){
$this->mchid = $mchid;
$this->appid = $appid;
$this->key = $key;
$this->sslcertPath = $sslcertPath;
$this->sslkeyPath = $sslkeyPath;
}
public function refund($openid,$totalFee,$outTradeNo,$outRefundNo,$refundFee){
$this->openid = $openid;
$this->totalFee = $totalFee * 100;
$this->outRefundNo = $outRefundNo;
$this->outTradeNo = $outTradeNo;
$this->refundFee = $refundFee * 100;
$result = $this->wxRefund();
return $result;
}
private function wxRefund(){
$parma = array(
'appid'=> $this->appid,
'mch_id'=> $this->mchid,
'nonce_str'=> $this->createNoncestr(),
'out_trade_no'=> $this->outTradeNo,
'out_refund_no'=> $this->outRefundNo,
'total_fee'=> $this->totalFee,
'refund_fee'=> $this->refundFee
);
$parma['sign'] = $this->getSign($parma);
$xmldata = $this->arrayToXml($parma);
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$xmlresult = $this->postXmlSSLCurl($xmldata,$url);
$result = $this->xmlToArray($xmlresult);
return $result;
}
public function withdraw($openid,$outTradeNo,$amount,$desc){
$this->openid = $openid;
$this->outTradeNo = $outTradeNo;
$this->amount = $amount * 100;
$this->desc = $desc;
$result = $this->wxWithdraw();
return $result;
}
private function wxWithdraw(){
$arr = array();
$arr['mch_appid'] = $this->appid;
$arr['mchid'] = $this->mchid;
$arr['nonce_str'] = $this->createNoncestr();
$arr['partner_trade_no'] = $this->outTradeNo;
$arr['openid'] = $this->openid;
$arr['check_name'] = 'NO_CHECK';
$arr['amount'] = $this->amount;
$arr['desc'] = $this->desc;
$arr['sign'] = $this->getSign($arr);
$var = $this->arrayToXml($arr);
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$xml = $this->postXmlSSLCurl($url, $var, 30);
$rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_code = (string)$rdata->return_code;
$result_code = (string)$rdata->result_code;
$return_code = trim(strtoupper($return_code));
$result_code = trim(strtoupper($result_code));
if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
$isrr = array(
'errmsg'=>'ok',
'error' => 1,
);
} else {
$returnmsg = (string)$rdata->err_code_des;
$isrr = array(
'error' => 0,
'errmsg' => $returnmsg,
);
}
return $isrr;
}
function postXmlSSLCurl($xml,$url,$second=30){
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $this->sslcertPath);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $this->sslkeyPath);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";die;
curl_close($ch);
return false;
}
}
protected function arrayToXml($arr){
$xml = "<xml>";
foreach ($arr as $key=>$val) {
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
protected function xmlToArray($xml){
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
protected function getSign($Obj){
foreach ($Obj as $k => $v){
$Parameters[$k] = $v;
}
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
$String = $String."&key=".$this->KEY;
$String = md5($String);
$result_ = strtoupper($String);
return $result_;
}
protected function formatBizQueryParaMap($paraMap, $urlencode){
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if($urlencode)
{
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = "";
if (strlen($buff) > 0)
{
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}
protected function createNoncestr($length = 32){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
}