Bläddra i källkod

Merge remote-tracking branch 'origin/master'

hmp 4 år sedan
förälder
incheckning
c89361e36b
21 ändrade filer med 237 tillägg och 115 borttagningar
  1. 1 0
      src/main/java/org/springblade/common/enums/ResCode.java
  2. 10 7
      src/main/java/org/springblade/gateway/shop_gateway/controller/ShopTradeController.java
  3. 2 0
      src/main/java/org/springblade/gateway/shop_gateway/entity/dto/BaseOrder.java
  4. 3 0
      src/main/java/org/springblade/gateway/shop_gateway/entity/dto/CodeOrder.java
  5. 1 1
      src/main/java/org/springblade/ldt/bills/entity/BalanceBills.java
  6. 10 5
      src/main/java/org/springblade/ldt/bills/entity/Bills.java
  7. 2 2
      src/main/java/org/springblade/ldt/bills/entity/PointBills.java
  8. 1 1
      src/main/java/org/springblade/ldt/bills/mapper/BalanceBillsMapper.xml
  9. 2 1
      src/main/java/org/springblade/ldt/bills/mapper/BillsMapper.xml
  10. 2 2
      src/main/java/org/springblade/ldt/bills/mapper/PointBillsMapper.xml
  11. 1 1
      src/main/java/org/springblade/ldt/shop/entity/Shop.java
  12. 1 1
      src/main/java/org/springblade/payment/callback/AgentChargeCallback.java
  13. 105 43
      src/main/java/org/springblade/payment/callback/UserPayCallback.java
  14. 16 0
      src/main/java/org/springblade/payment/entity/PaymentVO.java
  15. 11 1
      src/main/java/org/springblade/payment/entity/SuccessParams.java
  16. 28 19
      src/main/java/org/springblade/payment/handle/Trade.java
  17. 1 1
      src/main/java/org/springblade/payment/handle/handler/BalanceHandle.java
  18. 2 2
      src/main/java/org/springblade/payment/handle/handler/BaseHandle.java
  19. 15 8
      src/main/java/org/springblade/payment/handle/handler/ChannelPointHandle.java
  20. 20 18
      src/main/java/org/springblade/payment/handle/handler/DiscountHandle.java
  21. 3 2
      src/main/java/org/springblade/payment/handle/handler/WxPayHandle.java

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

