如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
微信公众号支付后,全额退款
<a href="/user/order_tk/{$order['order_id']}.html" onclick="{if(confirm('退款后不能恢复,您确定执行退款操作吗?')){return true;}return false;}">退款</a>
执行相关的方法
public function order_tk() { $id = intval($_GET[0]); $condition['order_id'] = $id; $order = $this->model->table('member_order')->where($condition)->find(); //获取信息 if (empty($order)) { $this->success('该订单不存在', 'back', 'alert'); } //退款微信 $transaction_id = $order['trade_no']; $out_refund_no = date('YmdHis').mt_rand(1000,9999); $total_fee = $order['goods_amount']; $refund_fee = $order['goods_amount']; $this->wxpay_config = array( 'appid' => 'wx6289*****a0039f', 'mch_id' => '140****502', 'pay_apikey'=> 'Hyflux******8051182150805118', 'api_cert'=> './module/cert/apiclient_cert.pem', 'api_key'=> './module/cert/apiclient_key.pem' ); $res = self::refund($transaction_id,$out_refund_no,$total_fee,$refund_fee); if($res['return_code']=='SUCCESS'){ 退款成功后的后台操作...... $this->success('订单退款成功!', $_GET['refreshUrl']); }else{ $this->error('系统错误', $_GET['refreshUrl']); } } /** * 微信退款(POST) * @param string(28) $transaction_id 在微信支付的时候,微信服务器生成的订单流水号,在支付通知中有返回 * @param string $out_refund_no 商品简单描述 * @param string $total_fee 微信支付的时候支付的总金额(单位:分) * @param string $refund_fee 此次要退款金额(单位:分) * @return string xml格式的数据 */ public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){ $config = $this->wxpay_config; //退款参数 $refundorder = array( 'appid' => $config['appid'], 'mch_id' => $config['mch_id'], 'nonce_str' => self::getNonceStr(), 'transaction_id'=> $transaction_id, 'out_refund_no' => $out_refund_no, 'total_fee' => $total_fee * 100, 'refund_fee' => $refund_fee * 100 ); $refundorder['sign'] = self::makeSign($refundorder); //请求数据,进行退款 $xmldata = self::array2xml($refundorder); $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; $res = self::curl_post_ssl($url, $xmldata); if(!$res){ return array('status'=>0, 'msg'=>"Can't connect the server" ); } // 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了 //file_put_contents('./log3.txt',$res,FILE_APPEND); $content = self::xml2array($res); if(strval($content['result_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des'])); } if(strval($content['return_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['return_msg'])); } return $content; } /** * 将一个数组转换为 XML 结构的字符串 * @param array $arr 要转换的数组 * @param int $level 节点层级, 1 为 Root. * @return string XML 结构的字符串 */ protected function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml>" : ''; foreach($arr as $tagname => $value) { if (is_numeric($tagname)) { $tagname = $value['TagName']; unset($value['TagName']); } if(!is_array($value)) { $s .= "<{$tagname}>".(!is_numeric($value) ? '<![CDATA[' : '').$value.(!is_numeric($value) ? ']]>' : '')."</{$tagname}>"; } else { $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>"; } } $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s); return $level == 1 ? $s."</xml>" : $s; } /** * 将xml转为array * @param string $xml xml字符串 * @return array 转换得到的数组 */ protected function xml2array($xml){ //禁止引用外部xml实体 libxml_disable_entity_loader(true); $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $result; } /** * * 产生随机字符串,不长于32位 * @param int $length * @return 产生的随机字符串 */ protected function getNonceStr($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } /** * 生成签名 * @return 签名 */ protected function makeSign($data){ //获取微信支付秘钥 $key = $this->wxpay_config['pay_apikey']; // 去空 $data=array_filter($data); //签名步骤一:按字典序排序参数 ksort($data); $string_a=http_build_query($data); $string_a=urldecode($string_a); //签名步骤二:在string后加入KEY $string_sign_temp=$string_a."&key=".$key; //签名步骤三:MD5加密 $sign = md5($string_sign_temp); // 签名步骤四:所有字符转为大写 $result=strtoupper($sign); return $result; } /** * 获取IP地址 * @return [String] [ip地址] */ protected function getip() { static $ip = ''; $ip = $_SERVER['REMOTE_ADDR']; if(isset($_SERVER['HTTP_CDN_SRC_IP'])) { $ip = $_SERVER['HTTP_CDN_SRC_IP']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) { foreach ($matches[0] AS $xip) { if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) { $ip = $xip; break; } } } return $ip; } /** * 微信支付发起请求 */ protected function curl_post_ssl($url, $xmldata, $second=30,$aHeader=array()){ $config = $this->wxpay_config; $ch = curl_init(); //超时时间 curl_setopt($ch,CURLOPT_TIMEOUT,$second); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); //这里设置代理,如果有的话 //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98'); //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,0); curl_setopt($ch, CURLOPT_SSLVERSION, 1); //默认格式为PEM,可以注释 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT,$config['api_cert']); //默认格式为PEM,可以注释 curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY,$config['api_key']); //curl_setopt($ch,CURLOPT_CAINFO,$config['rootca']); if( count($aHeader) >= 1 ){ curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); } curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata); $data = curl_exec($ch); if($data){ curl_close($ch); return $data; }else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } }
> 本文由子枫笔记快捷发布!