|
|
@@ -1,7 +1,11 @@
|
|
|
package org.springblade.payment.callback;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springblade.common.enums.OrderType;
|
|
|
import org.springblade.common.enums.ResCode;
|
|
|
import org.springblade.common.enums.SystemConstant;
|
|
|
@@ -17,9 +21,13 @@ import org.springblade.ldt.mall.service.IMallService;
|
|
|
import org.springblade.ldt.shop.entity.Shop;
|
|
|
import org.springblade.ldt.shop.service.IShopService;
|
|
|
import org.springblade.ldt.user.entity.LoginUser;
|
|
|
+import org.springblade.ldt.user.entity.UserChannelPoint;
|
|
|
import org.springblade.ldt.user.service.ILoginUserService;
|
|
|
+import org.springblade.ldt.user.service.IUserChannelPointService;
|
|
|
+import org.springblade.payment.entity.PaymentVO;
|
|
|
import org.springblade.payment.entity.SuccessParams;
|
|
|
import org.springblade.payment.event.UserPayEvent;
|
|
|
+import org.springblade.webSocket.WebSocketServer;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -36,6 +44,7 @@ import java.util.Objects;
|
|
|
*/
|
|
|
@Component
|
|
|
@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
public class UserPayCallback {
|
|
|
|
|
|
private ILoginUserService loginUserService;
|
|
|
@@ -44,6 +53,8 @@ public class UserPayCallback {
|
|
|
private IBalanceBillsService balanceBillsService;
|
|
|
private IShopService shopService;
|
|
|
private IMallService mallService;
|
|
|
+ private IUserChannelPointService userChannelPointService;
|
|
|
+ private WebSocketServer webSocketServer;
|
|
|
|
|
|
|
|
|
@EventListener
|
|
|
@@ -51,34 +62,48 @@ public class UserPayCallback {
|
|
|
@Transactional
|
|
|
public void PaySuccess(UserPayEvent userPayEvent){
|
|
|
SuccessParams successParams = userPayEvent.getSuccessParams();
|
|
|
+ PaymentVO paymentVO = PaymentVO.builder().isSuccess(Boolean.FALSE).msg("订单付款异常!").build();
|
|
|
+ try{
|
|
|
+ //获取对应交易的积分账单、余额账单、支付账单以及交易用户
|
|
|
+ LoginUser user = loginUserService.getById(successParams.getUserId());
|
|
|
+ Bills bills = Objects.isNull(successParams.getBillId())? null: billsService.getById(successParams.getBillId());
|
|
|
+ PointBills pointBills = Objects.isNull(successParams.getPointBillsId())? null: pointBillsService.getById(successParams.getPointBillsId());
|
|
|
+ BalanceBills balanceBills = Objects.isNull(successParams.getBalanceBillsId())? null: balanceBillsService.getById(successParams.getBalanceBillsId());
|
|
|
+
|
|
|
+ //更新各个账单的状态
|
|
|
+ if(!Objects.isNull(pointBills)){
|
|
|
+ Assert.isTrue(handlePointBills(pointBills,user,successParams.getChannelId()),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ }
|
|
|
|
|
|
- //获取对应交易的积分账单、余额账单、支付账单以及交易用户
|
|
|
- LoginUser user = loginUserService.getById(successParams.getUserId());
|
|
|
- Bills bills = Objects.isNull(successParams.getBillId())? null: billsService.getById(successParams.getBillId());
|
|
|
- PointBills pointBills = Objects.isNull(successParams.getPointBillsId())? null: pointBillsService.getById(successParams.getPointBillsId());
|
|
|
- BalanceBills balanceBills = Objects.isNull(successParams.getBalanceBillsId())? null: balanceBillsService.getById(successParams.getBalanceBillsId());
|
|
|
+ if(!Objects.isNull(balanceBills)){
|
|
|
+ Assert.isTrue(handleBalanceBills(balanceBills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ }
|
|
|
|
|
|
- //更新各个账单的状态
|
|
|
- if(!Objects.isNull(pointBills)){
|
|
|
- Assert.isTrue(handlePointBills(pointBills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
+ if(!Objects.isNull(bills)){
|
|
|
+ Assert.isTrue(handleBills(bills,user,pointBills,balanceBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ }
|
|
|
|
|
|
- if(!Objects.isNull(balanceBills)){
|
|
|
- Assert.isTrue(handleBalanceBills(balanceBills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
+ //修改用户积分余额
|
|
|
+ Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
|
|
|
- if(!Objects.isNull(bills)){
|
|
|
- Assert.isTrue(handleBills(bills,user,pointBills,balanceBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
+ //更新商家账户余额
|
|
|
+ Assert.isTrue(updateShopBalance(bills,pointBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
|
|
|
- //修改用户积分余额
|
|
|
- Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ //赠送用户积分
|
|
|
+ Assert.isTrue(sendPoint(bills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
|
|
|
- //更新商家账户余额
|
|
|
- Assert.isTrue(updateShopBalance(bills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ paymentVO.setIsSuccess(Boolean.TRUE);
|
|
|
+ paymentVO.setMsg("交易成功!");
|
|
|
|
|
|
- //赠送用户积分
|
|
|
- Assert.isTrue(sendPoint(bills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new ServiceException(ResCode.TRADE_ERROR);
|
|
|
+ }finally {
|
|
|
+ webSocketServer.sendInfo(
|
|
|
+ Convert.toStr(successParams.getShopId()),
|
|
|
+ JSON.toJSONString(paymentVO)
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -89,10 +114,13 @@ public class UserPayCallback {
|
|
|
/**
|
|
|
* 修改用户积分以及积分账单状态
|
|
|
* */
|
|
|
- private boolean handlePointBills(PointBills pointBills, LoginUser user) {
|
|
|
+ private boolean handlePointBills(PointBills pointBills, LoginUser user,Long channelId) {
|
|
|
+ UserChannelPoint channelPoint = userChannelPointService.getById(channelId);
|
|
|
+ channelPoint.setAvailable(channelPoint.getAvailable().subtract(pointBills.getPrice()));
|
|
|
user.setChannelPoint(user.getChannelPoint().subtract(pointBills.getPrice()));
|
|
|
pointBills.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
- return pointBillsService.saveOrUpdate(pointBills);
|
|
|
+
|
|
|
+ return pointBillsService.saveOrUpdate(pointBills) && userChannelPointService.saveOrUpdate(channelPoint);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -123,15 +151,15 @@ public class UserPayCallback {
|
|
|
* 商场、商家赠送用户积分
|
|
|
**/
|
|
|
private boolean sendPoint(Bills bills,LoginUser user){
|
|
|
+ if(!Objects.isNull(bills)){
|
|
|
+ //赠送用户积分
|
|
|
+ BigDecimal payNum = bills.getPrice();
|
|
|
+ Shop shop = shopService.getById(bills.getReceiveId());
|
|
|
+ Mall mall = mallService.getById(shop.getMallId());
|
|
|
|
|
|
- //赠送用户积分
|
|
|
- BigDecimal payNum = bills.getPrice();
|
|
|
- Shop shop = shopService.getById(bills.getReceiveId());
|
|
|
- Mall mall = mallService.getById(shop.getMallId());
|
|
|
-
|
|
|
- mallSendPoint(mall,user,payNum);
|
|
|
- shopSendPoint(shop,user,payNum);
|
|
|
-
|
|
|
+ mallSendPoint(mall,user,payNum);
|
|
|
+ shopSendPoint(shop,user,payNum);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -141,10 +169,17 @@ public class UserPayCallback {
|
|
|
* 商家账户余额更新
|
|
|
*
|
|
|
* @param bills*/
|
|
|
- private boolean updateShopBalance(Bills bills){
|
|
|
- Shop shop = shopService.getById(bills.getReceiveId());
|
|
|
- shop.setBalance(shop.getBalance().add(bills.getPrice()));
|
|
|
- return shopService.saveOrUpdate(shop);
|
|
|
+ private boolean updateShopBalance(Bills bills,PointBills pointBills){
|
|
|
+ Shop shop = null;
|
|
|
+ if(!Objects.isNull(bills)){
|
|
|
+ shop = shopService.getById(bills.getReceiveId());
|
|
|
+ shop.setBalance(shop.getBalance().add(bills.getPrice()));
|
|
|
+ }
|
|
|
+ if(!Objects.isNull(pointBills)){
|
|
|
+ shop = shopService.getById(pointBills.getReceiveId());
|
|
|
+ shop.setCharge(shop.getCharge().add(pointBills.getPrice()));
|
|
|
+ }
|
|
|
+ return Objects.isNull(shop)? true: shopService.saveOrUpdate(shop);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -154,7 +189,7 @@ public class UserPayCallback {
|
|
|
public boolean shopSendPoint(Shop shop, LoginUser user, BigDecimal payNum){
|
|
|
//商家若是开启了会员中心则赠送积分
|
|
|
if(shop.getIsOpenMember()!=0){
|
|
|
- PointBills sendBill = new PointBills();
|
|
|
+ BalanceBills sendBill = new BalanceBills();
|
|
|
sendBill.setType(OrderType.SHOP_SEND.name());
|
|
|
sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
|
|
|
sendBill.setTitle("商家赠送积分");
|
|
|
@@ -168,9 +203,9 @@ public class UserPayCallback {
|
|
|
|
|
|
|
|
|
if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
|
|
|
- sendBill.setDesc("账户余额不足或赠送积分值小于1");
|
|
|
+ sendBill.setBillDesc("账户余额不足或赠送积分值小于1");
|
|
|
}else{
|
|
|
- user.setChannelPoint(user.getChannelPoint().add(sendNum));
|
|
|
+ user.setBalance(user.getBalance().add(sendNum));
|
|
|
shop.setBalance(shop.getBalance().subtract(sendNum));
|
|
|
sendBill.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
@@ -178,9 +213,9 @@ public class UserPayCallback {
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
- sendBill.setDesc("商家不存在");
|
|
|
+ sendBill.setBillDesc("商家不存在");
|
|
|
}
|
|
|
- Assert.isTrue(pointBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ Assert.isTrue(balanceBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
@@ -203,19 +238,20 @@ public class UserPayCallback {
|
|
|
BigDecimal pointRate = mall.getPointRate();
|
|
|
BigDecimal sendNum = payNum.multiply(pointRate).multiply(mall.getSendPointUnnit());
|
|
|
|
|
|
-
|
|
|
if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
|
|
|
- sendBill.setDesc("账户余额不足或赠送积分值小于1");
|
|
|
+ sendBill.setBillsDesc("账户余额不足或赠送积分值小于1");
|
|
|
}else{
|
|
|
user.setChannelPoint(user.getChannelPoint().add(sendNum));
|
|
|
mall.setBalance(mall.getBalance().subtract(sendNum));
|
|
|
+ updateChannelAccount(mall,sendNum,user);
|
|
|
sendBill.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ Assert.isTrue(updateChannelAccount(mall,sendNum,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
Assert.isTrue(mallService.saveOrUpdate(mall),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
- sendBill.setDesc("商场不存在");
|
|
|
+ sendBill.setBillsDesc("商场不存在");
|
|
|
}
|
|
|
Assert.isTrue(pointBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
@@ -224,4 +260,30 @@ public class UserPayCallback {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 修改渠道积分账户
|
|
|
+ * @param mall
|
|
|
+ * @param sendNum
|
|
|
+ * @param user*/
|
|
|
+ private boolean updateChannelAccount(Mall mall, BigDecimal sendNum, LoginUser user){
|
|
|
+ UserChannelPoint channelPoint = userChannelPointService.getOne(new QueryWrapper<UserChannelPoint>()
|
|
|
+ .lambda()
|
|
|
+ .eq(UserChannelPoint::getChannelId, mall.getId())
|
|
|
+ .eq(UserChannelPoint::getUserId, user.getId()));
|
|
|
+
|
|
|
+ if(Objects.isNull(channelPoint)){
|
|
|
+ channelPoint = new UserChannelPoint();
|
|
|
+ channelPoint.setChannelId(mall.getId());
|
|
|
+ channelPoint.setUserId(user.getId());
|
|
|
+ channelPoint.setTotalPoint(BigDecimal.ZERO);
|
|
|
+ channelPoint.setAvailable(BigDecimal.ZERO);
|
|
|
+ channelPoint.setChannelLogo(mall.getLogo());
|
|
|
+ channelPoint.setChannelName(mall.getMallName());
|
|
|
+ channelPoint.setPointRate(mall.getPointRate());
|
|
|
+ channelPoint.setUsedPoint(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ channelPoint.setAvailable(channelPoint.getAvailable().add(sendNum));
|
|
|
+ channelPoint.setTotalPoint(channelPoint.getTotalPoint().add(sendNum));
|
|
|
+ return userChannelPointService.saveOrUpdate(channelPoint);
|
|
|
+ }
|
|
|
}
|