From b7262388fcc7d6e8eaafa775e8d2826c13997443 Mon Sep 17 00:00:00 2001 From: Seraph Date: Thu, 24 Nov 2022 16:25:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=80=9F=E9=98=85=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=EF=BC=8C=E6=9C=AA=E5=86=99=E6=AD=BBopenid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 +++++ .../com/gxj/config/WechatAccountConfig.java | 5 +++ .../java/com/gxj/config/WechatPayConfig.java | 35 ++++++++++++++++ .../com/gxj/controller/PayController.java | 41 +++++++++++++++++++ src/main/java/com/gxj/service/PayService.java | 8 ++++ .../com/gxj/service/impl/PayServiceImpl.java | 37 +++++++++++++++++ src/main/java/com/gxj/utils/JsonUtil.java | 13 ++++++ src/main/resources/application.yml | 6 ++- src/main/resources/static/pay.html | 27 ++++++++++++ src/main/resources/templates/pay/create.ftlh | 28 +++++++++++++ .../gxj/service/impl/PayServiceImplTest.java | 30 ++++++++++++++ 11 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/gxj/config/WechatPayConfig.java create mode 100644 src/main/java/com/gxj/controller/PayController.java create mode 100644 src/main/java/com/gxj/service/PayService.java create mode 100644 src/main/java/com/gxj/service/impl/PayServiceImpl.java create mode 100644 src/main/java/com/gxj/utils/JsonUtil.java create mode 100644 src/main/resources/static/pay.html create mode 100644 src/main/resources/templates/pay/create.ftlh create mode 100644 src/test/java/com/gxj/service/impl/PayServiceImplTest.java diff --git a/pom.xml b/pom.xml index 0fdb039..f64d3c2 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,17 @@ spring-boot-configuration-processor true + + + cn.springboot + best-pay-sdk + 1.1.0 + + + + org.springframework.boot + spring-boot-starter-freemarker + diff --git a/src/main/java/com/gxj/config/WechatAccountConfig.java b/src/main/java/com/gxj/config/WechatAccountConfig.java index 5f634dc..6cbb9fe 100644 --- a/src/main/java/com/gxj/config/WechatAccountConfig.java +++ b/src/main/java/com/gxj/config/WechatAccountConfig.java @@ -11,4 +11,9 @@ public class WechatAccountConfig { private String mpAppId; private String mpAppSecret; + private String mchId; + private String mchKey; + private String keyPath; + private String notifyUrl; + } diff --git a/src/main/java/com/gxj/config/WechatPayConfig.java b/src/main/java/com/gxj/config/WechatPayConfig.java new file mode 100644 index 0000000..e923ca8 --- /dev/null +++ b/src/main/java/com/gxj/config/WechatPayConfig.java @@ -0,0 +1,35 @@ +package com.gxj.config; + +import com.lly835.bestpay.config.WxPayH5Config; +import com.lly835.bestpay.service.impl.BestPayServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Component +@Configuration +public class WechatPayConfig { + + @Autowired + private WechatAccountConfig accountConfig; + + @Bean + public BestPayServiceImpl bestPayService() { + BestPayServiceImpl bestPayService = new BestPayServiceImpl(); + bestPayService.setWxPayH5Config(wxPayH5Config()); + return bestPayService; + } + + @Bean + public WxPayH5Config wxPayH5Config() { + WxPayH5Config wxPayH5Config = new WxPayH5Config(); + wxPayH5Config.setAppId(accountConfig.getMpAppId()); + wxPayH5Config.setAppSecret(accountConfig.getMpAppSecret()); + wxPayH5Config.setMchId(accountConfig.getMchId()); + wxPayH5Config.setMchKey(accountConfig.getMchKey()); + wxPayH5Config.setKeyPath(accountConfig.getKeyPath()); + wxPayH5Config.setNotifyUrl(accountConfig.getNotifyUrl()); + return wxPayH5Config; + } +} diff --git a/src/main/java/com/gxj/controller/PayController.java b/src/main/java/com/gxj/controller/PayController.java new file mode 100644 index 0000000..bed9e4e --- /dev/null +++ b/src/main/java/com/gxj/controller/PayController.java @@ -0,0 +1,41 @@ +package com.gxj.controller; + + +import com.gxj.dto.OrderDTO; +import com.gxj.enums.ResultEnum; +import com.gxj.exception.SellException; +import com.gxj.service.OrderService; +import com.gxj.service.PayService; +import com.lly835.bestpay.model.PayResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +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.servlet.ModelAndView; + +import java.util.Map; + +@Controller +@RequestMapping("/pay") +public class PayController { + @Autowired + private OrderService orderService; + + @Autowired + private PayService payService; + + @GetMapping("/create") + public ModelAndView create(@RequestParam("orderId") String orderId, + @RequestParam("returnUrl") String returnUrl, + Map map) { + OrderDTO orderDTO = orderService.findOne(orderId); + if (orderDTO == null) { + throw new SellException(ResultEnum.ORDER_NOT_EXIST); + } + PayResponse payResponse = payService.create(orderDTO); + map.put("payResponse", payResponse); + map.put("returnUrl", returnUrl); + return new ModelAndView("pay/create", map); + } +} diff --git a/src/main/java/com/gxj/service/PayService.java b/src/main/java/com/gxj/service/PayService.java new file mode 100644 index 0000000..a069114 --- /dev/null +++ b/src/main/java/com/gxj/service/PayService.java @@ -0,0 +1,8 @@ +package com.gxj.service; + +import com.gxj.dto.OrderDTO; +import com.lly835.bestpay.model.PayResponse; + +public interface PayService { + PayResponse create(OrderDTO orderDTO); +} diff --git a/src/main/java/com/gxj/service/impl/PayServiceImpl.java b/src/main/java/com/gxj/service/impl/PayServiceImpl.java new file mode 100644 index 0000000..ebcafd5 --- /dev/null +++ b/src/main/java/com/gxj/service/impl/PayServiceImpl.java @@ -0,0 +1,37 @@ +package com.gxj.service.impl; + +import com.gxj.dto.OrderDTO; +import com.gxj.service.PayService; +import com.gxj.utils.JsonUtil; +import com.lly835.bestpay.enums.BestPayTypeEnum; +import com.lly835.bestpay.model.PayRequest; +import com.lly835.bestpay.model.PayResponse; +import com.lly835.bestpay.service.impl.BestPayServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class PayServiceImpl implements PayService { + + private static final String ORDER_NAME = "微信点餐订单"; + + @Autowired + private BestPayServiceImpl bestPayService; + @Override + public PayResponse create(OrderDTO orderDTO) { + PayRequest payRequest = new PayRequest(); + payRequest.setOpenid(orderDTO.getBuyerOpenid()); + payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue()); + payRequest.setOrderId(orderDTO.getOrderId()); + payRequest.setOrderName(ORDER_NAME); + payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5); + log.info("【微信支付】request={}", JsonUtil.toJson(payRequest)); + + PayResponse payResponse = bestPayService.pay(payRequest); + log.info("【微信支付】response={}", JsonUtil.toJson(payResponse)); + + return payResponse; + } +} diff --git a/src/main/java/com/gxj/utils/JsonUtil.java b/src/main/java/com/gxj/utils/JsonUtil.java new file mode 100644 index 0000000..e9eadf6 --- /dev/null +++ b/src/main/java/com/gxj/utils/JsonUtil.java @@ -0,0 +1,13 @@ +package com.gxj.utils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class JsonUtil { + public static String toJson(Object object) { + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.setPrettyPrinting(); + Gson gson = gsonBuilder.create(); + return gson.toJson(object); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 86519da..ba9cf5a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -26,8 +26,12 @@ server: include-exception: true wechat: - mpAppId: wx7aef45e7ab0a62e4 + mpAppId: wxd898fcb01713c658 mpAppSecret: 4786ff747ab75b3e26ed59b28b72d3fb + mchId: 1483469312 + mchKey: 7mdApPMfXddfWWbbP4DUaVYm2wjyh3v3 + keyPath: /var/weixin_cert/h5.p12 + notifyUrl: http://mytestsell.mynatapp.cc/sell/pay/notify diff --git a/src/main/resources/static/pay.html b/src/main/resources/static/pay.html new file mode 100644 index 0000000..6b6ac83 --- /dev/null +++ b/src/main/resources/static/pay.html @@ -0,0 +1,27 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/pay/create.ftlh b/src/main/resources/templates/pay/create.ftlh new file mode 100644 index 0000000..1c5063a --- /dev/null +++ b/src/main/resources/templates/pay/create.ftlh @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/src/test/java/com/gxj/service/impl/PayServiceImplTest.java b/src/test/java/com/gxj/service/impl/PayServiceImplTest.java new file mode 100644 index 0000000..7047f81 --- /dev/null +++ b/src/test/java/com/gxj/service/impl/PayServiceImplTest.java @@ -0,0 +1,30 @@ +package com.gxj.service.impl; + +import com.gxj.dto.OrderDTO; +import com.gxj.service.OrderService; +import com.gxj.service.PayService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +class PayServiceImplTest { + + @Autowired + private PayService payService; + + @Autowired + private OrderService orderService; + + @Test + public void create() { + OrderDTO orderDTO = orderService.findOne("1669178724464732785"); + payService.create(orderDTO); + } + +} \ No newline at end of file