package com.sharemao.web.wx.ctrl; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.RoundingMode; import java.security.PrivateKey; import java.security.PublicKey; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import javax.servlet.http.HttpSession; import com.sharemao.web.wx.syb.SybConstants; import com.sharemao.web.wx.syb.SybUtil; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson2.JSON; import com.nianzai.base.mvc.Context; import com.nianzai.base.mvc.annotation.Controller; import com.nianzai.base.mvc.annotation.Path; import com.nianzai.base.mvc.view.SimpleJsonView; import com.nianzai.base.mvc.view.TextView; import com.nianzai.util.CodecUtil; import com.nianzai.util.Tools; import com.sharemao.web.wx.Constants; import com.sharemao.web.wx.JsApiReqData; import com.sharemao.web.wx.JsApiToJsData; import com.sharemao.web.wx.util.GzhUtil; import com.sharemao.web.wx.util.JsApiReqUtils; import com.sharemao.web.wx.util.Message2XmlUtil; import com.sharemao.web.wx.util.PaySign; import com.sharemao.web.wx.util.SignUtils; import com.sharemao.web.wx.util.XmlUtils; import com.sharemao.web.wx.util.YL; @Controller("/mifi") public class PayCtrl { private static final Log log = LogFactory.getLog(PayCtrl.class); /** * 跳转至商品下单界面 * @param context * @return */ @Path(value="/pdtOrder.do") public TextView pdtOrder(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String appuid = String.valueOf(session.getAttribute("appuid")); if(GzhUtil.isEmpty(appuid)) return new TextView(Constants.loginUrl); para.put("appuid", appuid); String addressId = para.get("addressId"); GzhUtil.toM3Sign(para); // 获取默认收货地址 String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); JSONObject dataJson = resJson.getJSONObject("data"); JSONArray addrJson = dataJson.getJSONArray("addr"); JSONObject address = new JSONObject(); if(addrJson.size() > 0) { if(!GzhUtil.isEmpty(addressId)) { for(int i=0; i 25) { detail = detail.substring(0, 25)+"..."; address.put("address", detail); } else address.put("address", detail); context.getRequest().setAttribute("addr", address); } @SuppressWarnings("unchecked") Map pdtInfo = (Map) session.getAttribute("pdtInfo"); context.getRequest().setAttribute("pdtInfo", pdtInfo); return new TextView("/order/pdtorder.jsp"); } /** * 跳转至套餐购买下单界面 * @param context * @return */ @Path(value="/settlement.do") public TextView settlement(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); log.info("para==>"+para); String devid = para.get("devid"); String pkgid = para.get("pkgid"); String price0 = para.get("price"); String sign = para.get("sign"); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); String wxCode = para.get("code"); String opt = para.get("opt"); if(GzhUtil.isEmpty(devid)) { return new TextView("/user/index.do"); } if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { if(!GzhUtil.isEmpty(wxCode)) { Map m=GzhUtil.getOpenId(wxCode,Constants.appId,Constants.appSecret); openid=m.get("openId"); String accessToken=m.get("accessToken"); session.setAttribute("openid", openid); session.setAttribute("accessToken", accessToken); log.info("wxCode="+wxCode+",openid="+openid); para.put("openid", openid); GzhUtil.toM3Sign(para); String u=Constants.m3IntAddr + "/user/wxIndex.do"; String result = GzhUtil.sendPost(u, para); log.info("url="+u+",result="+result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); if(dataJson!=null) { appuid=dataJson.getString("appuid"); } session.setAttribute("appuid", appuid); session.setAttribute("openid", openid); } } if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { para.put("devid", devid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getInfoByDevid.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); appuid=dataJson.getString("id"); openid=dataJson.getString("openid"); if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { return new TextView("/user/index.do"); } session.setAttribute("appuid", appuid); session.setAttribute("openid", openid); } else { return new TextView("/user/index.do"); } } } context.getRequest().setAttribute("devid", devid); context.getRequest().setAttribute("opt", opt); String signstr = pkgid+'#'+devid+'*'+price0; String signc = CodecUtil.str2md5(signstr); if(!signc.equals(sign)) { return new TextView("/user/index.do"); } if(!GzhUtil.isEmpty(pkgid)) { para.put("pkid", pkgid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getPkgByPkid.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info(resJson); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); dataJson = JSONObject.parseObject(dataJson.getString("pkg")); String pkgName = dataJson.getString("pkgName"); String pkgDesc = dataJson.getString("pkgDesc"); double price = dataJson.getDoubleValue("price"); String ts = dataJson.getString("ts"); String expiryDay = dataJson.getString("expiryDay"); String paytype = dataJson.getString("paytype"); Map pkgInfo = new HashMap(); pkgInfo.put("pkgName", pkgName); pkgInfo.put("pkgDesc", pkgDesc); if(!GzhUtil.isEmpty(price0)) { price = Double.parseDouble(price0); } pkgInfo.put("price", price); pkgInfo.put("pkgid", pkgid); pkgInfo.put("ts", ts); pkgInfo.put("expiryDay", expiryDay); pkgInfo.put("paytype", paytype); session.setAttribute("pkgInfo", pkgInfo); context.getRequest().setAttribute("pkgInfo", pkgInfo); } } para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String balance=dataJson.getString("balance"); context.getRequest().setAttribute("balance", balance); } else { context.getRequest().setAttribute("balance", "0.00"); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getSPkgs.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); log.info(dataJson); context.getRequest().setAttribute("spkgs", dataJson); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/packkey/getMember.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); context.getRequest().setAttribute("cons", dataJson); } return new TextView("/pkgordersyb.jsp"); // return new TextView("/pkgorder.jsp"); } /** * 跳转至加速包购买下单界面 * @param context * @return */ @Path(value="/settlementSpkgs.do") public TextView settlementSpkgs(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); log.info("spkgs para==>"+para); String devid = para.get("devid"); String spkgid = para.get("spkgid"); String price0 = para.get("price"); String sign = para.get("sign"); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); String wxCode = para.get("code"); if(GzhUtil.isEmpty(devid)) { return new TextView("/user/index.do"); } if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { if(!GzhUtil.isEmpty(wxCode)) { Map m=GzhUtil.getOpenId(wxCode,Constants.appId,Constants.appSecret); openid=m.get("openId"); String accessToken=m.get("accessToken"); session.setAttribute("openid", openid); session.setAttribute("accessToken", accessToken); log.info("wxCode="+wxCode+",openid="+openid); para.put("openid", openid); GzhUtil.toM3Sign(para); String u=Constants.m3IntAddr + "/user/wxIndex.do"; String result = GzhUtil.sendPost(u, para); log.info("url="+u+",result="+result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); if(dataJson!=null) { appuid=dataJson.getString("appuid"); } session.setAttribute("appuid", appuid); session.setAttribute("openid", openid); } } if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { para.put("devid", devid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getInfoByDevid.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); appuid=dataJson.getString("id"); openid=dataJson.getString("openid"); if(GzhUtil.isEmpty(appuid) || GzhUtil.isEmpty(openid)) { return new TextView("/user/index.do"); } session.setAttribute("appuid", appuid); session.setAttribute("openid", openid); } else { return new TextView("/user/index.do"); } } } context.getRequest().setAttribute("devid", devid); context.getRequest().setAttribute("amount", price0); context.getRequest().setAttribute("speedpackid", spkgid); String signstr = spkgid+'#'+devid+'*'+price0; String signc = CodecUtil.str2md5(signstr); if(!signc.equals(sign)) { return new TextView("/mifi/benefit.do"); } para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String balance=dataJson.getString("balance"); context.getRequest().setAttribute("balance", balance); } else { context.getRequest().setAttribute("balance", "0.00"); } return new TextView("/mifi/benefitSubmit.do"); } /** * 跳转至套餐购买下单界面 * @param context * @return */ @Path(value="/pkgOrder.do") public TextView pkgOrder(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String appuid = String.valueOf(session.getAttribute("appuid")); String cursn=(String)session.getAttribute("cursn"); int devtype=GzhUtil.getDeviceType(cursn); if(devtype==0) { if(GzhUtil.isEmpty(appuid)) return new TextView(Constants.loginUrl); } String devid = cursn; if (GzhUtil.isEmpty(devid)) { devid = GzhUtil.getDevID(session, para); } @SuppressWarnings("unchecked") Map pkgInfo = (Map) session.getAttribute("pkgInfo"); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String balance=dataJson.getString("balance"); context.getRequest().setAttribute("balance", balance); } else { context.getRequest().setAttribute("balance", "0.00"); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getSPkgs.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); log.info(dataJson); context.getRequest().setAttribute("spkgs", dataJson); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/packkey/getMember.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); context.getRequest().setAttribute("cons", dataJson); } context.getRequest().setAttribute("pkgInfo", pkgInfo); context.getRequest().setAttribute("devid", devid); if("151992".equals(appuid)) { return new TextView("/pkgordersyb.jsp"); } return new TextView("/pkgordersyb.jsp"); // return new TextView("/pkgorder.jsp"); } /** * 购买产品/套餐下单-支付 * @param context * @return */ @Path(value="/buyPdts.do") @SuppressWarnings("unchecked") public SimpleJsonView buyPdts(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String appuid = String.valueOf(session.getAttribute("appuid")); // if(GzhUtil.isEmpty(appuid)) // return new SimpleJsonView(Constants.noLoginMsg); String openid = (String) session.getAttribute("openid"); if(Tools.isEmpty(Constants.appId) || Tools.isEmpty(Constants.apikey) || Tools.isEmpty(Constants.appSecret) || Tools.isEmpty(Constants.mch_id)) { log.info("充值失败,支付信息为空>>>代理ID:"+Constants.orgid); return new SimpleJsonView("支付信息错误,请联系客服"); } String amount = ""; //订单价格 String outTradeNo = ""; //订单ID String body = ""; int type = Integer.parseInt(para.get("type")); log.info("type="+type); if(type == 0) { body = "商品购买"; Map pdtInfo = (Map) session.getAttribute("pdtInfo"); amount = String.valueOf(pdtInfo.get("total")); para.put("pdtid", String.valueOf(pdtInfo.get("pdtid"))); para.put("appuid", appuid); para.put("num", String.valueOf(pdtInfo.get("num"))); para.put("price", amount); /***********************下单,获取订单编号-BEGIN***********************/ log.info("设备购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/device/buyPdts.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买设备获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson.getString("msg")); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } else if(type == 1) { body = "套餐购买"; Map pkgInfo = (Map) session.getAttribute("pkgInfo"); //amount = String.valueOf(pkgInfo.get("price")); amount = String.valueOf(para.get("amount")); para.put("pkid", String.valueOf(pkgInfo.get("pkgid"))); para.put("appuid", appuid); para.put("price", amount); para.put("ts", String.valueOf(pkgInfo.get("ts"))); /***********************下单,获取订单编号-BEGIN***********************/ log.info("套餐购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyPkg.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买套餐获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } /***********************下单,获取订单编号-END***********************/ //执行统一下单接口,初始化各参数 String nonceStr = PaySign.getNonceStr(); String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); //初始化传入参数 BigDecimal dised = new BigDecimal(amount); // BigDecimal dised = new BigDecimal(0.01); //测试价格0.01,待修改为totalAmount zby BigDecimal diz = new BigDecimal(100); JsApiReqData jsApiReqData = new JsApiReqData(body,nonceStr,outTradeNo,dised.multiply(diz).intValue(),spBillCreateIP, openid,Constants.appId,Constants.mch_id,Constants.apikey); String info = Message2XmlUtil.JsApiReqData2xml(jsApiReqData).replaceAll("mch__id", "mch_id").replaceAll("nonce__str", "nonce_str").replaceAll("notify__url", "notify_url").replaceAll("out__trade__no", "out_trade_no").replaceAll("spbill__create__ip", "spbill_create_ip").replaceAll("total__fee>", "total_fee>").replaceAll("trade__type", "trade_type"); log.info("info========="+info); String prepay_id="err"; StringBuffer sb = JsApiReqUtils.httpsRequest(Constants.unifiedorderurl, "POST", info); log.info("sb=="+sb.toString()); Map payInfo = new HashMap(); if (!"".equals(sb.toString())) { try { //得到预支付ID,即prepay_id Map getMap = Message2XmlUtil.parseXml(new String(sb.toString().getBytes(), "utf-8")); if(getMap.get("prepay_id")!=null) { //在return_code 和result_code都为SUCCESS的时候有返回 prepay_id = getMap.get("prepay_id"); log.info("订单:"+outTradeNo+",请求微信支付!"); } else { log.info("获取预支付ID失败,返回参数为>>>>>>"+getMap); } } catch (UnsupportedEncodingException e) { log.info(Tools.getExceptionMessage(e)); } Integer timeStramp = (int)(System.currentTimeMillis()/1000); JsApiToJsData jsApiToJsData = new JsApiToJsData(timeStramp,nonceStr,prepay_id,Constants.appId,Constants.apikey); payInfo.put("appId", jsApiToJsData.getAppid()); payInfo.put("timeStamp", String.valueOf(jsApiToJsData.getTimeStamp())); payInfo.put("nonceStr", jsApiToJsData.getNonce_str()); payInfo.put("package", "prepay_id="+jsApiToJsData.getPrepay_id()); payInfo.put("signType", "MD5"); payInfo.put("paySign", jsApiToJsData.getSign()); log.info("支付信息:"+payInfo); } else { log.info("支付失败,支付参数初始化失败>>>{"+sb+"}"); } return new SimpleJsonView(payInfo); } //银联支付 @Path(value="/buyYLPdts.do") public SimpleJsonView buyYLPdts(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); if(GzhUtil.isEmpty(appuid)) { Map result = new HashMap(); result.put("status", "403"); result.put("msg", "请登录"); return new SimpleJsonView(result); } String amount=para.get("amount"); //订单价格 double af=Double.parseDouble(amount); //低于1元不能充值 if(af<1) { Map result = new HashMap(); result.put("status", "500"); result.put("msg", "出错了"); return new SimpleJsonView(result); } String outTradeNo = ""; //订单ID String notify=YL.notify; int type = Integer.parseInt(para.get("type")); log.info("type="+type); if(type == 0) { Map pdtInfo = (Map) session.getAttribute("pdtInfo"); //amount = String.valueOf(pdtInfo.get("total")); //amount=para.get("amount"); para.put("pdtid", String.valueOf(pdtInfo.get("pdtid"))); para.put("appuid", appuid); para.put("num", String.valueOf(pdtInfo.get("num"))); para.put("price", amount); /***********************下单,获取订单编号-BEGIN***********************/ log.info("设备购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/device/buyPdts.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买设备获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson.getString("msg")); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } else if(type == 1) { Map pkgInfo = (Map) session.getAttribute("pkgInfo"); //amount = String.valueOf(pkgInfo.get("price")); //amount=para.get("amount"); para.put("pkid", String.valueOf(pkgInfo.get("pkgid"))); para.put("appuid", appuid); para.put("price", amount); para.put("ts", String.valueOf(pkgInfo.get("ts"))); /***********************下单,获取订单编号-BEGIN***********************/ log.info("套餐购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyPkg.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买套餐获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } else if(type == 2) { amount=para.get("amount"); para.put("appuid", appuid); para.put("price", amount); //TODO pkgid pkgprice para.put("openid", openid); String pkgprice=para.get("pkgprice"); para.put("pkgprice", pkgprice); /***********************下单,获取订单编号-BEGIN***********************/ log.info("钱包充值,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyMember.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("钱包充值获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); notify = YL.membernotify; } else if(type == 3) { amount=para.get("amount"); para.put("appuid", appuid); para.put("price", amount); /***********************下单,获取订单编号-BEGIN***********************/ log.info("加速包购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buySpeedPack.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("购买加速包获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); notify = YL.ylmemberspeedpacknotify; } SortedMap map = new TreeMap(); String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); //初始化传入参数 BigDecimal dised = new BigDecimal(amount); // BigDecimal dised = new BigDecimal(0.01); //测试价格0.01,待修改为totalAmount zby BigDecimal diz = new BigDecimal(100); if("151992".equals(appuid) || "174056".equals(appuid)) { dised = new BigDecimal(0.01); } // String key = "432fc0caf293dde237e574fbf057f056"; // String mch_id="QRA6072481C0003"; // String notify="http://shuhai.sharemao.cn/mifi/ylnotify.do"; /*String key = YL.key; String mch_id=YL.mch_id; // String notify=YL.notify; map.put("body", "收款码收款_商品"); map.put("charset", "UTF-8"); map.put("is_raw", "1"); map.put("mch_create_ip", spBillCreateIP); map.put("mch_id", mch_id); map.put("nonce_str", "str"+System.currentTimeMillis()); map.put("notify_url", notify); map.put("out_trade_no", outTradeNo); map.put("service", "pay.weixin.jspay"); map.put("sign_type", "MD5"); map.put("sub_openid", openid); map.put("total_fee", String.valueOf(dised.multiply(diz).intValue())); map.put("version", "2.0"); map.put("sign", sign(map,key)); String reqUrl = "https://qra.95516.com/pay/gateway"; log.info("reqUrl:" + reqUrl); log.info("reqParams:" + XmlUtils.parseXML(map)); CloseableHttpResponse response = null; CloseableHttpClient client = null; String res = null; Map resultMap = null; Map payInfo = new HashMap(); try { HttpPost httpPost = new HttpPost(reqUrl); StringEntity entityParams = new StringEntity(XmlUtils.parseXML(map), "utf-8"); httpPost.setEntity(entityParams); httpPost.setHeader("Content-Type", "text/xml;utf-8"); client = HttpClients.createDefault(); response = client.execute(httpPost); if (response != null && response.getEntity() != null) { resultMap = XmlUtils.toMap(EntityUtils.toByteArray(response.getEntity()), "utf-8"); res = XmlUtils.toXml(resultMap); log.info("请求结果:" + res); if (!SignUtils.checkParam(resultMap, key)) { res = "验证签名不通过"; } else { if ("0".equals(resultMap.get("status")) && "0".equals(resultMap.get("result_code"))) { String pay_info = resultMap.get("pay_info"); log.info("pay_info : " + pay_info); res = "ok"; JSONObject resJson = JSONObject.parseObject(pay_info); payInfo.put("appId", resJson.getString("appId")); payInfo.put("timeStamp", resJson.getString("timeStamp")); payInfo.put("nonceStr", resJson.getString("nonceStr")); payInfo.put("package", resJson.getString("package")); payInfo.put("signType", "MD5"); payInfo.put("paySign", resJson.getString("paySign")); } } } else { res = "操作失败"; } } catch (Exception e) { log.error("操作失败,原因:",e); res = "系统异常"; } finally { if (response != null) { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (client != null) { try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Map result = new HashMap(); if("ok".equals(res)){ // result = payInfo; // return new SimpleJsonView(resultMap); result.put("status", "200"); result.put("msg", res); result.put("pay_info", resultMap.get("pay_info")); }else{ result.put("status", "500"); result.put("msg", res); }*/ Map result = new HashMap(); String nonceStr = PaySign.getNonceStr(); JsApiReqData jsApiReqData = new JsApiReqData(notify,"收款码收款_商品",nonceStr,outTradeNo,dised.multiply(diz).intValue(),spBillCreateIP, openid,Constants.appId,Constants.mch_id,Constants.apikey); String info = Message2XmlUtil.JsApiReqData2xml(jsApiReqData).replaceAll("mch__id", "mch_id").replaceAll("nonce__str", "nonce_str").replaceAll("notify__url", "notify_url").replaceAll("out__trade__no", "out_trade_no").replaceAll("spbill__create__ip", "spbill_create_ip").replaceAll("total__fee>", "total_fee>").replaceAll("trade__type", "trade_type"); log.info("info========="+info); String prepay_id="err"; StringBuffer sb = JsApiReqUtils.httpsRequest(Constants.unifiedorderurl, "POST", info); Map payInfo = new HashMap(); if (!"".equals(sb.toString())) { try { //得到预支付ID,即prepay_id Map getMap = Message2XmlUtil.parseXml(new String(sb.toString().getBytes(), "utf-8")); if(getMap.get("prepay_id")!=null) { //在return_code 和result_code都为SUCCESS的时候有返回 prepay_id = getMap.get("prepay_id"); log.info("订单:"+outTradeNo+",请求微信支付!"); } else { log.info("获取预支付ID失败,返回参数为>>>>>>"+getMap); } } catch (UnsupportedEncodingException e) { log.info(Tools.getExceptionMessage(e)); } Integer timeStramp = (int)(System.currentTimeMillis()/1000); JsApiToJsData jsApiToJsData = new JsApiToJsData(timeStramp,nonceStr,prepay_id,Constants.appId,Constants.apikey); payInfo.put("appId", jsApiToJsData.getAppid()); payInfo.put("timeStamp", String.valueOf(jsApiToJsData.getTimeStamp())); payInfo.put("nonceStr", jsApiToJsData.getNonce_str()); payInfo.put("package", "prepay_id="+jsApiToJsData.getPrepay_id()); payInfo.put("signType", "MD5"); payInfo.put("paySign", jsApiToJsData.getSign()); log.info("支付信息:"+payInfo); result.put("status", "200"); result.put("msg", "ok"); result.put("pay_info", JSON.toJSONString(payInfo)); } else { log.info("支付失败,支付参数初始化失败>>>{"+sb+"}"); result.put("status", "500"); result.put("msg", "支付失败,支付参数初始化失败>>>{"+sb+"}"); } return new SimpleJsonView(result); } // @Path(value="/buyYiZFPdts.do") // public SimpleJsonView buyYiZFPdts(Context context) // { // Map para = context.getParas(); // // HttpSession session = context.getRequest().getSession(); // String appuid = String.valueOf(session.getAttribute("appuid")); // String openid = (String) session.getAttribute("openid"); // // String amount=para.get("amount"); //订单价格 // // String outTradeNo = ""; //订单ID // // // Map pkgInfo = (Map) session.getAttribute("pkgInfo"); // //amount = String.valueOf(pkgInfo.get("price")); // //amount=para.get("amount"); // para.put("pkid", String.valueOf(pkgInfo.get("pkgid"))); // para.put("appuid", appuid); // para.put("price", amount); // para.put("ts", String.valueOf(pkgInfo.get("ts"))); // // /***********************下单,获取订单编号-BEGIN***********************/ // log.info("套餐购买,下单>>>"+para); // // GzhUtil.toM3PaySign(para); // String resulta = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyPkg.do", para); // JSONObject resJsona = JSONObject.parseObject(resulta); // log.info("购买套餐获取订单编号返回:"+resulta); // if(resJsona.getIntValue("code") != 0){ // return new SimpleJsonView(resJsona); // } // JSONObject dataJson = resJsona.getJSONObject("data"); // outTradeNo = dataJson.getString("orderid"); // // // String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); // Map payInfo = new HashMap(); // String res = null; // try // { // //接口地址 // String api = "/yeepay/pre_pay"; // //具体接口的业务参数 // Map businessData = new HashMap(); // businessData.put("appId", Constants.appId); // businessData.put("scene", "ONLINE"); // businessData.put("payWay", "WECHAT_OFFIACCOUNT"); // businessData.put("orderAmount", amount); // businessData.put("channel", "WECHAT"); // businessData.put("userIp", spBillCreateIP); // businessData.put("userId", openid); // businessData.put("goodsName", "1"+outTradeNo); // // String jsonData = JSON.toJSONString(businessData); // log.info(jsonData); // PrivateKey merchantPrivateKey = YiZF.getPrivateKey(YiZF.MERCHANT_PRIVATE_KEY); // PublicKey publicKey = YiZF.getPublicKey(YiZF.PUBLIC_KEY); // OpenParam requestParam = YiZF.buildOpenParam(jsonData, YiZF.BUSINESS_SUBJECT_ID, merchantPrivateKey, publicKey); // // //接口返回基本结构:code、message、data(OpenParam) // String responseJson = YiZF.doPost4String(YiZF.URL_HOST + api, JSON.toJSONString(requestParam),"Bearer "+YiZF.getToken5()); // log.info("url="+YiZF.URL_HOST + api+",Authorization="+"Bearer"+YiZF.getToken5()+",json="+JSON.toJSONString(requestParam)); // log.info("responseJson="+responseJson); // com.alibaba.fastjson2.JSONObject result = JSON.parseObject(responseJson); // OpenParam responseParam = JSON.parseObject(result.getString("data"), OpenParam.class); // String responseBusinessData = YiZF.verifyAndDecrypt(responseParam, YiZF.BUSINESS_SUBJECT_ID, merchantPrivateKey, publicKey); // log.info("responseBusinessData="+responseBusinessData); // // // JSONObject resJson = JSONObject.parseObject(responseBusinessData); // if(resJson.getString("code").equals("0000")) // { // res = "ok"; // resJson = JSONObject.parseObject(resJson.getString("data")); // resJson = JSONObject.parseObject(resJson.getString("prePayTn")); // payInfo.put("appId", resJson.getString("appId")); // payInfo.put("timeStamp", resJson.getString("timeStamp")); // payInfo.put("nonceStr", resJson.getString("nonceStr")); // payInfo.put("package", resJson.getString("package")); // payInfo.put("signType", resJson.getString("signType")); // payInfo.put("paySign", resJson.getString("paySign")); // } // } // catch(Exception e) // { // Tools.getExceptionMessage(e); // } // // Map result = new HashMap(); // if("ok".equals(res)){ // result = payInfo; // }else{ // result.put("status", "500"); // result.put("msg", res); // } // return new SimpleJsonView(result); // } @Path(value="/buyMember.do") public SimpleJsonView buyMember(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); String amount = ""; //订单价格 String outTradeNo = ""; //订单ID String memid=""; { amount=para.get("amount"); memid=para.get("memid"); para.put("appuid", appuid); para.put("price", amount); para.put("memid", memid); /***********************下单,获取订单编号-BEGIN***********************/ log.info("套餐购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyMember.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("购买套餐获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } /***********************下单,获取订单编号-END***********************/ //执行统一下单接口,初始化各参数 String nonceStr = PaySign.getNonceStr(); String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); //初始化传入参数 BigDecimal dised = new BigDecimal(amount); // BigDecimal dised = new BigDecimal(0.01); //测试价格0.01,待修改为totalAmount zby BigDecimal diz = new BigDecimal(100); String body = "会员充值"; String notify="/mifi/ylmembernotify.do"; JsApiReqData jsApiReqData = new JsApiReqData(notify,body,nonceStr,outTradeNo,dised.multiply(diz).intValue(),spBillCreateIP, openid,Constants.appId,Constants.mch_id,Constants.apikey); String info = Message2XmlUtil.JsApiReqData2xml(jsApiReqData).replaceAll("mch__id", "mch_id").replaceAll("nonce__str", "nonce_str").replaceAll("notify__url", "notify_url").replaceAll("out__trade__no", "out_trade_no").replaceAll("spbill__create__ip", "spbill_create_ip").replaceAll("total__fee>", "total_fee>").replaceAll("trade__type", "trade_type"); log.info("info========="+info); String prepay_id="err"; StringBuffer sb = JsApiReqUtils.httpsRequest(Constants.unifiedorderurl, "POST", info); Map payInfo = new HashMap(); if (!"".equals(sb.toString())) { try { //得到预支付ID,即prepay_id Map getMap = Message2XmlUtil.parseXml(new String(sb.toString().getBytes(), "utf-8")); if(getMap.get("prepay_id")!=null) { //在return_code 和result_code都为SUCCESS的时候有返回 prepay_id = getMap.get("prepay_id"); log.info("订单:"+outTradeNo+",请求微信支付!"); } else { log.info("获取预支付ID失败,返回参数为>>>>>>"+getMap); } } catch (UnsupportedEncodingException e) { log.info(Tools.getExceptionMessage(e)); } Integer timeStramp = (int)(System.currentTimeMillis()/1000); JsApiToJsData jsApiToJsData = new JsApiToJsData(timeStramp,nonceStr,prepay_id,Constants.appId,Constants.apikey); payInfo.put("appId", jsApiToJsData.getAppid()); payInfo.put("timeStamp", String.valueOf(jsApiToJsData.getTimeStamp())); payInfo.put("nonceStr", jsApiToJsData.getNonce_str()); payInfo.put("package", "prepay_id="+jsApiToJsData.getPrepay_id()); payInfo.put("signType", "MD5"); payInfo.put("paySign", jsApiToJsData.getSign()); log.info("支付信息:"+payInfo); } else { log.info("支付失败,支付参数初始化失败>>>{"+sb+"}"); } return new SimpleJsonView(payInfo); } @Path(value="/buyYLMember.do") public SimpleJsonView buyYLMember(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); String amount = ""; //订单价格 String outTradeNo = ""; //订单ID String memid=""; { amount=para.get("amount"); memid=para.get("memid"); para.put("appuid", appuid); para.put("price", amount); para.put("memid", memid); /***********************下单,获取订单编号-BEGIN***********************/ log.info("套餐购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyMember.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("购买套餐获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } SortedMap map = new TreeMap(); String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); //初始化传入参数 BigDecimal dised = new BigDecimal(amount); // BigDecimal dised = new BigDecimal(0.01); //测试价格0.01,待修改为totalAmount zby BigDecimal diz = new BigDecimal(100); // String key = "432fc0caf293dde237e574fbf057f056"; // String mch_id="QRA6072481C0003"; // String notify="http://xcwx.sharemao.cn/mifi/ylmembernotify.do"; String key = YL.key; String mch_id=YL.mch_id; String notify=YL.membernotify; map.put("body", "收款码收款_商品"); map.put("charset", "UTF-8"); map.put("is_raw", "1"); map.put("mch_create_ip", spBillCreateIP); map.put("mch_id", mch_id); map.put("nonce_str", "str"+System.currentTimeMillis()); map.put("notify_url", notify); map.put("out_trade_no", outTradeNo); map.put("service", "pay.weixin.jspay"); map.put("sign_type", "MD5"); map.put("sub_openid", openid); map.put("total_fee", String.valueOf(dised.multiply(diz).intValue())); map.put("version", "2.0"); map.put("sign", sign(map,key)); String reqUrl = "https://qra.95516.com/pay/gateway"; log.info("reqUrl:" + reqUrl); log.info("reqParams:" + XmlUtils.parseXML(map)); CloseableHttpResponse response = null; CloseableHttpClient client = null; String res = null; Map resultMap = null; Map payInfo = new HashMap(); try { HttpPost httpPost = new HttpPost(reqUrl); StringEntity entityParams = new StringEntity(XmlUtils.parseXML(map), "utf-8"); httpPost.setEntity(entityParams); httpPost.setHeader("Content-Type", "text/xml;utf-8"); client = HttpClients.createDefault(); response = client.execute(httpPost); if (response != null && response.getEntity() != null) { resultMap = XmlUtils.toMap(EntityUtils.toByteArray(response.getEntity()), "utf-8"); res = XmlUtils.toXml(resultMap); log.info("请求结果:" + res); if (!SignUtils.checkParam(resultMap, key)) { res = "验证签名不通过"; } else { if ("0".equals(resultMap.get("status")) && "0".equals(resultMap.get("result_code"))) { String pay_info = resultMap.get("pay_info"); log.info("pay_info : " + pay_info); res = "ok"; JSONObject resJson = JSONObject.parseObject(pay_info); payInfo.put("appId", resJson.getString("appId")); payInfo.put("timeStamp", resJson.getString("timeStamp")); payInfo.put("nonceStr", resJson.getString("nonceStr")); payInfo.put("package", resJson.getString("package")); payInfo.put("signType", "MD5"); payInfo.put("paySign", resJson.getString("paySign")); } } } else { res = "操作失败"; } } catch (Exception e) { log.error("操作失败,原因:",e); res = "系统异常"; } finally { if (response != null) { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (client != null) { try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Map result = new HashMap(); if("ok".equals(res)){ result = payInfo; }else{ result.put("status", "500"); result.put("msg", res); } return new SimpleJsonView(result); } // @Path(value="/buyYiZFMember.do") // public SimpleJsonView buyYiZFMember(Context context) // { // Map para = context.getParas(); // // HttpSession session = context.getRequest().getSession(); // String appuid = String.valueOf(session.getAttribute("appuid")); // String openid = (String) session.getAttribute("openid"); // // String amount = ""; //订单价格 // String outTradeNo = ""; //订单ID // String memid=""; // // amount=para.get("amount"); // memid=para.get("memid"); // para.put("appuid", appuid); // para.put("price", amount); // para.put("memid", memid); // // /***********************下单,获取订单编号-BEGIN***********************/ // log.info("套餐购买,下单>>>"+para); // // GzhUtil.toM3PaySign(para); // String resulta = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyMember.do", para); // log.info(resulta); // JSONObject resJsona = JSONObject.parseObject(resulta); // log.info("购买套餐获取订单编号返回:"+resulta); // if(resJsona.getIntValue("code") != 0){ // return new SimpleJsonView(resJsona); // } // JSONObject dataJson = resJsona.getJSONObject("data"); // outTradeNo = dataJson.getString("orderid"); // // String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); // Map payInfo = new HashMap(); // String res = null; // try // { // //接口地址 // String api = "/yeepay/pre_pay"; // //具体接口的业务参数 // Map businessData = new HashMap(); // businessData.put("appId", Constants.appId); // businessData.put("scene", "ONLINE"); // businessData.put("payWay", "WECHAT_OFFIACCOUNT"); // businessData.put("orderAmount", amount); // businessData.put("channel", "WECHAT"); // businessData.put("userIp", spBillCreateIP); // businessData.put("userId", openid); // businessData.put("goodsName", "9"+outTradeNo); // // String jsonData = JSON.toJSONString(businessData); // log.info(jsonData); // PrivateKey merchantPrivateKey = YiZF.getPrivateKey(YiZF.MERCHANT_PRIVATE_KEY); // PublicKey publicKey = YiZF.getPublicKey(YiZF.PUBLIC_KEY); // OpenParam requestParam = YiZF.buildOpenParam(jsonData, YiZF.BUSINESS_SUBJECT_ID, merchantPrivateKey, publicKey); // // //接口返回基本结构:code、message、data(OpenParam) // String responseJson = YiZF.doPost4String(YiZF.URL_HOST + api, JSON.toJSONString(requestParam),"Bearer "+YiZF.getToken5()); // log.info("url="+YiZF.URL_HOST + api+",Authorization="+"Bearer"+YiZF.getToken5()+",json="+JSON.toJSONString(requestParam)); // log.info("responseJson="+responseJson); // com.alibaba.fastjson2.JSONObject result = JSON.parseObject(responseJson); // OpenParam responseParam = JSON.parseObject(result.getString("data"), OpenParam.class); // String responseBusinessData = YiZF.verifyAndDecrypt(responseParam, YiZF.BUSINESS_SUBJECT_ID, merchantPrivateKey, publicKey); // log.info("responseBusinessData="+responseBusinessData); // // // JSONObject resJson = JSONObject.parseObject(responseBusinessData); // if(resJson.getString("code").equals("0000")) // { // res = "ok"; // resJson = JSONObject.parseObject(resJson.getString("data")); // resJson = JSONObject.parseObject(resJson.getString("prePayTn")); // payInfo.put("appId", resJson.getString("appId")); // payInfo.put("timeStamp", resJson.getString("timeStamp")); // payInfo.put("nonceStr", resJson.getString("nonceStr")); // payInfo.put("package", resJson.getString("package")); // payInfo.put("signType", resJson.getString("signType")); // payInfo.put("paySign", resJson.getString("paySign")); // } // } // catch(Exception e) // { // Tools.getExceptionMessage(e); // } // // Map result = new HashMap(); // if("ok".equals(res)){ // result = payInfo; // }else{ // result.put("status", "500"); // result.put("msg", res); // } // return new SimpleJsonView(result); // } @Path(value="/buyBalance.do") public SimpleJsonView buyBalance(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); Map pkgInfo = (Map) session.getAttribute("pkgInfo"); para.put("packid", String.valueOf(pkgInfo.get("pkgid"))); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/payment/memPackRecharge.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { return new SimpleJsonView("OK"); } return new SimpleJsonView(resJson.getString("msg")); } @Path(value="/memPackRechargeToSpeedPack.do") public SimpleJsonView memPackRechargeToSpeedPack(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/payment/memPackRechargeToSpeedPack.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { return new SimpleJsonView("OK"); } return new SimpleJsonView(resJson.getString("msg")); } public static String sign(SortedMap parameters,String key){ StringBuffer sb = new StringBuffer(); Set es = parameters.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); String k = (String)entry.getKey(); String v = (String)entry.getValue(); if (null != v && !"".equals(v)) { sb.append(k + "=" + parameters.get(k) + "&"); } } sb.append("key="+key); System.out.println(sb.toString()); try { byte[] bytes = sb.toString().getBytes("utf-8"); return DigestUtils.md5Hex(bytes).toUpperCase(); } catch (UnsupportedEncodingException e) { return ""; } } @Path(value="/notify.do") public void notify(Context context) { Map paramMap = Message2XmlUtil.xml2map(context.getRequest()); log.info("微信公众号回调:{"+paramMap+"}"); String result_code = paramMap.get("result_code").toString(); // 进行业务处理 if("SUCCESS".equals(result_code.toUpperCase())) { Map para = new HashMap(); String orderid = paramMap.get("out_trade_no").toString(); log.info("处理订单:orderno:"+orderid); para.put("orderid", orderid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/gzhRecharge.do", para); log.info("下单购买后回调结果:"+result); } else { log.info("订单支付通知:签名错误"); return; } } @Path(value="/ylnotify.do") public void ylnotify(Context context) { Map paramMap = Message2XmlUtil.xml2map(context.getRequest()); log.info("微信公众号回调:{"+paramMap+"}"); String result_code = paramMap.get("result_code").toString(); // 进行业务处理 if("0".equals(result_code) || "SUCCESS".equalsIgnoreCase(result_code)) { Map para = new HashMap(); String orderid = paramMap.get("out_trade_no").toString(); String transactionId = paramMap.get("transaction_id").toString(); log.info("处理订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", transactionId); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/gzhRecharge.do", para); log.info("下单购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } @Path(value="/ylmembernotify.do") public void ylmembernotify(Context context) { Map paramMap = Message2XmlUtil.xml2map(context.getRequest()); log.info("微信公众号回调:{"+paramMap+"}"); String result_code = paramMap.get("result_code").toString(); // 进行业务处理 if("0".equals(result_code) || "SUCCESS".equals(result_code.toUpperCase())) { Map para = new HashMap(); String orderid = paramMap.get("out_trade_no").toString(); String transactionId = paramMap.get("transaction_id").toString(); log.info("处理订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", transactionId); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/memRecharge.do", para); log.info("下单购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } @Path(value="/ylmemberspeedpacknotify.do") public void ylmemberspeedpacknotify(Context context) { Map paramMap = Message2XmlUtil.xml2map(context.getRequest()); log.info("微信公众号回调:{"+paramMap+"}"); String result_code = paramMap.get("result_code").toString(); // 进行业务处理 if("0".equals(result_code) || "SUCCESS".equals(result_code.toUpperCase())) { Map para = new HashMap(); String orderid = paramMap.get("out_trade_no").toString(); String transactionId = paramMap.get("transaction_id").toString(); log.info("处理加速包购买订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", transactionId); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/speedPackRecharge.do", para); log.info("加速包购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } @Path("/getMember2.do") public TextView getMember(Context context) { Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/packkey/getMember.do", para); if(!Tools.isEmpty(result)) { JSONObject resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); context.getRequest().setAttribute("cons", dataJson); } String devid =GzhUtil.getDevID(context.getRequest().getSession(), para); context.getRequest().setAttribute("devid", devid); HttpSession session = context.getRequest().getSession(); String phonenum = (String) session.getAttribute("phonenum"); context.getRequest().setAttribute("phonenum", phonenum); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); GzhUtil.toM3Sign(para); result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String balance=dataJson.getString("balance"); String jine=dataJson.getString("jine"); String zsong=dataJson.getString("zsong"); context.getRequest().setAttribute("balance", balance); context.getRequest().setAttribute("jine", jine); context.getRequest().setAttribute("zsong", zsong); } else { context.getRequest().setAttribute("balance", "0.00"); context.getRequest().setAttribute("jine", "0.00"); context.getRequest().setAttribute("zsong", "0.00"); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getSPkgs.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); log.info(dataJson); context.getRequest().setAttribute("spkgs", dataJson); } return new TextView("/membersyb.jsp"); // return new TextView("/member.jsp"); } @Path("/benefit.do") public TextView benefit(Context context) { Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/packkey/getMember.do", para); if(!Tools.isEmpty(result)) { JSONObject resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); context.getRequest().setAttribute("cons", dataJson); } String devid =GzhUtil.getDevID(context.getRequest().getSession(), para); context.getRequest().setAttribute("devid", devid); HttpSession session = context.getRequest().getSession(); String phonenum = (String) session.getAttribute("phonenum"); context.getRequest().setAttribute("phonenum", phonenum); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); GzhUtil.toM3Sign(para); result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); context.getRequest().setAttribute("balance", dataJson.getString("balance")); context.getRequest().setAttribute("jine", dataJson.getString("jine")); context.getRequest().setAttribute("zsong", dataJson.getString("zsong")); } else { context.getRequest().setAttribute("balance", "0.00"); context.getRequest().setAttribute("jine", "0.00"); context.getRequest().setAttribute("zsong", "0.00"); } result = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getSPkgs.do", para); if(!Tools.isEmpty(result)) { resJson = JSONObject.parseObject(result); JSONArray dataJson = resJson.getJSONArray("data"); log.info(dataJson); context.getRequest().setAttribute("spkgs", dataJson); } return new TextView("/benefit.jsp"); } @Path("/benefitSubmit.do") public TextView benefitSubmit(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String openid = (String) session.getAttribute("openid"); para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getBalance.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); context.getRequest().setAttribute("balance", dataJson.getString("balance")); } else { context.getRequest().setAttribute("balance", "0.00"); } para.forEach((k,v)->context.getRequest().setAttribute(k, v)); return new TextView("/benefitSubmitSyb.jsp"); // return new TextView("/benefitSubmit.jsp"); } @Path(value="/submitorder.do") public SimpleJsonView submitorder(Context context) throws Exception { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); String openid = (String) session.getAttribute("openid"); if(GzhUtil.isEmpty(appuid)) { Map result = new HashMap(); result.put("status", "403"); result.put("msg", "请登录"); return new SimpleJsonView(result); } String amount=para.get("amount"); //订单价格 double af=Double.parseDouble(amount); //低于1元不能充值 if(af<1) { Map result = new HashMap(); result.put("status", "500"); result.put("msg", "出错了"); return new SimpleJsonView(result); } String outTradeNo = ""; //订单ID String notify=SybConstants.NOTFIY_PKG_URL; int type = Integer.parseInt(para.get("type")); log.info("type="+type); if(type == 0) { Map pdtInfo = (Map) session.getAttribute("pdtInfo"); //amount = String.valueOf(pdtInfo.get("total")); //amount=para.get("amount"); para.put("pdtid", String.valueOf(pdtInfo.get("pdtid"))); para.put("appuid", appuid); para.put("num", String.valueOf(pdtInfo.get("num"))); para.put("price", amount); /***********************下单,获取订单编号-BEGIN***********************/ log.info("设备购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/device/buyPdts.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买设备获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson.getString("msg")); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } else if(type == 1) { Map pkgInfo = (Map) session.getAttribute("pkgInfo"); //amount = String.valueOf(pkgInfo.get("price")); //amount=para.get("amount"); para.put("pkid", String.valueOf(pkgInfo.get("pkgid"))); para.put("appuid", appuid); para.put("price", amount); para.put("ts", String.valueOf(pkgInfo.get("ts"))); /***********************下单,获取订单编号-BEGIN***********************/ log.info("套餐购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyPkg.do", para); JSONObject resJson = JSONObject.parseObject(result); log.info("购买套餐获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); } else if(type == 2) { amount=para.get("amount"); para.put("appuid", appuid); para.put("price", amount); //TODO pkgid pkgprice para.put("openid", openid); String pkgprice=para.get("pkgprice"); para.put("pkgprice", pkgprice); /***********************下单,获取订单编号-BEGIN***********************/ log.info("钱包充值,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buyMember.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("钱包充值获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); notify = SybConstants.NOTFIY_MEMBER_URL; } else if(type == 3) { amount=para.get("amount"); para.put("appuid", appuid); para.put("price", amount); /***********************下单,获取订单编号-BEGIN***********************/ log.info("加速包购买,下单>>>"+para); GzhUtil.toM3PaySign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/package/buySpeedPack.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); log.info("购买加速包获取订单编号返回:"+result); if(resJson.getIntValue("code") != 0){ return new SimpleJsonView(resJson); } JSONObject dataJson = resJson.getJSONObject("data"); outTradeNo = dataJson.getString("orderid"); notify = SybConstants.NOTFIY_SPEEDPACK_URL; } Map result = new HashMap(); result.put("status", "200"); result.put("amount", "amount"); result.put("notify", notify); result.put("outTradeNo", outTradeNo); return new SimpleJsonView(result); } /** * test 用于养号,方便充值 */ @Path(value="/testsyb.do") public TextView testsyb(Context context) throws Exception { Map para = context.getParas(); String appid = SybConstants.SYB_APPID; String cusid = SybConstants.SYB_CUSID; String version = SybConstants.VERSION; String charset = "utf-8"; BigDecimal dised = new BigDecimal(String.valueOf(para.get("amt"))); BigDecimal diz = new BigDecimal(100); String trxamt=dised.multiply( diz).toString(); String reqsn = System.currentTimeMillis()+""; String returl = SybConstants.RET_URL; String notify_url = SybConstants.NOTFIY_URL; String body ="收款码收款_商品"; String remark ="收款码收款_商品"; String randomstr = String.valueOf(System.currentTimeMillis()) ; String validtime = String.valueOf( SybConstants.VALID_TIME); String signtype = SybConstants.SIGN_TYPE; String appkey = SybConstants.SYB_RSACUSPRIKEY; TreeMap params = new TreeMap(); params.put("appid", appid); params.put("cusid", cusid); params.put("version", version); params.put("charset", charset); params.put("trxamt", trxamt); params.put("reqsn", reqsn); params.put("randomstr", randomstr); params.put("body", body); params.put("validtime", validtime); params.put("remark", remark); params.put("returl", returl); params.put("notify_url", notify_url); params.put("signtype",signtype); log.info("=================通联充值:"+params); String sign = SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE); context.getRequest().setAttribute("appid", appid); context.getRequest().setAttribute("cusid", cusid); context.getRequest().setAttribute("version", version); context.getRequest().setAttribute("charset", charset); context.getRequest().setAttribute("trxamt", trxamt); context.getRequest().setAttribute("reqsn", reqsn); context.getRequest().setAttribute("randomstr", randomstr); context.getRequest().setAttribute("body", body); context.getRequest().setAttribute("validtime", validtime); context.getRequest().setAttribute("remark", remark); context.getRequest().setAttribute("returl", returl); context.getRequest().setAttribute("notify_url", notify_url); context.getRequest().setAttribute("signtype", signtype); context.getRequest().setAttribute("sign", sign); return new TextView("/sybpay.jsp"); } @Path(value="/testwx.do") public TextView testwx(Context context) throws Exception { String wxCode = context.getParas().get("code"); Map m = GzhUtil.getOpenId(wxCode, Constants.appId, Constants.appSecret); String openid = m.get("openId"); context.getRequest().getSession().setAttribute("openid", openid); context.getRequest().setAttribute("u", "/mifi/testwxpay.do"); return new TextView("/testwx.jsp"); } @Path(value = "/testwxpay.do") public SimpleJsonView testwxpay(Context context) throws Exception { Map result = new HashMap(); String nonceStr = PaySign.getNonceStr(); String trxamt = String.valueOf(context.getParas().get("amt")); BigDecimal dised = new BigDecimal(trxamt); BigDecimal diz = new BigDecimal(100); String outTradeNo = System.currentTimeMillis() + ""; String spBillCreateIP = JsApiReqUtils.getRealIp(context.getRequest()); String openid = context.getRequest().getSession().getAttribute("openid").toString(); JsApiReqData jsApiReqData = new JsApiReqData("收款码收款_商品", nonceStr, outTradeNo, dised.multiply( diz).intValue(), spBillCreateIP, openid, Constants.appId, Constants.mch_id, Constants.apikey); String info = Message2XmlUtil.JsApiReqData2xml(jsApiReqData).replaceAll("mch__id", "mch_id").replaceAll("nonce__str", "nonce_str").replaceAll("notify__url", "notify_url").replaceAll("out__trade__no", "out_trade_no").replaceAll("spbill__create__ip", "spbill_create_ip").replaceAll("total__fee>", "total_fee>").replaceAll("trade__type", "trade_type"); log.info("微信支付充值=========" + info); String prepay_id = "err"; StringBuffer sb = JsApiReqUtils.httpsRequest(Constants.unifiedorderurl, "POST", info); Map payInfo = new HashMap(); if (!"".equals(sb.toString())) { try { //得到预支付ID,即prepay_id Map getMap = Message2XmlUtil.parseXml(new String(sb.toString().getBytes(), "utf-8")); if (getMap.get("prepay_id") != null) { //在return_code 和result_code都为SUCCESS的时候有返回 prepay_id = getMap.get("prepay_id"); log.info("订单:" + outTradeNo + ",请求微信支付!"); } else { log.info("获取预支付ID失败,返回参数为>>>>>>" + getMap); } } catch (UnsupportedEncodingException e) { log.info(Tools.getExceptionMessage(e)); } Integer timeStramp = (int) (System.currentTimeMillis() / 1000); JsApiToJsData jsApiToJsData = new JsApiToJsData(timeStramp, nonceStr, prepay_id, Constants.appId, Constants.apikey); payInfo.put("appId", jsApiToJsData.getAppid()); payInfo.put("timeStamp", String.valueOf(jsApiToJsData.getTimeStamp())); payInfo.put("nonceStr", jsApiToJsData.getNonce_str()); payInfo.put("package", "prepay_id=" + jsApiToJsData.getPrepay_id()); payInfo.put("signType", "MD5"); payInfo.put("paySign", jsApiToJsData.getSign()); log.info("支付信息:" + payInfo); result.put("status", "200"); result.put("msg", "ok"); result.put("pay_info", JSON.toJSONString(payInfo)); } else { log.info("支付失败,支付参数初始化失败>>>{" + sb + "}"); result.put("status", "500"); result.put("msg", "支付失败,支付参数初始化失败>>>{" + sb + "}"); } return new SimpleJsonView(result); } /** * 通联收款 */ @Path(value="/tosybpay.do") public TextView tosybpay(Context context) throws Exception { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); String amount=para.get("amount"); //订单价格 String outTradeNo=para.get("outTradeNo"); //订单价格 String notify=para.get("notify"); //订单价格 //初始化传入参数 BigDecimal dised = new BigDecimal(amount); // BigDecimal dised = new BigDecimal(0.01); //测试价格0.01,待修改为totalAmount zby BigDecimal diz = new BigDecimal(100); if("151992".equals(appuid)) { dised = new BigDecimal(0.01); } // String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/getPayType.do", para); // log.info(result); // JSONObject resJson = JSONObject.parseObject(result); // log.info("钱包充值获取订单编号返回:"+result); String appid = SybConstants.SYB_APPID; String cusid = SybConstants.SYB_CUSID; String version = SybConstants.VERSION; String charset = "utf-8"; String trxamt=String.valueOf(dised.multiply(diz).intValue()); String reqsn = outTradeNo; String returl = SybConstants.RET_URL; String notify_url = notify; String body ="收款码收款_商品"; String remark ="收款码收款_商品"; String randomstr = String.valueOf(System.currentTimeMillis()) ; String validtime = String.valueOf( SybConstants.VALID_TIME); String signtype = SybConstants.SIGN_TYPE; String appkey = SybConstants.SYB_RSACUSPRIKEY; // if(resJson.getIntValue("code") == 0){ // JSONObject dataJson = resJson.getJSONObject("data"); // appid = dataJson.getString("appid"); // cusid = dataJson.getString("mchid"); // appkey = dataJson.getString("prikey"); // } TreeMap params = new TreeMap(); params.put("appid", appid); params.put("cusid", cusid); params.put("version", version); params.put("charset", charset); params.put("trxamt", trxamt); params.put("reqsn", reqsn); params.put("randomstr", randomstr); params.put("body", body); params.put("validtime", validtime); params.put("remark", remark); params.put("returl", returl); params.put("notify_url", notify_url); params.put("signtype",signtype); log.info("=================签名参数:"+params); String sign = SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE); context.getRequest().setAttribute("appid", appid); context.getRequest().setAttribute("cusid", cusid); context.getRequest().setAttribute("version", version); context.getRequest().setAttribute("charset", charset); context.getRequest().setAttribute("trxamt", trxamt); context.getRequest().setAttribute("reqsn", reqsn); context.getRequest().setAttribute("randomstr", randomstr); context.getRequest().setAttribute("body", body); context.getRequest().setAttribute("validtime", validtime); context.getRequest().setAttribute("remark", remark); context.getRequest().setAttribute("returl", returl); context.getRequest().setAttribute("notify_url", notify_url); context.getRequest().setAttribute("signtype", signtype); context.getRequest().setAttribute("sign", sign); return new TextView("/sybpay.jsp"); } @Path(value="/sybpkgnotify.do") public void sybpkgnotify(Context context) throws Exception { log.info("充值套餐支付接收到通知"); Map paramMap = context.getRequest().getParameterMap(); paramMap.forEach((key, value) -> log.info(key + ":" + value[0])); // 进行业务处理 if(paramMap.get("outtrxid").length>0) { Map para = new HashMap(); String orderid = paramMap.get("outtrxid")[0]; // String transactionId = paramMap.get("transaction_id").toString(); log.info("处理订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", ""); // para.put("transaction_id", transactionId); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/gzhRecharge.do", para); log.info("下单购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } @Path(value="/sybmembernotify.do") public void sybmembernotify(Context context) { log.info("充值钱包支付接收到通知"); Map paramMap = context.getRequest().getParameterMap(); paramMap.forEach((key, value) -> log.info(key + ":" + value[0])); // 进行业务处理 if(paramMap.get("outtrxid").length>0) { Map para = new HashMap(); String orderid = paramMap.get("outtrxid")[0]; log.info("处理订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", ""); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/memRecharge.do", para); log.info("下单购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } @Path(value="/sybspeedpacknotify.do") public void sybspeedpacknotify(Context context) { log.info("充值钱包支付接收到通知"); Map paramMap = context.getRequest().getParameterMap(); paramMap.forEach((key, value) -> log.info(key + ":" + value[0])); // 进行业务处理 if(paramMap.get("outtrxid").length>0) { Map para = new HashMap(); String orderid = paramMap.get("outtrxid")[0]; log.info("处理加速包购买订单:orderno:"+orderid); para.put("orderid", orderid); para.put("transaction_id", ""); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr+"/payment/speedPackRecharge.do", para); log.info("加速包购买后回调结果:"+result); try { context.getResponse().getWriter().write("success"); } catch (IOException e) { e.printStackTrace(); } } else { log.info("订单支付通知:签名错误"); return; } } }