| | |
| | | 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;
|
| | |
|
| | | /**
|
| | |
| | | */
|
| | | 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<>();
|
| | |
| | | if (StringUtil.isNullOrEmpty(params.getTradeType()))
|
| | | throw new WXPlaceOrderParamsException(8, "请传入tradeType");
|
| | |
|
| | | // if (StringUtil.isNullOrEmpty(params.getOpenId()))
|
| | | // throw new WXPlaceOrderParamsException(9, "请传入openId");
|
| | | // if (StringUtil.isNullOrEmpty(params.getOpenId()))
|
| | | // throw new WXPlaceOrderParamsException(9, "请传入openId");
|
| | |
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | map.put("appid", params.getInfo().getAppId());
|
| | |
| | |
|
| | | 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);
|
| | | }
|
| | | }
|
| | |
|
| | | }
|