From aeae6d9d8efb1eab6b1f562daed317b5b4bdb2e8 Mon Sep 17 00:00:00 2001 From: caolin <1149034574@qq.com> Date: Sun, 20 Aug 2023 15:06:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=98=E7=9B=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jwl/driver/server/constant/Constants.java | 3 + .../server/controller/H5PayController.java | 17 +++-- .../controller/PayNoticeLogController.java | 18 +++++ .../controller/PayPrepayController.java | 6 +- .../jwl/driver/server/enums/PayStatus.java | 47 +++++++++++++ .../server/service/IOrderPayInfoService.java | 14 ++++ .../service/impl/OrderPayInfoServiceImpl.java | 69 +++++++++++++++++++ .../com/jwl/driver/server/vo/H5PayVo.java | 4 +- src/main/resources/application.yml | 7 +- 9 files changed, 176 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/jwl/driver/server/enums/PayStatus.java diff --git a/src/main/java/com/jwl/driver/server/constant/Constants.java b/src/main/java/com/jwl/driver/server/constant/Constants.java index d6c2d36..6291082 100644 --- a/src/main/java/com/jwl/driver/server/constant/Constants.java +++ b/src/main/java/com/jwl/driver/server/constant/Constants.java @@ -47,4 +47,7 @@ public class Constants { //字典表题目分类标识 public static String QUESTION_CATEGORY = "QusetionCategory"; + //微信支付 + public static String WECHAT_PAY = "weChatPay"; + } diff --git a/src/main/java/com/jwl/driver/server/controller/H5PayController.java b/src/main/java/com/jwl/driver/server/controller/H5PayController.java index 2f47a56..4a0589b 100644 --- a/src/main/java/com/jwl/driver/server/controller/H5PayController.java +++ b/src/main/java/com/jwl/driver/server/controller/H5PayController.java @@ -1,7 +1,9 @@ package com.jwl.driver.server.controller; import com.jwl.driver.server.config.WechatPayConfig; +import com.jwl.driver.server.entity.OrderPayInfo; import com.jwl.driver.server.response.BaseResponse; +import com.jwl.driver.server.service.IOrderPayInfoService; import com.jwl.driver.server.vo.H5PayVo; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAAutoCertificateConfig; @@ -12,12 +14,15 @@ import com.wechat.pay.java.service.payments.model.Transaction; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; +import java.math.BigDecimal; +import java.util.Objects; /** * 微信支付小程序 接口 @@ -31,23 +36,28 @@ public class H5PayController { @Resource private WechatPayConfig wechatPayConfig; + @Autowired + private IOrderPayInfoService orderPayInfoService; + //生成预支付订单 @ApiOperation("生成预支付订单") @PostMapping("/prepay") public BaseResponse createPrepay(@RequestBody H5PayVo payVo){ // 构建service H5Service service = createService(); + //在order_pay_info里新增一条数据 + OrderPayInfo orderPayInfo = orderPayInfoService.createOrderPayInfo(payVo); - // 请求下单参数 + // 请求下单参数 PrepayRequest request = new PrepayRequest(); Amount amount = new Amount(); - amount.setTotal(Integer.valueOf((payVo.getMoney()*100)+"")); + amount.setTotal(Integer.valueOf((payVo.getMoney().multiply(new BigDecimal("100")).setScale(0).toString())+"")); request.setAmount(amount); request.setAppid(wechatPayConfig.getAppId()); request.setMchid(wechatPayConfig.getMchId()); request.setDescription(payVo.getDescription()); request.setNotifyUrl(wechatPayConfig.getPayNoticeUrl()); - request.setOutTradeNo(payVo.getOutTradeNo()); + request.setOutTradeNo(orderPayInfo.getPayId().toString()); //场景参数 SceneInfo sceneInfo = new SceneInfo(); sceneInfo.setPayerClientIp(payVo.getClientIp()); @@ -57,7 +67,6 @@ public class H5PayController { // 使用微信扫描 code_url 对应的二维码,即可体验Native支付 // log.info(response); return BaseResponse.success(response); - } private H5Service createService() { diff --git a/src/main/java/com/jwl/driver/server/controller/PayNoticeLogController.java b/src/main/java/com/jwl/driver/server/controller/PayNoticeLogController.java index cece19b..b61a4dd 100644 --- a/src/main/java/com/jwl/driver/server/controller/PayNoticeLogController.java +++ b/src/main/java/com/jwl/driver/server/controller/PayNoticeLogController.java @@ -4,12 +4,16 @@ package com.jwl.driver.server.controller; import com.alibaba.fastjson.JSONObject; import com.jwl.driver.server.config.WechatPayConfig; import com.jwl.driver.server.response.BaseResponse; +import com.jwl.driver.server.service.IOrderPayInfoService; +import com.jwl.driver.server.service.ITdMemberService; +import com.jwl.driver.server.service.ITdSysUserMemberService; import com.wechat.pay.java.core.RSAAutoCertificateConfig; import com.wechat.pay.java.core.notification.NotificationConfig; import com.wechat.pay.java.core.notification.NotificationParser; import com.wechat.pay.java.core.notification.RequestParam; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpRequest; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -36,6 +40,12 @@ public class PayNoticeLogController { @Resource private WechatPayConfig wechatPayConfig; + @Autowired + private ITdSysUserMemberService userMemberService; + + @Autowired + private IOrderPayInfoService payInfoService; + /** * 支付回调接口 本接口不能做验证 * @param wechatPayCertificateSerialNumber @@ -76,11 +86,19 @@ public class PayNoticeLogController { System.out.println("decryptObject="+decryptObject.toJSONString()); String trade_state=decryptObject.getString("trade_state"); + //取业务id + String outTradeNo = decryptObject.getString("out_trade_no"); JSONObject jsonResponse = new JSONObject(); if(trade_state.equals("SUCCESS")) { //各种业务逻辑 + //1.订单详情表里修改数据 + payInfoService.payNotice(Long.parseLong(outTradeNo),Boolean.FALSE); + //2.冲会员 + }else{ //还是各种业务逻辑 + //1.订单详情表里修改数据 + payInfoService.payNotice(Long.parseLong(outTradeNo),Boolean.FALSE); } jsonResponse.put("code", "SUCCESS"); jsonResponse.put("message", "成功"); diff --git a/src/main/java/com/jwl/driver/server/controller/PayPrepayController.java b/src/main/java/com/jwl/driver/server/controller/PayPrepayController.java index a91116f..fb62959 100644 --- a/src/main/java/com/jwl/driver/server/controller/PayPrepayController.java +++ b/src/main/java/com/jwl/driver/server/controller/PayPrepayController.java @@ -4,6 +4,7 @@ package com.jwl.driver.server.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RestController; /** *

@@ -13,8 +14,9 @@ import org.springframework.stereotype.Controller; * @author Automated procedures * @since 2023-08-10 */ -@Controller -@RequestMapping("//payPrepay") +@RestController +@RequestMapping("/payPrepay") public class PayPrepayController { + } diff --git a/src/main/java/com/jwl/driver/server/enums/PayStatus.java b/src/main/java/com/jwl/driver/server/enums/PayStatus.java new file mode 100644 index 0000000..b44ce1b --- /dev/null +++ b/src/main/java/com/jwl/driver/server/enums/PayStatus.java @@ -0,0 +1,47 @@ +package com.jwl.driver.server.enums; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author 曹林 + * @description 支付状态 + * @create 2023/8/20 1:19 + */ +public enum PayStatus { + NOT(1, "未支付"), + SUCCESS(2, "支付成功"), + FAIL(3, "支付失败"); + + private Integer value; + private String name; + public static final Map valueMap = new HashMap(); + + public static PayStatus fromValue(Integer value) { + return (PayStatus)valueMap.get(value); + } + + private PayStatus(Integer value, String name) { + this.value = value; + this.name = name; + } + + public Integer getValue() { + return this.value; + } + + public String getName() { + return this.name; + } + + static { + PayStatus[] var0 = values(); + int var1 = var0.length; + + for(int var2 = 0; var2 < var1; ++var2) { + PayStatus value = var0[var2]; + valueMap.put(value.getValue(), value); + } + + } +} diff --git a/src/main/java/com/jwl/driver/server/service/IOrderPayInfoService.java b/src/main/java/com/jwl/driver/server/service/IOrderPayInfoService.java index dff972a..c3268de 100644 --- a/src/main/java/com/jwl/driver/server/service/IOrderPayInfoService.java +++ b/src/main/java/com/jwl/driver/server/service/IOrderPayInfoService.java @@ -2,6 +2,7 @@ package com.jwl.driver.server.service; import com.jwl.driver.server.entity.OrderPayInfo; import com.baomidou.mybatisplus.extension.service.IService; +import com.jwl.driver.server.vo.H5PayVo; /** *

@@ -13,4 +14,17 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface IOrderPayInfoService extends IService { + /** + * 微信支付创建预支付申请 + * @param payVo + * @return + */ + OrderPayInfo createOrderPayInfo(H5PayVo payVo); + + /** + * 支付结果 + * @param payId + * @param payResult + */ + Boolean payNotice(Long payId, Boolean payResult); } diff --git a/src/main/java/com/jwl/driver/server/service/impl/OrderPayInfoServiceImpl.java b/src/main/java/com/jwl/driver/server/service/impl/OrderPayInfoServiceImpl.java index fb17e19..297cbbd 100644 --- a/src/main/java/com/jwl/driver/server/service/impl/OrderPayInfoServiceImpl.java +++ b/src/main/java/com/jwl/driver/server/service/impl/OrderPayInfoServiceImpl.java @@ -1,10 +1,28 @@ package com.jwl.driver.server.service.impl; +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.StrUtil; +import com.jwl.driver.server.constant.Constants; import com.jwl.driver.server.entity.OrderPayInfo; +import com.jwl.driver.server.entity.TdMember; +import com.jwl.driver.server.entity.TdSysUser; +import com.jwl.driver.server.enums.PayStatus; +import com.jwl.driver.server.exception.BusinessException; import com.jwl.driver.server.mapper.OrderPayInfoMapper; import com.jwl.driver.server.service.IOrderPayInfoService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jwl.driver.server.service.ITdMemberService; +import com.jwl.driver.server.service.ITdSysUserService; +import com.jwl.driver.server.util.SecurityUtil; +import com.jwl.driver.server.vo.H5PayVo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Objects; /** *

@@ -15,6 +33,57 @@ import org.springframework.stereotype.Service; * @since 2023-08-10 */ @Service +@Slf4j public class OrderPayInfoServiceImpl extends ServiceImpl implements IOrderPayInfoService { + @Autowired + private ITdSysUserService userService; + + @Autowired + private ITdMemberService memberService; + + @Autowired + private Snowflake snowflake; + + @Override + public OrderPayInfo createOrderPayInfo(H5PayVo payVo) { + Long userId = StrUtil.isNotBlank(payVo.getUserId()) ? Long.parseLong(payVo.getUserId()) : SecurityUtil.getUserId(); + TdSysUser user = userService.getById(userId); + if (Objects.isNull(user)) { + log.error("数据异常,用户不存在" + userId); + throw new BusinessException("数据缺失"); + } + TdMember member = memberService.getById(Integer.parseInt(payVo.getOutTradeNo())); + if (Objects.isNull(member) || StrUtil.equals(Constants.IS_ACTIVE_FALSE, member.getIsActive())) { + log.error("数据异常,会员不存在或者已过期" + payVo.getOutTradeNo()); + throw new BusinessException("数据缺失"); + } + OrderPayInfo orderPayInfo = new OrderPayInfo() + .setPayId(snowflake.nextId()) + .setMemberId(Integer.parseInt(payVo.getOutTradeNo())) + .setMoney(payVo.getMoney()) + .setPaymentType(Constants.WECHAT_PAY) + .setDescription(user.getUserName() + "-" + member.getMemberName()) + .setPayStatus(PayStatus.NOT.getValue()) + .setDeleteStatus(0) + .setUserId(userId) + .setCreateTime(LocalDateTime.now()) + .setUpdateTime(LocalDateTime.now()); + + this.save(orderPayInfo); + return orderPayInfo; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Boolean payNotice(Long payId, Boolean payResult) { + + OrderPayInfo orderPayInfo = this.getById(payId); + if (Objects.isNull(orderPayInfo)){ + throw new BusinessException("订单信息不存在"); + } + + + return Boolean.TRUE; + } } diff --git a/src/main/java/com/jwl/driver/server/vo/H5PayVo.java b/src/main/java/com/jwl/driver/server/vo/H5PayVo.java index 62ea294..f0ecb4b 100644 --- a/src/main/java/com/jwl/driver/server/vo/H5PayVo.java +++ b/src/main/java/com/jwl/driver/server/vo/H5PayVo.java @@ -4,11 +4,13 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.math.BigDecimal; + @ApiModel("H5支付") @Data public class H5PayVo { @ApiModelProperty("支付金额") - private Double money;//金额 + private BigDecimal money;//金额 @ApiModelProperty("用户id") private String userId; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2d493a4..ca52e2a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -45,6 +45,11 @@ driver: - /tdQuestion/duima/list - /tdQuestion/duima/update - /payNoticeLog + - /tdQuestion/queryQuestionById + - /tdQuestion/queryQuestionByIdList + - /tdQuestion/queryQuestion + - /tdQuestion/getTestQuestion + - /tdQuestion/questionCategory # 需要权限校验url集合 needAuthEndPoints: @@ -58,8 +63,6 @@ message: code: expireTime: 300 - - wechatpay: appId: wx756a7425037609fb appSecret: 3e8053032b16c574e38d554ddd438cfd