diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java index c4bfb56c..ce15303b 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java @@ -259,15 +259,40 @@ public class HighTestController { @RequestMapping(value = "/unionPay", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "银联支付") - public ResponseData unionPay() { + public ResponseData unionPay(@RequestParam(name = "merchantId", required = true) Long merchantId) { try { - return ResponseMsgUtil.success(UnionPayConfig.apppayacc()); + return ResponseMsgUtil.success(UnionPayConfig.upPreOrder("HF"+System.currentTimeMillis(), 1L, "test")); } catch (Exception e) { log.error("HighOrderController --> getOrderById() error!", e); return ResponseMsgUtil.exception(e); } } + @RequestMapping(value = "/unionTradeQuery", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "银联交易查询") + public ResponseData unionTradeQuery(@RequestParam(name = "paySerialNo", required = true) String paySerialNo) { + try { + return ResponseMsgUtil.success(UnionPayConfig.tradeQuery(paySerialNo)); + } catch (Exception e) { + log.error("HighOrderController --> unionTradeQuery() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/unionZwrefund", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "银联交易退货") + public ResponseData unionZwrefund(@RequestParam(name = "orderNo", required = true) String orderNo, + @RequestParam(name = "paySerialNo", required = true) String paySerialNo) { + try { + return ResponseMsgUtil.success(UnionPayConfig.zwrefund(orderNo,paySerialNo,1L)); + } catch (Exception e) { + log.error("HighOrderController --> unionTradeQuery() error!", e); + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value = "/wxSplitAccount", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "微信分账") diff --git a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java index 78b92d62..b87255a1 100644 --- a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java @@ -504,6 +504,45 @@ public class HttpsUtils { return null; } + public static JSONObject doPostForm(String apiUrl, String body, Map header) { + CloseableHttpClient httpClient = null; + if (apiUrl.startsWith("https")) { + httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) + .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); + } else { + httpClient = HttpClients.createDefault(); + } + String httpStr = null; + CloseableHttpResponse response = null; + + try { + HttpPost httpPost = new HttpPost(apiUrl); + httpPost.setConfig(requestConfig); + for (Map.Entry entry : header.entrySet()) { + httpPost.setHeader(entry.getKey(), entry.getValue().toString()); + } + StringEntity stringEntity = new StringEntity(body);// 解决中文乱码问题 + stringEntity.setContentEncoding("UTF-8"); + stringEntity.setContentType("application/x-www-form-urlencoded"); + httpPost.setEntity(stringEntity); + response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + httpStr = EntityUtils.toString(entity, "UTF-8"); + return JSON.parseObject(httpStr); + } catch (Exception e) { + log.error(e.getMessage(),e); + } finally { + if (response != null) { + try { + EntityUtils.consume(response.getEntity()); + } catch (IOException e) { + log.error(e.getMessage(),e); + } + } + } + return null; + } + public static JSONObject doSmsPost(String apiUrl, Map body, Map header) { CloseableHttpClient httpClient = null; if (apiUrl.startsWith("https")) { diff --git a/hai-service/src/main/java/com/hai/config/CommonSysConfig.java b/hai-service/src/main/java/com/hai/config/CommonSysConfig.java index 51d1e0a2..80cf450e 100644 --- a/hai-service/src/main/java/com/hai/config/CommonSysConfig.java +++ b/hai-service/src/main/java/com/hai/config/CommonSysConfig.java @@ -31,6 +31,12 @@ public class CommonSysConfig { private String HuiLianTongUnionCardAccessCode; private String HuiLianTongUnionCardSignCode; + private String unionPayUrl; + private String unionPayChannelid; + private String unionPayMerid; + private String unionPayTermid; + private String unionPaySignKey; + private String wx_cert; private String telApiKey; @@ -231,4 +237,43 @@ public class CommonSysConfig { this.telUrl = telUrl; } + public String getUnionPayUrl() { + return unionPayUrl; + } + + public void setUnionPayUrl(String unionPayUrl) { + this.unionPayUrl = unionPayUrl; + } + + public String getUnionPayChannelid() { + return unionPayChannelid; + } + + public void setUnionPayChannelid(String unionPayChannelid) { + this.unionPayChannelid = unionPayChannelid; + } + + public String getUnionPayMerid() { + return unionPayMerid; + } + + public void setUnionPayMerid(String unionPayMerid) { + this.unionPayMerid = unionPayMerid; + } + + public String getUnionPayTermid() { + return unionPayTermid; + } + + public void setUnionPayTermid(String unionPayTermid) { + this.unionPayTermid = unionPayTermid; + } + + public String getUnionPaySignKey() { + return unionPaySignKey; + } + + public void setUnionPaySignKey(String unionPaySignKey) { + this.unionPaySignKey = unionPaySignKey; + } } diff --git a/hai-service/src/main/java/com/hai/config/UnionPayConfig.java b/hai-service/src/main/java/com/hai/config/UnionPayConfig.java index 252675d3..893bd526 100644 --- a/hai-service/src/main/java/com/hai/config/UnionPayConfig.java +++ b/hai-service/src/main/java/com/hai/config/UnionPayConfig.java @@ -7,6 +7,7 @@ import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.MD5Util; import org.apache.commons.lang3.StringUtils; +import java.math.BigDecimal; import java.util.*; /** @@ -15,39 +16,73 @@ import java.util.*; */ public class UnionPayConfig { - public static JSONObject apppayacc() throws Exception { + /** + * APP控件支付 + * 文档地址: https://doc.eycard.cn/web/#/42?page_id=1516 + * @param orderNo 订单号 + * @param payPrice 支付金额 【单位:分】 + * @param orderDesc 订单描述 或 商品描述;例如:购买中石化加油券300元。 + * @return 响应信息 + * @throws Exception + */ + public static JSONObject upPreOrder(String orderNo, Long payPrice, String orderDesc) throws Exception { Map paramMap = new HashMap<>(); -/* paramMap.put("channelid", "D01X66666666761"); - paramMap.put("merid", "531000012972390"); - paramMap.put("termid", "W1489519"); - paramMap.put("tradetrace", System.currentTimeMillis()); - paramMap.put("opt", "dirBankPay"); - paramMap.put("tradetype", "APP"); - paramMap.put("tradeamt", "1"); - paramMap.put("body", "test"); - paramMap.put("notifyurl", "notifyurl"); - paramMap.put("returnurl", "returnurl");*/ - - paramMap.put("channelid", "D01X66666666761"); - paramMap.put("merid", "531000012972390"); - paramMap.put("termid", "W1489519"); - paramMap.put("tradetrace", System.currentTimeMillis()); + paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid()); + paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid()); + paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid()); + paramMap.put("tradetrace", orderNo); paramMap.put("opt", "upPreOrder"); paramMap.put("tradetype", "APP"); - paramMap.put("tradeamt", "1"); - paramMap.put("body", "test"); + paramMap.put("tradeamt", payPrice); + paramMap.put("body", orderDesc); paramMap.put("notifyurl", "notifyurl"); + return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>()); + } - String reqStr = generateSignature(paramMap, null); - paramMap.put("sign", MD5Util.encode(generateSignature(paramMap,"sa72ivadl84273dlj2kdaia2idiadik5").getBytes()).toLowerCase()); - reqStr += "&sign=" + MD5Util.encode(generateSignature(paramMap,"sa72ivadl84273dlj2kdaia2idiadik5").getBytes()).toUpperCase(); - Map body = new LinkedHashMap<>(); - body.put("from", reqStr); - return HttpsUtils.doSmsPost("https://notify-test.eycard.cn:7443/WorthTech_Access_AppPaySystemV2/apppayacc", body, new HashMap<>()); + /** + * 交易查询 + * @param paySerialNo 第三方支付流水 + * @return + * @throws Exception + */ + public static JSONObject tradeQuery(String paySerialNo) throws Exception { + Map paramMap = new HashMap<>(); + paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid()); + paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid()); + paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid()); + paramMap.put("tradetrace", paySerialNo); + paramMap.put("opt", "tradeQuery"); + paramMap.put("tradetype", "samecardQuery"); + return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>()); } - public static String generateSignature(final Map data, String key){ - Set keySet = data.keySet(); + /** + * 交易退货 + * @param orderNo 订单号 + * @param paySerialNo 第三方订单号 + * @param refundPrice 退款金额 + * @return + * @throws Exception + */ + public static JSONObject zwrefund(String orderNo,String paySerialNo,Long refundPrice) throws Exception { + Map paramMap = new HashMap<>(); + paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid()); + paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid()); + paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid()); + paramMap.put("tradetrace", orderNo); + paramMap.put("oritradetrace", paySerialNo); + paramMap.put("tradeamt", refundPrice); + paramMap.put("opt", "zwrefund"); + return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>()); + } + + /** + * 参数排序 + * @param param + * @return + */ + private static String paramSort(final Map param) { + Set keySet = param.keySet(); String[] keyArray = keySet.toArray(new String[keySet.size()]); Arrays.sort(keyArray); StringBuilder sb = new StringBuilder(); @@ -56,15 +91,31 @@ public class UnionPayConfig { continue; } if (StringUtils.isBlank(sb.toString())) { - sb.append(k).append("=").append(data.get(k)); + sb.append(k).append("=").append(param.get(k)); } else { - sb.append("&").append(k).append("=").append(data.get(k)); + sb.append("&").append(k).append("=").append(param.get(k)); } } - if (StringUtils.isNotBlank(key)) { - sb.append("&key=").append(key); - } return sb.toString(); } + /** + * 生成请求参数 + * @param data + * @return + * @throws Exception + */ + private static String generateRequestParam(final Map data) throws Exception{ + String paramSort = paramSort(data); + + // 生产签名参数 + String signParam = paramSort; + // 签名参数拼接签名秘钥 + signParam += "&key=" + CommonSysConst.getSysConfig().getUnionPaySignKey(); + + // 请求参数拼接签名 + paramSort += "&sign=" + MD5Util.encode(signParam.getBytes()); + return paramSort; + } + } diff --git a/hai-service/src/main/resources/dev/commonConfig.properties b/hai-service/src/main/resources/dev/commonConfig.properties index 52a935fe..17b4fbd2 100644 --- a/hai-service/src/main/resources/dev/commonConfig.properties +++ b/hai-service/src/main/resources/dev/commonConfig.properties @@ -1,24 +1,36 @@ +# ???? tuanYouUrl=https://test02-motorcade-hcs.czb365.com tuanYouAppKey=208241666939552 tuanYouAppSecret=adecc3cff077834cb8632c8ab3bec0e6 tuanYouDieselAccount=9b115eao4400 tuanYouGasolineAccount=9b115e5g4400 +# ???? qinzhuUrl=https://live-test.qianzhu8.com qinzhuHtmlUrl=https://m-test.qianzhu8.com qinzhuPlatformId=10376 qinzhuSecret=ktxb49sh2jfhgn8g qianzhuOrderNotify=https://hsg.dctpay.com/crest/qianzhu/orderNotify +# ??????? huiliantongUrl=https://gzapitest.deepermobile.com.cn:441 huiliantongAppNo=guizhouhltcs huiliantongAppkey=g%2BNnjR54YSUWM2iKg%2Fd82A9x9hg2kYV7 huiliantongAppsecret=FA28E95ACABFA4B2B8E25857437B07F1 +# ???????? HuiLianTongUnionCardUrl=http://hltgz.com:4010/api/v2/execute.json HuiLianTongUnionCardAccessCode=6FCAE1470CEF465988351BB65ABAA8AE HuiLianTongUnionCardSignCode=F8E91A3C +# ?????? +unionPayUrl=https://notify-test.eycard.cn:7443/WorthTech_Access_AppPaySystemV2/apppayacc +unionPayChannelid=D01X66666666761 +unionPayMerid=531000012972390 +unionPayTermid=W1489519 +unionPaySignKey=sa72ivadl84273dlj2kdaia2idiadik5 + +# ?????? wx_cert=/home/project/wx_cert/ TelApiKey=2d01f6b520254b1a80f6b167832cea18