微信退款

退款需要安装证书等。

界面调用取消支付ajax

$.ajax({
																type : "POST",
																url : path + "/wxpay/refund.do",
																data : {
																	orderNum : orderNum,
																	payType:payType
																},
																dataType : 'json',
																success : function(data) {
																	if (data.success) {
																		location.reload();
																	}else{
																		myAlert(data.msg);
																	}
																}
															});

 refund 方法

@Action(value = "refund")
	public String refund() throws Exception {
		try{
			BaseDao dao = this.getDao();
			Map<String, Object> returnMap = new HashMap<String, Object>();
			FwhtemplateService fwhtemplateService = new FwhtemplateServiceImpl();
			
				if(model.getPayType().equals(Constants.cashpay)){
					//现金支付
					model.setOrderStatus(Constants.ORDER_STATUS_3);
					model.setCancelTime(DateUtil.newDate());
					dao.updateSql("order.upt_order", model);
					OrderModel o = (OrderModel) dao.selectObject("order.getOrder", model);
					//发送信鸽
					fwhtemplateService.cancelOrderToWorker(o);
					
					returnMap.put("success", true);
					this.doResponseWithJson(returnMap);
					return NONE;
					
				}else{
					//微信支付
					// 先判断对应的交易流水号在dd_trade_wxpay中是否存在,传入交易流水号
					String orderNum = model.getOrderNum();
					List<Notice> noticeList = dao.selectForList("wxpay.getTradeByNum", orderNum);
					if (ObjUtil.isNullOrEmpty(noticeList)) {
						returnMap.put("success", false);
						returnMap.put("msg", "退款失败");
						this.doResponseWithJson(returnMap);
						return NONE;
					}
					
					Notice notice = noticeList.get(0);
					
					String transaction_id = notice.getTransaction_id();
					String out_trade_no = notice.getOut_trade_no();
					double total_fee_str = notice.getTotal_fee();
					
					RefundReqData xmlObj = new RefundReqData(transaction_id, out_trade_no,
							null, total_fee_str);
					// 向微信发送接口数据
					String result = WeixinUtil.weixinKeyPostXml(Configure.REFUND, xmlObj);
					// 微信返回的数据
					RefundRespData retModel = (RefundRespData) Util.getObjectFromXML(
							result, RefundRespData.class);
					
					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 (!"SUCCESS".equals(retModel.getResult_code().toUpperCase())) {
						returnMap.put("success", false);
						returnMap.put("msg", retModel.getErr_code_des());
						this.doResponseWithJson(returnMap);
						return NONE;
					}
					
					// 退款成功,需要加入对应的数据库
					// 查看对应的订单号是否已经更新成退款的状态
					TradeWxpayRefund refund = (TradeWxpayRefund) dao.getObject("wxpay.getRefundById", retModel.getTransaction_id());
					
					if (ObjUtil.isNullOrEmpty(refund)) {
						// 说明还未退款成功,则执行退款的相关库表更新
						// 插入退款表
						refund = new TradeWxpayRefund();
						refund.setId(retModel.getTransaction_id());
						refund.setCouponRefundFee(retModel.getCoupon_refund_fee());
						refund.setOutRefundNo(retModel.getOut_refund_no());
						refund.setOutTradeNo(retModel.getOut_trade_no());
						refund.setRefundChannel(retModel.getRefund_channel());
						refund.setRefundFee(retModel.getRefund_fee());
						refund.setRefundId(retModel.getRefund_id());
						refund.setCreateTime(DateUtil.newDate());
						
						// 更新交易表dd_trade_wxpay,先根据Transaction_id找到对应的记录
						Notice pay = (Notice) dao.getObject("wxpay.getTradeByTid", retModel.getTransaction_id());
						if (!ObjUtil.isNullOrEmpty(pay)) {
							pay.setTrade_state("REFUND");
						} else {
							returnMap.put("success", false);
							returnMap.put("msg", "找不到对应的交易记录,请联系商家");
							this.doResponseWithJson(returnMap);
							return NONE;
						}
						
						// 更新订单表
						List<OrderModel> orderList = dao.selectForList("wxpay.getOrderByTno", retModel.getOut_trade_no());
						OrderModel order = null;
						if (!ObjUtil.isNullOrEmpty(orderList)) {
							order = orderList.get(0);
							order.setOrderStatus(Constants.ORDER_STATUS_3);
							order.setCancelTime(DateUtil.newDate());
						} else {
							returnMap.put("success", false);
							returnMap.put("msg", "找不到对应的交易记录,请联系商家");
							this.doResponseWithJson(returnMap);
							return NONE;
						}
						// 启动事务
						DaoUtil.getDaoManager().startTransaction();
						dao.insertSql("wxpay.insertRefund", refund);
						dao.updateSql("wxpay.upt_wxpayByTid", pay);
						dao.updateSql("wxpay.upt_orderByNum", order);

						DaoUtil.getDaoManager().commitTransaction();
						DaoUtil.getDaoManager().endTransaction();
						//发送信鸽
						fwhtemplateService.cancelOrderToWorker(order);
					}
					
					returnMap.put("success", true);
					returnMap.put("msg", "订单取消成功,相应款项已退回您的账下");
					this.doResponseWithJson(returnMap);
					return NONE;
				}
		}catch(Exception e){
			e.printStackTrace();
		}
		return NONE;
	}

 

你可能感兴趣的:(微信)