微信支付获取prepay_id以及回调地址

这段代码展示了如何在Java中处理微信支付的预处理id(prepay_id)获取和支付成功的回调处理。通过检查订单状态、创建支付记录并发送请求获取预处理id,以及接收并处理微信支付回调通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	/**
	 * 微信获取prepay_id 同时下单
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPrepayId", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> getPrepayId(HttpServletRequest request, String sn) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Order order = orderService.findBySn(sn);
		System.out.println("sn==========="+sn);
		Member member = memberService.getCurrent();
		if (order == null || !member.equals(order.getMember()) || order.getPaymentMethod() == null
				|| order.getAmountPayable().compareTo(BigDecimal.ZERO) <= 0) {
			System.out.println(ERROR_VIEW);
			resultMap.put("error_message", ERROR_VIEW);
			resultMap.put("success", 1);
			return resultMap;
		}
		if (PaymentMethod.Method.online.equals(order.getPaymentMethod().getMethod())) {
			if (orderService.isLocked(order, member, true)) {
				System.out.println(Message.warn("shop.order.locked"));
				resultMap.put("error_message", Message.warn("shop.order.locked"));
				resultMap.put("success", 1);
				return resultMap;
			}

			PaymentPlugin paymentPlugin = pluginService.getPaymentPlugin("wxpayPubPaymentPlugin");
			if (paymentPlugin != null) {
				if (paymentPlugin == null || !paymentPlugin.getIsEnabled()) {
					System.out.println(ERROR_VIEW);
					resultMap.put("error_message", ERROR_VIEW);
					resultMap.put("success", 1);
					return resultMap;
				}

				// 添加支付记录
				PaymentLog paymentLogtmp = paymentLogService.findBySn(order.getSn());
				System.out.println("sn==========="+order.getSn());
				System.out.println("paymentLogtmp==========="+paymentLogtmp);
				if(paymentLogtmp==null){
					PaymentLog paymentLog = new PaymentLog();
					paymentLog.setSn(order.getSn());
					paymentLog.setType(PaymentLog.Type.payment);
					paymentLog.setStatus(PaymentLog.Status.wait);
					paymentLog.setFee(paymentPlugin.calculateFee(order.getAmountPayable()));
					paymentLog.setAmount(paymentPlugin.calculateAmount(order.getAmountPayable()));
					paymentLog.setPaymentPluginId(paymentPlugin.getId());
					paymentLog.setPaymentPluginName(paymentPlugin.getName());
					paymentLog.setMember(member);
					paymentLog.setOrder(order);
					paymentLogService.save(paymentLog);
				}

				Map<String, String> paramMap = new HashMap<String, String>();
				paramMap.put("out_trade_no", order.getSn());
				paramMap.put("total_fee",
						order.getAmount() != null ? paymentPlugin.calculateAmount(order.getAmountPayable())
								.multiply(new BigDecimal("100")).toBigInteger().toString() : "0");
				paramMap.put("openid", member.getOpenId());
				String paramXms =CommonPayment.getPrepayIdParam(request, paramMap).toString();
				String str = HttpRequest.sendPost(CommonWeChat.PAYMENT_GET_PREPAYID_URL, paramXms);
				str = str.replaceAll("<![CDATA[|]]>", "");
				SortedMap<String, String> dataMap = CommonPayment.xmlToMap(str);

				SortedMap<String, String> data = new TreeMap<String, String>();
				if (dataMap.get("return_code").equalsIgnoreCase("SUCCESS")
						&& dataMap.get("result_code").equalsIgnoreCase("SUCCESS")) {

					data.put("appId", CommonWeChat.APPID.trim());
					data.put("timeStamp", Sha1Util.getTimeStamp().trim());
					data.put("nonceStr", Sha1Util.getNonceStr().trim());
					data.put("package", "prepay_id=" + dataMap.get("prepay_id").trim());
					data.put("signType", CommonWeChat.SIGNTYPE.trim());
					data.put("paySign", CommonPayment.getMD5Sign(data).trim());
					resultMap.put("success", 0);
					resultMap.put("resultData", data);
				} else if (dataMap.get("return_code").equalsIgnoreCase("FAIL")) {
					System.out.println(dataMap.get("return_msg"));
					resultMap.put("error_message", dataMap.get("return_msg"));
					resultMap.put("success", 1);
				} else if (dataMap.get("result_code").equalsIgnoreCase("FAIL")) {
					System.out.println(dataMap.get("err_code_des"));
					resultMap.put("error_message", dataMap.get("err_code_des"));
					resultMap.put("success", 1);
				} else {
					System.out.println(dataMap.get("数据有误"));
					resultMap.put("error_message", "数据有误");
					resultMap.put("success", 1);
				}
			} else {
				System.out.println(ERROR_VIEW);
				resultMap.put("error_message", ERROR_VIEW);
				resultMap.put("success", 1);
				return resultMap;
			}
		}

		return resultMap;
	}

	@RequestMapping(value = "/m_weixinNotify")
	public String m_weixinNotify(String sn, HttpServletRequest request, ModelMap model) {
		System.out.println("sn====="+sn);
		PaymentLog paymentLog = paymentLogService.findBySn(sn);
		System.out.println("paymentLog====="+paymentLog);
		model.addAttribute("paymentLog", paymentLog);
		return "payment/plugin_notify";
	}

	/**
	 * 微信支付成功通知
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/paySuccess")
	public void paySuccess(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("paySuccess: begin\n");
		try {
			ServletInputStream in = request.getInputStream();
			StringBuffer buff = new StringBuffer();
			try {
				byte[] b = new byte[4096];
				for (int length; (length = in.read(b)) != -1;) {
					buff.append(new String(b, 0, length));
				}
			} catch (IOException e) {
				System.out.println("streamToString : === " + e.getMessage());
				buff = buff.delete(0, buff.length());
				e.printStackTrace();
			}
			String result = buff.toString();
			
			System.out.println("result:== " + result);
			System.out.println("xStreamUtil begin...");
			
			

			if (result != null && !result.equals("")) {
				Map<String, String> map = CommonPayment.xmlToMap(result);
				System.out.println("map:" + map);
				for (Object keyValue : map.keySet()) {
//					System.out.println(keyValue + "=" + map.get(keyValue));
				}
				System.out.println("result_code:" + map.get("result_code").equalsIgnoreCase("SUCCESS"));
				if (map.get("result_code").equalsIgnoreCase("SUCCESS")) {
//					String sign = CommonPayment.SuccessSign(map, CommonWeChat.MCH_KEY);
//					System.out.println("215 sign=" + map.get("sign") + " APP_PAYKRY=" + sign);
//					if (sign != null && map.get("sign").equals(sign)) {
						String out_trade_no = map.get("out_trade_no");
						String total_fee = map.get("total_fee");
						//处理订单

						sendToCFT("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", response);
//					}
				}
			}
			sendToCFT("<xml><return_code><![CDATA[FAIL]]></return_code></xml>", response);
		} catch (Exception ex) {
			System.out.println("paySuccess Exception = " + ex.getMessage());
			ex.printStackTrace();
		}
		System.out.println("paySuccess  === end\n");

	}

	public void sendToCFT(String msg, HttpServletResponse response) throws IOException {
		String strHtml = msg;
		PrintWriter out = response.getWriter();
		out.println(strHtml);
		out.flush();
		out.close();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值