微信支付wxpay

j界面调用支付ajax 通过xml格式向微信调起支付密码窗

 

		// 微信支付
		$.ajax({
			type: "POST",
			url: path + "/wxpay/getPrepayId.do",
			dataType: "json",
			data: {
				totalFee : payCost,
				out_trade_no : orderNum,
			},
			success: function(data) {
				wx.chooseWXPay({
				    appId: data.appId,
					timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
				    nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
				    package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
				    signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
				    paySign: data.paySign, // 支付签名
				    success: function (res) {
				    	if (res.err_msg == "get_brand_wcpay_request:ok") {
				        	myLoading();
				        	queryOrder(0);
				        	return;
				        }
				        if (res.errMsg == "chooseWXPay:ok") {
				        	myLoading();
				        	queryOrder(0);
				        	return;
				        };
				        
				    },
				    error: function(res) {
				    	myAlert("请求超时,请重试");
				    },
				    complete: function() {
				    	myLoadingClose();
				    }
				});
			}
		});

 

 

getPrepayId 方法:

 

@Action(value = "getPrepayId")
	public String getPrepayId() throws Exception {
		
		ProjSession ps = this.getSession();
		
		//判断
		// 商品描述,根据提交上来的ID来获取商品名称
		String body = "服务";
		
		String totalFeeStr = this.getRequest().getParameter("totalFee");
		String out_trade_no = this.getRequest().getParameter("out_trade_no");
		// 本次订单的金额,单位为分
		Double d = Double.parseDouble(totalFeeStr) * 100;
		int totalFee = Integer.parseInt(new java.text.DecimalFormat("0").format(d));
		// 向微信发送的内容
		UnifiedorderReqData xmlObj = new UnifiedorderReqData(body, out_trade_no, totalFee, this.getRequest().getRemoteAddr(), ps.getOpenid());
		// 向微信发送接口数据
		String result = WeixinUtil.weixinPostXml(Configure.UNIFIEDORDER, xmlObj);
		// 微信返回的数据
		UnifiedorderRespData retModel = (UnifiedorderRespData) Util.getObjectFromXML(result, UnifiedorderRespData.class);
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		if (ObjUtil.isNullOrEmpty(retModel)) {
			// 支付请求失败
			returnMap.put("success", false);
			returnMap.put("msg", "支付请求失败");
			this.doResponseWithJson(returnMap);
			return NONE;
		}
		
		if (!"SUCCESS".equals(retModel.getReturn_code().toUpperCase())) {
			// 支付请求失败
			returnMap.put("success", false);
			returnMap.put("msg", retModel.getReturn_msg());
			this.doResponseWithJson(returnMap);
			return NONE;
		}
		
		if (!retModel.getResult_code().toUpperCase().equals("SUCCESS")) {
			returnMap.put("success", false);
			returnMap.put("msg", retModel.getErr_code_des());
			this.doResponseWithJson(returnMap);
			return NONE;
		}
			
			//判断是否有预支付订单
			OrderModel smodel=new OrderModel();
			smodel.setOpenId(model.getOpenId());
			smodel.setOrderStatus(Constants.ORDER_STATUS_2);
			List<OrderModel> olist =this.getDao().selectForList("order.getOpenIdStatus", smodel);
			if(ObjUtil.isNullOrEmpty(olist)){
				// 插入dd_order表
				//预支付
				model.setOrderStatus(Constants.ORDER_STATUS_2);
				model.setOrderWebStatus(Constants.ORDER_STATUS_2);
				this.getDao().insertSql("order.add_order", model);
			}else{
				model.setId(olist.get(0).getId());
				model.setOrderStatus(Constants.ORDER_STATUS_2);
				model.setOrderWebStatus(Constants.ORDER_STATUS_2);
				this.getDao().updateSql("order.upt_order_openId", model);
			}
		
		returnMap.put("appId", Constants.APPID);
		returnMap.put("timeStamp", Long.toString(System.currentTimeMillis() / 1000));
		returnMap.put("nonceStr", RandomStringGenerator.getRandomStringByLength(32));
		returnMap.put("package", "prepay_id="+retModel.getPrepay_id());
		returnMap.put("signType", "MD5");
		returnMap.put("paySign", Signature.getSign(returnMap));
		
		this.doResponseWithJson(returnMap);
		
		return NONE;
	}

 如果调用支付窗口成功,输入密码成功。

先调用WxNotifyAction.java

