|
|
@@ -0,0 +1,693 @@
|
|
|
+package com.happy.action;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
+import com.happy.Model.*;
|
|
|
+import com.happy.Model.weixin.Pay;
|
|
|
+import com.happy.Model.weixin.PayResult;
|
|
|
+import com.happy.Model.weixin.WechatUnifiedOrder;
|
|
|
+import com.happy.Until.HttpUtils;
|
|
|
+import com.happy.Until.ResUtil;
|
|
|
+import com.happy.Until.ResponseUtil;
|
|
|
+import com.happy.Until.weixin.EndPay;
|
|
|
+import com.happy.Until.weixin.JaxbUtil;
|
|
|
+import com.happy.Until.weixin.PayWxUtil;
|
|
|
+import com.happy.Until.weixin.TongYiReturn;
|
|
|
+import com.happy.common.http.HttpsClient;
|
|
|
+import com.happy.common.wx.WxConfig;
|
|
|
+import com.happy.common.wx.WxConstants;
|
|
|
+import com.happy.common.wx.WxUtil;
|
|
|
+import com.happy.service.ConsumeService;
|
|
|
+import com.happy.service.RechargeService;
|
|
|
+import com.happy.service.UserService;
|
|
|
+import com.happy.service.WxMenuService;
|
|
|
+import com.opensymphony.xwork2.ActionSupport;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.struts2.ServletActionContext;
|
|
|
+import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class Wp extends ActionSupport implements ServletRequestAware {
|
|
|
+
|
|
|
+ private HttpServletRequest request;
|
|
|
+ public HttpServletResponse response;
|
|
|
+
|
|
|
+ public String code;
|
|
|
+ public String stu_number;
|
|
|
+ public String id_card;
|
|
|
+ public String begin_time;
|
|
|
+ public String collect_code;
|
|
|
+ public String user_name;
|
|
|
+ public String re_time;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ public UserService userService;
|
|
|
+ @Resource
|
|
|
+ public ConsumeService consumeService;
|
|
|
+ @Resource
|
|
|
+ public RechargeService rechargeService;
|
|
|
+ @Resource
|
|
|
+ public WxMenuService wxMenuService;
|
|
|
+
|
|
|
+ public String getUser_name() {
|
|
|
+ return user_name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUser_name(String user_name) {
|
|
|
+ this.user_name = user_name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCollect_code() {
|
|
|
+ return collect_code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCollect_code(String collect_code) {
|
|
|
+ this.collect_code = collect_code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getBegin_time() {
|
|
|
+ return begin_time;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setBegin_time(String begin_time) {
|
|
|
+ this.begin_time = begin_time;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId_card() {
|
|
|
+ return id_card;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setId_card(String id_card) {
|
|
|
+ this.id_card = id_card;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getStu_number() {
|
|
|
+ return stu_number;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setStu_number(String stu_number) {
|
|
|
+ this.stu_number = stu_number;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCode() {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCode(String code) {
|
|
|
+ this.code = code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpServletRequest getRequest() {
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRequest(HttpServletRequest request) {
|
|
|
+ this.request = request;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setServletRequest(HttpServletRequest request) {
|
|
|
+ this.request = request;
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpServletResponse getResponse() {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setResponse(HttpServletResponse response) {
|
|
|
+ this.response = response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getRe_time() {
|
|
|
+ return re_time;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRe_time(String re_time) {
|
|
|
+ this.re_time = re_time;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String openid() throws Exception {
|
|
|
+ Gson gson=new Gson();
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?" +
|
|
|
+ "appid="+ WeiXinUtil.appid +
|
|
|
+ "&secret="+ WeiXinUtil.screct +
|
|
|
+ "&js_code="+code+
|
|
|
+ "&grant_type=authorization_code";
|
|
|
+ if (code==null){
|
|
|
+ resultJson.put("mess", "code不能为空");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ String json = HttpsClient.sendPost(url, "");
|
|
|
+ HashMap<String, String> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, String>>(){}.getType());
|
|
|
+ String openid = "";
|
|
|
+ try {
|
|
|
+ openid = userMap.get("openid").toString();
|
|
|
+ }catch (Exception e){
|
|
|
+ resultJson.put("mess", "openid获取失败");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ System.out.println("openid: "+openid);
|
|
|
+ int num = userService.updateOpenid(openid, stu_number, id_card);
|
|
|
+ if (num > 0){
|
|
|
+ resultJson.put("mess", "绑定成功");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ resultJson.put("mess", "绑定异常");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String get_stu() throws Exception {
|
|
|
+ Gson gson=new Gson();
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?" +
|
|
|
+ "appid="+ WeiXinUtil.appid +
|
|
|
+ "&secret="+ WeiXinUtil.screct +
|
|
|
+ "&js_code="+code+
|
|
|
+ "&grant_type=authorization_code";
|
|
|
+ if (code==null){
|
|
|
+ resultJson.put("mess", "code不能为空");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ String json = HttpsClient.sendPost(url, "");
|
|
|
+ HashMap<String, String> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, String>>(){}.getType());
|
|
|
+ String openid = "";
|
|
|
+ try{
|
|
|
+ openid = userMap.get("openid").toString();
|
|
|
+ }catch (Exception e){
|
|
|
+ resultJson.put("mess", "code超时");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<Users> use = userService.queryByopenid(openid);
|
|
|
+ if (use == null){
|
|
|
+ resultJson.put("mess", "未查询到用户信息");
|
|
|
+ }else {
|
|
|
+ resultJson.put("mess", "返回成功");
|
|
|
+ resultJson.put("info", use);
|
|
|
+ }
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String queryConsume() throws Exception {
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ if (stu_number==null){
|
|
|
+ resultJson.put("mess", "学号异常");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ List<Consume> consumes = consumeService.queryRecordByStu(stu_number, begin_time);
|
|
|
+ if (consumes==null){
|
|
|
+ resultJson.put("mess", "本月无消费记录");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ resultJson.put("mess", "返回成功");
|
|
|
+ resultJson.put("data", consumes);
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统一下单生成 第四步,页面传入用户输入金额,ip,生成预支付订单,把微信回调的参数传递给页面,页面就可以调起支付
|
|
|
+ public String Pay() throws Exception {
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ Gson gson=new Gson();
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?" +
|
|
|
+ "appid="+ WeiXinUtil.appid +
|
|
|
+ "&secret="+ WeiXinUtil.screct +
|
|
|
+ "&js_code="+code+
|
|
|
+ "&grant_type=authorization_code";
|
|
|
+ if (code==null){
|
|
|
+ resultJson.put("mess", "code不能为空");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ String jsons = HttpsClient.sendPost(url, "");
|
|
|
+ HashMap<String, String> userMap = gson.fromJson(jsons.toString(), new TypeToken<HashMap<String, String>>(){}.getType());
|
|
|
+ String open_id = "";
|
|
|
+ try{
|
|
|
+ open_id = userMap.get("openid").toString();
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ resultJson.put("mess", "code超时");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<Users> u = userService.queryByopenid(open_id); // 查找是不是会员,不是会员不能充值
|
|
|
+ String num = request.getParameter("num");
|
|
|
+ session.setAttribute("num", num);// 存入金额session中
|
|
|
+ if (u != null ) {
|
|
|
+ String ip = request.getParameter("ip");
|
|
|
+ double money = Double.parseDouble(request.getParameter("num"));
|
|
|
+ int a = (int) (money * 100);
|
|
|
+ WechatUnifiedOrder w = new WechatUnifiedOrder();
|
|
|
+ w.setAppid(WeiXinUtil.appid);
|
|
|
+ w.setAttach("chuanghai");
|
|
|
+ w.setBody("chuanghai");
|
|
|
+ w.setMch_id(WeiXinUtil.account);
|
|
|
+ w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
|
|
|
+ w.setNotify_url(WeiXinUtil.ip + "wppayResulet.action");// 支付结果回调地址
|
|
|
+ w.setOpenid(open_id);
|
|
|
+ w.setOut_trade_no("" + new Date().getTime());
|
|
|
+ w.setSpbill_create_ip(ip);
|
|
|
+ w.setTotal_fee(a);
|
|
|
+ w.setTrade_type("JSAPI");
|
|
|
+ Map<String, String> params = new HashMap<String, String>();
|
|
|
+ params.put("attach", w.getAttach());
|
|
|
+ params.put("appid", w.getAppid());
|
|
|
+ params.put("mch_id", w.getMch_id());
|
|
|
+ params.put("nonce_str", w.getNonce_str());
|
|
|
+ params.put("body", w.getBody());
|
|
|
+ params.put("out_trade_no", w.getOut_trade_no());
|
|
|
+ params.put("total_fee", w.getTotal_fee() + "");
|
|
|
+ params.put("spbill_create_ip", w.getSpbill_create_ip());
|
|
|
+ params.put("notify_url", w.getNotify_url());
|
|
|
+ params.put("trade_type", w.getTrade_type());
|
|
|
+ params.put("openid", w.getOpenid());
|
|
|
+ w.setSign(PayWxUtil.getSign(params, WeiXinUtil.APIid));
|
|
|
+ params.put("sign", w.getSign());
|
|
|
+ String retXml = JaxbUtil.getRequestXml(params);
|
|
|
+ String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", retXml);
|
|
|
+ System.out.println(444);
|
|
|
+ if (msg.indexOf("FAIL") > -1) {
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("pay", "error");
|
|
|
+ ResponseUtil.write(json, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ JaxbUtil requestBinder = new JaxbUtil(TongYiReturn.class, JaxbUtil.CollectionWrapper.class);
|
|
|
+ TongYiReturn to = requestBinder.fromXml(msg);
|
|
|
+ if (to.getReturn_code().equals("SUCCESS") && to.getResult_code().equals("SUCCESS")) {
|
|
|
+ EndPay pay = new EndPay();
|
|
|
+ pay.setAppId(WeiXinUtil.appid);
|
|
|
+ pay.setSignType("MD5");
|
|
|
+ pay.setTimeStamp(System.currentTimeMillis() / 1000 + "");
|
|
|
+ pay.setPrepay_id(to.getPrepay_id());
|
|
|
+ pay.setNonceStr(PayWxUtil.getNonceStr());
|
|
|
+ Map<String, String> requestMap = new HashMap<String, String>();
|
|
|
+ requestMap.put("appId", pay.getAppId());
|
|
|
+ requestMap.put("timeStamp", pay.getTimeStamp());
|
|
|
+ requestMap.put("nonceStr", pay.getNonceStr());
|
|
|
+ requestMap.put("package", "prepay_id=" + pay.getPrepay_id());
|
|
|
+ requestMap.put("signType", "MD5");
|
|
|
+ pay.setPaySign(PayWxUtil.getSign(requestMap, WeiXinUtil.APIid));
|
|
|
+ // requestMap.put("sign",pay.getPaySign());
|
|
|
+ // String ret = JaxbUtil.getRequestXml(requestMap);
|
|
|
+ // System.out.println(ret);
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("pay", pay);
|
|
|
+ if (session.getAttribute("p_biao") != null) { // 说明时从打转盘页面进来的,支付完成后继续调到转盘页面
|
|
|
+ json.put("p_biao", 1);
|
|
|
+ session.removeAttribute("p_biao");
|
|
|
+ } else {
|
|
|
+ json.put("p_biao", 0);
|
|
|
+ }
|
|
|
+ ResUtil.write(json, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else { // 说明不是会员
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("pay", 1);
|
|
|
+ ResUtil.write(json, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第五步:支付页面用户输完密码后微信会把支付结果回调到这里,我们根据需要存储支付记录,和执行不同的方法
|
|
|
+ public String payResulet() throws Exception { // 微信支付结果通知
|
|
|
+// System.out.println("进来了");
|
|
|
+ BufferedReader reader = null;// BufferedReader 字符输入流
|
|
|
+
|
|
|
+ reader = request.getReader();
|
|
|
+ String line = "";
|
|
|
+ String xmlString = null;
|
|
|
+ StringBuffer inputString = new StringBuffer();
|
|
|
+
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ inputString.append(line);
|
|
|
+ }
|
|
|
+ xmlString = inputString.toString();
|
|
|
+ request.getReader().close();
|
|
|
+
|
|
|
+ JaxbUtil requestBinder = new JaxbUtil(PayResult.class, PayResult.class);
|
|
|
+ PayResult result = requestBinder.fromXml(xmlString);// Xml到Java,用fromXML()方法
|
|
|
+
|
|
|
+ if (result.getResult_code().equals("SUCCESS")) { // 交易成功,支付结果转换为对象
|
|
|
+ String account = result.getTransaction_id();// 微信支付订单号
|
|
|
+
|
|
|
+ System.out.println("支付成功了");
|
|
|
+ System.out.println("result=" + result.toString());
|
|
|
+
|
|
|
+ Recharge pays = rechargeService.findPayByNo(account);// 根据 微信支付订单号 查找支付记录信息
|
|
|
+
|
|
|
+ if (pays != null) { // 有交易记录 发送成功消息给商家
|
|
|
+ String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
|
|
+ HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
+ response.setContentType("text/html;charset=utf-8");
|
|
|
+ PrintWriter out = response.getWriter();
|
|
|
+ out.println(returnMsg);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+// System.out.println("pay不为空");
|
|
|
+ } else { // 交易记录开始记录
|
|
|
+// if(openid.equals(result.getOpenid()) && num == result.getTotal_fee() / 100){ //支付成功
|
|
|
+ // 获得交易支付成功用户的信息
|
|
|
+ Recharge pay1 = new Recharge();
|
|
|
+ List<Users> u = userService.queryByopenid(result.getOpenid()); // 查找充值的会员信息
|
|
|
+ pay1.setAccount((double) result.getTotal_fee() / 100);
|
|
|
+ System.out.println("金额:"+ result.getTotal_fee());
|
|
|
+ pay1.setUser_name(u.get(0).getUser_name());
|
|
|
+ pay1.setStu_number(u.get(0).getStu_number());
|
|
|
+ pay1.setCard_number(u.get(0).getCard_number());
|
|
|
+ pay1.setOrder_num(new String(account.getBytes("utf-8")));
|
|
|
+ SimpleDateFormat simp = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ SimpleDateFormat simp1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ Date date = simp.parse(result.getTime_end());
|
|
|
+ String time = simp1.format(date);
|
|
|
+ pay1.setRe_time(time); // 支付结束时间
|
|
|
+
|
|
|
+ System.out.println("金额:"+pay1.getAccount());
|
|
|
+ rechargeService.insertRecharge(pay1);
|
|
|
+ double bal = u.get(0).getBalance() + (double) result.getTotal_fee() / 100;
|
|
|
+ consumeService.updateBalance(u.get(0).getStu_number(), bal);
|
|
|
+
|
|
|
+ // userService.update2((double) result.getTotal_fee() / 10, u.getId());
|
|
|
+ String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
|
|
+ HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
+ response.setContentType("text/html;charset=utf-8");
|
|
|
+ PrintWriter out = response.getWriter();
|
|
|
+ out.println(returnMsg);
|
|
|
+ out.flush();
|
|
|
+ out.close(); // 发送成功消息给商家
|
|
|
+// }
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String start() throws Exception {
|
|
|
+ Gson gson=new Gson();
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
|
|
|
+ String time = df.format(new Date());
|
|
|
+ String dang = WxUtil.mchOrderNo();
|
|
|
+ double basic_amount = 10.00;
|
|
|
+ double residue = 0.00;
|
|
|
+ double use_size = 0.00;
|
|
|
+ if (stu_number == null){
|
|
|
+ resultJson.put("mess", "学号为空");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotBlank(collect_code)) {
|
|
|
+ String sign = wxMenuService.wxUrl(dang, WxConstants.SING_MD5, collect_code, time);
|
|
|
+ String requestUrl = "https://app.dev.9kbs.com/shopOpenApi/services/deviceAntiScode/getStartCode?apicode=" + WxConfig.apicode
|
|
|
+ + "&collect_code=" + collect_code + "&order_no=" + dang + "&amount=10×tamp="
|
|
|
+ + time + "&sign=" + sign;
|
|
|
+ String json = HttpsClient.sendPost2(requestUrl, "");
|
|
|
+ HashMap<String, Object> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, Object>>(){}.getType());
|
|
|
+ String status = "";
|
|
|
+ try {
|
|
|
+ status = userMap.get("status").toString();
|
|
|
+ }catch (Exception e){
|
|
|
+ resultJson.put("mess", "第三方接口异常");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (status.equals("succ")) {
|
|
|
+ String data = userMap.get("data").toString();
|
|
|
+ HashMap<String, Object> device = gson.fromJson(data.toString(), new TypeToken<HashMap<String, Object>>() {}.getType());
|
|
|
+ String start_code = device.get("start_code").toString();
|
|
|
+ System.out.println("启动码:"+start_code);
|
|
|
+ String collect_data = device.get("collect_data").toString();
|
|
|
+ HashMap<String, Object> money = gson.fromJson(collect_data.toString(), new TypeToken<HashMap<String, Object>>() {}.getType());
|
|
|
+ String device_code = money.get("device_code").toString();
|
|
|
+ String order_no = new BigDecimal(money.get("order_no").toString()).toString();
|
|
|
+ String use_amount = money.get("use_amount").toString();
|
|
|
+ // 根据订单号获取上次消费记录
|
|
|
+ List<Consume> reconsumes = consumeService.queryRecordByOrder(order_no);
|
|
|
+ // 本次消息信息
|
|
|
+ List<Users> user0 = userService.selectByStu(stu_number);
|
|
|
+ // 上次无用户消费
|
|
|
+ if (reconsumes == null){
|
|
|
+ if (user0 != null) { // 本次消费信息,余额
|
|
|
+ Double balance = user0.get(0).getBalance();
|
|
|
+ if (balance > basic_amount) {
|
|
|
+ // 保存本次消费信息,但是没有消费金额
|
|
|
+ Consume cons = new Consume();
|
|
|
+ cons.setStu_number(stu_number);
|
|
|
+ cons.setUser_name(user0.get(0).getUser_name());
|
|
|
+ cons.setOrder_no(dang);
|
|
|
+ cons.setBegin_time(time);
|
|
|
+ cons.setStart_code(start_code);
|
|
|
+ cons.setDevice_code(device_code);
|
|
|
+ cons.setUse_amount(0.00);
|
|
|
+ cons.setUse_size(0.00);
|
|
|
+ int num = consumeService.insertComsume(cons);
|
|
|
+ if (num > 0) { // 保存成功
|
|
|
+ resultJson.put("mess", "启动成功");
|
|
|
+ resultJson.put("start_code", start_code);
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "余额小于10元!请充值");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "卡号异常");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // 上次消费卡号
|
|
|
+ String restu_number = reconsumes.get(0).getStu_number();
|
|
|
+ // 上次用户信息
|
|
|
+ List<Users> reusers = userService.selectByStu(restu_number);
|
|
|
+ if (reusers != null ) {
|
|
|
+ if (reconsumes.get(0).getState() == 0) { // 上次消费未结算
|
|
|
+ Double rebalance = reusers.get(0).getBalance();
|
|
|
+ double use_amounts = Double.parseDouble(use_amount);
|
|
|
+ if (rebalance > use_amounts) {
|
|
|
+ residue = rebalance - use_amounts;
|
|
|
+ List<Price> ws = consumeService.water_price("热水");
|
|
|
+ if (ws!=null){
|
|
|
+ System.out.println("热水有价");
|
|
|
+ use_size = use_amounts/ws.get(0).getPrice();
|
|
|
+ }
|
|
|
+ int num1 = consumeService.updateConsume2(order_no, time, use_amounts, use_size, 1);
|
|
|
+ int num2 = consumeService.updateBalance(restu_number, residue);
|
|
|
+ // 上次消费扣除完毕
|
|
|
+ if (num1 > 0 && num2 > 0) {
|
|
|
+ // 查询本次用户信息
|
|
|
+ System.out.println("学号:"+stu_number);
|
|
|
+ List<Users> users2 = userService.selectByStu(stu_number);
|
|
|
+ if (users2 != null ) {
|
|
|
+ // 保存本次消费记录,但是没有消费金额
|
|
|
+ Consume cons = new Consume();
|
|
|
+ cons.setCard_number(stu_number);
|
|
|
+ cons.setUser_name(user_name);
|
|
|
+ cons.setOrder_no(dang);
|
|
|
+ cons.setBegin_time(time);
|
|
|
+ cons.setStart_code(start_code);
|
|
|
+ cons.setDevice_code(device_code);
|
|
|
+ cons.setUse_amount(0.00);
|
|
|
+ cons.setUse_size(0.00);
|
|
|
+ int num = consumeService.insertComsume(cons);
|
|
|
+ if (num > 0) {
|
|
|
+ double bal = users2.get(0).getBalance();
|
|
|
+ if (bal > basic_amount) {
|
|
|
+ resultJson.put("mess", "启动成功");
|
|
|
+ resultJson.put("start_code", start_code);
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "余额不足!请充值");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "本次订单提交失败!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "用户信息异常!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "结算失败!请联系管理员");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "上次消费用户余额不足!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else { // 上次消费已结算
|
|
|
+ // 查询本次用户信息
|
|
|
+ List<Users> users2 = userService.selectByStu(stu_number);
|
|
|
+ if (users2 != null ) {
|
|
|
+ // 保存本次消费记录,但是没有消费金额
|
|
|
+ Consume cons = new Consume();
|
|
|
+ cons.setStu_number(stu_number);
|
|
|
+ cons.setUser_name(users2.get(0).getUser_name());
|
|
|
+ cons.setOrder_no(dang);
|
|
|
+ cons.setBegin_time(time);
|
|
|
+ cons.setStart_code(start_code);
|
|
|
+ cons.setDevice_code(device_code);
|
|
|
+ int num = consumeService.insertComsume(cons);
|
|
|
+ if (num > 0) {
|
|
|
+ double bal = users2.get(0).getBalance();
|
|
|
+ if (bal > basic_amount) {
|
|
|
+ System.out.println("启动码:"+start_code);
|
|
|
+ resultJson.put("mess", "启动成功");
|
|
|
+ resultJson.put("start_code", start_code);
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "余额不足!请充值");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "本次订单提交失败!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "用户信息异常!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "用户信息异常!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ resultJson.put("mess", "请求参数错误!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ resultJson.put("mess", "收集码获取失败!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String consume() throws Exception {
|
|
|
+ Gson gson=new Gson();
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
|
|
|
+ String time = df.format(new Date());
|
|
|
+ String dang = WxUtil.mchOrderNo();
|
|
|
+ double basic_amount = 0.00;
|
|
|
+ double residue = 0.00;
|
|
|
+ double use_size = 0.00;
|
|
|
+ System.out.println(2222);
|
|
|
+ if(StringUtils.isNotBlank(collect_code)) {
|
|
|
+ System.out.println(3333);
|
|
|
+ String sign = wxMenuService.wxUrl("", WxConstants.SING_MD5, collect_code, time);
|
|
|
+ String requestUrl = "https://app.dev.9kbs.com/shopOpenApi/services/deviceAntiScode/getStartCode?apicode=" + WxConfig.apicode
|
|
|
+ + "&collect_code=" + collect_code + "&order_no=" + "" + "&amount=10×tamp="
|
|
|
+ + time + "&sign=" + sign;
|
|
|
+ String json = HttpsClient.sendPost2(requestUrl, "");
|
|
|
+ System.out.println(json);
|
|
|
+ HashMap<String, Object> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, Object>>(){}.getType());
|
|
|
+ String status = userMap.get("status").toString();
|
|
|
+ if (status.equals("succ")) {
|
|
|
+ String data = userMap.get("data").toString();
|
|
|
+ HashMap<String, Object> device = gson.fromJson(data.toString(), new TypeToken<HashMap<String, Object>>() {}.getType());
|
|
|
+ String collect_data = device.get("collect_data").toString();
|
|
|
+ HashMap<String, Object> money = gson.fromJson(collect_data.toString(), new TypeToken<HashMap<String, Object>>() {}.getType());
|
|
|
+ String order_no = new BigDecimal(money.get("order_no").toString()).toString();
|
|
|
+ String use_amount = money.get("use_amount").toString();
|
|
|
+ // 根据订单号获取本次消费记录
|
|
|
+ List<Consume> reconsumes = consumeService.queryRecordByOrder(order_no);
|
|
|
+ // 订单异常
|
|
|
+ if (reconsumes == null){
|
|
|
+ resultJson.put("mess", "订单异常,请联系管理员!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }else {
|
|
|
+ // 上次消费卡号
|
|
|
+ String restu_number = reconsumes.get(0).getStu_number();
|
|
|
+ // 本次用户信息
|
|
|
+ List<Users> reusers = userService.selectByStu(restu_number);
|
|
|
+ if (reusers != null ) {
|
|
|
+ if (reconsumes.get(0).getState() == 0) { // 本次消费未结算
|
|
|
+ double rebalance = reusers.get(0).getBalance();
|
|
|
+ double use_amounts = Double.parseDouble(use_amount);
|
|
|
+ if (rebalance > use_amounts) {
|
|
|
+ residue = rebalance - use_amounts;
|
|
|
+ List<Price> ws = consumeService.water_price("热水");
|
|
|
+ if (ws!=null){
|
|
|
+ use_size = use_amounts / ws.get(0).getPrice();
|
|
|
+ }
|
|
|
+ int num1 = consumeService.updateConsume2(order_no, time, use_amounts, use_size, 1);
|
|
|
+ int num2 = consumeService.updateBalance(restu_number, residue);
|
|
|
+ // 本次消费扣除完毕
|
|
|
+ if (num1 > 0 && num2 > 0) {
|
|
|
+ resultJson.put("mess", "支付成功");
|
|
|
+ resultJson.put("use_amount", use_amounts);
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "结算异常!请联系管理员");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "上次消费用户余额不足!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else { // 本次消费已结算
|
|
|
+ resultJson.put("mess", "已支付!请勿重复支付");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resultJson.put("mess", "用户信息异常!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ resultJson.put("mess", "请求参数错误!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ resultJson.put("mess", "收集码获取失败!");
|
|
|
+ ResUtil.write(resultJson, ServletActionContext.getResponse());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|