/** * 解析获取的授权令牌 * * @author ZHANG Nan * @param responseStr * @return * @throws Exception */ private AlipayWAPResponseResult praseResult(String responseStr) throws Exception { AlipayWAPResponseResult result = new AlipayWAPResponseResult(); if (responseStr.contains("<err>")) { result.setSuccess(false); String businessResult = AlipayUtil.getParameter(responseStr, "res_error"); // 转换错误信息 XMap xmap = new XMap(); xmap.register(AlipayWAPErrorCode.class); AlipayWAPErrorCode errorCode = (AlipayWAPErrorCode) xmap.load( new ByteArrayInputStream( businessResult.getBytes(alipayWAPPostData.getCharset()))); result.setErrorMessage(errorCode); } else { result.setSuccess(true); result.setBusinessResult(AlipayUtil.getParameter(responseStr, "res_data")); } return result; }
@Action("/pay/alipayWAP") public void alipayWAP() { if (payment()) { try { LOG.info("alipayWAP request init data:" + alipayWAPPostData.getRequestParams()); // 获取授权令牌 AlipayWAPResponseResult resResult = getRequestToken(); log.info(StringUtil.printParam(resResult)); String businessResult = ""; if (resResult.isSuccess()) { businessResult = resResult.getBusinessResult(); } else { // 授权令牌错误 String errorMessage = "errorCode:" + resResult.getErrorMessage().getCode() + ",subErrorCode:" + resResult.getErrorMessage().getSubCode() + ",errorMsg:" + resResult.getErrorMessage().getMsg() + ",errorDetail:" + resResult.getErrorMessage().getDetail(); log.error(errorMessage); PayPayment payment = new PayPayment(); payment.setPaymentTradeNo(alipayWAPPostData.getOutTradeNo()); payment.setCallbackInfo(errorMessage); payment.setCallbackTime(new Date()); getPayPaymentService().callBackPayPayment(payment, false); return; } XMap xmap = new XMap(); xmap.register(AlipayWAPDirectTradeCreateRes.class); AlipayWAPDirectTradeCreateRes directTradeCreateRes = (AlipayWAPDirectTradeCreateRes) xmap.load( new ByteArrayInputStream( businessResult.getBytes(alipayWAPPostData.getCharset()))); String requestToken = directTradeCreateRes.getRequestToken(); // 封装交易参数 Map<String, String> authParams = alipayWAPPostData.initExecuteParams(requestToken); String redirectURL = alipayWAPPostData.getReqUrl() + "?" + AlipayUtil.mapToUrl(authParams); log.info("redirectURL:" + redirectURL); // sendAjaxMsg(redirectURL); // getRequest().getRequestDispatcher(redirectURL).forward(getRequest(), getResponse()); getResponse().sendRedirect(redirectURL); } catch (Exception e) { e.printStackTrace(); } } }
/** * 发起获取授权令牌请求 * * @author ZHANG Nan * @return * @throws Exception */ private AlipayWAPResponseResult getRequestToken() throws Exception { String invokeUrl = alipayWAPPostData.getReqUrl() + "?"; URL serverUrl = new URL(invokeUrl); HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.connect(); String params = AlipayUtil.mapToUrl(alipayWAPPostData.getRequestParams()); conn.getOutputStream().write(params.getBytes()); InputStream is = conn.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } String responseStr = URLDecoder.decode(buffer.toString(), alipayWAPPostData.getCharset()); conn.disconnect(); return praseResult(responseStr); }
/** * 根据反馈回来的信息,生成签名结果 * * @param Params 通知返回来的参数数组 * @return 生成的签名结果 */ private static String getMysign(Map<String, String> Params) { Map<String, String> sParaNew = AlipayCore.paraFilter(Params); // 过滤空值、sign与sign_type参数 String mysign = AlipayUtil.buildMysign(sParaNew); // 获得签名结果 return mysign; }