微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
1.按照微信官方文档统一下单接口的描述,将请求参数放入Map
protected Map<String, Object> processPost(HttpServletRequest request, HttpServletResponse response) throws BaseException {
String attach = ConvertUtil.getTrimStringFromRequestParam(request, "attach", "");
String device_info = ConvertUtil.getTrimStringFromRequestParam(request, "device_info", WXPayConstants.DEVICE_INFO_DEFAULT.WEB.toString());
String body = ConvertUtil.getNonEmptyStringFromRequestParam(request, "body");
String detail = ConvertUtil.getTrimStringFromRequestParam(request, "detail", "");
String out_trade_no = ConvertUtil.getNonEmptyStringFromRequestParam(request, "out_trade_no");
String total_fee = ConvertUtil.getNonEmptyStringFromRequestParam(request, "total_fee");
String trade_type = ConvertUtil.getTrimStringFromRequestParam(request, "trade_type", WXPayConstants.TRADE_TYPE.JSAPI.toString());
String product_id = ConvertUtil.getNonEmptyStringFromRequestParam(request, "product_id");
String openid = ConvertUtil.getNonEmptyStringFromRequestParam(request, "openid");
Map<String, Object> parametersMap = CommonUtils.createMap("appid", WXPayConstants.APP_ID);//appid
CommonUtils.generateMap(parametersMap, "attach", attach);//附加数据
CommonUtils.generateMap(parametersMap, "mch_id", WXPayConstants.PAY_STORE_ID);//商户号
CommonUtils.generateMap(parametersMap, "device_info", device_info);//设备号 自定义参数,可以为终端设备号(门店号或收银设备ID),PC网页或公众号内支付可以传"WEB"
CommonUtils.generateMap(parametersMap, "nonce_str", WXPayUtil.generateNonceStr());
CommonUtils.generateMap(parametersMap, "sign_type", WXPayConstants.SignType.MD5);
CommonUtils.generateMap(parametersMap, "body", body);//商品描述
CommonUtils.generateMap(parametersMap, "detail", detail);//商品详情
CommonUtils.generateMap(parametersMap, "out_trade_no", out_trade_no);//自己系统的订单号
CommonUtils.generateMap(parametersMap, "fee_type", WXPayConstants.FEE_TYPE.CNY);
CommonUtils.generateMap(parametersMap, "total_fee", total_fee);//单位位分
CommonUtils.generateMap(parametersMap, "spbill_create_ip", CommonUtils.getIpAddr(request));
CommonUtils.generateMap(parametersMap, "notify_url", WXPayConstants.NOTIFY_URL);
CommonUtils.generateMap(parametersMap, "trade_type", trade_type);
CommonUtils.generateMap(parametersMap, "product_id", product_id);//商品id
CommonUtils.generateMap(parametersMap, "openid", openid);//open id
return WechatService.prepayId(parametersMap);
}
2.用微信自带的工具类对map进行加密并获取签名
/**
* 统一支付(获取预支付id)
* @param
* @return
* @author lichenyi
* @date 2017-8-25 0025 10:44
*/
public static Map<String, Object> prepayId(Map<String, Object> parametersMap) throws HttpRequestFailedException, MD5Exception {
String resultString = null;
try {
String parameterXML = WXPayUtil.generateSignedXml(parametersMap);
//wechat origin code
WXPayRequest wxPayRequest = new WXPayRequest(new WXPayConfigBean());
resultString = wxPayRequest.requestWithoutCert(WXPayConstants.UNIFIEDORDER_URL_SUFFIX, WXPayUtil.generateNonceStr(), parameterXML, false);
logger.info("【预支付订单信息】 --> "+WXPayUtil.xmlToMap(resultString));
return WXPayUtil.xmlToMap(resultString);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
写的粗糙,有疑问,可以留言。
如果觉得文章真心好, 请打赏下我吧http://blog.csdn.net/assassinsshadow/article/details/76086401