@Namespace("/notify")
public class WxNotifyAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TradeService tradeService = new TradeServiceImpl(); 
	
	@Action(value = "notify")
	public String notification() throws Exception {
		System.out.println("!!!!!!!!!!!into notify");
		HttpServletRequest req = this.getRequest();
		HttpServletResponse resp = this.getResponse();
		req.setCharacterEncoding("utf-8");
		InputStream reqBody = req.getInputStream();
		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/xml; charset=utf-8");
        String receiveXml = Util.inputStreamToString(reqBody);
        System.out.println(receiveXml);        
        Notice retModel = (Notice) Util.getObjectFromXML(receiveXml, Notice.class);
        PrintWriter out = resp.getWriter();
        
        if (ObjUtil.isNullOrEmpty(retModel)) {
        	out.print("<xml><return_code>FAIL</return_code><return_msg>NO_HANDLER_REPORTED</return_msg></xml>");
        	return NONE;
        }
        
        if (!"SUCCESS".equals(retModel.getReturn_code().toUpperCase())) {
        	out.print("<xml><return_code>FAIL</return_code><return_msg>NO_HANDLER_REPORTED</return_msg></xml>");
        	return NONE;
        }
        
        if (!"SUCCESS".equals(retModel.getResult_code().toUpperCase())) {
        	out.print("<xml><return_code>FAIL</return_code><return_msg>NO_HANDLER_REPORTED</return_msg></xml>");
        	return NONE;
        }
        
        // 调用成功,插入数据库
        boolean flag = tradeService.wxPayTrade(retModel);
System.out.println("--------------"+flag+"---------------");
//        if (flag) {
        	out.print("<xml><return_code>SUCCESS</return_code></xml>");
//        } else {
//        	out.print("<xml><return_code>FAIL</return_code><return_msg>NO_HANDLER_REPORTED</return_msg></xml>");
//        }
		return NONE;
	}
}

 然后调用js判断是否支付成功,wxpay表是否有数据插入

function queryOrder(requestTimes) {
	$.ajax({
		type : "POST",
		url : path + "/wxpay/orderQuery.do",
		data : { orderNum : orderNum},
		dataType : "json",
		success : function(data) {
			if (!data.success) {
				if (requestTimes < 3) {						
					requestTimes = requestTimes + 1;
					setTimeout(function(){queryOrder(requestTimes);}, 1000);
				} else {
					myAlert("请求超时,请核对订单列表,确定是否成功下单");
				}
			} else {
				myLoadingClose();
				successPng();
			}
		}
	});
}

 orderQuery

@Action(value = "orderQuery")
	public String orderQuery() throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		ProjSession ps = this.getSession();
		OrderModel o = new OrderModel();
		// 页面需要把订单编号告诉我
		String orderNum = this.getRequest().getParameter("orderNum");
		o.setOrderNum(orderNum);
		o.setPayType(Constants.wxpay);
		o.setOpenId(ps.getOpenid());
		o.setOrderStatus(Constants.ORDER_STATUS_1);
		List<OrderModel> list = this.getDao().selectForList("wxpay.checkStatus", o);
		if (!ObjUtil.isNullOrEmpty(list)) {
			// 交易已经成功
			System.out.println("++++++交易已经成功");
			returnMap.put("success", true);
			this.doResponseWithJson(returnMap);
		} else {
			// 交易未完成
			// 向微信服务器查询订单是否完成
			System.out.println("++++++交易未成功");
			OrderqueryReqData xmlObj = new OrderqueryReqData(orderNum);
			String result = WeixinUtil.weixinPostXml(Configure.ORDERQUERY, xmlObj);
			OrderqueryRespData retModel = (OrderqueryRespData)Util.getObjectFromXML(result, OrderqueryRespData.class);
			if (ObjUtil.isNullOrEmpty(retModel)) {
				// 支付请求失败
				returnMap.put("success", false);
				this.doResponseWithJson(returnMap);
			}

			if (!"SUCCESS".equals(retModel.getReturn_code().toUpperCase())) {
				// 支付请求失败
				returnMap.put("success", false);
				this.doResponseWithJson(returnMap);
			}

			if (!"SUCCESS".equals(retModel.getResult_code().toUpperCase())) {
				returnMap.put("success", false);
				this.doResponseWithJson(returnMap);
			}
			Notice n = new Notice();
			BeanUtils.copyProperties(n, retModel);
			boolean flag = tradeService.wxPayTrade(n);
			returnMap.put("success", flag);
			this.doResponseWithJson(returnMap);
		}
		
		return NONE;
	}

 

 

你可能感兴趣的:(XP)