From 98b1a0affd69bbe63223c21fdd2c404e8bedfccb Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 20 五月 2020 17:25:08 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/div' into 2.1.2 --- utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 257 insertions(+), 17 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java b/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java index 15006fc..2f3168c 100644 --- a/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java +++ b/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java @@ -1,13 +1,23 @@ package org.yeshi.utils.wx; +import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.PostMethod; +import org.dom4j.DocumentException; import org.yeshi.utils.HttpUtil; import org.yeshi.utils.StringUtil; import org.yeshi.utils.entity.wx.RedPackParams; +import org.yeshi.utils.entity.wx.WXAPPInfo; +import org.yeshi.utils.entity.wx.WXPlaceOrderParams; +import org.yeshi.utils.exception.WXOrderException; +import org.yeshi.utils.exception.WXPlaceOrderParamsException; /** * 寰俊鏀粯甯姪绫� @@ -17,6 +27,36 @@ */ public class WXPayUtil { + private static String post(String url, String entity) { + HttpClient client = new HttpClient(); + PostMethod method = new PostMethod(url); + method.setRequestBody(entity); + try { + client.executeMethod(method); + return method.getResponseBodyAsString(); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + /** + * 浠樻鍒伴浂閽� + * + * @param appId + * @param openId + * @param mchId + * @param key + * @param pwd + * @param cert + * @param orderNo + * @param money + * @param desc + * @param ip + * @return + */ public static String payToOpenId(String appId, String openId, String mchId, String key, String pwd, InputStream cert, String orderNo, BigDecimal money, String desc, String ip) { Map<String, String> map = new HashMap<>(); @@ -42,9 +82,9 @@ return null; } - /** * 鍙戞斁寰俊绾㈠寘 + * * @param params * @param pwd * @param cert @@ -54,7 +94,7 @@ // 杞寲鎴愬垎 BigDecimal money = params.getMoney(); money = money.multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN); - + Map<String, String> map = new HashMap<>(); map.put("nonce_str", StringUtil.getRandomCode(32)); map.put("mch_billno", params.getBillno()); @@ -63,43 +103,46 @@ map.put("send_name", params.getSendName()); map.put("re_openid", params.getOpenid()); map.put("total_amount", money.toString()); - map.put("total_num", params.getTotalNum()+ ""); + map.put("total_num", params.getTotalNum() + ""); map.put("wishing", params.getWishing()); map.put("client_ip", params.getClientIp()); map.put("act_name", params.getActName()); map.put("remark", params.getRemark()); - + if (!StringUtil.isNullOrEmpty(params.getSceneId())) map.put("scene_id", params.getSceneId()); - + if (!StringUtil.isNullOrEmpty(params.getRiskInfo())) map.put("risk_info", params.getRiskInfo()); - + map.put("sign", WXUtil.getSignMD5(map, params.getKey())); String entity = WXUtil.loadWXMessage(map); try { - String result = HttpUtil.httpsPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", - entity, pwd, cert); + String result = HttpUtil.httpsPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", entity, + pwd, cert); return result; } catch (Exception e) { e.printStackTrace(); } return null; } - - + /** * 鏌ヨ绾㈠寘棰嗗彇璁板綍 - * @param billno 鍟嗘埛璁㈠崟鍙� - * @param mchId 鍟嗘埛鍙� + * + * @param billno + * 鍟嗘埛璁㈠崟鍙� + * @param mchId + * 鍟嗘埛鍙� * @param key - * @param appId Appid + * @param appId + * Appid * @param pwd * @param cert * @return */ - public static String getRedPackRecord(String billno, String mchId, String appId, String key, - String pwd, InputStream cert) { + public static String getRedPackRecord(String billno, String mchId, String appId, String key, String pwd, + InputStream cert) { Map<String, String> map = new HashMap<>(); map.put("mch_billno", billno); map.put("mch_id", mchId); @@ -109,12 +152,209 @@ map.put("sign", WXUtil.getSignMD5(map, key)); String entity = WXUtil.loadWXMessage(map); try { - String result = HttpUtil.httpsPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo", - entity, pwd, cert); + String result = HttpUtil.httpsPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo", entity, pwd, + cert); return result; } catch (Exception e) { e.printStackTrace(); } return null; } + + /** + * 寰俊鏀粯缁熶竴涓嬪崟 + * + * @param params + * @return + * @throws WXPlaceOrderParamsException + */ + public static Map<String, String> produceOrder(WXPlaceOrderParams params) throws WXPlaceOrderParamsException { + if (params == null) + throw new WXPlaceOrderParamsException(1, "璇蜂紶鍏ヤ笅鍗曞弬鏁�"); + + if (params.getInfo() == null) + throw new WXPlaceOrderParamsException(2, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�"); + + if (StringUtil.isNullOrEmpty(params.getInfo().getAppId())) + throw new WXPlaceOrderParamsException(201, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-appId"); + if (StringUtil.isNullOrEmpty(params.getInfo().getAppSecret())) + throw new WXPlaceOrderParamsException(202, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-appSecret"); + if (StringUtil.isNullOrEmpty(params.getInfo().getMchId())) + throw new WXPlaceOrderParamsException(203, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-mchId"); + if (StringUtil.isNullOrEmpty(params.getInfo().getMchKey())) + throw new WXPlaceOrderParamsException(204, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-mchKey"); + if (StringUtil.isNullOrEmpty(params.getBody())) + throw new WXPlaceOrderParamsException(3, "璇蜂紶鍏ody"); + + if (StringUtil.isNullOrEmpty(params.getOrderNo())) + throw new WXPlaceOrderParamsException(4, "璇蜂紶鍏rderNo"); + + if (params.getFee() == null) + throw new WXPlaceOrderParamsException(5, "璇蜂紶鍏ee"); + + if (StringUtil.isNullOrEmpty(params.getIp())) + throw new WXPlaceOrderParamsException(6, "璇蜂紶鍏p"); + + if (StringUtil.isNullOrEmpty(params.getNotifyUrl())) + throw new WXPlaceOrderParamsException(7, "璇蜂紶鍏otifyUrl"); + + if (StringUtil.isNullOrEmpty(params.getTradeType())) + throw new WXPlaceOrderParamsException(8, "璇蜂紶鍏radeType"); + + // if (StringUtil.isNullOrEmpty(params.getOpenId())) + // throw new WXPlaceOrderParamsException(9, "璇蜂紶鍏penId"); + + Map<String, String> map = new HashMap<String, String>(); + map.put("appid", params.getInfo().getAppId()); + map.put("mch_id", params.getInfo().getMchId()); + map.put("nonce_str", StringUtil.getRandomCode(32)); + map.put("body", params.getBody()); + map.put("out_trade_no", params.getOrderNo()); + map.put("total_fee", "" + params.getFee().multiply(new BigDecimal(100)).intValue()); + map.put("spbill_create_ip", params.getIp()); + map.put("notify_url", params.getNotifyUrl()); + map.put("trade_type", params.getTradeType()); + if (!StringUtil.isNullOrEmpty(params.getOpenId())) + map.put("openid", params.getOpenId()); + map.put("sign", WXUtil.getSignMD5(map, params.getInfo().getMchKey())); + + String entity = WXUtil.loadWXMessage(map); + + String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", entity); + try { + System.out.println("缁熶竴涓嬪崟缁撴灉:" + new String(result.getBytes("GBK"), "UTF-8")); + } catch (Exception e) { + e.printStackTrace(); + } + Map<String, String> resultMap = WXUtil.parseXML(result); + + return resultMap; + } + + /** + * 璁㈠崟閫�娆� + * + * @param orderNo-璁㈠崟鍙� + * @param orderMoney-璁㈠崟鎬昏祫閲� + * @param refundMoney-閫�娆鹃噾棰� + * @param reason-閫�娆惧師鍥� + * @param appInfo + * @param pwd-璇佷功瀵嗙爜 + * @param cert-璇佷功 + */ + public static boolean refund(String orderNo, BigDecimal orderMoney, BigDecimal refundMoney, String reason, + WXAPPInfo appInfo, String pwd, InputStream cert) throws WXOrderException { + Map<String, String> map = new HashMap<String, String>(); + map.put("appid", appInfo.getAppId()); + map.put("mch_id", appInfo.getMchId()); + map.put("nonce_str", StringUtil.getRandomCode(32)); + map.put("out_trade_no", orderNo); + map.put("out_refund_no", orderNo);// 鍟嗘埛閫�娆惧崟鍙� + map.put("total_fee", orderMoney.multiply(new BigDecimal(100)).intValue() + "");// 璁㈠崟閲戦 + map.put("refund_fee", orderMoney.multiply(new BigDecimal(100)).intValue() + "");// 閫�娆鹃噾棰� + if (!StringUtil.isNullOrEmpty(reason)) + map.put("refund_desc", reason); + map.put("sign", WXUtil.getSignMD5(map, appInfo.getMchKey())); + try { + String result = HttpUtil.httpsPost("https://api.mch.weixin.qq.com/secapi/pay/refund", + WXUtil.loadWXMessage(map), pwd, cert); + System.out.println("璁㈠崟閫�娆剧粨鏋�:" + result); + Map<String, String> resultMap = WXUtil.parseXML(result); + if ("SUCCESS".equalsIgnoreCase(resultMap.get("return_code")) + && "SUCCESS".equalsIgnoreCase(resultMap.get("result_code"))) + return true; + throw new WXOrderException(100, "寰俊鏀粯鎺ュ彛鍑洪敊:" + result); + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + /** + * 鏌ヨ璁㈠崟鍙锋槸鍚︽敮浠樻垚鍔� + * + * @param orderNo + * @param app + * @return + * @throws WXOrderException + */ + public static boolean isPaySuccess(String orderNo, WXAPPInfo app) throws WXOrderException { + Map<String, String> map = new HashMap<String, String>(); + map.put("appid", app.getAppId()); + map.put("mch_id", app.getMchId()); + map.put("nonce_str", StringUtil.getRandomCode(32)); + map.put("out_trade_no", orderNo); + map.put("sign", WXUtil.getSignMD5(map, app.getMchKey())); + String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/orderquery", WXUtil.loadWXMessage(map)); + try { + try { + result = new String(result.getBytes("GBK"), "UTF-8"); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + System.out.println("璁㈠崟鏌ヨ缁撴灉:" + new String(result.getBytes("GBK"), "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + Map<String, String> resultMap = null; + try { + resultMap = WXUtil.parseXML(result); + } catch (Exception e) { + try { + result = new String(result.getBytes("GBK"), "UTF-8"); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + resultMap = WXUtil.parseXML(result); + } + if ("SUCCESS".equalsIgnoreCase(resultMap.get("return_code")) + && "SUCCESS".equalsIgnoreCase(resultMap.get("result_code"))) { + if ("SUCCESS".equalsIgnoreCase(resultMap.get("trade_state")))// 鏀粯鎴愬姛 + return true; + else + return false; + } else { + throw new WXOrderException(100, "寰俊鏀粯鎺ュ彛鍑洪敊:" + result); + } + } + + /** + * + * @param refundOrderNo + * -閫�娆惧崟鍙� + * @param app + * @return + * @throws WXOrderException + */ + public static boolean isRefundSuccess(String refundOrderNo, WXAPPInfo app) throws WXOrderException { + Map<String, String> map = new HashMap<String, String>(); + map.put("appid", app.getAppId()); + map.put("mch_id", app.getMchId()); + map.put("nonce_str", StringUtil.getRandomCode(32)); + map.put("out_refund_no", refundOrderNo); + map.put("sign", WXUtil.getSignMD5(map, app.getMchKey())); + String result = post("https://api.mch.weixin.qq.com/pay/refundquery", WXUtil.loadWXMessage(map)); + System.out.println("璁㈠崟鏌ヨ缁撴灉:" + result); + Map<String, String> resultMap = null; + try { + resultMap = WXUtil.parseXML(result); + } catch (Throwable e) { + try { + result = new String(result.getBytes("GBK"), "UTF-8"); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + resultMap = WXUtil.parseXML(result); + } + if ("SUCCESS".equalsIgnoreCase(resultMap.get("return_code")) + && "SUCCESS".equalsIgnoreCase(resultMap.get("result_code"))) { + if ("SUCCESS".equalsIgnoreCase(resultMap.get("refund_status_0")))// 閫�娆炬垚鍔� + return true; + else + return false; + } else { + throw new WXOrderException(100, "寰俊鏀粯鎺ュ彛鍑洪敊:" + result); + } + } + } -- Gitblit v1.8.0