Skip to content

Commit

Permalink
完成借阅账号,未写死openid
Browse files Browse the repository at this point in the history
  • Loading branch information
seraphJ committed Nov 24, 2022
1 parent 40eb691 commit b726238
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>cn.springboot</groupId>
<artifactId>best-pay-sdk</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/gxj/config/WechatAccountConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
35 changes: 35 additions & 0 deletions src/main/java/com/gxj/config/WechatPayConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/gxj/controller/PayController.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/gxj/service/PayService.java
Original file line number Diff line number Diff line change
@@ -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);
}
37 changes: 37 additions & 0 deletions src/main/java/com/gxj/service/impl/PayServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/gxj/utils/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down
27 changes: 27 additions & 0 deletions src/main/resources/static/pay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script>
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"wxd898fcb01713c658", //公众号名称,由商户传入
"timeStamp":"1669264645", //时间戳,自1970年以来的秒数
"nonceStr":"rnL6sKZ7Y8vncfIG", //随机串
"package":"prepay_id=wx241237254558002c52059d9721ea6a0000",
"signType":"MD5", //微信签名方式:
"paySign":"D06B8476D98B2818AB5CDFB26FF97AAB" //微信签名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
</script>
28 changes: 28 additions & 0 deletions src/main/resources/templates/pay/create.ftlh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"${payResponse.appId}", //公众号名称,由商户传入
"timeStamp":"${payResponse.timeStamp}", //时间戳,自1970年以来的秒数
"nonceStr":"${payResponse.nonceStr}", //随机串
"package":"${payResponse.packAge}",
"signType":"MD5", //微信签名方式:
"paySign":"${payResponse.paySign}" //微信签名
},
function(res){
// if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
location.href = "${returnUrl}"
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
</script>
30 changes: 30 additions & 0 deletions src/test/java/com/gxj/service/impl/PayServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit b726238

Please sign in to comment.