SpringBoot项目用到微信支付功能
微信小程序官方文档
下载java微信sdk
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
需要用到这4个类
1、WXPayUtil
2、HttpClientUtil
3、WXPayConstants
4、WXPayXmlUtil
封装好的参数
public String openid;
private Long id;
private String ordersNo;
private String idCard;
需要返回给前端这些参数,然后让其请求微信支付
appId: ""
nonceStr: ""
package: "prepay_id"
paySign: ""
signType: "MD5"
timeStamp: ""
支付接口
@RestController
@RequestMapping("/payment/")
public class PaymentController {
/*调用支付接口*/
@ApiOperation(httpMethod = "POST", value = "调用支付接口")
@RequestMapping(value = "prePay", method = RequestMethod.POST)
public Map<String, Object> prePay(@RequestBody Pay pay,HttpServletRequest request){
log.info("调用支付接口");
QueryWrapper queryWrapper = new QueryWrapper();
// 返回参数
Map<String, Object> resMap = new HashMap<>();
//获取当前请求ip地址
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getRemoteAddr();
}
if(ip.indexOf(",")!=-1){
String[] ips = ip.split(",");
ip = ips[0].trim();
}
try {
//这里可以写业务逻辑
if (){
if (idOrder!=null){
//如果获取到订单就删除,重新生成
}
}else {
}
//前台传输一个openid--每个用户对应小程序都会生成一个独一无二的openid
Map<String, Object> paraMap = new HashMap<>();
String body = "cs";
// 封装11个必需的参数
//小程序ID
paraMap.put("appid", environment.getProperty("appId"));
//商家ID
paraMap.put("mch_id", environment.getProperty("mchId"));
//获取随机字符串 Nonce Str
paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
//商品名称
paraMap.put("body", body)
Double money = hpOrder.getTotalFee();//租金
//支付金额,单位:分,这边需要转成字符串类型,否则后面的签名会失败
String totalFee= BigDecimal.valueOf(money).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP) + "";
paraMap.put("total_fee",totalFee);
//订单号
paraMap.put("out_trade_no", getOrderNo());
//ip地址
paraMap.put("spbill_create_ip", ip);
// 此路径是微信服务器调用支付结果通知路径
paraMap.put("notify_url", "http://rzk.com/payment/callBack");
//小程序和公众号支付都是填JSAPI
paraMap.put("trade_type", "JSAPI");
paraMap.put("openid", pay.getOpenid());
String sign = WXPayUtil.generateSignature(paraMap, environment.getProperty("mchKey"));
//生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
paraMap.put("sign", sign);
//将所有参数(map)转xml格式
String xml = WXPayUtil.mapToXml(paraMap);
log.info("xml=: "+xml);
// 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder
String unifiedorder_url = WXPayConstants.UNIFIEDORDER_URL;
log.info("统一下单接口unifiedorder_url:"+unifiedorder_url);
//发送post请求"统一下单接口"返回预支付id:prepay_id
String xmlStr = HttpClientUtil.doPostXml(unifiedorder_url, xml);
//以下内容是返回前端页面的json数据
//预支付id
String prepay_id = "";
if (xmlStr.indexOf("SUCCESS") != -1) {
Map<String, Object> map = WXPayUtil.xmlToMap(xmlStr);//XML格式字符串转换为Map
//获取封装好的预支付id
prepay_id = map.get("prepay_id").toString();
log.info("prepay_id_1= "+prepay_id);
}
// 封装所需6个参数调支付页面
Map<String, Object> payMap = new HashMap<String, Object>();
payMap.put("appId", environment.getProperty("appId"));
payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp()+"");//获取当前时间戳,单位秒
payMap.put("nonceStr", WXPayUtil.generateNonceStr());//获取随机字符串 Nonce Str
payMap.put("signType", "MD5");
//这里被坑,需要这样写 "prepay_id=" +
payMap.put("package", "prepay_id=" + prepay_id);
//生成带有 sign 的 XML 格式字符串
String paySign = WXPayUtil.generateSignature(payMap, environment.getProperty("mchKey"));
payMap.put("paySign", paySign);
// 封装正常情况返回数据
resMap.put("success",true);
resMap.put("payMap",payMap);
} catch (Exception e) {
//异常删除订单
// 封装异常情况返回数据
log.info("调用统一订单接口错误");
resMap.put("success",false);
resMap.put("message","调用统一订单接口错误");
e.printStackTrace();
}
return resMap;
}
}
回调地址
/*支付成功回调*/
@ApiOperation(httpMethod = "POST", value = "调用支付成功回调的地址")
@RequestMapping(value = "callBack")
public Result callBack(HttpServletRequest request, HttpServletResponse response){
Result result = new Result();
log.info("调用微信支付成功回调");
InputStream is = null;
String resXml = "";
try {
is = request.getInputStream();//获取请求的流信息(这里是微信发的xml格式所有只能使用流来读)
String xml = WXPayUtil.inputStream2String(is);
Map<String, Object> notifyMap = WXPayUtil.xmlToMap(xml);//将微信发的xml转map
if(notifyMap.get("return_code").equals("SUCCESS")){
try {
//这里写业务逻辑
//获取到商户订单号
String ordersNo = notifyMap.get("out_trade_no").toString();//商户订单号
log.info("订单号"+ordersNo);
Order = iOrderService.getByIdOrder(ordersNo);
log.info("查询租房是否有该订单: "+hpOrder);
//如果查询出来有
if (order!=null) {
//商户订单号
iOrderService.updateByIdOrders(ordersNo);
log.info("订单状态修改成功");
}else{
Order order = iOrderService.getByIdOrder(ordersNo);
log.info("查询写字楼是否有该订单: "+order);
if (order!=null) {
//商户订单号
iOrderService.updateByIdOrders(ordersNo);
log.info("订单状态修改成功");
}
}
resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
+ "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
System.err.println("返回给微信的值:"+resXml.getBytes());
is.close();
}catch (Exception e){
log.info("订单状态修改失败");
result.setMsg("订单状态修改失败");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}