WeChat支払い-返金操作(JAVA版Spring Cloud Spring Boot)Linuxシステムベース
5267 ワード
本demoは、微信スキャンコードに基づいて返金を支払う
公式住所:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4
返金ロジックコード
公式住所:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4
/**
*
* @author hubl
* @date 2020/1/14
* @param * @param json 1
* @return com.qiang.base.page.JsonResult
*/
public JsonResult refund(JSONObject json) throws Exception {
Integer total_fee = json.getBigDecimal("total_fee").multiply(new BigDecimal("100")).setScale(2).intValue();
Integer refund_fee = json.getBigDecimal("refund_fee").multiply(new BigDecimal("100")).setScale(2).intValue();
HashMap map = new HashMap<>();
String appid = WxForm.appid;
String mch_id = WxForm.mch_id;
String nonce_str = WxUtils.createNonceStr();
String transaction_id = json.getString("transaction_id");
String out_refund_no = UtilUuId.get18UUID();
map.put("appid", appid);
map.put("mch_id", mch_id);
map.put("nonce_str", nonce_str);
map.put("sign_type", "MD5");
map.put("transaction_id", transaction_id);
map.put("out_refund_no",out_refund_no);
map.put("total_fee", total_fee.toString());
map.put("refund_fee", refund_fee.toString());
String stringA = WxUtils.formatBizQueryParaMap(map, false);// key=value , ASCII
System.out.println(stringA);
String stringSignTemp = stringA + "&key=" + WxForm.key;// API
String sign = MdFive.MD5(stringSignTemp).toUpperCase();
System.out.println(sign);
String xml = "" +
"" + appid + " " +
"" + mch_id + " " +
"" + nonce_str + " " +
"MD5 " +
""+transaction_id+" "+
""+out_refund_no+" "+
""+total_fee+" "+
""+refund_fee+" "+
"" + sign + " " +
" ";
System.out.println(xml);
String createOrderURL = "https://api.mch.weixin.qq.com/secapi/pay/refund";//
try {
//
Map map1 = WebUtils.doRefund(mch_id,createOrderURL, xml);// .p12 , , 。
String return_code = (String) map1.get("return_code");
String return_msg = (String) map1.get("return_msg");
if ("SUCCESS".equals(return_code) && "OK".equals(return_msg)) {
return JsonResult.ok("success").put("out_refund_no", out_refund_no);
} else {
System.out.println(JSONObject.toJSONString(map));
return JsonResult.error(" ");
}
} catch (Exception e) {
e.printStackTrace();
return JsonResult.error(" ");
}
}
返金ロジックコード
/**
*
* @param mchId ID
* @param url URL
* @param data
* @return
* @throws Exception
*/
public static Map doRefund(String mchId, String url, String data) throws Exception {
/**
* PKCS12 -》 -》 API
*/
KeyStore keyStore = KeyStore.getInstance("PKCS12");//
// FileInputStream
ClassPathResource cl = new ClassPathResource("xxx cert.p12");// linux ,
try {
// .. MCHID
keyStore.load(cl.getInputStream(), mchId.toCharArray());
} finally {
cl.getInputStream().close();
}
SSLContext sslcontext = SSLContexts.custom()
//
.loadKeyMaterial(keyStore, mchId.toCharArray())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new StringEntity(data, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
//
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);
Map map = XMLUtils.parseXmlToList(jsonStr);
return map;
} finally {
response.close();
}
} finally {
httpclient.close();
}
}