-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/test/java/com/gxj/service/impl/PayServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |