E企盈营销工具技术服务商 热线:4006-838-530

【微信小程序】退款功能教程(含沿河E企盈小程序申请退款和退款回调)

1、一定要区分小程序和公众号的退款,唯一的区别就是 appid不一样,其他的都是一样的。不废话,直接写代码了啊。 放大招!!!然后,需要注意的:最好是把证书放在下面的php的同级或者下级。证书的路径一定要是服务器的根路径,比如E:tupuuWWWXXX。而像http://www.xxx.com/../.. 是不行的,会报58错误。DEMO1、用来调试退款流程,在浏览器直接访问这个php文件。 ?php/** * 微信公众号和小程序退款申请接口-demo * ==================================================== * 注意:同一笔单的部分退款需要设置相同的订单号和不同的 * out_refund_no。一笔退款失败后重新提交,要采用原来的 * out_refund_no。总退款金额不能超过用户实际支付金额(现 * 金券金额不能退款)。*///include_once(S_ROOT .”xxpay/WxPayPubHelper/WxPayPubHelper.miniprogram.php”);     //输入需退款的订单号     if (!isset($_POST[“out_trade_no”]) || !isset($_POST[“refund_fee”]))     {         $out_trade_no = ” “;         $refund_fee = “1”;     }else{         //echo “退款订单号:”.$_POST[‘out_trade_no’];         $order_msg= array(            ‘out_trade_no’=’homeX20171206155833_1180’,             ‘out_trade_no’=’homeX20171206155833_1180’,             ‘order_amount’=’0.02’,                      );                  $appid =        ‘wx60****d’;//如果是公众号 就是公众号的appid;小程序就是小程序的appid         $mch_id =       ‘126****01’;         $KEY = ‘xixi***09000908bkj’;         $nonce_str =    randomkeys(32);//随机字符串         $op_user_id = $mch_id;         $out_trade_no = $order_msg[‘out_trade_no’];//商户传微信的订单号         $out_refund_no = $order_msg[‘out_trade_no’];//用户请求退款id对应的out_trade_no订单号         $refund_fee =1;//退款金额         $total_fee = 2;//订单总金额                  //这里是按照顺序的 因为下面的签名是按照(字典序)顺序 排序错误 肯定出错         $post[‘appid’] = $appid;         $post[‘mch_id’] = $mch_id;         $post[‘nonce_str’] = $nonce_str;//随机字符串         $post[‘op_user_id’] = $mch_id;         $post[‘out_refund_no’] = $out_refund_no;         $post[‘out_trade_no’] = $out_trade_no;         $post[‘refund_fee’] = $refund_fee;         $post[‘total_fee’] = $total_fee;        //总金额 最低为一分钱 必须是整数                  $sign = MakeSign($post,$KEY);              //签名                  $post_xml = ‘xml                       appid’.$appid.’/appid                       mch_id’.$mch_id.’/mch_id                       nonce_str’.$nonce_str.’/nonce_str                       op_user_id’.$mch_id.’/op_user_id                       out_refund_no’.$out_refund_no.’/out_refund_no                       out_trade_no’.$out_trade_no.’/out_trade_no                       refund_fee’.$refund_fee.’/refund_fee                       total_fee’.$total_fee.’/total_fee                       sign’.$sign.’/sign                    /xml’;         //echo $post_xml;         //申请退款接口         $url = ‘https://api.mch.weixin.qq.com/secapi/pay/refund’;         $xml = curl_post_ssl($url,$post_xml);     //POST方式请求http,支付无需证书;申请退款api需要证书校验         $array = xml2array($xml);               //将【申请退款】api返回xml数据转换成数组,全要大写         //var_dump($array);         if($array[‘RETURN_CODE’] == ‘SUCCESS’ && $array[‘RESULT_开通空调清洗小程序电话:4006-838-530CODE’] == ‘SUCCESS’){//退款业务已受理//             //          $up[‘order_status’] = ‘refunding’;  //退款申请中    //          $up[‘order_id’] = $order_id;    //          $up[‘order_time’] = time();         //退款申请时间    //          if(M(“home_order”,”xxf_witkey_”)-save($up)){    //              $model_demo = new HomeModelHomeorderModel(‘home_order’,’xxf_witkey_’);    //              $res = $model_demo-home_order_list($userwx_info[‘uid’],19);    //              if(!$res){    //                  exit(‘订单信息有误’);    //              }else{    //                  echo json_encode($res);exit;    //              }    //          }          // for 调试             echo “业务结果:”.$array[‘RETURN_CODE’].”br”;             echo “错误代码:”.$array[‘err_code’].”br”;             echo “错误代码描述:”.$array[‘err_code_des’].”br”;             echo “公众账号ID:”.$array[‘APPID’].”br”;             echo “商户号:”.$array[‘MCH_ID’].”br”;             echo “子商户号:”.$array[‘sub_mch_id’].”br”;             echo “设备号:”.$array[‘device_info’].”br”;             echo “签名:”.$array[‘SIGN’].”br”;             echo “微信订单号:”.$array[‘transaction_id’].”br”;             echo “商户订单号:”.$array[‘OUT_TRADE_NO’].”br”;             echo “商户退款单号:”.$array[‘OUT_REFUND_NO’].”br”;             echo “微信退款单号:”.$array[‘refund_idrefund_id’].”br”;             echo “订单总金额:”.$array[‘TOTAL_FEE’].”br”;             echo “退款金额:”.$array[‘REFUND_FEE’].”br”;             echo “现金券退款金额:”.$array[‘coupon_refund_fee’].”br”;                  }else{            echo “RETURN_CODE:”.$array[‘RETURN_CODE’].”br”;             echo “RESULT_CODE:”.$array[‘RESULT_CODE’].”br”;             echo $array[‘RETURN_MSG’].”br”;             echo “错误代码:”.$array[‘ERR_CODE’].”br”;             echo “错误代码描述:”.$array[‘ERR_CODE_DES’].”br”;             exit;echo $array[‘RETURN_MSG’];exit;         }     }          /* php获取随机字符(数字+英文)函数,长度可以进行控制 */     function randomkeys($length) {         $key = null;         $pattern = ‘1234567890abcdefghijklmnopqrstuvwxyz                   ABCDEFGHIJKLOMNOPQRSTUVWXYZ’;         for ($i = 0; $i $length; $i++) {             $key .= $pattern {mt_rand(0, 35)};         }         return $key;     }     /**      * 生成签名, $KEY就是支付key      * @return 签名      */     function MakeSign( $params,$KEY){         //签名步骤一:按字典序排序数组参数         ksort($params);         $string = ToUrlParams($params);  //参数进行拼接key=value&k=v         //签名步骤二:在string后加入KEY         $string = $string . “&key=”.$KEY;         //签名步骤三:MD5加密         $string = md5($string);         //签名步骤四:所有字符转为大写         $result = strtoupper($string);         return $result;     }     /**      * 将参数拼接为url: key=value&key=value      * @param $params      * @return string      */     function ToUrlParams( $params ){         $string = ”;         if( !empty($params) ){             $array = array();             foreach( $params as $key = $value ){                 $array[] = $key.’=’.$value;             }             $string = implode(“&”,$array);         }         return $string;     }     //获取xml里面数据,转换成array     function xml2array($xml){         $p = xml_parser_create();         xml_parse_into_struct($p, $xml, $vals, $index);         xml_parser_free($p);         $data = “”;         foreach ($index as $key=$value) {             if($key == ‘xml’ || $key == ‘XML’) continue;   开通ps美工技能小程序电话:4006-838-530          $tag = $vals[$value[0]][‘tag’];             $value = $vals[$value[0]][‘value’];             $data[$tag] = $value;         }         return $data;     }    function curl_post_ssl($url, $vars, $second=30,$aHeader=array())     {         $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,FALSE);              //以下两种方式需选择一种              //第一种方法,cert 与 key 分别属于两个.pem文件         //默认格式为PEM,可以注释         curl_setopt($ch,CURLOPT_SSLCERTTYPE,’PEM’);         curl_setopt($ch,CURLOPT_SSLCERT,getcwd().’/cert/apiclient_cert.pem’);//getcwd()=》当前工作目录,不含最下级的/         //默认格式为PEM,可以注释         curl_setopt($ch,CURLOPT_SSLKEYTYPE,’PEM’);         curl_setopt($ch,CURLOPT_SSLKEY,getcwd().’/cert/apiclient_key.pem’);              //第二种方式,两个文件合成一个.pem文件         //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().’/all.pem’);              if( count($aHeader) = 1 ){             curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);         }              curl_setopt($ch,CURLOPT_POST, 1);         curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);         $data = curl_exec($ch); //返回xml         if($data){             curl_close($ch);             return $data;         }         else {             $error = curl_errno($ch);             echo “call faild, errorCode:$error”;             curl_close($ch);             return false;         }     }    ?!DOCTYPE HTMLhtmlhead    meta charset=”UTF-8″    title微信安全支付/title/headbody    /br/br/br/br    div align=”center”        form  action=”./refund_miniprogram.php” method=”post”            p申请退款:/p            p退款单号: input type=”text” name=”out_trade_no” value=?php echo $out_trade_no; ? /p            p退款金额(分): input type=”text” name=”refund_fee” value=?php echo $refund_fee; ? /p            button type=”submit” 提交/button        /form                /br        a href=”../index.php”返回首页/a    /div/body/html    

  DEMO2、在其他的php文件中引用该退款流程//在这里将订单的id、out_trade_no、out_refund_no、总金额、退款金额都拿到,然后进行下面步骤include_once S_ROOT.’xxpay/wxzf/refund_miniprogram.php’;    //小程序退款

