GpayUtil.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.xc.utils.pay;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.xc.utils.PropertiesUtil;
  4. import org.apache.commons.codec.digest.DigestUtils;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.HttpClientBuilder;
  12. import org.apache.http.util.EntityUtils;
  13. import java.text.SimpleDateFormat;
  14. import java.util.*;
  15. public class GpayUtil {
  16. //公户
  17. final static String UID = PropertiesUtil.getProperty("gpay.uid");
  18. final static String MCHID = PropertiesUtil.getProperty("gpay.mchid");
  19. final static String SECRET = PropertiesUtil.getProperty("gpay.secret");
  20. final static String ORDER_URL= PropertiesUtil.getProperty("gpay.url") + "/payapi/v3/orders";
  21. final static String NOTIFY_URL= PropertiesUtil.getProperty("gpay.notify.url");
  22. //私户
  23. final static String UID2 = PropertiesUtil.getProperty("gpay.uid2");
  24. final static String MCHID2 = PropertiesUtil.getProperty("gpay.mchid2");
  25. final static String SECRET2 = PropertiesUtil.getProperty("gpay.secret2");
  26. final static String shopid365 = PropertiesUtil.getProperty("gpay.shopid");
  27. final static String key365 = PropertiesUtil.getProperty("gpay.key");
  28. final static String return_url365 = PropertiesUtil.getProperty("gpay.return_url");
  29. final static String callback_url365 = PropertiesUtil.getProperty("gpay.callback_url");
  30. final static String url365 = PropertiesUtil.getProperty("gpay.url365");
  31. // public static void main(String[] args) {
  32. // Map<String, Object> map = new HashMap<String, Object>();
  33. // map.put("uid", UID);
  34. // map.put("mchid", MCHID);
  35. // map.put("mch_orderid", "test123456");
  36. // map.put("amount", "10.00");
  37. // map.put("channel", "300"); // 100 支付寶 200 微信 300 銀行卡; 開通請聯繫運營
  38. // map.put("notify_url", "http://0.0.0.0:8081/test/notify"); // 回調地址
  39. // map.put("return_url", "http://0.0.0.0:8081/test/return"); // 支付成功後跳轉地址
  40. // map.put("product_name", "商品1");
  41. // map.put("member_name", "會員1");
  42. // map.put("extra", "透傳");
  43. // map.put("timestamp", "2020-12-17 13:18:36");
  44. // // 签名
  45. // String sign = sign(map, SECRET);
  46. // JSONObject json = new JSONObject(map);
  47. // json.put("sign", sign);
  48. // // 下单
  49. // JSONObject response = doPost(ORDER_URL, json);
  50. // System.out.println("ORDER_URL: "+ORDER_URL);
  51. // System.out.println("response: "+response);
  52. // // 回调
  53. // String data = "{'tradeid':'5fc49d05e5bf59cbad7f92ec', 'uid':'5fc4843fe5bf59cbad7f8ff2', 'mchid':'5fc4843fe5bf59cbad7f8ff3', 'mch_orderid':'123','channel':'300', 'currency':'CNY', 'amount':'10.00', 'timestamp':'2020-11-30 15:20:41', 'extra':'透傳', 'status':'success', 'sign':'3a2ac4dfc1456c2301b2227d6b303045'}";
  54. // notify(data);
  55. // }
  56. public static void main(String[] args) {
  57. // JSONObject jb = gPay("19.00" , "778899" , "55555");
  58. // System.out.println(jb.getString("msg"));
  59. // JSONObject data = (JSONObject) jb.get("data");
  60. // JSONObject jbb = JSON.parseObject(data.getString("result"));
  61. // System.out.println(jbb.getString("url"));
  62. // JSONArray result = data.getJSONArray("result");
  63. // String url = (String)jb.get("result");
  64. // System.out.println(result);
  65. // String data = "{'tradeid':'5fdb22d04ff77f70fef29523', 'uid':'5fc5ae01ae8c2dea7a6eea55', 'mchid':'5fc5ae01ae8c2dea7a6eea56', 'mch_orderid':'778899','channel':'300', 'amount':'19.00', 'timestamp':'2020-12-17 17:20:17', 'extra':'extra', 'status':'success', 'sign':'2c87ce12af764362115f442b0a596cc2'}";
  66. // JSONObject json = JSONObject.parseObject(data);
  67. // Map<String, Object> map = (Map<String, Object>) json;
  68. // String sign = sign(map, SECRET);
  69. //
  70. //回调
  71. // if (!map.get("sign").toString().equals(sign)) {
  72. // System.out.println("签名不一致:"+map.get("sign").toString());
  73. // }
  74. // notify365("{\"order_status\":4,\"amount\":\"110.00\",\"orderno\":\"21123\",\"seller_name\":\"200\",\"price\":\"6.56\",\"qty\":\"16.768292\",\"sign\":\"D55BF386C8E1DC24151B014E915DCB70\",\"buyer_name\":\"无\"}");
  75. // gPay365("130.00","202012241515","测试12241514","1211");
  76. }
  77. public static JSONObject gPay(String amount , String ordersn , String phone){
  78. Map<String, Object> map = new HashMap();
  79. map.put("uid", UID);
  80. map.put("mchid", MCHID);
  81. map.put("mch_orderid", ordersn);
  82. map.put("amount", amount);
  83. map.put("channel", "300");
  84. map.put("notify_url", NOTIFY_URL);
  85. map.put("member_name", phone);
  86. map.put("extra", "extra");
  87. map.put("timestamp", generateTime());
  88. // 签名
  89. String sign = sign(map, SECRET);
  90. JSONObject json = new JSONObject(map);
  91. json.put("sign", sign);
  92. // System.out.println("uid:" + UID);
  93. // System.out.println("mchid:" + MCHID);
  94. // System.out.println("notify_url:" + NOTIFY_URL);
  95. // System.out.println("SECRET:" + SECRET);
  96. // 下单
  97. JSONObject response = doPost(ORDER_URL, json);
  98. return response;
  99. }
  100. public static JSONObject gPay2(String amount , String ordersn , String phone){
  101. Map<String, Object> map = new HashMap();
  102. map.put("uid", UID2);
  103. map.put("mchid", MCHID2);
  104. map.put("mch_orderid", ordersn);
  105. map.put("amount", amount);
  106. map.put("channel", "300");
  107. map.put("notify_url", NOTIFY_URL);
  108. map.put("member_name", phone);
  109. map.put("extra", "extra");
  110. map.put("timestamp", generateTime());
  111. // 签名
  112. String sign = sign(map, SECRET2);
  113. JSONObject json = new JSONObject(map);
  114. json.put("sign", sign);
  115. // 下单
  116. JSONObject response = doPost(ORDER_URL, json);
  117. // System.out.println(response.toString());
  118. return response;
  119. }
  120. // sign
  121. public static String sign(Map<String, Object> map, String signKey) {
  122. if (map == null) {
  123. return null;
  124. }
  125. List<String> keyList = new ArrayList<>(map.keySet());
  126. Collections.sort(keyList);
  127. StringBuffer sb = new StringBuffer();
  128. for (int i = 0; i < keyList.size(); i++) {
  129. String key = keyList.get(i);
  130. Object value = map.get(key);
  131. if (key.equals("sign") || value.equals("")) {
  132. continue;
  133. }
  134. sb.append(key + "=" + value + "&");
  135. }
  136. String signStr = sb.substring(0, sb.length() - 1) + signKey;
  137. // System.out.println("before sign: " + signStr);
  138. String md5Str = DigestUtils.md5Hex(signStr);
  139. // System.out.println("after sign: " + md5Str);
  140. return md5Str;
  141. }
  142. // http request
  143. public static JSONObject doPost(String url, JSONObject jsonObject) {
  144. HttpClient client = HttpClientBuilder.create().build();
  145. HttpPost post = new HttpPost(url);
  146. JSONObject response = null;
  147. try {
  148. StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
  149. s.setContentType("application/json;utf-8");
  150. post.setEntity(s);
  151. HttpResponse res = client.execute(post);
  152. HttpEntity entity = res.getEntity();
  153. String result = EntityUtils.toString(entity);
  154. response = JSONObject.parseObject(result);
  155. if (res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
  156. throw new Error(response.toJSONString());
  157. }
  158. } catch (Exception e) {
  159. throw new RuntimeException(e);
  160. }
  161. return response;
  162. }
  163. // 回调通知
  164. public String notify(String data) {
  165. System.out.println(data);
  166. JSONObject json = JSONObject.parseObject(data);
  167. Map<String, Object> map = (Map<String, Object>) json;
  168. System.out.println(json.toString());
  169. System.out.println(map.toString());
  170. // 验证签名
  171. String sign = sign(map, SECRET);
  172. if (!map.get("sign").toString().equals(sign)) {
  173. System.out.println("签名不一致:"+map.get("sign").toString());
  174. return "签名不一致";
  175. }
  176. String amount = map.get("amount").toString();
  177. String orderSn = map.get("mch_orderid").toString();
  178. String tradeid = map.get("tradeid").toString();
  179. System.out.println(amount +"@" + orderSn +"@"+ tradeid);
  180. try {
  181. }catch (Exception e){
  182. return "fail";
  183. }
  184. // TODO: timestamp, 如回调超时N小时后,是否处理依据具体业务
  185. return "success";
  186. }
  187. public static String generateTime() {
  188. return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
  189. }
  190. /*--------- pay365 相关接口Start------------ */
  191. // 回调通知 验证签名
  192. public static String notify365(String data) {
  193. JSONObject json = JSONObject.parseObject(data);
  194. Map<String, Object> map = (Map<String, Object>) json;
  195. // 验证签名
  196. return getPay365Sign(map, key365);
  197. }
  198. public static JSONObject gPay365(String amount,String ordersn , String phone,String userid){
  199. Map<String, Object> map = new HashMap();
  200. map.put("amount", amount); // 金额
  201. map.put("kyc_name", phone); // 姓名 -
  202. map.put("orderno", ordersn); // 订单号 mch_orderid
  203. map.put("shopid", shopid365); // 商户id
  204. map.put("exid", userid); //买家id
  205. map.put("return_url", return_url365); //订单完成后跳转地址
  206. map.put("callback_url",callback_url365); //异步回调地址
  207. // 签名
  208. String sign = getPay365Sign(map, key365);
  209. map.put("sign", sign);
  210. JSONObject json = new JSONObject(map);
  211. JSONObject response = doPost(url365, json);
  212. return response;
  213. }
  214. public static String getPay365Sign(Map<String ,Object> $params , String key){
  215. String $signData = ksort($params);
  216. $signData = $signData+"&key="+key;
  217. $signData = DigestUtils.md5Hex($signData);
  218. $signData = $signData.toUpperCase();
  219. return $signData ;
  220. }
  221. public static String ksort(Map<String, Object> map) {
  222. List<String> keyList = new ArrayList<>(map.keySet());
  223. Collections.sort(keyList);
  224. StringBuffer sb = new StringBuffer();
  225. for (int i = 0; i < keyList.size(); i++) {
  226. String key = keyList.get(i);
  227. Object value = map.get(key);
  228. if (key.equals("sign")) {
  229. continue;
  230. }
  231. sb.append(key + "=" + value + "&");
  232. }
  233. return sb.toString().substring(0, sb.toString().length() - 1) ;
  234. }
  235. /*--------- pay365 相关接口end ------------ */
  236. }