From 148af6b2f57be264c84d0991237fbd7ddea491a4 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期一, 04 十一月 2019 09:55:55 +0800
Subject: [PATCH] Merge branch 'div' of ssh://193.112.35.168:29418/fanli-server into div

---
 utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java |   89 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 87 insertions(+), 2 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 d2878a4..9c3b6c0 100644
--- a/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java
@@ -10,6 +10,7 @@
 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;
 
 /**
@@ -20,6 +21,21 @@
  */
 public class WXPayUtil {
 
+	/**
+	 * 浠樻鍒伴浂閽�
+	 * 
+	 * @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<>();
@@ -164,8 +180,8 @@
 		if (StringUtil.isNullOrEmpty(params.getTradeType()))
 			throw new WXPlaceOrderParamsException(8, "璇蜂紶鍏radeType");
 
-//		if (StringUtil.isNullOrEmpty(params.getOpenId()))
-//			throw new WXPlaceOrderParamsException(9, "璇蜂紶鍏penId");
+		// if (StringUtil.isNullOrEmpty(params.getOpenId()))
+		// throw new WXPlaceOrderParamsException(9, "璇蜂紶鍏penId");
 
 		Map<String, String> map = new HashMap<String, String>();
 		map.put("appid", params.getInfo().getAppId());
@@ -189,4 +205,73 @@
 
 		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));
+		System.out.println("璁㈠崟鏌ヨ缁撴灉:" + result);
+		Map<String, String> 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);
+		}
+	}
+
 }

--
Gitblit v1.8.0