导航
微信小程序   /* * 微信退款-适用于微信公号及小程序 * $orderid 订单ID * $ordernum 订单编号 * $totalprice 订单金额 * $refundmoney 退款金额 * $refund_desc 备注信息 */ public function wxrefund($orderid,$ordernum,$totalprice,$refundmoney,$refund_desc=''){ $key = config('wechat.mch_key');//商户密钥未设置 if(!$refundmoney || $refundmoney<=0){ return 3; //退款金额不能小于0 } $pars = array(); $pars['appid'] = config('wechat.appid'); $pars['mch_id'] = config('wechat.mch_id'); $pars['nonce_str'] = random(32); $pars['out_trade_no'] = $ordernum; $pars['out_refund_no'] = $ordernum. '_' . rand(1000, 9999); $pars['total_fee'] = $totalprice * 100; $pars['refund_fee'] = $refundmoney * 100; if($refund_desc) $pars['refund_desc'] = $refund_desc; ksort($pars, SORT_STRING); $string1 = ''; foreach ($pars as $k => $v) { $string1 .= "{$k}={$v}&"; } $string1 .= "key=" . $key; $pars['sign'] = strtoupper(md5($string1)); $xml = $this->array2xml($pars); if($this->getOs()==1){ //linux系统 $sslcert = '../public/wxcert/apiclient_cert.pem'; $sslkey = '../public/wxcert/apiclient_key.pem'; }else{ $sslcert = str_replace('//','/',ROOT_PATH.str_replace(PRE_URL,'','public\wxcert\apiclient_cert.pem')); $sslkey = str_replace('//','/',ROOT_PATH.str_replace(PRE_URL,'','public\wxcert\apiclient_key.pem')); } $ch = curl_init (); $MENU_URL="https://api.mch.weixin.qq.com/secapi/pay/refund"; curl_setopt ( $ch, CURLOPT_URL, $MENU_URL ); curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); curl_setopt($ch,CURLOPT_SSLCERT,$sslcert); curl_setopt($ch,CURLOPT_SSLKEY,$sslkey); curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); $info = curl_exec ( $ch ); if (curl_errno ( $ch )) { return array('status'=>0,'msg'=>curl_error ( $ch )); } curl_close ( $ch ); $resp = (array)(simplexml_load_string($info,'SimpleXMLElement', LIBXML_NOCDATA)); if($resp['return_code'] == 'SUCCESS' && $resp['result_code']=='SUCCESS'){ return 1; }else{ return 2; } } 注:$sslcert和$sslkey为证书地址,linux和windows系统下写法不同,需自行调试 获取系统类型请参考:https://hongkediguo.com/jishufenxiang/5.html
获取当前服务器系统,用于根据不同系统设置不同的地址,如微信退款的验证文件地址   public function getOs() { $os_name = PHP_OS; if(strpos($os_name,"Linux")!==false){ $os_str = 1; //linux系统 }else if(strpos($os_name,"WIN")!==false){ $os_str = 2; //win系统 } return $os_str; }