瀏覽代碼

交易处理

lianghanqiang 4 年之前
父節點
當前提交
c100a1ebf0

+ 26 - 0
src/main/java/org/springblade/agent/listener/AgentFeeHandle.java

@@ -0,0 +1,26 @@
+package org.springblade.agent.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springblade.gateway.common_gateway.entity.BillSuccessEvent;
+import org.springblade.payment.entity.SuccessParams;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+/**
+ * @author: lianghanqiang
+ * @description: 根据订单信息,处理响应代理关系以及代理费
+ * @since: 8/31/21 -- 9:49 AM
+ */
+@Component
+@Slf4j
+public class AgentFeeHandle {
+
+	@EventListener
+	@Async
+	@Transactional
+	public void addAgentFee(BillSuccessEvent billSuccessEvent){
+		SuccessParams successParams = billSuccessEvent.getSuccessParams();
+	}
+}

+ 2 - 0
src/main/java/org/springblade/common/enums/ResCode.java

@@ -18,8 +18,10 @@ public enum ResCode implements IResultCode {
 	PAY_TYPE_ERROR(501,"支付方式错误"),
 	PAY_SCENE_ERROR(501,"支付场景错误"),
 	TRADE_ERROR(501,"交易异常"),
+	INIT_ORDER_FAIL(501,"初始化订单失败"),
 	CHANNEL_NOT_FOUND(501,"积分渠道查找失败!"),
 	USER_PAY_CALLBACK_ERROR(501,"用户支付回调异常"),
+	SEND_POINT_ERROR(501,"赠送积分异常"),
 
 
 	/*	动态密码	*/

+ 21 - 0
src/main/java/org/springblade/gateway/common_gateway/entity/BillSuccessEvent.java

@@ -0,0 +1,21 @@
+package org.springblade.gateway.common_gateway.entity;
+
+import lombok.Getter;
+import org.springblade.payment.entity.SuccessParams;
+import org.springframework.context.ApplicationEvent;
+
+/**
+ * @author: lianghanqiang
+ * @description: 交易成功事件
+ * @since: 8/31/21 -- 9:52 AM
+ */
+
+@Getter
+public class BillSuccessEvent extends ApplicationEvent {
+	private SuccessParams successParams;
+
+	public BillSuccessEvent(SuccessParams successParams) {
+		super(successParams);
+		this.successParams =successParams;
+	}
+}

+ 2 - 0
src/main/java/org/springblade/ldt/bills/entity/Bills.java

@@ -134,5 +134,7 @@ public class Bills implements Serializable {
 		@ApiModelProperty(value = "第三方平台订单号")
 		private String thirdOrderId;
 
+		private BigDecimal fee;
+		private BigDecimal pointFee;
 
 }

+ 1 - 1
src/main/java/org/springblade/payment/callback/AgentChargeCallback.java

@@ -44,7 +44,7 @@ public class AgentChargeCallback {
 	public void PaySuccess(AgentChargeEvent agentChargeEvent){
 		SuccessParams successParams = agentChargeEvent.getSuccessParams();
 		//获取订单信息
-		Bills bills = billsService.getById(successParams.getBillId());
+		Bills bills = billsService.getById(successParams.getBills().getId());
 		Assert.notNull(bills, "支付记录中无此订单");
 		bills.setChannelId(successParams.getThirdOrderId());
 

+ 50 - 158
src/main/java/org/springblade/payment/callback/UserPayCallback.java

@@ -10,6 +10,7 @@ import org.springblade.common.enums.OrderType;
 import org.springblade.common.enums.ResCode;
 import org.springblade.common.enums.SystemConstant;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.gateway.common_gateway.entity.BillSuccessEvent;
 import org.springblade.ldt.bills.entity.BalanceBills;
 import org.springblade.ldt.bills.entity.Bills;
 import org.springblade.ldt.bills.entity.PointBills;
@@ -28,6 +29,7 @@ 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.ApplicationEventPublisher;
 import org.springframework.context.event.EventListener;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
@@ -55,50 +57,63 @@ public class UserPayCallback {
 	private IMallService mallService;
 	private IUserChannelPointService userChannelPointService;
 	private WebSocketServer webSocketServer;
+	private ApplicationEventPublisher applicationEventPublisher;
 
 
 	@EventListener
 	@Async
 	@Transactional
-	public void PaySuccess(UserPayEvent userPayEvent){
+	public void PaySuccess(UserPayEvent userPayEvent) {
 		SuccessParams successParams = userPayEvent.getSuccessParams();
 		PaymentVO paymentVO = PaymentVO.builder().isSuccess(Boolean.FALSE).msg("订单付款异常!").build();
-		try{
+		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());
+			Bills bills = successParams.getBills();
+			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);});
+			if (!Objects.isNull(pointBills)) {
+				Assert.isTrue(handlePointBills(pointBills, user, successParams.getChannelId()), () -> {
+					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);});
+			if (!Objects.isNull(balanceBills)) {
+				Assert.isTrue(handleBalanceBills(balanceBills, 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);});
-			}
+			bills.setPaystatus(SystemConstant.BillPayStatus.付款成功.name());
+
+			//更新订单信息
+			Assert.isTrue(billsService.saveOrUpdate(bills), () -> {
+				throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
+			});
 
 			//修改用户积分余额
-			Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+			Assert.isTrue(loginUserService.saveOrUpdate(user), () -> {
+				throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
+			});
 
 			//更新商家账户余额
-			Assert.isTrue(updateShopBalance(bills,pointBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+			Assert.isTrue(updateShopBalance(bills, pointBills), () -> {
+				throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
+			});
+
 
-			//赠送用户积分
-			Assert.isTrue(sendPoint(bills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+			//通知处理 赠送用户积分 代理费等
+			applicationEventPublisher.publishEvent(new BillSuccessEvent(successParams));
 
 			paymentVO.setIsSuccess(Boolean.TRUE);
 			paymentVO.setMsg("交易成功!");
 
-		}catch (Exception e){
+		} catch (Exception e) {
 			log.error(e.getMessage());
 			throw new ServiceException(ResCode.TRADE_ERROR);
-		}finally {
+		} finally {
 			webSocketServer.sendInfo(
 				Convert.toStr(successParams.getShopId()),
 				JSON.toJSONString(paymentVO)
@@ -107,25 +122,20 @@ public class UserPayCallback {
 	}
 
 
-
-
-
-
 	/**
 	 * 修改用户积分以及积分账单状态
-	 * */
-	private boolean handlePointBills(PointBills pointBills, LoginUser user,Long channelId) {
+	 */
+	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) && userChannelPointService.saveOrUpdate(channelPoint);
 	}
 
 	/**
 	 * 修改用户账户余额以及余额账单状态
-	 * */
+	 */
 	private boolean handleBalanceBills(BalanceBills balanceBills, LoginUser user) {
 		user.setBalance(user.getBalance().subtract(balanceBills.getPrice()));
 		balanceBills.setStatus(SystemConstant.BillPayStatus.付款成功.name());
@@ -134,156 +144,38 @@ public class UserPayCallback {
 
 	/**
 	 * 修改支付账单状态
-	 * */
+	 */
 	private boolean handleBills(Bills bills, LoginUser user, PointBills pointBills, BalanceBills balanceBills) {
 		//补充更新订单信息
-		if(!Objects.isNull(pointBills)){
+		if (!Objects.isNull(pointBills)) {
 			bills.setPointNum(pointBills.getPrice());
 		}
-		if(!Objects.isNull(balanceBills)){
+		if (!Objects.isNull(balanceBills)) {
 			bills.setBalanceNum(balanceBills.getPrice());
 		}
 		bills.setPaystatus(SystemConstant.BillPayStatus.付款成功.name());
-		return billsService.saveOrUpdate(bills) && sendPoint(bills,user);
-	}
-
-	/**
-	 * 商场、商家赠送用户积分
-	 **/
-	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());
-
-			mallSendPoint(mall,user,payNum);
-			shopSendPoint(shop,user,payNum);
-		}
-		return true;
+		return billsService.saveOrUpdate(bills);
 	}
 
 
-
 	/**
-	 * 	商家账户余额更新
+	 * 商家账户余额更新
 	 *
-	 * @param bills*/
-	private boolean updateShopBalance(Bills bills,PointBills pointBills){
+	 * @param bills
+	 */
+	private boolean updateShopBalance(Bills bills, PointBills pointBills) {
 		Shop shop = null;
-		if(!Objects.isNull(bills)){
-			 shop = shopService.getById(bills.getReceiveId());
+		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());
+		if (!Objects.isNull(pointBills)) {
+			shop = shopService.getById(pointBills.getReceiveId());
 			shop.setCharge(shop.getCharge().add(pointBills.getPrice()));
 		}
-		return Objects.isNull(shop)? true: shopService.saveOrUpdate(shop);
-	}
-
-
-	/**
-	 * 	商家赠送积分
-	 * */
-	public boolean shopSendPoint(Shop shop, LoginUser user, BigDecimal payNum){
-		//商家若是开启了会员中心则赠送积分
-		if(shop.getIsOpenMember()!=0){
-			BalanceBills sendBill = new BalanceBills();
-			sendBill.setType(OrderType.SHOP_SEND.name());
-			sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
-			sendBill.setTitle("商家赠送积分");
-			sendBill.setReceiveId(user.getId());
-			sendBill.setPayId(shop.getId());
-
-			if(!Objects.isNull(shop)){
-				BigDecimal balance = shop.getBalance();
-				BigDecimal pointRate = shop.getPointRate();
-				BigDecimal sendNum = payNum.multiply(pointRate).multiply(shop.getSendPointUnit());
-
-
-				if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
-					sendBill.setBillDesc("账户余额不足或赠送积分值小于1");
-				}else{
-					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);});
-					Assert.isTrue(shopService.saveOrUpdate(shop),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
-				}
-
-			}else{
-				sendBill.setBillDesc("商家不存在");
-			}
-			Assert.isTrue(balanceBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
-		}
-		return true;
+		return Objects.isNull(shop) ? true : shopService.saveOrUpdate(shop);
 	}
+}
 
-	/**
-	 * 	商场赠送积分
-	 * */
-	public boolean mallSendPoint(Mall mall, LoginUser user, BigDecimal payNum){
-		//商场若是开启了会员中心则赠送积分
-		if(mall.getUseMember()!=0){
-			PointBills sendBill = new PointBills();
-			sendBill.setType(OrderType.MALL_SEND.name());
-			sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
-			sendBill.setTitle("商场赠送积分");
-			sendBill.setReceiveId(user.getId());
-			sendBill.setPayId(mall.getId());
-
-			if(!Objects.isNull(mall)){
-				BigDecimal balance = mall.getBalance();
-				BigDecimal pointRate = mall.getPointRate();
-				BigDecimal sendNum = payNum.multiply(pointRate).multiply(mall.getSendPointUnnit());
-
-				if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
-					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.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);});
-			Assert.isTrue(mallService.saveOrUpdate(mall),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
-		}
-		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);
-	}
-}

+ 6 - 4
src/main/java/org/springblade/payment/entity/SuccessParams.java

@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.springblade.ldt.bills.entity.Bills;
 
 import java.math.BigDecimal;
 
@@ -21,10 +22,6 @@ public class SuccessParams {
 	public static final String PAY_STATUS_SUCCESS = "success";
 	public static final String PAY_STATUS_FAIL = "fail";
 
-	/**
-	 * 订单id
-	 */
-	private Long billId;
 
 	/**
 	 * 支付状态
@@ -70,4 +67,9 @@ public class SuccessParams {
 	 * 	商户id
 	 * */
 	private Long shopId;
+
+	/**
+	 * 交易账单
+	 * */
+	private Bills bills;
 }

+ 27 - 1
src/main/java/org/springblade/payment/handle/Trade.java

@@ -1,10 +1,13 @@
 package org.springblade.payment.handle;
 
+import cn.hutool.core.convert.Convert;
 import lombok.AllArgsConstructor;
 import org.springblade.common.enums.OrderType;
+import org.springblade.common.enums.PaymentScene;
 import org.springblade.common.enums.ResCode;
 import org.springblade.common.enums.SystemConstant;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.ldt.bills.entity.Bills;
 import org.springblade.ldt.user.service.IUserChannelPointService;
 import org.springblade.payment.handle.entity.HandleData;
 import org.springblade.payment.handle.entity.Order;
@@ -72,12 +75,15 @@ public class Trade {
 
 	 	BigDecimal remain = order.getMoney();
 
-	 	//支付参数
+		Bills bills = initBill(order,channelId);
+
+		//支付参数
 		SuccessParams successParams  = SuccessParams.builder()
 			.orderType(OrderType.USER_PAY.name())
 			.status(SystemConstant.BillPayStatus.待付款.name())
 			.userId(order.getLoginUser().getId())
 			.totalPrice(order.getMoney())
+			.bills(bills)
 			.shopId(order.getShopId())
 			.channelId(channelId)
 			.build();
@@ -98,4 +104,24 @@ public class Trade {
 
 	 	return true;
 	}
+
+
+	/**
+	 * 初始化交易账单
+	 * */
+	private Bills initBill(Order order, long channelId){
+		Bills bills = new Bills();
+		bills.setPaystatus(SystemConstant.BillPayStatus.待付款.name());
+		bills.setAppid(order.getAppId());
+		bills.setCost(Convert.toStr(order.getMoney()));
+		bills.setOpenid(order.getOpenId());
+		bills.setChannelId(Convert.toStr(channelId));
+		bills.setPrice(BigDecimal.ZERO);
+		bills.setFee(BigDecimal.ZERO);
+		bills.setPointFee(BigDecimal.ZERO);
+		bills.setTitle(order.getBillsTitle());
+		bills.setPayId(order.getLoginUser().getId());
+		bills.setReceiveId(order.getShopId());
+		return bills;
+	}
 }

+ 2 - 0
src/main/java/org/springblade/payment/handle/handler/BalanceHandle.java

@@ -36,6 +36,7 @@ public class BalanceHandle implements BaseHandle {
 
 		//用户账户余额为0
 		if(user.getBalance().compareTo(BigDecimal.ZERO)<=0){
+			successParams.getBills().setBalanceNum(BigDecimal.ZERO);
 			return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 		}
 
@@ -52,6 +53,7 @@ public class BalanceHandle implements BaseHandle {
 
 		BalanceBills balanceBills = addBillRecord(order, user, handlePrice);
 		successParams.setBalanceBillsId(balanceBills.getId());
+		successParams.getBills().setBalanceNum(handlePrice);
 
 		return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 	}

+ 3 - 0
src/main/java/org/springblade/payment/handle/handler/ChannelPointHandle.java

@@ -42,6 +42,7 @@ public class ChannelPointHandle implements BaseHandle  {
 
 		//用户渠道积分为0或者用户渠道积分查找失败
 		if( Objects.isNull(channelPoint) || channelPoint.getAvailable().compareTo(BigDecimal.ZERO)<=0){
+			successParams.getBills().setPointNum(BigDecimal.ZERO);
 			return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 		}
 
@@ -56,7 +57,9 @@ public class ChannelPointHandle implements BaseHandle  {
 		}
 
 		PointBills pointBills = addBillRecord(order, user, handlePrice,channelPoint);
+
 		successParams.setPointBillsId(pointBills.getId());
+		successParams.getBills().setPointNum(handlePrice);
 
 		return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 	}

+ 10 - 3
src/main/java/org/springblade/payment/handle/handler/DiscountHandle.java

@@ -44,15 +44,22 @@ public class DiscountHandle implements BaseHandle {
 			Activity activity  = activityService.getById(joinRecord.getActivityId());
 
 			//国信服务费
-			BigDecimal serviceFee = BigDecimal.valueOf(platformConfig.getServiceFee()).add(BigDecimal.ONE);
+			BigDecimal serviceFee = BigDecimal.valueOf(platformConfig.getServiceFee());
 			//商场活动折扣
 			BigDecimal mallDiscount =  activity.getDiscount();
 			//商家折扣
 			BigDecimal shopDiscount = joinRecord.getDiscount();
 
 			remain =  remain.multiply(mallDiscount)
-				.multiply(shopDiscount)
-				.multiply(serviceFee);
+				.multiply(shopDiscount);
+
+			/*国信收取的服务费*/
+			BigDecimal chargeFee  = remain.multiply(serviceFee);
+			successParams.getBills().setFee(chargeFee);
+
+			/*最终的交易金额*/
+			remain = remain.add(serviceFee);
+
 		}
 
 		return HandleData.builder().successParams(successParams).remain(remain).isSuccess(Boolean.TRUE).build();

+ 3 - 19
src/main/java/org/springblade/payment/handle/handler/WxPayHandle.java

@@ -1,17 +1,14 @@
 package org.springblade.payment.handle.handler;
 
-import cn.hutool.core.convert.Convert;
 import com.alibaba.fastjson.JSON;
 import lombok.AllArgsConstructor;
 import org.springblade.common.enums.PaymentScene;
-import org.springblade.common.enums.PaymentType;
 import org.springblade.common.enums.ResCode;
-import org.springblade.common.enums.SystemConstant;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.ldt.bills.service.IBillsService;
 import org.springblade.payment.handle.entity.HandleData;
 import org.springblade.payment.handle.entity.Order;
 import org.springblade.ldt.bills.entity.Bills;
-import org.springblade.ldt.bills.service.IBillsService;
 import org.springblade.payment.entity.SuccessParams;
 import org.springblade.webSocket.WebSocketServer;
 import org.springframework.util.Assert;
@@ -25,7 +22,6 @@ import java.math.BigDecimal;
  */
 @AllArgsConstructor
 public class WxPayHandle implements BaseHandle {
-
 	private IBillsService billsService;
 	private WebSocketServer webSocketServer;
 
@@ -35,25 +31,13 @@ public class WxPayHandle implements BaseHandle {
 	@Override
 	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) throws ServiceException {
 
-		Bills bills = new Bills();
-		bills.setPaystatus(SystemConstant.BillPayStatus.待付款.name());
-		bills.setPayPlugin(PaymentType.YEE_PAY.name());
-		bills.setAppid(order.getAppId());
-		bills.setCost(Convert.toStr(order.getMoney()));
-		bills.setOpenid(order.getOpenId());
-		bills.setChannelId(Convert.toStr(successParams.getChannelId()));
+		Bills bills = successParams.getBills();
 		bills.setPayway(PaymentScene.MINI_PROGRAM.name());
 		bills.setPrice(remain);
-		bills.setTitle(order.getBillsTitle());
-		bills.setPayId(order.getLoginUser().getId());
-		bills.setReceiveId(order.getShopId());
-		Assert.isTrue(billsService.saveOrUpdate(bills),()->{ throw new ServiceException(ResCode.TRADE_ERROR) ;});
 
 		//通知用户调起支付
 		webSocketServer.sendInfo(order.getLoginUser().getId().toString(), JSON.toJSONString(bills));
-
-		successParams.setBillId(bills.getId());
-
+		Assert.isTrue(billsService.saveOrUpdate(bills),()->{ throw new ServiceException(ResCode.INIT_ORDER_FAIL) ;});
 		return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 	}
 }

+ 1 - 1
src/main/java/org/springblade/payment/plugin/YeePayPlugin.java

@@ -83,7 +83,7 @@ public class YeePayPlugin implements Payment {
 		//封装回调参数
 		SuccessParams successParams = SuccessParams
 			.builder()
-			.billId(bill.getId())
+			.bills(bill)
 			.orderType(OrderType.valueOf(bill.getType()).name())
 			.build();
 

+ 179 - 0
src/main/java/org/springblade/point/listener/SendPointHandle.java

@@ -0,0 +1,179 @@
+package org.springblade.point.listener;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import lombok.extern.slf4j.Slf4j;
+import org.springblade.common.enums.OrderType;
+import org.springblade.common.enums.ResCode;
+import org.springblade.common.enums.SystemConstant;
+import org.springblade.core.log.exception.ServiceException;
+import org.springblade.gateway.common_gateway.entity.BillSuccessEvent;
+import org.springblade.ldt.bills.entity.BalanceBills;
+import org.springblade.ldt.bills.entity.Bills;
+import org.springblade.ldt.bills.entity.PointBills;
+import org.springblade.ldt.bills.service.IBalanceBillsService;
+import org.springblade.ldt.bills.service.IBillsService;
+import org.springblade.ldt.bills.service.IPointBillsService;
+import org.springblade.ldt.mall.entity.Mall;
+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.SuccessParams;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
+
+import java.math.BigDecimal;
+import java.util.Objects;
+
+/**
+ * @author: lianghanqiang
+ * @description: 交易成功后,商家以及商户赠送相应积分给用户
+ * @since: 8/31/21 -- 9:57 AM
+ */
+@Component
+@Slf4j
+public class SendPointHandle  {
+	private ILoginUserService loginUserService;
+	private IBalanceBillsService balanceBillsService;
+	private IShopService shopService;
+	private IMallService mallService;
+	private IPointBillsService pointBillsService;
+	private IUserChannelPointService userChannelPointService;
+	private IBillsService billsService;
+	
+	@EventListener
+	@Transactional
+	@Async
+	public void handleSendPoint(BillSuccessEvent billSuccessEvent){
+		SuccessParams successParams = billSuccessEvent.getSuccessParams();
+		Bills bill = successParams.getBills();
+		LoginUser user = loginUserService.getById(successParams.getUserId());
+		Assert.isTrue(sendPoint(bill,user),() -> {throw new ServiceException(ResCode.SEND_POINT_ERROR);});
+	}
+
+	/**
+	 * 	商家赠送积分
+	 * */
+	public boolean shopSendPoint(Shop shop, LoginUser user, BigDecimal payNum){
+		//商家若是开启了会员中心则赠送积分
+		if(shop.getIsOpenMember()!=0){
+			BalanceBills sendBill = new BalanceBills();
+			sendBill.setType(OrderType.SHOP_SEND.name());
+			sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
+			sendBill.setTitle("商家赠送积分");
+			sendBill.setReceiveId(user.getId());
+			sendBill.setPayId(shop.getId());
+
+			if(!Objects.isNull(shop)){
+				BigDecimal balance = shop.getBalance();
+				BigDecimal pointRate = shop.getPointRate();
+				BigDecimal sendNum = payNum.multiply(pointRate).multiply(shop.getSendPointUnit());
+
+
+				if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
+					sendBill.setBillDesc("账户余额不足或赠送积分值小于1");
+				}else{
+					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);});
+					Assert.isTrue(shopService.saveOrUpdate(shop),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+				}
+
+			}else{
+				sendBill.setBillDesc("商家不存在");
+			}
+			Assert.isTrue(balanceBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+		}
+		return true;
+	}
+
+	/**
+	 * 	商场赠送积分
+	 * */
+	public boolean mallSendPoint(Mall mall, LoginUser user, BigDecimal payNum){
+		//商场若是开启了会员中心则赠送积分
+		if(mall.getUseMember()!=0){
+			PointBills sendBill = new PointBills();
+			sendBill.setType(OrderType.MALL_SEND.name());
+			sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
+			sendBill.setTitle("商场赠送积分");
+			sendBill.setReceiveId(user.getId());
+			sendBill.setPayId(mall.getId());
+
+			if(!Objects.isNull(mall)){
+				BigDecimal balance = mall.getBalance();
+				BigDecimal pointRate = mall.getPointRate();
+				BigDecimal sendNum = payNum.multiply(pointRate).multiply(mall.getSendPointUnnit());
+
+				if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
+					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.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);});
+			Assert.isTrue(mallService.saveOrUpdate(mall),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
+		}
+		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);
+	}
+
+	/**
+	 * 商场、商家赠送用户积分
+	 **/
+	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());
+
+			mallSendPoint(mall,user,payNum);
+			shopSendPoint(shop,user,payNum);
+		}
+		return true;
+	}
+}