@@ -18,6 +18,7 @@ public enum ResCode implements IResultCode {
 	PAY_TYPE_ERROR(501,"支付方式错误"),
 	PAY_SCENE_ERROR(501,"支付场景错误"),
 	TRADE_ERROR(501,"交易异常"),
+	CHANNEL_NOT_FOUND(501,"积分渠道查找失败!"),
 	USER_PAY_CALLBACK_ERROR(501,"用户支付回调异常"),
 
 

+ 10 - 7
src/main/java/org/springblade/gateway/shop_gateway/controller/ShopTradeController.java

@@ -41,10 +41,11 @@ public class ShopTradeController {
 
 		//解析校验授权码
 		Map<String, String> decodeParams = OtpUtils.decodeParams(codeOrder.getAuthCode());
-		Assert.isTrue(OtpUtils.validate(decodeParams.get(OtpUtils.PARAMS_ID),decodeParams.get(OtpUtils.PARAMS_SECRET)),
-			()->{ throw new ServiceException(ResCode.SECRET_VALIDATE_ERROR); });
 
-		LoginUser loginUser = loginUserService.getById(codeOrder.getLoginUserId());
+//		Assert.isTrue(OtpUtils.validate(decodeParams.get(OtpUtils.PARAMS_ID),decodeParams.get(OtpUtils.PARAMS_SECRET)),
+//			()->{ throw new ServiceException(ResCode.SECRET_VALIDATE_ERROR); });
+
+		LoginUser loginUser = loginUserService.getById(decodeParams.get(OtpUtils.PARAMS_ID));
 		Assert.notNull(loginUser,() -> {throw  new ServiceException(ResCode.USER_NOT_FOUNT);});
 
 		//处理订单
@@ -59,10 +60,12 @@ public class ShopTradeController {
 			.openId(codeOrder.getOpenId())
 			.build();
 
-		try{
-			trade.trade(order);
-		}catch (Exception e){
-			new ServiceException(ResCode.TRADE_ERROR);
+
+		try {
+			trade.trade(order,codeOrder.getChannelId());
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw  new ServiceException(ResCode.TRADE_ERROR);
 		}
 
 		return R.success("交易成功!");

+ 2 - 0
src/main/java/org/springblade/gateway/shop_gateway/entity/dto/BaseOrder.java

@@ -1,5 +1,6 @@
 package org.springblade.gateway.shop_gateway.entity.dto;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -32,6 +33,7 @@ public class BaseOrder {
 	@ApiModelProperty("支付标题")
 	private String billsTitle;
 
+	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
 	@ApiModelProperty("expireTime")
 	private DateTime expireTime;
 

+ 3 - 0
src/main/java/org/springblade/gateway/shop_gateway/entity/dto/CodeOrder.java

@@ -21,4 +21,7 @@ public class CodeOrder extends BaseOrder {
 
 	@ApiModelProperty("授权码")
 	private String authCode;
+	
+	@ApiModelProperty("积分渠道ID")
+	private long channelId;
 }

+ 1 - 1
src/main/java/org/springblade/ldt/bills/entity/BalanceBills.java

@@ -67,7 +67,7 @@ public class BalanceBills implements Serializable {
 	* 交易说明
 	*/
 		@ApiModelProperty(value = "交易说明")
-		private String desc;
+		private String billDesc;
 	/**
 	* 手续费
 	*/

+ 10 - 5
src/main/java/org/springblade/ldt/bills/entity/Bills.java

@@ -79,14 +79,14 @@ public class Bills implements Serializable {
 		@ApiModelProperty(value = "付款方式")
 		private String payway;
 	/**
-	* 付款渠道
+	* 付款插件
 	*/
-		@ApiModelProperty(value = "付款渠道")
-		private String channel;
+		@ApiModelProperty(value = "付款插件")
+		private String payPlugin;
 	/**
-	* 渠道订单号
+	* 渠道积分id
 	*/
-		@ApiModelProperty(value = "渠道订单号")
+		@ApiModelProperty(value = "渠道积分id")
 		private String channelId;
 	/**
 	* 待付款,付款成功,取消付款
@@ -128,6 +128,11 @@ public class Bills implements Serializable {
 	*/
 		@ApiModelProperty(value = "消耗余额")
 		private BigDecimal balanceNum;
+	/**
+	* 第三方平台订单号
+	*/
+		@ApiModelProperty(value = "第三方平台订单号")
+		private String thirdOrderId;
 
 
 }

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

@@ -67,7 +67,7 @@ public class PointBills implements Serializable {
 	* 交易说明
 	*/
 		@ApiModelProperty(value = "交易说明")
-		private String desc;
+		private String billsDesc;
 	/**
 	* 手续费
 	*/
@@ -97,7 +97,7 @@ public class PointBills implements Serializable {
 	* 渠道logo
 	*/
 		@ApiModelProperty(value = "渠道logo")
-		private String channelLog;
+		private String channelLogo;
 	/**
 	* 渠道名称
 	*/

+ 1 - 1
src/main/java/org/springblade/ldt/bills/mapper/BalanceBillsMapper.xml

@@ -9,7 +9,7 @@
         <result column="pay_id" property="payId"/>
         <result column="title" property="title"/>
         <result column="create_time" property="createTime"/>
-        <result column="desc" property="desc"/>
+        <result column="bill_desc" property="billDesc"/>
         <result column="fee" property="fee"/>
         <result column="receive_id" property="receiveId"/>
         <result column="status" property="status"/>

+ 2 - 1
src/main/java/org/springblade/ldt/bills/mapper/BillsMapper.xml

@@ -12,7 +12,7 @@
         <result column="price" property="price"/>
         <result column="type" property="type"/>
         <result column="payway" property="payway"/>
-        <result column="channel" property="channel"/>
+        <result column="pay_plugin" property="payPlugin"/>
         <result column="channel_id" property="channelId"/>
         <result column="paystatus" property="paystatus"/>
         <result column="title" property="title"/>
@@ -22,6 +22,7 @@
         <result column="openid" property="openid"/>
         <result column="point_num" property="pointNum"/>
         <result column="balance_num" property="balanceNum"/>
+        <result column="third_order_id" property="thirdOrderId"/>
     </resultMap>
 
 

+ 2 - 2
src/main/java/org/springblade/ldt/bills/mapper/PointBillsMapper.xml

@@ -9,13 +9,13 @@
         <result column="pay_id" property="payId"/>
         <result column="title" property="title"/>
         <result column="create_time" property="createTime"/>
-        <result column="desc" property="desc"/>
+        <result column="bills_desc" property="billsDesc"/>
         <result column="fee" property="fee"/>
         <result column="receive_id" property="receiveId"/>
         <result column="status" property="status"/>
         <result column="type" property="type"/>
         <result column="channel_id" property="channelId"/>
-        <result column="channel_log" property="channelLog"/>
+        <result column="channel_logo" property="channelLogo"/>
         <result column="channel_name" property="channelName"/>
         <result column="is_cheak" property="isCheak"/>
     </resultMap>

+ 1 - 1
src/main/java/org/springblade/ldt/shop/entity/Shop.java

@@ -176,7 +176,7 @@ public class Shop implements Serializable {
 	* 账单应收
 	*/
 		@ApiModelProperty(value = "账单应收")
-		private BigDecimal charge;
+			private BigDecimal charge;
 	/**
 	* 经度
 	*/

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

@@ -46,7 +46,7 @@ public class AgentChargeCallback {
 		//获取订单信息
 		Bills bills = billsService.getById(successParams.getBillId());
 		Assert.notNull(bills, "支付记录中无此订单");
-		bills.setChannelId(successParams.getChannelId());
+		bills.setChannelId(successParams.getThirdOrderId());
 
 		//判断订单状态,已付款订单处理 (单笔支付可能接收多笔回调)
 		if (Objects.equals(bills.getPaystatus(), SystemConstant.BillPayStatus.付款成功.name())) {

+ 105 - 43
src/main/java/org/springblade/payment/callback/UserPayCallback.java

@@ -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);
+	}
 }

+ 16 - 0
src/main/java/org/springblade/payment/entity/PaymentVO.java

@@ -0,0 +1,16 @@
+package org.springblade.payment.entity;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author: lianghanqiang
+ * @description:
+ * @since: 8/30/21 -- 8:47 PM
+ */
+@Data
+@Builder
+public class PaymentVO {
+	Boolean isSuccess;
+	String msg;
+}

+ 11 - 1
src/main/java/org/springblade/payment/entity/SuccessParams.java

@@ -54,10 +54,20 @@ public class SuccessParams {
 	/**
 	 * 第三方平台交易订单号
 	 * */
-	private String channelId;
+	private String thirdOrderId;
 
 	/**
 	 * 	交易用户id
 	 * */
 	private Long userId;
+
+	/**
+	 * 积分渠道id
+	 * */
+	private Long channelId;
+
+	/**
+	 * 	商户id
+	 * */
+	private Long shopId;
 }

+ 28 - 19
src/main/java/org/springblade/payment/handle/Trade.java

@@ -5,6 +5,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.ldt.user.service.IUserChannelPointService;
 import org.springblade.payment.handle.entity.HandleData;
 import org.springblade.payment.handle.entity.Order;
 import org.springblade.payment.handle.handler.*;
@@ -17,6 +18,7 @@ import org.springblade.ldt.shop.service.IShopService;
 import org.springblade.payment.entity.SuccessParams;
 import org.springblade.payment.service.IPaymentService;
 import org.springblade.webSocket.WebSocketServer;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
@@ -31,37 +33,42 @@ import java.util.List;
  * @since: 8/26/21 -- 5:53 PM
  */
 @Component
-@AllArgsConstructor
 public class Trade {
-
+	@Autowired
 	private IBillsService billsService;
+	@Autowired
 	private IPointBillsService pointBillsService;
+	@Autowired
 	private IBalanceBillsService balanceBillsService;
+	@Autowired
 	private WebSocketServer webSocketServer;
+	@Autowired
 	private IPaymentService paymentService;
+	@Autowired
 	private IShopService shopService;
+	@Autowired
 	private IActivityService activityService;
+	@Autowired
 	private IJoinRecordService joinRecordService;
+	@Autowired
+	private IUserChannelPointService userChannelPointService ;
 
-	/**
-	 * 	交易处理链,每个handle处理负责一个业务节点
-	 * 	1、处理各种商家折扣,计算实际交易金额
-	 * 	2、用户渠道积分用以抵消交易金额
-	 * 	3、用户账户余额抵消交易金额
-	 * 	4、剩余金额用户微信支付
-	 * */
-	private  List<BaseHandle> chain ;
-
-	 {
-	 	 chain = new ArrayList<>();
-	 	 chain.add(new DiscountHandle(shopService,activityService,joinRecordService));
-		 chain.add(new ChannelPointHandle(pointBillsService));
-		 chain.add(new BalanceHandle(balanceBillsService));
-		 chain.add(new WxPayHandle(billsService,webSocketServer));
-	}
 
 	@Transactional
-	public boolean trade(Order order){
+	public boolean trade(Order order, long channelId) throws Exception {
+		/**
+		 * 	交易处理链,每个handle处理负责一个业务节点
+		 * 	1、处理各种商家折扣,计算实际交易金额
+		 * 	2、用户渠道积分用以抵消交易金额
+		 * 	3、用户账户余额抵消交易金额
+		 * 	4、剩余金额用户微信支付
+		 * */
+		List<BaseHandle> chain = new ArrayList(){{
+			add(new DiscountHandle(shopService,activityService,joinRecordService));
+			add(new ChannelPointHandle(pointBillsService,userChannelPointService));
+			add(new BalanceHandle(balanceBillsService));
+			add(new WxPayHandle(billsService,webSocketServer));
+		}} ;
 
 	 	BigDecimal remain = order.getMoney();
 
@@ -71,6 +78,8 @@ public class Trade {
 			.status(SystemConstant.BillPayStatus.待付款.name())
 			.userId(order.getLoginUser().getId())
 			.totalPrice(order.getMoney())
+			.shopId(order.getShopId())
+			.channelId(channelId)
 			.build();
 
 		//处理各个节点

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

@@ -30,7 +30,7 @@ public class BalanceHandle implements BaseHandle {
 	 * 	不足抵消,remain将会流转到下一个节点处理
 	 * */
 	@Override
-	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) {
+	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) throws ServiceException {
 		LoginUser user = order.getLoginUser();
 
 

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

@@ -11,7 +11,7 @@ import java.math.BigDecimal;
  * @description:
  * @since: 8/26/21 -- 5:52 PM
  */
-public interface BaseHandle {
+public interface BaseHandle  {
 
-	HandleData handle(BigDecimal remain, Order order, SuccessParams successParams);
+	HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) throws Exception;
 }

+ 15 - 8
src/main/java/org/springblade/payment/handle/handler/ChannelPointHandle.java

@@ -5,6 +5,8 @@ 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.ldt.user.entity.UserChannelPoint;
+import org.springblade.ldt.user.service.IUserChannelPointService;
 import org.springblade.payment.handle.entity.HandleData;
 import org.springblade.payment.handle.entity.Order;
 import org.springblade.ldt.bills.entity.PointBills;
@@ -14,6 +16,7 @@ import org.springblade.payment.entity.SuccessParams;
 import org.springframework.util.Assert;
 
 import java.math.BigDecimal;
+import java.util.Objects;
 
 
 /**
@@ -22,28 +25,29 @@ import java.math.BigDecimal;
  * @since: 8/26/21 -- 1:51 PM
  */
 @AllArgsConstructor
-public class ChannelPointHandle implements BaseHandle {
+public class ChannelPointHandle implements BaseHandle  {
 
 	private IPointBillsService pointBillsService;
+	private IUserChannelPointService userChannelPointService;
 
 	/**
 	 * 	抵消用户的渠道积分,积分足够抵消交易金额,剩余支付金额remain为0,交易结束
 	 * 	不足抵消,remain将会流转到下一个节点处理
 	 * */
 	@Override
-	public HandleData handle(BigDecimal remain, Order order,SuccessParams successParams) {
+	public HandleData handle(BigDecimal remain, Order order,SuccessParams successParams) throws ServiceException {
 
 		LoginUser user = order.getLoginUser();
+		UserChannelPoint channelPoint = userChannelPointService.getById(successParams.getChannelId());
 
-
-		//用户渠道积分为0
-		if(user.getChannelPoint().compareTo(BigDecimal.ZERO)<=0){
+		//用户渠道积分为0或者用户渠道积分查找失败
+		if( Objects.isNull(channelPoint) || channelPoint.getAvailable().compareTo(BigDecimal.ZERO)<=0){
 			return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
 		}
 
 		//积分抵消情况
 		BigDecimal handlePrice = order.getMoney();
-		remain = user.getChannelPoint().subtract(remain);
+		remain = channelPoint.getAvailable().subtract(remain);
 		if(remain.compareTo(BigDecimal.ZERO)>=0){
 			remain =BigDecimal.ZERO;
 		}else{
@@ -51,7 +55,7 @@ public class ChannelPointHandle implements BaseHandle {
 			handlePrice = user.getChannelPoint();
 		}
 
-		PointBills pointBills = addBillRecord(order, user, handlePrice);
+		PointBills pointBills = addBillRecord(order, user, handlePrice,channelPoint);
 		successParams.setPointBillsId(pointBills.getId());
 
 		return HandleData.builder().isSuccess(Boolean.TRUE).successParams(successParams).remain(remain).build();
@@ -61,7 +65,7 @@ public class ChannelPointHandle implements BaseHandle {
 	 * 	添加积分抵消记录
 	 *
 	 * @return*/
-	private PointBills addBillRecord(Order order, LoginUser user, BigDecimal price) {
+	private PointBills addBillRecord(Order order, LoginUser user, BigDecimal price,UserChannelPoint userChannelPoint) {
 		PointBills pointBills = new PointBills();
 		pointBills.setType(OrderType.PAY_CONSUMER.name());
 		pointBills.setPayId(user.getId());
@@ -69,6 +73,9 @@ public class ChannelPointHandle implements BaseHandle {
 		pointBills.setStatus(SystemConstant.BillPayStatus.付款成功.name());
 		pointBills.setTitle("商家扫码收款");
 		pointBills.setPrice(price);
+		pointBills.setChannelId(userChannelPoint.getId());
+		pointBills.setChannelName(userChannelPoint.getChannelName());
+		pointBills.setChannelLogo(userChannelPoint.getChannelLogo());
 		Assert.isTrue(pointBillsService.save(pointBills),()->{throw new ServiceException(ResCode.TRADE_ERROR);});
 		return pointBills;
 	}

+ 20 - 18
src/main/java/org/springblade/payment/handle/handler/DiscountHandle.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.AllArgsConstructor;
 import org.springblade.common.cache.PlatformCache;
 import org.springblade.common.config.entity.PlatformConfig;
+import org.springblade.core.log.exception.ServiceException;
 import org.springblade.payment.handle.entity.HandleData;
 import org.springblade.payment.handle.entity.Order;
 import org.springblade.ldt.activity.entity.Activity;
@@ -16,6 +17,7 @@ import org.springblade.payment.entity.SuccessParams;
 
 import java.math.BigDecimal;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author: lianghanqiang
@@ -30,25 +32,28 @@ public class DiscountHandle implements BaseHandle {
 	private IJoinRecordService joinRecordService;
 
 	@Override
-	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) {
+	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) throws ServiceException {
 		PlatformConfig platformConfig = PlatformCache.platFormConfig();
 
 		//获取相关商家、活动、参与情况
 		Shop shop = shopService.getById(order.getShopId());
-		Activity activity = activityService.getById(shop.getActivityId());
 		JoinRecord joinRecord = activityJoin(shop.getId());
 
+		//判断商家是否参与了活动,参与活动计算相应的活动折扣
+		if(!Objects.isNull(joinRecord)){
+			Activity activity  = activityService.getById(joinRecord.getActivityId());
 
-		//国信服务费
-		BigDecimal serviceFee = BigDecimal.valueOf(platformConfig.getServiceFee()).add(BigDecimal.ONE);
-		//商场活动折扣
-		BigDecimal mallDiscount = activity.getDiscount();
-		//商家折扣
-		BigDecimal shopDiscount = joinRecord.getDiscount();
+			//国信服务费
+			BigDecimal serviceFee = BigDecimal.valueOf(platformConfig.getServiceFee()).add(BigDecimal.ONE);
+			//商场活动折扣
+			BigDecimal mallDiscount =  activity.getDiscount();
+			//商家折扣
+			BigDecimal shopDiscount = joinRecord.getDiscount();
 
-		remain =  remain.multiply(mallDiscount)
-			.multiply(shopDiscount)
-			.multiply(serviceFee);
+			remain =  remain.multiply(mallDiscount)
+				.multiply(shopDiscount)
+				.multiply(serviceFee);
+		}
 
 		return HandleData.builder().successParams(successParams).remain(remain).isSuccess(Boolean.TRUE).build();
 	}
@@ -57,13 +62,10 @@ public class DiscountHandle implements BaseHandle {
 	 * 	获取参与活动中,折扣最小的
 	 * */
 	private JoinRecord activityJoin(Long id) {
-		List<JoinRecord> joinList = joinRecordService.list(new QueryWrapper<JoinRecord>().lambda().eq(JoinRecord::getJoinId, id));
-		JoinRecord joinRecord = joinList.get(0);
-		for (JoinRecord join:joinList) {
-			if(join.getDiscount().compareTo(joinRecord.getDiscount()) <0 ){
-				joinRecord=join;
-			}
-		}
+		JoinRecord joinRecord = joinRecordService.getOne(new QueryWrapper<JoinRecord>()
+			.lambda()
+			.eq(JoinRecord::getJoinId, id).orderByDesc(JoinRecord::getDiscount)
+			.last("limit 1"));
 		return joinRecord;
 	}
 }

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

@@ -33,14 +33,15 @@ public class WxPayHandle implements BaseHandle {
 	 * 	未抵消的余额,发送订单信息到用户,用户根据订单信息调起微信支付
 	 * */
 	@Override
-	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) {
+	public HandleData handle(BigDecimal remain, Order order, SuccessParams successParams) throws ServiceException {
 
 		Bills bills = new Bills();
 		bills.setPaystatus(SystemConstant.BillPayStatus.待付款.name());
-		bills.setChannel(PaymentType.YEE_PAY.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.setPayway(PaymentScene.MINI_PROGRAM.name());
 		bills.setPrice(remain);
 		bills.setTitle(order.getBillsTitle());