大体思路:在项目中有一个购物车结算页面,点击提交订单,controller中处理,查看此订单的付款方式,如果是在线支付就跳转到另一个controller中,这个controller也就是demo中的处理支付跳转的jsp中的代码,还要配置一个return_url用来同步页面通知,这个配置同样也是一个controller来处理跳转到一个success.jsp支付成功的页面,这个controller除了跳转到支付成功页面还可以在里面处理订单状态改为已支付一类的业务逻辑
首先是点击提交订单,需要创建一个订单,然后判断订单状态,是货到付款还是在线支付
@RequestMapping("/create")
public String orderCreate(Order order, Model model,HttpServletRequest request){
try {
System.out.println("支付方式为:"+order.getPaymentType());
//获取用户信息,因为order下都是拦截器拦截过的,拦截器把用户信息存入了请求中
TbUser user = (TbUser) request.getAttribute("user");
//设置用户信息,提交订单之前补全用户信息
order.setBuyerNick(user.getUsername());
order.setUserId(user.getId());
String orderId = orderService.createOrder(order);
//创建完订单之后,如果订单的支付方式为4,代表在线支付,我们要将其跳转到在线支付页面
if(order.getPaymentType() == 4){
return "redirect:/order/goAliPay/"+orderId+".html";
}
model.addAttribute("paymentType",order.getPaymentType());
model.addAttribute("orderId",orderId);
model.addAttribute("payment",order.getPayment());
model.addAttribute("date",new DateTime().plusDays(3).toString("yyyy-MM-dd"));
return "success";
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("message","出现了一点问题...");
return "/error/exception";
}
}
在线支付,跳转到另一个controller,注意这里有一个编码设置,如果不设置为UTF-8的话会出现验签失败,而且界面的css样式消失的问题。
@RequestMapping(value = "/goAliPay/{orderId}",produces = "text/html; charset=UTF-8")
@ResponseBody
public String goAlipay(@PathVariable String orderId, HttpServletRequest request, HttpServletRequest response) throws Exception {
//根据orderId获取订单信息
Order order = orderService.getOrderByOrderId(orderId);
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商户订单号,商户网站订单系统中唯一订单号,必填
String out_trade_no = orderId;
//付款金额,必填
String total_amount = order.getPayment();
//订单名称,必填
String subject = order.getOrderItems().get(0).getTitle();
//商品描述,可空
String body = order.getOrderItems().get(0).getTitle();
// 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
String timeout_express = "1c";
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"timeout_express\":\""+ timeout_express +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}
支付成功同步通知页面controller
@RequestMapping("/paySuccess")
public String paySuccess(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
//获取支付宝GET过来反馈信息
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.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
if(signVerified) {
//商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
Order order = orderService.getOrderByOrderId(out_trade_no);
order.setStatus(2);
//修改订单状态
Integer integer = orderService.changeStatus(order);
//返回正常修改成功
if (integer == 200){
//还要额外加订单支付方式
model.addAttribute("paymentType",order.getPaymentType());
model.addAttribute("orderId",order.getOrderId());
model.addAttribute("payment",order.getPayment());
model.addAttribute("date",new DateTime().plusDays(3).toString("yyyy-MM-dd"));
return "success";
}else{
return "/error/exception";
}
}else {
System.out.println("验签失败");
}
return "/error/exception";
}
这个也是复制的demo中的代码稍作修改即可
整体整合流程就这样,整合的关键是分析一下demo中的alipay.trade.page.pay.jsp和return_url.jsp和notify_url.jsp,里面都有很详细的说明!
支付接口与项目整合到此结束了