fix(商城): 添加支付宝支付

master
waynaqua 3 years ago
parent def0654df1
commit d94a943656

@ -44,7 +44,7 @@ public interface IOrderService extends IService<Order> {
* @param request
* @return r
*/
R h5pay(String orderSn, Integer payType, HttpServletRequest request);
R h5pay(String orderSn, Integer payType, HttpServletRequest request) throws UnsupportedEncodingException;
/**
*

@ -367,7 +367,49 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
public R prepay(String orderSn, Integer payType, HttpServletRequest request) throws UnsupportedEncodingException {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
Long userId = order.getUserId();
String checkMsg = checkOrderOperator(order);
if (!SysConstants.STRING_TRUE.equals(checkMsg)) {
return R.error(checkMsg);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error("订单不能支付");
}
// 设置支付方式
order.setPayType(payType);
Member member = iMemberService.getById(MobileSecurityUtils.getUserId());
String openid = member.getWeixinOpenid();
if (openid == null) {
return R.error("订单不能支付");
}
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setOpenid(openid);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
e.printStackTrace();
return R.error("订单不能支付");
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public R h5pay(String orderSn, Integer payType, HttpServletRequest request) throws UnsupportedEncodingException {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
String checkMsg = checkOrderOperator(order);
if (!SysConstants.STRING_TRUE.equals(checkMsg)) {
return R.error(checkMsg);
@ -381,16 +423,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
order.setPayType(payType);
switch (Objects.requireNonNull(PayTypeEnum.of(payType))) {
case WX:
Member member = iMemberService.getById(MobileSecurityUtils.getUserId());
String openid = member.getWeixinOpenid();
if (openid == null) {
return R.error("订单不能支付");
}
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setOpenid(openid);
orderRequest.setTradeType(WxPayConstants.TradeType.MWEB);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
@ -402,7 +439,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
return R.error("订单不能支付");
}
case ALI:
@ -473,66 +510,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public R h5pay(String orderSn, Integer payType, HttpServletRequest request) {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
String checkMsg = checkOrderOperator(order);
if (!SysConstants.STRING_TRUE.equals(checkMsg)) {
return R.error(checkMsg);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error("订单不能支付");
}
// 设置支付方式
order.setPayType(payType);
if (PayTypeEnum.WX.getCode() == payType) {
WxPayMwebOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setTradeType("MWEB");
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("data", result);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error("支付失败");
}
} else {
// todo 暂时没有实现支付宝支付,直接更新支付状态为已支付
order.setPayId("xxxxx0987654321-ali");
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
order.setUpdateTime(new Date());
if (!updateById(order)) {
return R.error("更新数据已失效");
}
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
}
// 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId());
// 取消订单超时未支付任务
taskService.removeTask(new OrderUnpaidTask(order.getId()));
return R.success();
}
}
@Override
public R payNotify(HttpServletRequest request, HttpServletResponse response) {
String xmlResult;

Loading…
Cancel
Save