<?php
namespace app\common\controller;;
use think\facade\Env;
use think\Db;
class WxpayService
{
// 退款地址
private $refund_url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
// 证书文件放在与入口文件同级的目录下
private $SSLCERT_PATH; // api证书路径
private $SSLKEY_PATH; // 密钥证书路径
protected $mchid;
protected $appid;
protected $appKey;
protected $apiKey;
public $data = null;
public function __construct($mchid, $appid,$key)
{
$config=Db::name('setting')->find(1);
$this->SSLCERT_PATH = 'public'.$config['apiclient_cert'];
$this->SSLKEY_PATH = 'public'.$config['apiclient_key'];
$this->mchid = $mchid; //https://pay.weixin.qq.com 产品中心-开发配置-商户号
$this->appid = $appid; //微信支付申请对应的公众号的APPID
// $this->appKey = $appKey; //微信支付申请对应的公众号的APP Key
$this->apiKey = $key; //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
}
/**调用
if($total_fee>0){
$mch_id=Config('app.mch_id');
$appid=Config('app.AppID');
$apiKey=Config('app.wx_key');
$total_fee=$total_fee;
$refund_fee=$total_fee;
$transaction_id=$transaction_id;
$Wxtk=new WxpayService($mch_id,$appid,$apiKey);
$result= $Wxtk->wx_refund($get_order_num,$total_fee,$refund_fee,$transaction_id);
return $result;
// dump($result);die;
// if ($result['code']!=200){
// return message('退款失败',false);
// }
}
*/
/**
* 微信退款
* [wx_refund description]
* @Author 念天地之悠悠
* @DateTime 2019-03-11
* @param [type] $this->appid [description] APPID
* @param [type] $this->mchid [description] 商户号
* @param [type] $order_no [description] 商户退款单号
* @param [type] $total_fee [description] 订单金额
* @param [type] $refund_fee [description] 退款金额 单位分
* @param [type] $transaction_id [description] 微信单号
* @param [type] $this->apiKey [description] 申请支付后设置的key
* @return [type] [description]
*/
public function wx_refund($order_no,$total_fee,$refund_fee,$transaction_id){
$post_data = [];
$nonce_str = $this->nonce_str();
$post_data['appid'] = $this->appid; // APPID
$post_data['mch_id'] = $this->mchid; // 商户号
$post_data['nonce_str'] = $nonce_str; // 随机码
$post_data['out_refund_no'] = $order_no; // 商户退款单号
$post_data['refund_fee'] = (int)$refund_fee; // 退款金额
$post_data['total_fee'] = (int)$total_fee; // 订单金额
$post_data['transaction_id'] = $transaction_id; // 微信单号
$sign = $this->sign($post_data,$this->apiKey); // 签名
$post_xml = '<xml>
<appid>'.$this->appid.'</appid>
<mch_id>'.$this->mchid.'</mch_id>
<nonce_str>'.$nonce_str.'</nonce_str>
<out_refund_no>'.$order_no.'</out_refund_no>
<refund_fee>'.$refund_fee.'</refund_fee>
<total_fee>'.$total_fee.'</total_fee>
<transaction_id>'.$transaction_id.'</transaction_id>
<sign>'.$sign.'</sign>
</xml>';
// return $post_data;
$xml = $this->postXmlSSLCurl($this->refund_url,$post_xml);
// 将xml转数组
$arr = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($arr === false) {
return message('parse xml error',false);
}
if ($arr->return_code != 'SUCCESS') {
// return $arr->return_msg;
return message($arr->return_msg,false);
}
if ($arr->result_code != 'SUCCESS') {
// return $arr->err_code;
return message($arr->err_code,false);
}
return message();
// return true;
}
private function nonce_str(){
$result = '';
$str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
for ($i = 0; $i < 32; $i++) {
$result .= $str[rand(0,48)];
}
return $result;
}
/**
* 签名
* [sign description]
* @Author 念天地之悠悠
* @DateTime 2019-03-11
* @param [type] $data [description] 数据集
* @param [type] $wx_key [description] 微信支付key
* @return [type] [description]
*/
private function sign($data,$wx_key = ''){
$stringA = '';
foreach ($data as $key => $value) {
if (!$value) continue;
if ($stringA) $stringA .= '&'.$key."=".$value;
else $stringA = $key."=".$value;
}
if ($wx_key == '') {
$stringSignTemp = $stringA;
}else{
$stringSignTemp = $stringA.'&key='.$wx_key;
}
return strtoupper(md5($stringSignTemp));
}
/**
* 需要证书的curl请求
* [postXmlSSLCurl description]
* @Author 念天地之悠悠
* @DateTime 2019-03-12
* @param [type] $url [description] 访问地址
* @param [type] $xml [description] mxl数据体
* @param integer $second [description] 超时时间
* @return [type] [description]
*/
public function postXmlSSLCurl($url,$xml,$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, Env::get('root_path').$this->SSLCERT_PATH);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, Env::get('root_path').$this->SSLKEY_PATH);
//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;
}
}
}
微信订单金额原路返回
于 2023-04-14 09:49:46 首次发布