申请退款

Posted For_elegant

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了申请退款相关的知识,希望对你有一定的参考价值。

微信公众号支付请看上一篇文章: 
http://blog.csdn.net/aofavx/article/details/52220394 
需要用到的Java类都在: 
http://download.csdn.net/detail/aofavx/9606697

微信公众号退款相对支付来说就简单多了,唯一需要注意的地方就是:在支付的时候需要用到商户的证书。 
退款代码如下: 
jsp提交退款申请:

//退款
function refundWXPay() {
    window.location.href="refundPay?orderId=${orderId}";
}
  • 1
  • 2
  • 3
  • 4

java代码:

    /**
     *微信公众号申请退款
     * @param orderId 订单id
     * @param total_fee 退款金额
     * @param refund_fee 退款金额
     * @param response
     * @return
     * @throws Exception 
     */
    @RequestMapping(value="/refundPay")
    @ResponseBody
    private ModelAndView refundwx(HttpServletRequest request,HttpServletResponse response) throws Exception{
        logBefore(logger, "退款");
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();

        /*--------1.初始化数据参数----------*/
        String appId= "wxfd7c065eee11112222";
        String secret= "b5b3a627f5d1f8888888888888";
        String shh= "111111111;
        String key= "mmmmmmmmmmmmmmm";
        String filePath = "E:\\证书\\apiclient_cert.p12"; //退款需要提供证书数据,所以需要根据证书路径读取证书
        //需要退款的商户订单号,对应提交订单中的out_trade_no
        String orderId = request.getParameter("orderId").toString();
        String total_fee=WeChat.getMoney(request.getParameter("total_fee"));  //也可以根据业务场景从数据库中获取总金额和退款金额
        String refund_fee=WeChat.getMoney(request.getParameter("refund_fee"));
        Map<String,String> result = (Map<String, String>) wxRefund(request,response,appId,secret,shh,key,orderId,total_fee,refund_fee,filePath);

        /*
        根据result的返回状态,处理你的业务逻辑
        .....
        */

        mv.setViewName("wechat/refundFind");
        return mv;
    }

    private Object wxRefund(HttpServletRequest request,HttpServletResponse response,String appId,
                        String secret,String shh,String key,String orderId,String total_fee,String refund_fee,String path){
        Map<String,String> result=new HashMap<String,String>(); 
        PageData pd = new PageData();
        pd = this.getPageData();
        String refundid = UuidUtil.get32UUID();
        String nonce_str = MD5.getMessageDigest(String.valueOf(new Random().nextInt(10000)).getBytes());

        /*-----  1.生成预支付订单需要的的package数据-----*/
        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("appid", appId);  
        packageParams.put("mch_id", shh);  
        packageParams.put("nonce_str", nonce_str);  
        packageParams.put("op_user_id", shh);  
        packageParams.put("out_trade_no", orderId);  
        packageParams.put("out_refund_no", refundid);  
        packageParams.put("total_fee",total_fee);  
        packageParams.put("refund_fee", refund_fee);  
        /*----2.根据package生成签名sign---- */
        RequestHandler reqHandler = new RequestHandler(request, response);
        reqHandler.init(appId, secret, key);
        String sign = reqHandler.createSign(packageParams);

        /*----3.拼装需要提交到微信的数据xml---- */
        String xml="<xml>"
        +"<appid>"+appId+"</appid>"
        + "<mch_id>"+shh+"</mch_id>"
        + "<nonce_str>"+nonce_str+"</nonce_str>"
        + "<op_user_id>"+shh+"</op_user_id>"
        + "<out_trade_no>"+orderId+"</out_trade_no>"
        + "<out_refund_no>"+refundid+"</out_refund_no>"
        + "<refund_fee>"+refund_fee+"</refund_fee>"
        + "<total_fee>"+total_fee+"</total_fee>"
        + "<sign>"+sign+"</sign>"
        +"</xml>";
         try {
             /*----4.读取证书文件,这一段是直接从微信支付平台提供的demo中copy的,所以一般不需要修改---- */
             KeyStore keyStore  = KeyStore.getInstance("PKCS12");
             FileInputStream instream = new FileInputStream(new File(path));
             try {
                 keyStore.load(instream, shh.toCharArray());
             } finally {
                 instream.close();
             }
             // Trust own CA and all self-signed certs
             SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, shh.toCharArray()).build();
             // Allow TLSv1 protocol only
             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null,
                     SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
             CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

             /*----5.发送数据到微信的退款接口---- */
             String url="https://api.mch.weixin.qq.com/secapi/pay/refund";
             HttpPost httpost= HttpClientConnectionManager.getPostMethod(url);
             httpost.setEntity(new StringEntity(xml, "UTF-8"));
             HttpResponse weixinResponse = httpClient.execute(httpost);
             String jsonStr = EntityUtils.toString(weixinResponse.getEntity(), "UTF-8");
             logger.info(jsonStr);

             Map map = GetWxOrderno.doXMLParse(jsonStr);
             if("success".equalsIgnoreCase((String) map.get("return_code"))){
                 logger.info("退款成功");
                 result.put("returncode", "ok");
                 result.put("returninfo", "退款成功");
             }else{
                 logger.info("退款失败");
                 result.put("returncode", "error");
                 result.put("returninfo", "退款失败");
             }
        } catch (Exception e) {
            logger.info("退款失败");
            result.put("returncode", "error");
            result.put("returninfo", "退款失败");
        }
        return result;

    }




来源:http://blog.csdn.net/yuexianchang/article/details/52433966

以上是关于申请退款的主要内容,如果未能解决你的问题,请参考以下文章

浅析微信支付:申请退款退款回调接口查询退款

微信小程序订单完成怎么申请退款流程?

申请退款后,优惠券有没有退回呢

淘宝使用了优惠券后怎么申请退款

Wirecard 退款申请

申请退款