微信支付java(以上线)
//支付里面的sdk都是用官网的下载好,改好路径就可以使用
//action代码
public void wxmoney() throws Exception {
// 获取请求ip地址
String openId = getMemberBean().getOpenid();
List<Member> wxlist = memberService.findidcard(openId);
// String wxidcard=wxlist.get(0).getWxidcard();
Integer type = getMemberBean().getType();// 选择的午晚餐
String wxdate = getMemberBean().getDate();// 用户选择的日期
Integer status = wxlist.get(0).getShStatus();// 每次支付判断状态 首先会判断前端的缓存如果没有则判断后台
SimpleDateFormat wxda = new SimpleDateFormat("yyyy-MM-dd");
Date wxdate1 = wxda.parse(wxdate);
String wxdate2 = DateFormatUtils.format(wxdate1, "yyyy-MM-dd 00:00:00");
SimpleDateFormat wxdate3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date wxdate5 = wxdate3.parse(wxdate2);// 转换类型格式
double tol;
if (status != 1) {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("statusCode", 200);
jsonMap.put("message", status);
String json = JsonUtil.convert2JSONString(jsonMap);
System.out.println(json);
printlnToClient(json);
} else {
// 已付款的状态
String str = "yfk";
List<com.hz.persist.order.Order> mobiewx = orderService.getorder(wxdate5, str, openId, type);
// 如果该用户已经够买过此时间的商品则跳出前端提示
if (mobiewx.isEmpty()) {
// 获取今天的时间
/*
* String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
* String date1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59");
* System.out.println(date); System.out.println(date1); SimpleDateFormat sdf =
* new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date3 = sdf.parse(date);
* Date date4 = sdf.parse(date1);
*/
// 查询用户预订的时间是否有支付过订单
List<com.hz.persist.order.Order> listnewdate = orderService.seOrder(wxdate5, str, openId);
// 获取本月的第一天
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
// 获取本月最后一天
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, 1);
calendar1.set(Calendar.DAY_OF_MONTH, 0);
// 查询本月优惠是否购买超过22次
BigDecimal mypice = new BigDecimal(10); // 本月的订单小于10的都查出来
List<com.hz.persist.order.Order> listorder = orderService.sOrder(str, openId, calendar.getTime(),
calendar1.getTime(), mypice);
// 查询商品信息
Integer proid = getMemberBean().getProid();// 商品id
List<Goods> fanshi = orderService.findorder(getMemberBean(), proid);
Goods list1 = fanshi.get(0);
BigDecimal price = list1.getMarketPrice();// 饭票价格
String price1 = valueOf(price);
double price2 = Double.parseDouble(price1);
// 判断该用户当天是否有订单没有就优惠,有就没优惠,如果该月订单超过22次
if (listnewdate.isEmpty() && listorder.size() <= 22) {
tol = price2 * 100 - 1000;
// tol=15*100-1490;
System.out.println("该用户有优惠");
} else {
tol = price2 * 100;
// tol=15*100-1480;
System.out.println("该用户这个月优惠已使用");
}
// 获取饭票价格
// Integer id=list1.getId();//饭票id
String name = list1.getName();// 饭票名称
/* List<Goods> list = orderService.findGoods(getOrderBean(),proid); */
String ip = "183.0.98.81";
/*
* 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(); }
*/
// DecimalFormat df=new DecimalFormat("0");
// df.format(price);
//下面是支付的主要代码
DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
String Strprice = decimalFormat.format(tol); // 传入map集合发送请求所需要的的参数
Map<String, String> paraMap = new HashMap<String, String>();
paraMap.put("body", name); // 商品名称
paraMap.put("openid", openId);// 微信用户openid通过登录的code换取
paraMap.put("out_trade_no", UUID.randomUUID().toString().replaceAll("-", ""));// 订单号,每次都不同
paraMap.put("spbill_create_ip", ip);// ip地址
paraMap.put("total_fee", Strprice);// 商品金额 注意是以分为单位需要*100
paraMap.put("trade_type", "JSAPI"); // 支付类型默认
System.out.println("trade_type" + "JSAPI");
System.out.println("================ 分隔线 ==============");
// 支付成功的回调,暂时没完成该功能
final String SUCCESS_NOTIFY = "http://localhost:8080/uniAPP/wx/money/yes.action";
boolean useSandbox = false;
// 是否使⽤沙盒环境⽀付API,是的话不会真正扣钱
WXPayConfig config = new MyWXPayConfig();
// 使用WXPay工具加密为sign签名
WXPay wxPay = new WXPay(config, SUCCESS_NOTIFY, false, useSandbox);
// 3. fillRequestData会将上述参数⽤key=value形式和mchKey⼀起加密为sign,验证地址:
// https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1
Map<String, String> map = wxPay.unifiedOrder(wxPay.fillRequestData(paraMap), 15000, 15000);
// 4. 发送post请求"统⼀下单接⼝"(https://api.mch.weixin.qq.com/pay/unifiedorder),
// //返回预⽀付id:prepay_id
String prepayId = (String)map.get("prepay_id");
System.out.println("xmlStr 为 :" + map);
System.out.println("================ 分隔线 ==============");
Map<String, String> payMap = new HashMap<String, String>();
payMap.put("appId", WechatConstant.APPID);// 小程序id
payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
// 微信工具生成的
payMap.put("nonceStr", WXPayUtil.generateNonceStr());// 微信工具生成的
if (useSandbox) {
payMap.put("signType", WXPayConstants.MD5);
} else {
payMap.put("signType", WXPayConstants.HMACSHA256);
}
payMap.put("package", "prepay_id=" + prepayId);
// 5. 通过appId, timeStamp, nonceStr, signType, package及商户密钥进⾏key=value形式拼接并加密
String paySign = null;
if (useSandbox) {
paySign = WXPayUtil.generateSignature(payMap, WechatConstant.MCH_KEY, WXPayConstants.SignType.MD5);
} else {
paySign = WXPayUtil.generateSignature(payMap, WechatConstant.MCH_KEY,
WXPayConstants.SignType.HMACSHA256);
}
payMap.put("paySign", paySign);
// 6. 将这个6个参数传给前端
System.out.println("payMap:" + payMap);
String proid1 = paraMap.get("out_trade_no");
System.out.println("商户订单号:" + proid1);
Map<String, Object> jsonMap = new HashMap<String, Object>();
// JSONObject list=JSONObject.fromObject(paySign);
jsonMap.put("statusCode", 200);
jsonMap.put("list", payMap);// 暂时不用支付功能
jsonMap.put("list1", proid1);
//jsonMap.put("list2", "成功返回");
jsonMap.put("message", status);
jsonMap.put("price", tol);
//jsonMap.put("listorder", payMap);
// 返回的格式必须bjui规定的格式:{"statusCode" : 200,"message" : "处理成功!"}
String json = JsonUtil.convert2JSONString(jsonMap);
System.out.println(json);
// 返回结果给客户端,供json使用,需要用到response中的writer
printlnToClient(json);
} else {
// 如果该用户已经够买过此时间的商品则跳出
Map<String, Object> jsonMap = new HashMap<String, Object>();
// JSONObject list=JSONObject.fromObject(paySign);
jsonMap.put("statusCode", 200);
jsonMap.put("message", status);
// jsonMap.put("listorder", payMap);
// 返回的格式必须bjui规定的格式:{"statusCode" : 200,"message" : "处理成功!"}
String json = JsonUtil.convert2JSONString(jsonMap);
System.out.println(json);
// 返回结果给客户端,供json使用,需要用到response中的writer
printlnToClient(json);
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-8110.html