Java开源生鲜电商平台-支付模块的设计与架构(源码可下载)(1)

该博客介绍了Java开源生鲜电商平台的支付模块设计,包括调用支付宝接口进行支付的实现,涉及订单号、金额、优惠券等参数校验,以及支付日志的创建。同时提到了支付宝服务端回调的处理,确保交易的可靠性。代码示例展示了如何处理支付请求、验证支付宝参数及回调通知。
摘要由CSDN通过智能技术生成
  • @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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值