From 2ba431be9c12a79783e0f9ef249292b7fa95f2a1 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期一, 25 一月 2021 19:13:58 +0800 Subject: [PATCH] 拼多多备案调整 --- utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java | 105 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 94 insertions(+), 11 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 9c3b6c0..143f1d3 100644 --- a/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java +++ b/utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java @@ -1,10 +1,16 @@ 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; @@ -20,6 +26,21 @@ * */ 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 ""; + } /** * 浠樻鍒伴浂閽� @@ -151,16 +172,16 @@ if (params == null) throw new WXPlaceOrderParamsException(1, "璇蜂紶鍏ヤ笅鍗曞弬鏁�"); - if (params.getInfo() == null) + if (params.getApp() == null) throw new WXPlaceOrderParamsException(2, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�"); - if (StringUtil.isNullOrEmpty(params.getInfo().getAppId())) + if (StringUtil.isNullOrEmpty(params.getApp().getAppId())) throw new WXPlaceOrderParamsException(201, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-appId"); - if (StringUtil.isNullOrEmpty(params.getInfo().getAppSecret())) + if (StringUtil.isNullOrEmpty(params.getApp().getAppSecret())) throw new WXPlaceOrderParamsException(202, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-appSecret"); - if (StringUtil.isNullOrEmpty(params.getInfo().getMchId())) + if (StringUtil.isNullOrEmpty(params.getApp().getMchId())) throw new WXPlaceOrderParamsException(203, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-mchId"); - if (StringUtil.isNullOrEmpty(params.getInfo().getMchKey())) + if (StringUtil.isNullOrEmpty(params.getApp().getMchKey())) throw new WXPlaceOrderParamsException(204, "璇蜂紶鍏ヤ笅鍗曞簲鐢ㄤ俊鎭�-mchKey"); if (StringUtil.isNullOrEmpty(params.getBody())) throw new WXPlaceOrderParamsException(3, "璇蜂紶鍏ody"); @@ -184,8 +205,8 @@ // 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("appid", params.getApp().getAppId()); + map.put("mch_id", params.getApp().getMchId()); map.put("nonce_str", StringUtil.getRandomCode(32)); map.put("body", params.getBody()); map.put("out_trade_no", params.getOrderNo()); @@ -195,12 +216,16 @@ 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())); + map.put("sign", WXUtil.getSignMD5(map, params.getApp().getMchKey())); String entity = WXUtil.loadWXMessage(map); String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", entity); - System.out.println("缁熶竴涓嬪崟缁撴灉:" + result); + 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; @@ -261,8 +286,27 @@ 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); + 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")))// 鏀粯鎴愬姛 @@ -274,4 +318,43 @@ } } + /** + * + * @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