1. 程式人生 > >申請退款

申請退款

pac 金額 create script cep 業務 mmm ets tor

微信公眾號支付請看上一篇文章:
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

申請退款