您好,欢迎访问一九零五行业门户网

在PHP中怎样实现微信退款功能

这次给大家带来在php中怎样实现微信退款功能,在php中实现微信退款功能的注意事项有哪些,下面就是实战案例,一起来看一下。
本文实例讲述了php开发实现微信退款功能。分享给大家供大家参考,具体如下:
最近在调微信退款接口,发现有许多坑,更大家分享一下
① 要是在测试的时候,网页提示 curl 58 说明 证书的路径出现问题(这里要填物理路径,也就是绝对路径)
② 网页提示curl 52 说明你的证书引入少了,在官方的demo上只有两个证书 apiclient_cert.pem和 apiclient_key.pem  你还需要引入一个证书 rootca.pem,这个证书需要你登录到
你的商户平台上下载
③ 要是网页提示  http/1.1 200 ok server: nginx date: tue, 20 jun 2017 08:08:01 gmt content-type: text/plain content-length: 852 connection: keep-alive keep-alive: timeout=8
说明你的代码是没有问题的 ,应该是参数出错了 我之前错的地方是把 out_refund_no和 out_trade_no写的一样,微信官网上也是这样写的 ,但是就是出现问题,我把out_refund_no 修改了一下 发现可以了
注意:金额的单位是分
下面就是我写的接口
function home_index() {   date_default_timezone_set(asia/shanghai);   $date = date(ymdhis);   $appid = ;   $mch_id = ;   $out_trade_no = 14487658021497944120;   $op_user_id = ;   $out_refund_no = $date;   $total_fee = 500;   $refund_fee = 500; //  $transaction_id = 4009542001201706206596667604;   $key = ;   $nonce_str = noncestr();   $ref = strtoupper(md5(appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&op_user_id=$op_user_id           . &out_refund_no=$out_refund_no&out_trade_no=$out_trade_no&refund_fee=$refund_fee&total_fee=$total_fee           . &key=$key)); //sign加密md5   $refund = array(   'appid' =>$appid, //应用id,固定   'mch_id' => $mch_id, //商户号,固定   'nonce_str' => $nonce_str, //随机字符串   'op_user_id' => $op_user_id, //操作员   'out_refund_no' => $out_refund_no, //商户内部唯一退款单号   'out_trade_no' => $out_trade_no, //商户订单号,pay_sn码 1.1二选一,微信生成的订单号,在支付通知中有返回   // 'transaction_id'=>'1',//微信订单号 1.2二选一,商户侧传给微信的订单号   'refund_fee' => $refund_fee, //退款金额   'total_fee' => $total_fee, //总金额   'sign' => $ref//签名   );   $url = https://api.mch.weixin.qq.com/secapi/pay/refund;   ; //微信退款地址,post请求   $xml = arraytoxml($refund);   $ch = curl_init();   curl_setopt($ch, curlopt_url, $url);   curl_setopt($ch, curlopt_header, 1);   curl_setopt($ch, curlopt_returntransfer, 1);   curl_setopt($ch, curlopt_ssl_verifypeer, 1); //证书检查   if ($usecert == true) {     // 设置证书     curl_setopt($ch, curlopt_sslcerttype, 'pem');     curl_setopt($ch, curlopt_sslcert, dirname(file) . '/wxpay/cert/apiclient_cert.pem');     curl_setopt($ch, curlopt_sslcerttype, 'pem');     curl_setopt($ch, curlopt_sslkey, dirname(file) . '/wxpay/cert/apiclient_key.pem');     curl_setopt($ch, curlopt_sslcerttype, 'pem');     curl_setopt($ch, curlopt_cainfo, dirname(file) . '/wxpay/cert/rootca.pem');   }   curl_setopt($ch, curlopt_post, 1);   curl_setopt($ch, curlopt_postfields, $xml);   $xml = curl_exec($ch);   // 返回结果0的时候能只能表明程序是正常返回不一定说明退款成功而已   if ($xml) {     curl_close($ch);     // 把xml转化成数组     libxml_disable_entity_loader(true);     $xmlstring = simplexml_load_string($xml, 'simplexmlelement', libxml_nocdata); //    var_dump($xmlstring);     $result['errnum'] = 0;     $result['info'] = object_to_array($xmlstring); //    var_dump($result);     return $result;   } else {     $error = curl_errno($ch);     curl_close($ch);     // 错误的时候返回错误码。     $result['errnum'] = $error;     return $result;   } } function arraytoxml($arr) {   $xml = <root>;   foreach ($arr as $key => $val) {     if (is_array($val)) {       $xml .= <" . $key . "> . arraytoxml($val) . </" . $key . ">;     } else {       $xml .= <" . $key . "> . $val . </" . $key . ">;     }   }   $xml .= </root>;   return $xml; } function object_to_array($obj) {   $obj = (array) $obj;   foreach ($obj as $k => $v) {     if (gettype($v) == 'resource') {       return;     }     if (gettype($v) == 'object' || gettype($v) == 'array') {       $obj[$k] = (array) object_to_array($v);     }   }   return $obj; } function noncestr() {   $chars = abcdefghijklmnopqrstuvwxyz0123456789;   $str = ;   $length = 32;   for ($i = 0; $i < $length; $i++) {     $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);   }   // 随机字符串   return $str; } home_index();
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
anaconda的新手使用大全
php有哪些生成随机数的方法
以上就是在php中怎样实现微信退款功能的详细内容。
其它类似信息

推荐信息