1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
public Object devicePay(int userId,String deviceCode, double money,String time) {
ResultBean result = new ResultBean();
/*自身业务逻辑代码*/
//判断设备是否存在
Device isExist = deviceDao.selectByDeviceCode(deviceCode);
if(isExist==null){
throw new AppException(ResultEnums.DEVICE_NOT_EXIST);
}
//判断用户是否存在
User user = userDao.getId((long)userId);
if(user==null){
throw new AppException(ResultEnums.USER_NOT_EXIST);
}
String uuid = UUID.randomUUID().toString().substring(0,8);
String nowTime = DateUtils.getNowTimestamp();
//生成订单编号
String historyCode = nowTime+uuid;
DeviceHistory deviceHistory = new DeviceHistory(historyCode,deviceCode,userId,money,time,new DateUtils().getNowTime());
//新建一个订单
deviceHistoryDao.insertDeviceHistory(deviceHistory);
/*构建微信下单请求参数*/
Map<String, String> map = new HashMap<String, String>();
map.put("appid",GlobalParas.APPID);//appid
map.put("mch_id",GlobalParas.MCH_ID);//商户号
map.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
map.put("sign_type", WXPayConstants.SignType.MD5.toString());//签名类型
map.put("body",isExist.getDeviceName());//商品描述
map.put("out_trade_no",historyCode);//商户订单号,不能重复
map.put("trade_type","JSAPI");//小程序支付都填写 JSAPI
map.put("notify_url","https://xxxxx/pay/complete/"+historyCode);//通知地址,不能传递参数
int totalFee = (int) (money*100);
map.put("total_fee",String.valueOf(totalFee));//金额,以分为单位
map.put("openid",user.getOpenId());//小程序支付时,这个必须填,为用户唯一的编号 openid
String sign = "";
try {
//先进行签名
sign = WXPayUtil.generateSignature(map, GlobalParas.APIKEY);
map.put("sign",sign);
//转换为xml格式
String xml = WXPayUtil.generateSignedXml(map,GlobalParas.APIKEY);
//统一下单接口
String resultXml = HttpUtil.sendPostXml(GlobalParas.PAY_API_URL,xml);
Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml);
//判断是否请求成功
String RETURN_CODE = "return_code";
String return_code = null;
if (resultMap.containsKey(RETURN_CODE)) {
return_code = resultMap.get(RETURN_CODE);
}
String success = "SUCCESS";
if(!success.equals(return_code)){
throw new AppException(ResultEnums.FAIL);
}
//判断签名是否正确
if (!WXPayUtil.isSignatureValid(resultMap, GlobalParas.APIKEY, WXPayConstants.SignType.MD5)) {
throw new AppException(ResultEnums.SIGN_FAIL);
}
//获取prepay_id(预付单信息)
String prepayId = resultMap.get("prepay_id");
String timeStamp = String.valueOf(WXPayUtil.getCurrentTimestamp());
String nonceStr = WXPayUtil.generateNonceStr();
String Package = "prepay_id="+prepayId;
Map paySignMap = new HashMap();
paySignMap.put("appId",GlobalParas.APPID);
paySignMap.put("timeStamp",timeStamp);//时间戳
paySignMap.put("nonceStr",nonceStr);//随机字符串
paySignMap.put("package",Package);//固定格式
paySignMap.put("signType","MD5");
//再次生成签名(含有预付单信息)
String paySign = WXPayUtil.generateSignature(paySignMap, GlobalParas.APIKEY);
JSONObject json = new JSONObject();
json.put("paySign",paySign);
json.put("timeStamp",timeStamp);
json.put("nonceStr",nonceStr);
json.put("Package",Package);
json.put("appId",GlobalParas.APPID);
//返回信息
result.setStatus(true);
result.setCode(ResultEnums.SELECT_SUCCESS.getCode());
result.setMsg(ResultEnums.SELECT_SUCCESS.getMessage());
result.setData(json);
} catch (Exception e) {
throw new AppException(ResultEnums.UN_KNOW_ERROR);
}
return result;
}
|