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;
}
微信退款
最新推荐文章于 2021-03-20 18:06:52 发布