引入alipay-sdk-java依赖
- 创建AlipayConfig类,包含支付宝相关配置信息
public class AlipayConfig {// 商户appidpublic static String APPID = "";// 私钥public static String RSA_PRIVATE_KEY = "";// 支付宝公钥public static String ALIPAY_PUBLIC_KEY = "";// 服务器异步通知页面路径public static String notify_url = "";// 页面跳转同步通知页面路径public static String return_url = "";// 签名方式public static String sign_type = "RSA2";// 字符编码格式public static String CHARSET = "UTF-8";// 支付宝网关public static String GATEWAY_URL = "https://openapi.alipay.com/gateway.do";// 日志记录目录public static String log_path = "C:\\";
}
- 创建AlipayUtil类,包含支付宝支付相关方法
public class AlipayUtil {/*** 支付宝手机网站支付请求* @param out_trade_no 订单号* @param total_amount 订单金额* @param subject 订单标题* @param response HttpServletResponse* @throws IOException*/public static void alipayTradeWapPay(String out_trade_no, String total_amount, String subject, HttpServletResponse response) throws IOException {AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAY_URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();model.setOutTradeNo(out_trade_no);model.setTotalAmount(total_amount);model.setSubject(subject);model.setProductCode("QUICK_WAP_PAY");model.setTimeoutExpress("30m");AlipayTradeWapPayRequest request = new AlipayTradeapapPayRequest();request.setReturnUrl(AlipayConfig.return_url);request.setNotifyUrl(AlipayConfig.notify_url);request.setBizModel(model);String form = "";try {form = alipayClient.pageExecute(request).getBody();response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);response.getWriter().write(form);//直接将完整的表单html输出到页面response.getWriter().flush();response.getWriter().close();} catch (AlipayApiException e) {e.printStackTrace();}}/*** 支付宝手机网站支付回调处理* @param request HttpServletRequest* @return* @throws AlipayApiException*/public static boolean alipayReturn(HttpServletRequest request) throws AlipayApiException {Map<String, String> params = new HashMap<String, String>();Map<String, String[]> requestParams = request.getParameterMap();for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {String name = (String) iter.next();String[] values = (String[]) requestParams.get(name);String valueStr = "";for (int i = 0; i values values.length; i++) {valueStr = (i == values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";}params.put(name, valueStr);}boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type); //调用SDK验证签名return signVerified;}/*** 支付宝异步通知处理* @param request HttpServletRequest* @return* @throws AlipayApiException*/public static boolean alipayNotify(HttpServletRequest request) throws AlipayApiException {Map<String, String> params = new HashMap<String, String>();Map<String, String[]> requestParams = request.getParameterMap();for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {String name = (String) iter.next();String[] values = (String[]) requestParams.get(name);String valueStr = "";for (int i = 0; i values values.length; i++) {valueStr = (i == values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";}params.put(name, valueStr);}boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type); //调用SDK验证签名if (signVerified) {String trade_status = request.getParameter("trade_status"); //交易状态if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {//处理订单业务逻辑return true;} else {return false;}} else {return false;}}
}
- 创建AlipayController类,处理支付宝支付请求和回调
@RestController
@RequestMapping("/")
public class AlipayController {@RequestMapping("alipay")public void alipay(HttpServletRequest request, HttpServletResponse response) throws IOException {String out_trade_no = "商户订单号";String total_amount = "支付金额";String subject = "交易标题";AlipayUtil.alipayTradeWapPay(out_trade_no, total_amount, subject, response);}@RequestMapping("alipayReturn")public String alipayReturn(HttpServletRequest request) throws AlipayApiException {boolean result = AlipayUtil.alipayReturn(request);//处理支付宝同步通知业务逻辑if (result) {return "支付成功";} else {return "支付失败";}}@RequestMapping("alipayNotify")public String alipayNotify(HttpServletRequest request) throws AlipayApiException {boolean result = AlipayUtil.alipayNotify(request);//处理支付异异步通知业务逻辑if (result) {return "success";} else {return "fail";}}
}