微信退款

 public PaymentResult refund(String orderNumber,Double payAmount, Double refundAmount, String title, String description) {
        JSONObject retJson = new JSONObject();
        try {

            SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
//            商戶信息(固定)
            packageParams.put("appid", APPID);      //公衆賬號ID
            packageParams.put("mch_id", MCHID);     //商戶號
            packageParams.put("nonce_str", WechatUtils.getNonce_str());      //隨機字符串

            //需要傳遞的參數
            packageParams.put("out_trade_no", orderNumber);        //商戶訂單號
            packageParams.put("out_refund_no", "TK"+orderNumber);     //商戶系統內部的退款單號,商戶系統內部唯一,同一退款單號多次請求只退一筆
            packageParams.put("total_fee", String.valueOf((long)(payAmount*100)));        //標價金額  訂單總金額,單位爲分,詳見支付金額
            packageParams.put("refund_fee", String.valueOf((long)(refundAmount*100)));        //退款金額
            packageParams.put("op_user_id", MCHID);//操作人員,默認爲商戶賬號

            //生成簽名字符串
            String sign = PayCommonUtil.createSign("UTF-8", packageParams, KEY);
            packageParams.put("sign", sign);    //簽名

            String requestXML = PayCommonUtil.getRequestXml(packageParams);

            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File("C:/apiclient_cert.p12"));//放退款證書的路徑
            try {
                keyStore.load(instream, MCHID.toCharArray());
            } finally {
                instream.close();
            }

            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, MCHID.toCharArray()).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    new String[] { "TLSv1" },
                    null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            log.info("退款請求:" + requestXML);
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            try {

                HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");//退款接口

                log.info("executing request" + httpPost.getRequestLine());
                StringEntity reqEntity  = new StringEntity(requestXML);
                // 設置類型
                reqEntity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(reqEntity);
                CloseableHttpResponse chr = httpclient.execute(httpPost);
                try {
                    HttpEntity entity = chr.getEntity();

                    System.out.println("----------------------------------------");
                    System.out.println(chr.getStatusLine());
                    StringBuffer rspXml = new StringBuffer();
                    if (entity != null) {
                        System.out.println("Response content length: " + entity.getContentLength());
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
                        String text;
                        while ((text = bufferedReader.readLine()) != null) {
                            rspXml.append(text);
                        }

                    }
                    log.info("退款響應:"+rspXml.toString());
                    SortedMap map = XmlUtils.parseXmlStr(rspXml.toString());
                    EntityUtils.consume(entity);
                    PaymentResult result = new PaymentResult();
                    result.setPayment(Payment.WeChat);
                    if(map.get("result_code").equals("SUCCESS")){
                        result.setCode("SUCCESS");
                        return result;
                    }else {
                        result.setCode("FAIL");
                        result.setData(map.get("err_code_des").toString());
                        return result;
                    }

                } finally {
                    chr.close();
                }
            } finally {
                httpclient.close();
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章