微信退款

 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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值