diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/NotifyController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/NotifyController.java index 151bcbd..2368162 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/NotifyController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/NotifyController.java @@ -1,11 +1,14 @@ package com.chaozhanggui.system.cashierservice.controller; +import cn.hutool.core.map.MapProxy; import cn.hutool.core.util.ObjectUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.chaozhanggui.system.cashierservice.entity.TbPointsExchangeRecord; import com.chaozhanggui.system.cashierservice.interceptor.RequestWrapper; import com.chaozhanggui.system.cashierservice.service.PayService; +import com.chaozhanggui.system.cashierservice.service.TbPointsExchangeRecordService; import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.util.DateUtils; import lombok.extern.slf4j.Slf4j; @@ -24,10 +27,12 @@ import java.util.Map; public class NotifyController { - @Autowired PayService payService; + @Autowired + private TbPointsExchangeRecordService tbPointsExchangeRecordService; + @RequestMapping("memberInCallBack") public String memberInCallBack(HttpServletRequest request) { @@ -47,20 +52,20 @@ public class NotifyController { @RequestMapping("fstmemberInCallBack") public String fstmemberInCallBack(HttpServletRequest request) throws Exception { - Map map= getParameterMap(request); - log.info("fstmemberInCallBack回调返回信息:{}",JSONUtil.toJsonStr(map)); - if(ObjectUtil.isNotEmpty(map)&&map.containsKey("code")&&"000000".equals(map.get("code")+"")){ + Map map = getParameterMap(request); + log.info("fstmemberInCallBack回调返回信息:{}", JSONUtil.toJsonStr(map)); + if (ObjectUtil.isNotEmpty(map) && map.containsKey("code") && "000000".equals(map.get("code") + "")) { // Map object=(Map)map.get("bizData"); - JSONObject object=JSONUtil.parseObj(map.get("bizData").toString()); - if(ObjectUtil.isNotEmpty(object)&&object.containsKey("state")){ - if("TRADE_SUCCESS".equals(object.get("state").toString())){ - String orderNo=object.get("mchOrderNo").toString(); - String tradeNo=object.get("payOrderId").toString(); - String payType=object.getStr("payType"); - return payService.fstMemberInSuccess(orderNo,tradeNo, payType); - } + JSONObject object = JSONUtil.parseObj(map.get("bizData").toString()); + if (ObjectUtil.isNotEmpty(object) && object.containsKey("state")) { + if ("TRADE_SUCCESS".equals(object.get("state").toString())) { + String orderNo = object.get("mchOrderNo").toString(); + String tradeNo = object.get("payOrderId").toString(); + String payType = object.getStr("payType"); + return payService.fstMemberInSuccess(orderNo, tradeNo, payType); } + } } return null; @@ -68,18 +73,18 @@ public class NotifyController { } @RequestMapping("notifyCallBack") - public String notifyCallBack(HttpServletRequest request){ + public String notifyCallBack(HttpServletRequest request) { - Map map= getParameterMap(request); - log.info("回调返回信息:{}",JSONUtil.toJsonStr(map)); - if(ObjectUtil.isNotEmpty(map)&&map.containsKey("code")&&"200".equals(map.get("code")+"")){ - JSONObject object=JSONUtil.parseObj(map.get("data")); - if(ObjectUtil.isNotEmpty(object) + Map map = getParameterMap(request); + log.info("回调返回信息:{}", JSONUtil.toJsonStr(map)); + if (ObjectUtil.isNotEmpty(map) && map.containsKey("code") && "200".equals(map.get("code") + "")) { + JSONObject object = JSONUtil.parseObj(map.get("data")); + if (ObjectUtil.isNotEmpty(object) && object.containsKey("status") && "1".equals(object.getStr("status")) && !"0100".equals(object.getStr("payType")) - ){ - String orderNo=object.getStr("orderNumber"); + ) { + String orderNo = object.getStr("orderNumber"); return payService.callBackPay(orderNo); } @@ -90,6 +95,7 @@ public class NotifyController { /** * 支付取消 + * * @return 影响数量 */ @PostMapping("cancel") @@ -99,21 +105,21 @@ public class NotifyController { @RequestMapping("notifyfstCallBack") - public String notifyfstCallBack(HttpServletRequest request){ + public String notifyfstCallBack(HttpServletRequest request) { - Map map= getParameterMap(request); - log.info("notifyfstCallBack回调返回信息:{}",JSONUtil.toJsonStr(map)); - if(ObjectUtil.isNotEmpty(map)&&map.containsKey("code")&&"000000".equals(map.get("code")+"")){ + Map map = getParameterMap(request); + log.info("notifyfstCallBack回调返回信息:{}", JSONUtil.toJsonStr(map)); + if (ObjectUtil.isNotEmpty(map) && map.containsKey("code") && "000000".equals(map.get("code") + "")) { - JSONObject object=JSONUtil.parseObj(map.get("bizData").toString()); - if(ObjectUtil.isNotEmpty(object)&&object.containsKey("state")){ - if("TRADE_SUCCESS".equals(object.get("state").toString())){ - String orderNo=object.get("mchOrderNo").toString(); - String tradeNo=object.get("payOrderId").toString(); - String payType=object.getStr("payType"); - return payService.callBackPayFST(tradeNo,payType); - }else { - String tradeNo=object.get("payOrderId").toString(); + JSONObject object = JSONUtil.parseObj(map.get("bizData").toString()); + if (ObjectUtil.isNotEmpty(object) && object.containsKey("state")) { + if ("TRADE_SUCCESS".equals(object.get("state").toString())) { + String orderNo = object.get("mchOrderNo").toString(); + String tradeNo = object.get("payOrderId").toString(); + String payType = object.getStr("payType"); + return payService.callBackPayFST(tradeNo, payType); + } else { + String tradeNo = object.get("payOrderId").toString(); return String.valueOf(payService.activateOrder(tradeNo)); } } @@ -123,15 +129,15 @@ public class NotifyController { } @RequestMapping("notifyCallBackGroupYsk") - public String notifyCallBackGroupYsk(HttpServletRequest request){ - Map map= getParameterMap(request); - log.info("团购卷回调返回信息:{}",JSONUtil.toJsonStr(map)); - if(ObjectUtil.isNotEmpty(map)&&map.containsKey("code")&&"200".equals(map.get("code")+"")){ - JSONObject object=JSONUtil.parseObj(map.get("data")); - if(ObjectUtil.isNotEmpty(object)&&object.containsKey("status")&&"1".equals(object.getStr("status"))){ - String orderNo=object.getStr("orderNumber"); + public String notifyCallBackGroupYsk(HttpServletRequest request) { + Map map = getParameterMap(request); + log.info("团购卷回调返回信息:{}", JSONUtil.toJsonStr(map)); + if (ObjectUtil.isNotEmpty(map) && map.containsKey("code") && "200".equals(map.get("code") + "")) { + JSONObject object = JSONUtil.parseObj(map.get("data")); + if (ObjectUtil.isNotEmpty(object) && object.containsKey("status") && "1".equals(object.getStr("status"))) { + String orderNo = object.getStr("orderNumber"); String payTime = object.getStr("payTime"); - return payService.callBackGroupPay(orderNo,payTime); + return payService.callBackGroupPay(orderNo, payTime); } } return null; @@ -153,6 +159,7 @@ public class NotifyController { /** * 点歌支付回调 + * * @param request * @return */ @@ -163,7 +170,7 @@ public class NotifyController { if (ObjectUtil.isNotEmpty(map) && map.containsKey("code") && "000000".equals(map.get("code") + "")) { JSONObject object = JSONUtil.parseObj(map.get("bizData")); if (ObjectUtil.isNotEmpty(object) && object.containsKey("state") && "TRADE_SUCCESS".equals(object.getStr("state"))) { - String tradeNo=object.get("payOrderId").toString(); + String tradeNo = object.get("payOrderId").toString(); String orderNo = object.getStr("mchOrderNo"); return payService.songOrderSuccess(orderNo, tradeNo); } @@ -171,13 +178,48 @@ public class NotifyController { return null; } + @RequestMapping("pointsGoodsOrderCallBack") + public String pointsGoodsOrderCallBack(HttpServletRequest request, @RequestBody Map params) { + log.info("积分商城订单支付回调返回信息:{}", JSONUtil.toJsonStr(params)); + //Map params = getParameterMap(request); + log.info("积分商城订单支付回调返回信息:{}", JSONUtil.toJsonStr(params)); + MapProxy proxy = MapProxy.create(params); + String code = proxy.getStr("code"); + String msg = proxy.getStr("msg"); + if (!"000000".equals(code)) { + log.error("支付失败:{}", msg); + return "FAIL"; + } + String bizData = proxy.getStr("bizData"); + JSONObject data = JSONUtil.parseObj(bizData); + String state = data.getStr("state"); + if (!"TRADE_SUCCESS".equals(state)) { + log.error("支付失败:{}", state); + return "FAIL"; + } + String payOrderId = data.getStr("payOrderId"); + String channelTradeNo = data.getStr("channelTradeNo"); + Date payTime = data.getDate("payTime", new Date()); + TbPointsExchangeRecord record = new TbPointsExchangeRecord(); + record.setPayOrderId(payOrderId); + record.setChannelTradeNo(channelTradeNo); + record.setPayTime(payTime); + try { + tbPointsExchangeRecordService.callback(record); + } catch (Exception e) { + log.error("支付失败:{}", e); + return "FAIL"; + } + return "SUCCESS"; + } + private Map getParameterMap(HttpServletRequest request) { RequestWrapper requestWrapper = new RequestWrapper(request); String body = requestWrapper.getBody(); if (ObjectUtil.isNotEmpty(body)) { return JSONUtil.toBean(body, Map.class); - }else { + } else { Map parameterMap = request.getParameterMap(); return parameterMap; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsController.java index 16a7994..99fde14 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsController.java @@ -7,9 +7,9 @@ import com.chaozhanggui.system.cashierservice.service.TbMemberPointsService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Map; @@ -24,7 +24,7 @@ import java.util.Map; @RequestMapping("/api/points/member-points") public class TbMemberPointsController { - @Resource + @Autowired private TbMemberPointsService tbMemberPointsService; /** diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsLogController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsLogController.java index c1dbd79..9fd9e15 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsLogController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbMemberPointsLogController.java @@ -4,12 +4,12 @@ import com.chaozhanggui.system.cashierservice.entity.TbMemberPointsLog; import com.chaozhanggui.system.cashierservice.service.TbMemberPointsLogService; import com.chaozhanggui.system.cashierservice.sign.Result; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import javax.annotation.Resource; import java.util.Map; @@ -23,7 +23,7 @@ import java.util.Map; @RequestMapping("/api/points/member-points-log") public class TbMemberPointsLogController { - @Resource + @Autowired private TbMemberPointsLogService tbMemberPointsLogService; /** diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsBasicSettingController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsBasicSettingController.java index 84639c8..2c4291e 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsBasicSettingController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsBasicSettingController.java @@ -4,10 +4,9 @@ import com.chaozhanggui.system.cashierservice.entity.TbPointsBasicSetting; import com.chaozhanggui.system.cashierservice.service.TbPointsBasicSettingService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; - /** * 积分基本设置 @@ -19,7 +18,7 @@ import javax.annotation.Resource; @RequestMapping("/api/points/basic-setting") public class TbPointsBasicSettingController { - @Resource + @Autowired private TbPointsBasicSettingService tbPointsBasicSettingService; /** diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsExchangeRecordController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsExchangeRecordController.java index 57ade59..1f1881e 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsExchangeRecordController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsExchangeRecordController.java @@ -4,10 +4,12 @@ import com.chaozhanggui.system.cashierservice.entity.TbPointsExchangeRecord; import com.chaozhanggui.system.cashierservice.service.TbPointsExchangeRecordService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; +import com.chaozhanggui.system.cashierservice.util.IpUtil; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import java.util.Map; @@ -21,7 +23,7 @@ import java.util.Map; @RequestMapping("/api/points/exchange-record") public class TbPointsExchangeRecordController { - @Resource + @Autowired private TbPointsExchangeRecordService tbPointsExchangeRecordService; /** @@ -60,18 +62,57 @@ public class TbPointsExchangeRecordController { return Result.success(CodeEnum.SUCCESS); } + /** - * 兑换 + * 创建订单 * * @param record * @return */ - @PostMapping("exchange") - public Result exchange(@RequestBody TbPointsExchangeRecord record) { - TbPointsExchangeRecord data = tbPointsExchangeRecordService.exchange(record); + @PostMapping("create") + public Result create(@RequestBody TbPointsExchangeRecord record) { + TbPointsExchangeRecord data = tbPointsExchangeRecordService.create(record); return Result.successWithData(data); } + /** + * 支付 + * + * @param record + * @return + */ + @PostMapping("pay") + public Result pay(HttpServletRequest request, @RequestBody TbPointsExchangeRecord record) { + String ip = IpUtil.getIpAddr(request); + record.setIp(ip); + TbPointsExchangeRecord data = tbPointsExchangeRecordService.pay(record); + return Result.successWithData(data); + } + + /** + * 取消 + * + * @param record + * @return + */ + @PostMapping("cancel") + public Result cancel(@RequestBody TbPointsExchangeRecord record) { + tbPointsExchangeRecordService.cancel(record); + return Result.success(CodeEnum.SUCCESS); + } + + /** + * 退单 + * + * @param record + * @return + */ + @PostMapping("refund") + public Result exchange(@RequestBody TbPointsExchangeRecord record) { + tbPointsExchangeRecordService.refund(record); + return Result.success(CodeEnum.SUCCESS); + } + /** * 统计 * diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsGoodsSettingController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsGoodsSettingController.java index 0bcb124..e5ab1a5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsGoodsSettingController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/TbPointsGoodsSettingController.java @@ -5,9 +5,9 @@ import com.chaozhanggui.system.cashierservice.service.TbPointsGoodsSettingServic import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; import java.util.Map; @@ -21,7 +21,7 @@ import java.util.Map; @RequestMapping("/api/points/goods-setting") public class TbPointsGoodsSettingController { - @Resource + @Autowired private TbPointsGoodsSettingService tbPointsGoodsSettingService; /** diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPoints.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPoints.java index 27a5356..9e111f3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPoints.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPoints.java @@ -1,6 +1,7 @@ package com.chaozhanggui.system.cashierservice.entity; import com.baomidou.mybatisplus.annotation.*; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; @@ -56,6 +57,7 @@ public class TbMemberPoints { * 最近一次积分变动时间 */ @TableField("last_points_change_time") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date lastPointsChangeTime; /** * 最近一次浮动积分 diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPointsLog.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPointsLog.java index c638cc9..abeddd9 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPointsLog.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMemberPointsLog.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; @@ -64,5 +65,6 @@ public class TbMemberPointsLog { /** * 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsBasicSetting.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsBasicSetting.java index 83fff42..1cf6299 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsBasicSetting.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsBasicSetting.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; @@ -65,5 +66,6 @@ public class TbPointsBasicSetting { /** * 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsExchangeRecord.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsExchangeRecord.java index 9cde1e2..cc6f5b1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsExchangeRecord.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsExchangeRecord.java @@ -1,6 +1,9 @@ package com.chaozhanggui.system.cashierservice.entity; import com.baomidou.mybatisplus.annotation.*; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import lombok.EqualsAndHashCode; @@ -14,84 +17,136 @@ import java.util.Date; * @since 2.0 2024-10-25 */ @Data -@EqualsAndHashCode(callSuper=false) +@EqualsAndHashCode(callSuper = false) @TableName("tb_points_exchange_record") public class TbPointsExchangeRecord { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * id - */ - @TableId(type = IdType.AUTO) - private Long id; - /** - * 订单编号 - */ - private String orderNo; - /** - * 店铺id - */ - private Long shopId; - /** - * 积分商品id - */ - private Long pointsGoodsId; - /** - * 积分商品名称 - */ - private String pointsGoodsName; - /** - * 商品图片URL - */ - private String goodsImageUrl; - /** - * 领取方式 self-自取 post-邮寄 - */ - private String pickupMethod; - /** - * 会员id - */ - private Long memberId; - /** - * 会员名称 - */ - private String memberName; - /** - * 手机号码 - */ - private String mobile; - /** - * 会员头像 - */ - private String avatarUrl; - /** - * 消耗积分 - */ - private Integer spendPoints; - /** - * 额外支付 - */ - private BigDecimal extraPaymentAmount; - /** - * 兑换券券码 - */ - private String couponCode; - /** - * 状态 waiting-待自取 done-已完成 - */ - private String status; - /** - * 创建时间(下单时间) - */ - private Date createTime; - /** - * 更新时间(核销时间) - */ - private Date updateTime; + /** + * id + */ + @TableId(type = IdType.AUTO) + private Long id; + /** + * 订单编号 + */ + private String orderNo; + /** + * 店铺id + */ + private Long shopId; + /** + * 积分商品id + */ + private Long pointsGoodsId; + /** + * 积分商品名称 + */ + private String pointsGoodsName; + /** + * 商品图片URL + */ + private String goodsImageUrl; + /** + * 领取方式 self-自取 post-邮寄 + */ + private String pickupMethod; + /** + * 会员id + */ + private Long memberId; + /** + * 会员名称 + */ + private String memberName; + /** + * 手机号码 + */ + private String mobile; + /** + * 会员头像 + */ + private String avatarUrl; + /** + * 消耗积分 + */ + private Integer spendPoints; + /** + * 额外支付 + */ + private BigDecimal extraPaymentAmount; + /** + * 兑换券券码 + */ + private String couponCode; + /** + * 支付平台订单号 + */ + private String payOrderId; + /** + * 渠道订单号(微信/支付宝订单号) + */ + private String channelTradeNo; + /** + * 支付方式 积分支付/积分+微信/积分+支付宝 + */ + private String payMethod; + /** + * 支付类型 POINTS-积分 WECHAT-微信 ALIPAY-支付宝 UNIONPAY-银联云闪付 + */ + private String payType; + /** + * 状态 unpaid-待支付 waiting-待自取 done-已完成 cancel-已取消 + */ + private String status; + /** + * 取消/退款原因 + */ + private String cancelOrRefundReason; + /** + * 取消/退款时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date cancelOrRefundTime; + /** + * 实际支付时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date payTime; + /** + * 创建时间(下单时间) + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createTime; + /** + * 更新时间(核销时间) + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; - @TableField(value = "count(*)", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER) - private Long count; + @TableField(value = "count(*)", select = false, insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER) + private Long count; - @TableField(value = "sum(extra_payment_amount)", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER) - private BigDecimal totalAmount; + @TableField(value = "sum(extra_payment_amount)", select = false, insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER) + private BigDecimal totalAmount; + + /** + * 用户ip + */ + @JsonIgnore + @TableField(exist = false) + private String ip; + + /** + * 微信openId/支付完userId + */ + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @TableField(exist = false) + private String openId; + + /** + * 拉起支付所需信息 + */ + @TableField(exist = false) + private String payInfo; } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsGoodsSetting.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsGoodsSetting.java index 1c14c39..94adb70 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsGoodsSetting.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPointsGoodsSetting.java @@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.entity; import com.baomidou.mybatisplus.annotation.*; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; @@ -74,10 +75,12 @@ public class TbPointsGoodsSetting { /** * 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; /** * 更新时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date updateTime; /** * 逻辑删除标志 1-是 0-否 diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserInfo.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserInfo.java index 6bb7f5a..455b94e 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserInfo.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserInfo.java @@ -106,6 +106,8 @@ public class TbUserInfo implements Serializable { private Integer couponAll = 0; //储值数量 private BigDecimal balanceAll = BigDecimal.ZERO; + //积分数量 + private Integer pointsAll = 0; public String getAvatar() { @@ -525,4 +527,12 @@ public class TbUserInfo implements Serializable { public void setBalanceAll(BigDecimal balanceAll) { this.balanceAll = balanceAll; } + + public Integer getPointsAll() { + return pointsAll; + } + + public void setPointsAll(Integer pointsAll) { + this.pointsAll = pointsAll; + } } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/mapper/TbMemberPointsMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/mapper/TbMemberPointsMapper.java index 04ab248..9522dc2 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/mapper/TbMemberPointsMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/mapper/TbMemberPointsMapper.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.chaozhanggui.system.cashierservice.entity.TbMemberPoints; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 会员积分 @@ -13,4 +14,6 @@ import org.apache.ibatis.annotations.Mapper; @Mapper public interface TbMemberPointsMapper extends BaseMapper { + Integer totalMemberPointsByUserId(@Param("userId") Integer userId); + } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index 183fd0e..6e5bfb5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -63,6 +63,10 @@ public class LoginService { @Autowired TbUserShopMsgMapper tbUserShopMsgMapper; + + @Autowired + TbMemberPointsService tbMemberPointsService; + private final TbShopOpenIdMapper shopOpenIdMapper; private final static int[] MSG_TYPE_LIST = new int[] {0, 1, 2}; @@ -504,7 +508,7 @@ public class LoginService { } tbUserInfo.setBalanceAll(tbShopUserMapper.countAmount(userId)); tbUserInfo.setCouponAll(inRecordMapper.countCouponNum(userId)); - + tbUserInfo.setPointsAll(tbMemberPointsService.totalMemberPoints(userId)); return Result.success(CodeEnum.ENCRYPT, tbUserInfo); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 8164930..71be3ca 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -470,6 +470,7 @@ public class ProductService { AtomicDouble sum = new AtomicDouble(0.0); BigDecimal lowerPrice = null; + BigDecimal lowMemberPrice = null; for (TbProductSku item : tbProductSkus) { if (item.getRealSalesNumber() != null) { sum.addAndGet(item.getRealSalesNumber()); @@ -477,14 +478,24 @@ public class ProductService { if (lowerPrice == null || lowerPrice.compareTo(item.getSalePrice()) > 0) { lowerPrice = item.getSalePrice(); } + if (lowMemberPrice == null || lowMemberPrice.compareTo(item.getMemberPrice()) > 0) { + lowMemberPrice = item.getMemberPrice(); + } } - //销量 + // 销量 it.setStockNumber(sum.intValue()); + + // 售价 if (lowerPrice == null) { lowerPrice = BigDecimal.ZERO; } - //售价 it.setLowPrice(lowerPrice); + + // 会员价 + if (lowMemberPrice == null) { + lowMemberPrice = BigDecimal.ZERO; + } + it.setLowMemberPrice(lowMemberPrice); it.setProductSkuResult(skuResult); if (isVip) { it.setLowPrice(BigDecimal.ZERO); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/TbMemberPointsService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/TbMemberPointsService.java index cac766d..63d1cc5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/TbMemberPointsService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/TbMemberPointsService.java @@ -32,6 +32,14 @@ public interface TbMemberPointsService extends IService { */ TbMemberPoints getMemberPoints(Long memberId); + /** + * 获取会员平台总积分 + * + * @param userId 用户id + * @return + */ + int totalMemberPoints(Integer userId); + /** * 初始化会员积分 * diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/TbPointsExchangeRecordService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/TbPointsExchangeRecordService.java index f15a22a..0fb64c6 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/TbPointsExchangeRecordService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/TbPointsExchangeRecordService.java @@ -20,7 +20,15 @@ public interface TbPointsExchangeRecordService extends IService total(Map params); } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsLogServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsLogServiceImpl.java index 14284ba..31b4d67 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsLogServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsLogServiceImpl.java @@ -11,6 +11,7 @@ import com.chaozhanggui.system.cashierservice.mapper.TbMemberPointsLogMapper; import com.chaozhanggui.system.cashierservice.service.TbMemberPointsLogService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import java.util.Map; @@ -22,6 +23,7 @@ import java.util.Map; * @since 2.0 2024-10-25 */ @Service +@Primary public class TbMemberPointsLogServiceImpl extends ServiceImpl implements TbMemberPointsLogService { private LambdaQueryWrapper getWrapper(Map params) { @@ -32,6 +34,7 @@ public class TbMemberPointsLogServiceImpl extends ServiceImpl wrapper = Wrappers.lambdaQuery(); wrapper.eq(TbMemberPointsLog::getShopId, param.getShopId()); + wrapper.eq(param.getMemberId() != null, TbMemberPointsLog::getMemberId, param.getMemberId()); wrapper.like(StrUtil.isNotEmpty(param.getMemberName()), TbMemberPointsLog::getMemberName, param.getMemberName()); wrapper.like(StrUtil.isNotEmpty(param.getMobile()), TbMemberPointsLog::getMobile, param.getMobile()); wrapper.like(StrUtil.isNotEmpty(param.getContent()), TbMemberPointsLog::getContent, param.getContent()); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsServiceImpl.java index cc729fd..9bbc188 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbMemberPointsServiceImpl.java @@ -23,11 +23,11 @@ import com.chaozhanggui.system.cashierservice.service.TbMemberPointsService; import com.chaozhanggui.system.cashierservice.service.TbPointsBasicSettingService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Date; @@ -43,15 +43,15 @@ import java.util.Map; @Primary public class TbMemberPointsServiceImpl extends ServiceImpl implements TbMemberPointsService { - @Resource + @Autowired private TbMemberPointsLogMapper tbMemberPointsLogMapper; - @Resource + @Autowired private TbPointsBasicSettingMapper tbPointsBasicSettingMapper; - @Resource + @Autowired private TbShopUserMapper tbShopUserMapper; - @Resource + @Autowired private TbOrderInfoMapper tbOrderInfoMapper; - @Resource + @Autowired private TbPointsBasicSettingService tbPointsBasicSettingService; private LambdaQueryWrapper getWrapper(Map params) { @@ -82,6 +82,12 @@ public class TbMemberPointsServiceImpl extends ServiceImpllambdaQuery().eq(TbMemberPoints::getMemberId, memberId)); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsBasicSettingServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsBasicSettingServiceImpl.java index a6c266b..f0305cb 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsBasicSettingServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsBasicSettingServiceImpl.java @@ -7,6 +7,7 @@ import com.chaozhanggui.system.cashierservice.entity.TbPointsBasicSetting; import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.mapper.TbPointsBasicSettingMapper; import com.chaozhanggui.system.cashierservice.service.TbPointsBasicSettingService; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,6 +20,7 @@ import java.util.Date; * @since 2.0 2024-10-25 */ @Service +@Primary public class TbPointsBasicSettingServiceImpl extends ServiceImpl implements TbPointsBasicSettingService { diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsExchangeRecordServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsExchangeRecordServiceImpl.java index 6a26e6d..eb2d578 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsExchangeRecordServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsExchangeRecordServiceImpl.java @@ -5,22 +5,30 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Snowflake; import cn.hutool.core.map.MapProxy; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.chaozhanggui.system.cashierservice.dao.TbMerchantThirdApplyMapper; import com.chaozhanggui.system.cashierservice.entity.*; import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.mapper.*; import com.chaozhanggui.system.cashierservice.service.TbPointsExchangeRecordService; +import com.chaozhanggui.system.cashierservice.thirdpay.resp.OrderReturnResp; +import com.chaozhanggui.system.cashierservice.thirdpay.resp.PublicResp; +import com.chaozhanggui.system.cashierservice.thirdpay.resp.WxScanPayResp; +import com.chaozhanggui.system.cashierservice.thirdpay.service.ThirdPayService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; @@ -33,20 +41,41 @@ import java.util.Map; * @since 2.0 2024-10-25 */ @Service +@Primary public class TbPointsExchangeRecordServiceImpl extends ServiceImpl implements TbPointsExchangeRecordService { - @Resource + private static final Map payMethod = MapUtil.builder("POINTS", "积分支付") + .put("WECHAT", "积分+微信") + .put("ALIPAY", "积分+支付宝") + .build(); + + @Value("${thirdPay.url}") + private String thirdUrl; + + @Value("${thirdPay.pointsGoodsOrderCallBack}") + private String pointsGoodsOrderCallBack; + + @Autowired private TbPointsBasicSettingMapper tbPointsBasicSettingMapper; - @Resource + @Autowired private TbPointsGoodsSettingMapper tbPointsGoodsSettingMapper; - @Resource + @Autowired private TbMemberPointsMapper tbMemberPointsMapper; - @Resource + @Autowired private TbMemberPointsLogMapper tbMemberPointsLogMapper; + @Autowired + private MpShopInfoMapper mpShopInfoMapper; + + @Autowired + private TbMerchantThirdApplyMapper tbMerchantThirdApplyMapper; + + @Autowired + private ThirdPayService thirdPayService; + private LambdaQueryWrapper getWrapper(Map params) { MapProxy mapProxy = MapProxy.create(params); String keywords = mapProxy.getStr("keywords"); @@ -89,6 +118,7 @@ public class TbPointsExchangeRecordServiceImpl extends ServiceImpllambdaQuery().eq(TbPointsBasicSetting::getShopId, record.getShopId())); if (basic == null) { - throw new MsgException("未配置积分锁客基本设置"); + throw new MsgException("商家未配置积分锁客基本设置"); } if (basic.getEnablePointsMall() != 1) { throw new MsgException("积分商城未开启"); @@ -140,26 +177,183 @@ public class TbPointsExchangeRecordServiceImpl extends ServiceImpllambdaQuery().eq(TbMemberPoints::getMobile, record.getMobile())); + TbMemberPoints memberPoints = tbMemberPointsMapper.selectOne(Wrappers.lambdaQuery().eq(TbMemberPoints::getMemberId, record.getMemberId())); if (memberPoints == null) { - throw new MsgException("该会员积分不足无法兑换这个商品"); + throw new MsgException("会员积分不足无法兑换此商品"); } Integer accountPoints = memberPoints.getAccountPoints(); Integer requiredPoints = goods.getRequiredPoints(); if (accountPoints < requiredPoints) { - throw new MsgException("该会员积分不足无法兑换这个商品"); + throw new MsgException("会员积分不足无法兑换此商品"); } - BigDecimal extraPrice = goods.getExtraPrice(); - record.setExtraPaymentAmount(extraPrice); - record.setSpendPoints(requiredPoints); + return new Object[]{basic, goods, memberPoints}; + } + + @Override + public TbPointsExchangeRecord create(TbPointsExchangeRecord record) { + Object[] verify = verify(record); + TbPointsBasicSetting basic = (TbPointsBasicSetting) verify[0]; + TbPointsGoodsSetting goods = (TbPointsGoodsSetting) verify[1]; + TbMemberPoints memberPoints = (TbMemberPoints) verify[2]; + record.setExtraPaymentAmount(goods.getExtraPrice()); + record.setSpendPoints(goods.getRequiredPoints()); Snowflake seqNo = IdUtil.getSnowflake(0, 0); String orderNo = DateUtil.format(new Date(), "yyyyMMddHH") + StrUtil.subSuf(seqNo.nextIdStr(), -12); record.setOrderNo(orderNo); - record.setCouponCode(IdUtil.getSnowflakeNextIdStr()); - record.setStatus("waiting"); + record.setStatus("unpaid"); record.setCreateTime(new Date()); // 生成订单 super.save(record); + return record; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public TbPointsExchangeRecord pay(TbPointsExchangeRecord record) { + if (record.getId() == null) { + throw new MsgException("订单ID不能为空"); + } + TbPointsExchangeRecord entity = super.getById(record.getId()); + if (entity == null) { + throw new MsgException("订单不存在"); + } + try { + verify(entity); + } catch (Exception e) { + throw new MsgException(e.getMessage().concat(",请您取消订单")); + } + if (!"unpaid".equals(entity.getStatus())) { + throw new MsgException("订单状态异常,请刷新后重试"); + } + if (StrUtil.isBlank(record.getPayType())) { + throw new MsgException("支付方式不能为空"); + } + entity.setPayType(record.getPayType().toUpperCase()); + if (!payMethod.keySet().contains(record.getPayType().toUpperCase())) { + throw new MsgException("支付方式不合法"); + } + // 设置支付方式 + entity.setPayMethod(payMethod.get(record.getPayType().toUpperCase())); + // 纯积分支付并且商品需要额外支付金额,需要抛出异常 + if ("POINTS".equals(entity.getPayType()) && NumberUtil.isGreater(entity.getExtraPaymentAmount(), BigDecimal.ZERO)) { + throw new MsgException("此商品需要额外支付金额"); + } + if (!"POINTS".equals(entity.getPayType()) && NumberUtil.equals(entity.getExtraPaymentAmount(), BigDecimal.ZERO)) { + throw new MsgException("此商品不需要额外支付金额"); + } + // 如果不需要额外支付 + if (NumberUtil.equals(entity.getExtraPaymentAmount(), BigDecimal.ZERO)) { + // 设置核销券码 + entity.setCouponCode(IdUtil.getSnowflakeNextIdStr()); + entity.setStatus("waiting"); + entity.setPayTime(new Date()); + entity.setPayType(record.getPayType().toUpperCase()); + super.updateById(entity); + waitingAfter(entity); + return entity; + } + if (StrUtil.isBlank(record.getOpenId())) { + throw new MsgException("openId/userId不能为空"); + } + if (StrUtil.isBlank(record.getIp())) { + throw new MsgException("无法获取用户ip地址"); + } + // 需要额外支付 + TbShopInfo shopInfo = mpShopInfoMapper.selectById(entity.getShopId()); + if (shopInfo == null) { + throw new MsgException("店铺信息不存在"); + } + TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(shopInfo.getMerchantId())); + if (thirdApply == null) { + throw new MsgException("支付通道不存在"); + } + if ("alipay".equalsIgnoreCase(entity.getPayType()) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) { + throw new MsgException("店铺未配置支付宝小程序appId"); + } + // 准备支付参数 + // 应用ID + String appId = thirdApply.getAppId(); + String appToken = thirdApply.getAppToken(); + String storeId = thirdApply.getStoreId(); + + // 订单标题(商品名称) + String subject = entity.getPointsGoodsName(); + // 商品描述 + String body = StrUtil.format("兑换商品:{} * {}", entity.getPointsGoodsName(), "1"); + // 交易金额 单位分 100 表示1元 + long amount = NumberUtil.mul(entity.getExtraPaymentAmount(), new BigDecimal("100")).longValue(); + // 支付方式 + String payType = entity.getPayType().toUpperCase(); + // 子商户APPID + String subAppid = thirdApply.getSmallAppid(); + if ("alipay".equalsIgnoreCase(entity.getPayType())) { + subAppid = thirdApply.getAlipaySmallAppid(); + } + // 用户唯一标识 微信支付时,传入用户的openId;支付宝支付和银联支付时,传入用户的userId + String openId = record.getOpenId(); + // 用户IP + String clientIp = record.getIp(); + // 商户订单号 mchOrderNo + String mchOrderNo = IdUtil.getSnowflakeNextIdStr(); + // 回调地址 + String notifyUrl = pointsGoodsOrderCallBack; + // 支付成功后的跳转页面 + String returnUrl = null; + PublicResp publicResp; + try { + publicResp = thirdPayService.scanpay(thirdUrl, appId, subject, body, amount, payType, subAppid, openId, clientIp, mchOrderNo, storeId, notifyUrl, returnUrl, appToken); + } catch (Exception e) { + log.error("拉起支付失败:", e); + throw new MsgException(StrUtil.format("拉起支付失败:{}", e.getMessage())); + } + if (publicResp == null) { + throw new MsgException("拉起支付失败:无响应"); + } + if (!"000000".equals(publicResp.getCode())) { + throw new MsgException(publicResp.getMsg()); + } + WxScanPayResp payResp = publicResp.getObjData(); + if (!"TRADE_AWAIT".equals(payResp.getState())) { + throw new MsgException(StrUtil.format("拉起支付失败:{}", payResp.getState())); + } + entity.setPayOrderId(payResp.getPayOrderId()); + super.updateById(entity); + entity.setPayInfo(payResp.getPayInfo()); + return entity; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void callback(TbPointsExchangeRecord record) { + TbPointsExchangeRecord entity = baseMapper.selectOne(Wrappers.lambdaQuery().eq(TbPointsExchangeRecord::getPayOrderId, record.getPayOrderId())); + if (entity == null) { + throw new MsgException("对应的支付订单不存在"); + } + entity.setStatus("waiting"); + entity.setPayTime(record.getPayTime()); + entity.setChannelTradeNo(record.getChannelTradeNo()); + entity.setCouponCode(IdUtil.getSnowflakeNextIdStr()); + super.updateById(entity); + waitingAfter(entity); + } + + /** + * 支付完成的后续操作 + * + * @param entity + */ + private void waitingAfter(TbPointsExchangeRecord entity) { + TbPointsGoodsSetting goods = tbPointsGoodsSettingMapper.selectById(entity.getPointsGoodsId()); + if (goods == null) { + throw new MsgException("积分商品不存在"); + } + TbMemberPoints memberPoints = tbMemberPointsMapper.selectOne(Wrappers.lambdaQuery().eq(TbMemberPoints::getMemberId, entity.getMemberId())); + if (memberPoints == null) { + throw new MsgException("会员积分不足无法兑换此商品"); + } + Integer quantity = goods.getQuantity(); + Integer accountPoints = memberPoints.getAccountPoints(); + Integer requiredPoints = goods.getRequiredPoints(); // 扣减积分 memberPoints.setAccountPoints(accountPoints - requiredPoints); memberPoints.setLastPointsChangeTime(new Date()); @@ -171,21 +365,124 @@ public class TbPointsExchangeRecordServiceImpl extends ServiceImpllambdaQuery().eq(TbMemberPoints::getMemberId, entity.getMemberId())); + if (memberPoints == null) { + throw new MsgException("会员信息不存在"); + } + memberPoints.setAccountPoints(memberPoints.getAccountPoints() + entity.getSpendPoints()); + memberPoints.setLastPointsChangeTime(new Date()); + memberPoints.setLastFloatPoints(entity.getSpendPoints()); + tbMemberPointsMapper.updateById(memberPoints); + // 回滚库存 + TbPointsGoodsSetting goods = tbPointsGoodsSettingMapper.selectById(entity.getPointsGoodsId()); + if (goods == null) { + throw new MsgException("积分商品不存在"); + } + goods.setQuantity(goods.getQuantity() + 1); goods.setUpdateTime(new Date()); tbPointsGoodsSettingMapper.updateById(goods); - return record; + // 记录积分浮动流水 + TbMemberPointsLog log = new TbMemberPointsLog(); + log.setShopId(entity.getShopId()); + log.setMemberId(entity.getMemberId()); + log.setMemberName(entity.getMemberName()); + log.setMobile(entity.getMobile()); + log.setAvatarUrl(entity.getAvatarUrl()); + log.setContent(StrUtil.format("(退单)兑换商品:{} * {}", entity.getPointsGoodsName(), "1")); + log.setFloatType("add"); + log.setFloatPoints(+entity.getSpendPoints()); + log.setCreateTime(new Date()); + tbMemberPointsLogMapper.insert(log); + // 如果额外付款了则需要退款 + if (NumberUtil.equals(entity.getExtraPaymentAmount(), BigDecimal.ZERO)) { + return; + } + // 需要额外支付 + TbShopInfo shopInfo = mpShopInfoMapper.selectById(entity.getShopId()); + if (shopInfo == null) { + throw new MsgException("店铺信息不存在"); + } + TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(shopInfo.getMerchantId())); + if (thirdApply == null) { + throw new MsgException("支付通道不存在"); + } + if ("alipay".equalsIgnoreCase(entity.getPayType()) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) { + throw new MsgException("店铺未配置支付宝小程序appId"); + } + // 准备退款参数 + // 应用ID + String appId = thirdApply.getAppId(); + String appToken = thirdApply.getAppToken(); + // 交易金额 单位分 100 表示1元 + long amount = NumberUtil.mul(record.getExtraPaymentAmount(), new BigDecimal("100")).longValue(); + // 退款订单号 + String mchRefundNo = entity.getOrderNo(); + PublicResp publicResp; + try { + publicResp = thirdPayService.returnOrder(thirdUrl, appId, mchRefundNo, entity.getPayOrderId(), null, entity.getCancelOrRefundReason(), amount, null, null, appToken); + } catch (Exception e) { + super.log.error("发起退款失败:", e); + throw new MsgException(StrUtil.format("发起退款失败:{}", e.getMessage())); + } + if (publicResp == null) { + throw new MsgException("拉起支付失败:无响应"); + } + if (!"000000".equals(publicResp.getCode())) { + throw new MsgException(publicResp.getMsg()); + } + OrderReturnResp returnResp = publicResp.getObjData(); + if (!"SUCCESS".equals(returnResp.getState())) { + throw new MsgException(StrUtil.format("退款失败原因:{}", returnResp.getState())); + } } @Override diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsGoodsSettingServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsGoodsSettingServiceImpl.java index 852c52b..89d6735 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsGoodsSettingServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbPointsGoodsSettingServiceImpl.java @@ -16,6 +16,7 @@ import com.chaozhanggui.system.cashierservice.mapper.TbPointsGoodsSettingMapper; import com.chaozhanggui.system.cashierservice.service.TbPointsGoodsSettingService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import java.util.Date; @@ -28,6 +29,7 @@ import java.util.Map; * @since 2.0 2024-10-25 */ @Service +@Primary public class TbPointsGoodsSettingServiceImpl extends ServiceImpl implements TbPointsGoodsSettingService { diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/thirdpay/service/ThirdPayService.java b/src/main/java/com/chaozhanggui/system/cashierservice/thirdpay/service/ThirdPayService.java index d0cc415..47505cc 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/thirdpay/service/ThirdPayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/thirdpay/service/ThirdPayService.java @@ -1,8 +1,6 @@ package com.chaozhanggui.system.cashierservice.thirdpay.service; import cn.hutool.http.HttpRequest; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.TypeReference; import com.chaozhanggui.system.cashierservice.thirdpay.constants.SignTypeEnum; import com.chaozhanggui.system.cashierservice.thirdpay.req.*; import com.chaozhanggui.system.cashierservice.thirdpay.resp.*; @@ -27,6 +25,8 @@ public class ThirdPayService { private static String trade="/api/open/query/trade"; + private static String refund="/api/open/order/refund"; + /** * 被扫接口 * @param url @@ -209,7 +209,7 @@ public class ThirdPayService { param.setSign(sign); String reqbody = JSONUtil.toJSONString(param); log.info("请求参数:{}", reqbody); - String response = HttpRequest.post(url.concat(trade)).body(reqbody).execute().body(); + String response = HttpRequest.post(url.concat(refund)).body(reqbody).execute().body(); log.info("返回结果:{}", response); PublicResp resp =JSONUtil.parseJSONStr2T(response,PublicResp.class); resp.setObjData(JSONUtil.parseJSONStr2T(resp.getBizData(),OrderReturnResp.class)); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 897205d..f72ed44 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -61,6 +61,7 @@ thirdPay: callInBack: https://wxcashiertest.sxczgkj.cn/cashierService/notify/fstmemberInCallBack callFSTBack: https://wxcashiertest.sxczgkj.cn/cashierService/notify/notifyfstCallBack songOrderBack: https://wxcashiertest.sxczgkj.cn/cashierService/notify/songOrderCallBack + pointsGoodsOrderCallBack: https://wxcashiertest.sxczgkj.cn/cashierService/notify/pointsGoodsOrderCallBack server: port: 9888 prod: dev1 diff --git a/src/main/resources/application-pre.yml b/src/main/resources/application-pre.yml index 4110b3c..55c9b69 100644 --- a/src/main/resources/application-pre.yml +++ b/src/main/resources/application-pre.yml @@ -54,6 +54,7 @@ thirdPay: callInBack: https://pre-cashier.sxczgkj.cn/cashierService/notify/fstmemberInCallBack callFSTBack: https://pre-cashier.sxczgkj.cn/cashierService/notify/notifyfstCallBack songOrderBack: https://pre-cashier.sxczgkj.cn/cashierService/notify/songOrderCallBack + pointsGoodsOrderCallBack: https://pre-cashier.sxczgkj.cn/cashierService/notify/pointsGoodsOrderCallBack server: port: 9888 prod: dev1 diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 8af19f6..d7de4c8 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -55,6 +55,7 @@ thirdPay: callInBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/fstmemberInCallBack callFSTBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/notifyfstCallBack songOrderBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/songOrderCallBack + pointsGoodsOrderCallBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/pointsGoodsOrderCallBack prod: prod1 queue: cart_queue_putprod1 subscribe: diff --git a/src/main/resources/application-prod2.yml b/src/main/resources/application-prod2.yml index 1648b66..55d2754 100644 --- a/src/main/resources/application-prod2.yml +++ b/src/main/resources/application-prod2.yml @@ -55,6 +55,7 @@ thirdPay: callInBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/fstmemberInCallBack callFSTBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/notifyfstCallBack songOrderBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/songOrderCallBack + pointsGoodsOrderCallBack: https://cashier.sxczgkj.cn${server.servlet.context-path}notify/pointsGoodsOrderCallBack prod: prod2 queue: cart_queue_putprod2 diff --git a/src/main/resources/mapper/TbMemberPointsLogDao.xml b/src/main/resources/mapper/TbMemberPointsLogMapper.xml similarity index 100% rename from src/main/resources/mapper/TbMemberPointsLogDao.xml rename to src/main/resources/mapper/TbMemberPointsLogMapper.xml diff --git a/src/main/resources/mapper/TbMemberPointsDao.xml b/src/main/resources/mapper/TbMemberPointsMapper.xml similarity index 55% rename from src/main/resources/mapper/TbMemberPointsDao.xml rename to src/main/resources/mapper/TbMemberPointsMapper.xml index d2dea21..05b5371 100644 --- a/src/main/resources/mapper/TbMemberPointsDao.xml +++ b/src/main/resources/mapper/TbMemberPointsMapper.xml @@ -3,4 +3,7 @@ + \ No newline at end of file diff --git a/src/main/resources/mapper/TbPointsBasicSettingDao.xml b/src/main/resources/mapper/TbPointsBasicSettingMapper.xml similarity index 100% rename from src/main/resources/mapper/TbPointsBasicSettingDao.xml rename to src/main/resources/mapper/TbPointsBasicSettingMapper.xml diff --git a/src/main/resources/mapper/TbPointsExchangeRecordDao.xml b/src/main/resources/mapper/TbPointsExchangeRecordMapper.xml similarity index 100% rename from src/main/resources/mapper/TbPointsExchangeRecordDao.xml rename to src/main/resources/mapper/TbPointsExchangeRecordMapper.xml diff --git a/src/main/resources/mapper/TbPointsGoodsSettingDao.xml b/src/main/resources/mapper/TbPointsGoodsSettingMapper.xml similarity index 100% rename from src/main/resources/mapper/TbPointsGoodsSettingDao.xml rename to src/main/resources/mapper/TbPointsGoodsSettingMapper.xml