<?php
/**
* Class WeiXinRefund
*/
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;
/**
* WeiXinRefund constructor.
* @param $mchid 商户号
* @param $appid 小程序appid
* @param $key 商户号秘钥
* @param $sslcertPath 证书文件路径
* @param $sslkeyPath 证书秘钥文件路径
*/
public function __construct($mchid, $appid, $key, $sslcertPath, $sslkeyPath){
$this->mchid = $mchid;//商户号
$this->appid = $appid;//小程序appid
$this->key = $key;//商户号秘钥
$this->sslcertPath = $sslcertPath;//证书文件路径
$this->sslkeyPath = $sslkeyPath;//证书秘钥路径
}
/**
* 退款
* @param $openid 用户的openid
* @param $totalFee 订单金额
* @param $outTradeNo 商户退款单号
* @param $outRefundNo 商户单号
* @param $refundFee 退款金额
* @return mixed
*/
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(){
//通过微信api进行退款流程
$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;
}
/**
* 转账(注:商户号已入驻90日且截止今日回推30天商户号保持连续不间断的交易)
* @param $openid 用户的openid
* @param $outTradeNo 自定义订单号
* @param $amount 转账金额
* @param $desc 描述
* @return array
*/
public function withdraw($openid,$outTradeNo,$amount,$desc){
$this->openid = $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_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch,CURLOPT_HEADER,FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $this->sslcertPath);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $this->sslkeyPath);
//post提交方式
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后加入KEY
$String = $String."&key=".$this->KEY;
//签名步骤三:MD5加密
$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 .= strtolower($k) . "=" . $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;
}
}
微信转账、退款
最新推荐文章于 2024-06-13 13:58:29 发布