?php/** * 退款申请接口-demo * ==================================================== * 注意:同一笔单的部分退款需要设置相同的订单号和不同的 * out_refund_no。一笔退款失败后重新提交,要采用原来的 * out_refund_no。总退款金额不能超过用户实际支付金额(现 * 金券金额不能退款)。*/                  $appid =        ‘wx6***1d’;//如果是公众号 就是公众号的appid;小程序就是小程序的appid         $mch_id =       ’12*****201′;         $KEY = ‘xi***********kj’;         $nonce_str =    randomkeys(32);//随机字符串         $op_user_id = $mch_id;         $out_trade_no = $order_no[‘out_trade_no’];//商户传微信的订单号         $out_refund_no = $order_no[‘out_trade_no’];//用户请求退款id对应的out_trade_no订单号//          $refund_fee = 1;//退款金额//          $total_fee = 2;//订单总金额         $refund_fee = intval($refund_amount*100);//退款金额         $total_fee = intval($total_amount*100);//订单总金额                  //这里是按照顺序的 因为下面的签名是按照(字典序)顺序 排序错误 肯定出错         $post[‘appid’] = $appid;         $post[‘mch_id’] = $mch_id;         $post[‘nonce_str’] = $nonce_str;//随机字符串         $post[‘op_user_id’] = $mch_id;         $post[‘out_refund_no’] = $out_refund_no;         $post[‘out_trade_no’] = $out_trade_no;         $post[‘refund_fee’] = $refund_fee;         $post[‘total_fee’] = $total_fee;        //总金额 最低为一分钱 必须是整数                  $sign = MakeSign($post,$KEY);              //签名                  $post_xml = ‘xml                       appid’.$appid.’/appid                       mch_id’.$mch_id.’/mch_id                       nonce_str’.$nonce_str.’/nonce_str                       op_user_id’.$mch_id.’/op_user_id                       out_refund_no’.$out_refund_no.’/out_refund_no                       out_trade_no’.$out_trade_no.’/out_trade_no                       refund_fee’.$refund_fee.’/refund_fee                       total_fee’.$total_fee.’/total_fee                       sign’.$sign.’/sign                    /xml’;         //echo $post_xml;         //申请退款接口         $url = ‘https://api.mch.weixin.qq.com/secapi/pay/refund’;         $xml = curl_post_ssl($url,$post_xml);     //POST方式请求http,支付无需证书;申请退款api需要证书校验         $array = xml2array($xml);               //将【申请退款】api返回xml数据转换成数组,全要大写         //var_dump($array);         if($array[‘RETURN_CODE’] == ‘SUCCESS’ && $array[‘RESULT_CODE’] == ‘SUCCESS’){//退款业务已受理//            //数据库更新操作                 /* for 调试  *///              echo “业务结果:”.$array[‘RETURN_CODE’].”br”;//              echo “错误代码:”.$array[‘err_code’].”br”;//              echo “错误代码描述:”.$array[‘err_code_des’].”br”;//              echo “公众账号ID:”.$array[‘APPID’].”br”;//              echo “商户号:”.$array[‘MCH_ID’].”br”;//              echo “子商户号:”.$array[‘sub_mch_id’].”br”;//              echo “设备号:”.$array[‘device_info’].”br”;//              echo “签名:”.$array[‘SIGN’].”br”;//              echo “微信订单号:”.$array[‘transaction_id’].”br”;//              echo “商户订单号:”.$array[‘OUT_TRADE_开通连衣裙小程序电话:4006-838-530NO’].”br”;//              echo “商户退款单号:”.$array[‘OUT_REFUND_NO’].”br”;//              echo “微信退款单号:”.$array[‘refund_idrefund_id’].”br”;//              echo “订单总金额:”.$array[‘TOTAL_FEE’].”br”;//              echo “退款金额:”.$array[‘REFUND_FEE’].”br”;//              echo “现金券退款金额:”.$array[‘coupon_refund_fee’].”br”;                  }else{             echo “RETURN_CODE:”.$array[‘RETURN_CODE’].”br”;             echo “RESULT_CODE:”.$array[‘RESULT_CODE’].”br”;             echo $array[‘RETURN_MSG’].”br”;             echo “错误代码:”.$array[‘ERR_CODE’].”br”;             echo “错误代码描述:”.$array[‘ERR_CODE_DES’].”br”;             exit;         }//      }          /* php获取随机字符(数字+英文)函数,长度可以进行控制 */

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 【微信小程序】退款功能教程(含沿河E企盈小程序申请退款和退款回调)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

模王系统开发

联系我们升级日志

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