/** * 生成签名 * * @param sortMap * @return */ protected static String createSign(SortedMap<String, String> sortMap) { return SignUtil.createSign(sortMap, payconfig.getPartnerKey()); }
/** * 发起微信转账(提现) * * @param model 发起提现的包装类 * @return * @throws MutilsErrorException */ public static Map<String, String> createWithdrawXml(WithdrawModel model) throws MutilsErrorException { String xml = model.toXml(payconfig.getPartnerKey()); log.info("withdraw xml is {}", xml); CloseableHttpClient httpclient = null; CloseableHttpResponse response = null; try { httpclient = HttpClientUtil.getSSLInstance(payconfig.getPartnerId(), payconfig.getCertificatePath(), payconfig.getCertificateFormat()); HttpPost httpost = HttpClientUtil.getPostMethod(payconfig.getWithdrawUrl()); httpost.setEntity(new StringEntity(xml, "UTF-8")); response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); log.info("withdraw json is {}", jsonStr); return ParseXmlUtil.doXMLParse(jsonStr); } catch (Exception e) { throw new MutilsErrorException(e, "发起转账失败"); } finally { IOUtil.close(httpclient, response); } }
/** * 发起退款申请 * * @param model * @return * @throws MutilsErrorException */ protected static RefundReturnModel createRefundRequest(RefundModel model) throws MutilsErrorException { String xmlParam = model.toXml(payconfig.getPartnerKey()); log.info("refund xml is {}", xmlParam); CloseableHttpClient httpclient = null; CloseableHttpResponse response = null; try { httpclient = HttpClientUtil.getSSLInstance(payconfig.getPartnerId(), payconfig.getCertificatePath(), payconfig.getCertificateFormat()); HttpPost httpost = HttpClientUtil.getPostMethod(payconfig.getRefundUrl()); httpost.setEntity(new StringEntity(xmlParam, "UTF-8")); response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); log.info("refund json is {}", jsonStr); return MapUtil.mapToObject(ParseXmlUtil.doXMLParse(jsonStr), RefundReturnModel.class); } catch (Exception e) { throw new MutilsErrorException(e, "发起退款失败"); } finally { IOUtil.close(httpclient, response); } }
/** * 创建APP支付的请求参数 APP将用其发起微信支付 * * @param model 下单时的包装对象 * @return APP能发起的请求的包装内容 * @throws Exception */ public static Map<String, String> createAppPayParamter(AppOrderPayModel model) throws Exception{ Map<String, String> doXMLParse = createUnifiedOrder(model); checkMap(doXMLParse); SortedMap<String, String> sortMap = new TreeMap<>(); String appId = doXMLParse.get("appid"); String nonceStr = doXMLParse.get("nonce_str"); String prepayid = doXMLParse.get("prepay_id"); String timeStamp = String.valueOf(System.currentTimeMillis() / 1000); sortMap.put("appid", appId); sortMap.put("partnerid", payconfig.getPartnerId()); sortMap.put("noncestr", nonceStr); sortMap.put("package", "Sign=WXPay"); sortMap.put("timestamp", timeStamp); sortMap.put("prepayid", prepayid); sortMap.put("sign", createSign(sortMap)); return sortMap; }
/** * 统一下单接口 用于生成 预支付id 及二维码id * * @param model 预下单的对象 * @return * @throws ParseException * @throws MutilsErrorException * @throws IOException * @throws JDOMException */ protected static Map<String, String> createUnifiedOrder(BaseWeChatPayModel model) throws ParseException, IOException, MutilsErrorException, JDOMException { CloseableHttpClient httpclient = HttpClientUtil.getInstance();// 先初始化; CloseableHttpResponse response = null; try { HttpPost httpost = HttpClientUtil.getPostMethod(payconfig.getUnifiedOrderUrl()); String xmlParam = model.toXml(payconfig.getPartnerKey()); log.info("createUnifiedOrder xml is {}", xmlParam); httpost.setEntity(new StringEntity(xmlParam, "UTF-8")); response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); log.info("createUnifiedOrder json is {}", jsonStr); if (jsonStr.indexOf("FAIL") != -1) { throw new MutilsErrorException(jsonStr); } return ParseXmlUtil.doXMLParse(jsonStr); } finally { IOUtil.close(httpclient, response); } }