UnionPay.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.ym.mec.thirdparty.union;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.commons.codec.binary.Base64;
  4. import org.apache.commons.codec.digest.DigestUtils;
  5. import org.apache.commons.lang3.RandomStringUtils;
  6. import org.apache.commons.lang3.time.DateFormatUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import javax.crypto.Mac;
  9. import javax.crypto.spec.SecretKeySpec;
  10. import java.math.BigDecimal;
  11. import java.net.URI;
  12. import java.net.URLEncoder;
  13. import java.security.InvalidKeyException;
  14. import java.security.NoSuchAlgorithmException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.UUID;
  20. /**
  21. * 银联商务主扫
  22. */
  23. public class UnionPay {
  24. private UnionPayFeignService unionPayFeignService;
  25. static String aliPayUrl = "http://58.247.0.18:29015/v1/netpay/trade/h5-pay";//支付宝
  26. static String qmfPayUrl = "http://58.247.0.18:29015/v1/netpay/qmf/h5-pay";//银联无卡
  27. static String H5AppId = "10037e6f6a4e6da4016a670fd4530012";
  28. static String h5AppKey = "f7a74b6c02ae4e1e94aaba311c04acf2";
  29. static String h5Mid = "898310148160568";
  30. static String h5Tid = "88880001";
  31. static String wpPayUrl = "http://58.247.0.18:29015/v1/netpay/webpay/pay";//微信
  32. static String wpAppId = "10037e6f6a4e6da4016a62a47e51000c";
  33. static String wpAppKey = "b4b70d123a724972bc21f445b0b9f75c";
  34. static String wpMid = "898460107420248";
  35. static String wpTid = "00000001";
  36. static String queryUrl = "http://58.247.0.18:29015/v1/netpay/query"; //订单查询地址
  37. static String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
  38. static String nonce = UUID.randomUUID().toString().replace("-", "");
  39. public UnionPay(UnionPayFeignService unionPayFeignService) {
  40. this.unionPayFeignService = unionPayFeignService;
  41. }
  42. private static byte[] hmacSHA256(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
  43. String algorithm = "HmacSHA256";
  44. Mac mac = Mac.getInstance(algorithm);
  45. mac.init(new SecretKeySpec(key, algorithm));
  46. return mac.doFinal(data);
  47. }
  48. private static String getOpenBodySig(String appId, String appKey, String timestamp, String nonce, String content, String method) throws Exception {
  49. String signatureStr = appId + timestamp + nonce + DigestUtils.sha256Hex(content);
  50. byte[] localSignature = hmacSHA256(signatureStr.getBytes(), appKey.getBytes());
  51. if (method.toUpperCase().equals("POST")) {
  52. return ("OPEN-BODY-SIG AppId=" + "\"" + appId + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + Base64.encodeBase64String(localSignature) + "\"");
  53. } else {
  54. return ("authorization=OPEN-FORM-PARAM" + "&appId=" + appId + "&timestamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(content, "UTF-8") + "&signature=" + URLEncoder.encode(Base64.encodeBase64String(localSignature), "UTF-8"));
  55. }
  56. }
  57. /**
  58. * 获取支付链接的Map
  59. *
  60. * @param amount
  61. * @param orderNo
  62. * @param notifyUrl
  63. * @param returnUrl
  64. * @param orderSubject
  65. * @return
  66. * @throws Exception
  67. */
  68. public static Map<String, String> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject) throws Exception {
  69. Map<String, String> PayMap = new HashMap<>();
  70. JSONObject json = new JSONObject();
  71. //H5支付链接生成
  72. json.put("totalAmount", amount.multiply(new BigDecimal("100")).intValue());
  73. json.put("instMid", "H5DEFAULT");
  74. json.put("mid", h5Mid);
  75. json.put("tid", h5Tid);
  76. json.put("orderDesc", orderSubject);
  77. json.put("merOrderId", "1017" + orderNo);
  78. json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
  79. json.put("expireTime", DateFormatUtils.format(new Date().getTime() + 300000, "yyyy-MM-dd HH:mm:ss"));
  80. // json.put("notifyUrl", notifyUrl);
  81. json.put("returnUrl", returnUrl);
  82. String param = getOpenBodySig(H5AppId, h5AppKey, timestamp, nonce, json.toString(), "GET");
  83. PayMap.put("aliPay", aliPayUrl + "?" + param);
  84. PayMap.put("qmfPay", qmfPayUrl + "?" + param);
  85. //微信公众号支付链接生成
  86. json.put("instMid", "YUEDANDEFAULT");
  87. json.put("mid", wpMid);
  88. json.put("tid", wpTid);
  89. param = getOpenBodySig(wpAppId, wpAppKey, timestamp, nonce, json.toString(), "GET");
  90. PayMap.put("weChatPay", wpPayUrl + "?" + param);
  91. return PayMap;
  92. }
  93. /**
  94. * 订单查询
  95. *
  96. * @param orderNo 自己系统订单号
  97. * @return 1017201910111756231647562047 测试订单号
  98. */
  99. public Map<String, Object> query(String orderNo) throws Exception {
  100. JSONObject json = new JSONObject();
  101. json.put("instMid", "H5DEFAULT");
  102. json.put("mid", h5Mid);
  103. json.put("tid", h5Tid);
  104. json.put("merOrderId", "1017" + orderNo);
  105. json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
  106. String authorization = getOpenBodySig(wpAppId, wpAppKey, timestamp, nonce, json.toString(), "POST");
  107. HashMap<String, String> header = new HashMap<>();
  108. header.put("Authorization", authorization);
  109. header.put("Content-Type", "application/json; charset=utf-8");
  110. Map query = unionPayFeignService.query(json.getInnerMap(), header);
  111. return query;
  112. }
  113. public static void main(String[] args) throws Exception {
  114. String orderNo = "1017" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomStringUtils.randomNumeric(7);
  115. System.out.println(orderNo);
  116. Map<String, String> payMap = getPayMap(new BigDecimal("0.01"), orderNo, "http://pay.dayaedu.com/pay/notify", "http://dev.daya.com", "大雅测试订单");
  117. System.out.println("weChat= " + payMap.get("weChatPay"));
  118. System.out.println("H5= " + payMap.get("aliPay"));
  119. System.out.println("qmfPay= " + payMap.get("qmfPay"));
  120. }
  121. }