-
@param response
-
@return
*/
@RequestMapping(value="/alipay/invoke",method={RequestMethod.GET,RequestMethod.POST})
public JsonResult alipayInvoke(HttpServletRequest req, HttpServletResponse resp)
{
String result="";
try
{
/*订单号/
String orderNumber=this.getNotNull(“orderNumber”, req);
/*金额/
String money=this.getNotNull(“money”, req);
/*优惠券id/
String couponReceiveId = req.getParameter(“couponReceiveId”);
if(StringUtils.isBlank(orderNumber) || StringUtils.isBlank(money))
{
return new JsonResult(JsonResultCode.FAILURE,“请求参数有误,请稍后重试”,"");
}
//对比金额
OrderInfo orderInfo=this.orderInfoService.getOrderInfoByOrderNumber(orderNumber);
if(orderInfo==null)
{
return new JsonResult(JsonResultCode.FAILURE,“订单号不存在,请稍后重试”,"");
}
//获取订单的金额
BigDecimal orderAmount=orderInfo.getOrderAmount();
//减余额
Long buyerId=orderInfo.getBuyerId();
Buyer buyer=this.buyerService.getBuyerById(buyerId);
//用户余额
BigDecimal balanceMoney=buyer.getBalanceMoney();
//计算最终需要给支付宝的金额
BigDecimal payAmount=orderAmount.subtract(balanceMoney);
//买家支付时抵扣优惠券
if(StringUtils.isNotBlank(couponReceiveId)){
Long id = Long.parseLong(couponReceiveId);
payAmount = couponReceiveService.deductionCouponMoney(id, orderNumber, payAmount);
}
logger.info("[AlipayController][alipayInvoke] orderNumber:" +orderNumber +" money:" +money+" orderAmount:"+orderAmount+" balanceMoney:"+balanceMoney+" payAmount:" +payAmount);
//考虑重复订单的问题,会产生重复日志
PayLogs payLogs=this.payLogsService.getPayLogsByOrderNumber(orderNumber);
if(payLogs==null)
{
//创建订单日志
PayLogs logs=new PayLogs();
logs.setUserId(buyerId);
logs.setOrderId(orderInfo.getOrderId());
logs.setOrderNumber(orderNumber);
logs.setOrderAmount(payAmount);
logs.setStatus(PAY_LOGS_READY);
logs.setCreateTime(new Date());
int payLogsResult=payLogsService.addPayLogs(logs);
logger.info("[AlipayController][alipayInvoke] 创建订单日志结果:" + (payLogsResult>0));
}else
{
logger.info("[AlipayController][alipayInvoke] 创建重复订单");
}
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”,"","", “json”,“UTF-8”,"",“RSA2”);
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody("");
model.setSubject(“xxx”);
model.setOutTradeNo(orderNumber);
model.setTimeoutExpress(“15m”);
model.setTotalAmount(payAmount.toString());
model.setProductCode(“QUICK_MSECURITY_PAY”);
request.setBizModel(model);
request.setNotifyUrl(NOTIFY_URL);
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
result=response.getBody();
logger.info("[AlipayController][alipayNotify] result: " +result);
} catch (AlipayApiException ex)
{
logger.error("[AlipayController][alipayNotify] exception:",ex);
return new JsonResult(JsonResultCode.FAILURE,“系统错误,请稍后重试”,"");
}
return new JsonResult(JsonResultCode.SUCCESS,“操作成功”,result);
}
}
支付宝服务端回调代码
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSe