退款

  public function tipsRefund()
{
$model = D('Common/Order');
$order_sn = $model->makeOrderSn();
$transaction_id=I("transaction_id",0);
$money=I("money",0);
$order_code=I("order_code",0);
$openid=I("openid",0);
$gid=I("gid",0);
// $transaction_id="4200000826202011216599359363";
// $money=0.1;
// $order_code="AB21447453209446";
// $openid="oxOUw5bmXqK2RzTSXfDLpJoy1pF8";

$user=M("user")->where(["openid"=>$openid])->find();
$appid= '';
$data['appid']=$appid;
$data['mch_id'] ="";
$data['nonce_str'] =$order_sn;//退款单号
$data['transaction_id'] = $transaction_id;//交易单号 流水号
$data['refund_money'] = $money*100;//退款金额
$data['total_money'] = $money*100;//退款总金额
$data['out_refund_no'] = $order_code;//商户内部唯一退款单号
$res = $this->weChatrefund($data);//小费退款
$data = array("result" => '1', "message" => "成功!","list"=>$res);


if ($res['return_code'] =='SUCCESS') {
M("order")->where(["order_code"=>$order_code])->save(['state'=>9]);
//记录小费退款日志
$log2['uid'] = $user['id'];
$log2['order_code'] = $order_code;
$log2['status'] = 2;
$log2['money'] =$money;
$log2['addtime'] = time();
$log2['gid'] = $gid;
M('refund_record ')->add($log2);
return true;
} else {
return false;
}
}


private function weChatrefund($data)
{
$param = array(
'appid'=>"wx67f5c80eb987ee40",//应用ID,固定
'mch_id'=>"1601268116",//商户号,固定
'nonce_str'=> $this->createNoncestr(),
'out_refund_no'=> $data['out_refund_no'],//商户内部唯一退款单号
// 'out_trade_no' => $out_trade_no, //商户订单号,pay_sn码 1.1二选一,微信生成的订单号,在支付通知中有返回
'transaction_id'=>$data['transaction_id'],//微信订单号 1.2二选一,商户侧传给微信的订单号
'total_fee'=> $data['refund_money'],//退款金额
'refund_fee'=> $data['total_money'],//总金额
);
$param['sign'] = $this->getSign($param);//签名
$xmldata = $this->arrayToXml($param);
$xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');
$result = $this->xmlToArray($xmlresult);
return $result;
}
/*
* 对要发送到微信统一下单接口的数据进行签名
*/
protected function getSign($Obj){
foreach ($Obj as $k => $v){
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//签名步骤二:在string后加入KEY
$String = $String."&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;
}
//数组转字符串方法
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 static function xmlToArray($xml){
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}

//需要使用证书的请求
private function postXmlSSLCurl($xml,$url,$second=30)
{

$server= $_SERVER['DOCUMENT_ROOT'];
$isdir = "$server/wxzf/cert/apiclient_cert.pem";//证书位置
$isdir1 = "$server/wxzf/cert/apiclient_key.pem";//证书位置
$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, getcwd().'/apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLCERT, $isdir);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
// curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
curl_setopt($ch,CURLOPT_SSLKEY,$isdir1);
//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>";
curl_close($ch);
return false;
}
}
posted @ 2020-12-08 10:29  php毛宏历的博客  阅读(178)  评论(0编辑  收藏  举报