| | |
| | | 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;
|
| | |
| | | *
|
| | | */
|
| | | 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 "";
|
| | | }
|
| | |
|
| | | /**
|
| | | * 付款到零钱
|
| | |
| | |
|
| | | 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 (UnsupportedEncodingException e) {
|
| | | System.out.println("统一下单结果:" + new String(result.getBytes("GBK"), "UTF-8"));
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | Map<String, String> resultMap = WXUtil.parseXML(result);
|
| | |
| | | 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")))// 支付成功
|
| | |
| | |
|
| | | /**
|
| | | *
|
| | | * @param refundOrderNo -退款单号
|
| | | * @param refundOrderNo
|
| | | * -退款单号
|
| | | * @param app
|
| | | * @return
|
| | | * @throws WXOrderException
|
| | | */
|
| | | public static boolean isRefundSuccess(String refundOrderNo, WXAPPInfo app) 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 = HttpUtil.post("https://api.mch.weixin.qq.com/pay/refundquery", WXUtil.loadWXMessage(map));
|
| | | String result = post("https://api.mch.weixin.qq.com/pay/refundquery", WXUtil.loadWXMessage(map));
|
| | | System.out.println("订单查询结果:" + result);
|
| | | Map<String, String> resultMap = WXUtil.parseXML(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")))// 退款成功
|