|
@@ -9,13 +9,14 @@ import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import static org.apache.http.HttpHeaders.ACCEPT;
|
|
|
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
|
|
@@ -29,8 +30,42 @@ public class WxPayService {
|
|
|
private WxPayCfg wxPayCfg;
|
|
|
|
|
|
// 调用微信统一下单接口
|
|
|
- public String wxUnifiedorder(UnifiedorderDto unifiedorderDto) throws Exception {
|
|
|
- System.out.println("微信统一下单接口入参,外部订单号="+unifiedorderDto.getOut_trade_no());
|
|
|
+// public String wxUnifiedorder(UnifiedorderDto unifiedorderDto) throws Exception {
|
|
|
+// System.out.println("微信统一下单接口入参,外部订单号=" + unifiedorderDto.getOut_trade_no());
|
|
|
+// HttpPost httpPost = new HttpPost(wxPayCfg.getWxUnifiedorder());
|
|
|
+// Object o = JSONArray.toJSON(unifiedorderDto);
|
|
|
+// String reqData = o.toString();
|
|
|
+// StringEntity entity = new StringEntity(reqData, Charset.forName("UTF-8"));
|
|
|
+// entity.setContentType("application/json");
|
|
|
+// httpPost.setEntity(entity);
|
|
|
+// httpPost.addHeader("Accept", "application/json");
|
|
|
+// httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+// CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpPost);
|
|
|
+//
|
|
|
+// try {
|
|
|
+// int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+// if (statusCode == 200) {
|
|
|
+// String s = EntityUtils.toString(response.getEntity());
|
|
|
+// JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+// String prepayId = jsonObject.getString("prepay_id");
|
|
|
+//
|
|
|
+// return prepayId;
|
|
|
+// } else if (statusCode == 204) {
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// String msg = EntityUtils.toString(response.getEntity());
|
|
|
+// System.out.println("微信统一下单返回异常,body=" + msg);
|
|
|
+// throw new IOException(msg);
|
|
|
+// }
|
|
|
+// } finally {
|
|
|
+// response.close();
|
|
|
+// }
|
|
|
+//
|
|
|
+// return "";
|
|
|
+// }
|
|
|
+
|
|
|
+ public Map<String, String> wxUnifiedorder(UnifiedorderDto unifiedorderDto) throws Exception {
|
|
|
+ System.out.println("微信统一下单接口入参,外部订单号=" + unifiedorderDto.getOut_trade_no());
|
|
|
HttpPost httpPost = new HttpPost(wxPayCfg.getWxUnifiedorder());
|
|
|
Object o = JSONArray.toJSON(unifiedorderDto);
|
|
|
String reqData = o.toString();
|
|
@@ -39,30 +74,33 @@ public class WxPayService {
|
|
|
httpPost.setEntity(entity);
|
|
|
httpPost.addHeader("Accept", "application/json");
|
|
|
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+
|
|
|
+
|
|
|
CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpPost);
|
|
|
|
|
|
- try {
|
|
|
- int statusCode = response.getStatusLine().getStatusCode();
|
|
|
- if (statusCode == 200) {
|
|
|
- String s = EntityUtils.toString(response.getEntity());
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
- String prepayId = jsonObject.getString("prepay_id");
|
|
|
-
|
|
|
- return prepayId;
|
|
|
- } else if (statusCode == 204) {
|
|
|
-
|
|
|
- } else {
|
|
|
- String msg = EntityUtils.toString(response.getEntity());
|
|
|
- System.out.println("微信统一下单返回异常,body="+msg);
|
|
|
- throw new IOException(msg);
|
|
|
- }
|
|
|
- } finally {
|
|
|
- response.close();
|
|
|
+
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+
|
|
|
+ String res = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ HashMap<String, String> resultMap = new HashMap<>();
|
|
|
+
|
|
|
+ if (statusCode != 200) {
|
|
|
+ String errCode = jsonObject.getString("code");
|
|
|
+ String message = jsonObject.getString("message");
|
|
|
+ resultMap.put("errCode", errCode);
|
|
|
+ resultMap.put("message", message);
|
|
|
+ return resultMap;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- return "";
|
|
|
+ String prepayId = jsonObject.getString("prepay_id");
|
|
|
+ resultMap.put("prepayId", prepayId);
|
|
|
+ return resultMap;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 调用关闭订单接口
|
|
|
*
|
|
@@ -90,7 +128,6 @@ public class WxPayService {
|
|
|
CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpPost);
|
|
|
|
|
|
|
|
|
-
|
|
|
try {
|
|
|
//响应码
|
|
|
statusCode = response.getStatusLine().getStatusCode();
|
|
@@ -115,35 +152,60 @@ public class WxPayService {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public String wxQueryOrder(String out_trade_no) throws Exception {
|
|
|
+// public String wxQueryOrder(String out_trade_no) throws Exception {
|
|
|
+// String mchId = wxPayCfg.getMchId();
|
|
|
+// String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + out_trade_no + "?mchid=" + mchId;
|
|
|
+// System.out.println("订单查询接口url==="+url);
|
|
|
+// HashMap<String, String> resultMap = new HashMap<>();
|
|
|
+// HttpGet httpGet = new HttpGet(url);
|
|
|
+// httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());
|
|
|
+// CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpGet);
|
|
|
+//
|
|
|
+// try {
|
|
|
+// int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+// if (statusCode == 200) {
|
|
|
+// String s = EntityUtils.toString(response.getEntity());
|
|
|
+// JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+// String tradeState = jsonObject.getString("trade_state");
|
|
|
+// resultMap.put("code", "0");
|
|
|
+// resultMap.put("code", "ok");
|
|
|
+// resultMap.put("status", "tradeState");
|
|
|
+// return tradeState;
|
|
|
+// } else if (statusCode == 204) {
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// String msg = EntityUtils.toString(response.getEntity());
|
|
|
+// throw new IOException(msg);
|
|
|
+// }
|
|
|
+// } finally {
|
|
|
+// response.close();
|
|
|
+// }
|
|
|
+// return "";
|
|
|
+// }
|
|
|
+ public Map<String, String> wxQueryOrder(String out_trade_no) throws Exception {
|
|
|
String mchId = wxPayCfg.getMchId();
|
|
|
String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + out_trade_no + "?mchid=" + mchId;
|
|
|
+ System.out.println("订单查询接口url===" + url);
|
|
|
HashMap<String, String> resultMap = new HashMap<>();
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
|
httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());
|
|
|
- CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpGet);
|
|
|
-
|
|
|
- try {
|
|
|
- int statusCode = response.getStatusLine().getStatusCode();
|
|
|
- if (statusCode == 200) {
|
|
|
- String s = EntityUtils.toString(response.getEntity());
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
- String tradeState = jsonObject.getString("trade_state");
|
|
|
- resultMap.put("code", "0");
|
|
|
- resultMap.put("code", "ok");
|
|
|
- resultMap.put("status", "tradeState");
|
|
|
-
|
|
|
- return tradeState;
|
|
|
- } else if (statusCode == 204) {
|
|
|
-
|
|
|
- } else {
|
|
|
- String msg = EntityUtils.toString(response.getEntity());
|
|
|
- throw new IOException(msg);
|
|
|
- }
|
|
|
- } finally {
|
|
|
- response.close();
|
|
|
+ CloseableHttpClient clint = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key());
|
|
|
+ CloseableHttpResponse response = clint.execute(httpGet);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+
|
|
|
+ String res = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ if (statusCode != 200) {
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
+ String message = jsonObject.getString("message");
|
|
|
+ resultMap.put("errCode", code);
|
|
|
+ resultMap.put("message", message);
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
- return "";
|
|
|
+
|
|
|
+ String tradeState = jsonObject.getString("trade_state");
|
|
|
+ resultMap.put("tradeState", tradeState);
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
|
|
|
|