service层
- IPayService
public interface IPayService {
/*
* 创建支付
* */
PayResponse create(Long orderId, BigDecimal amount);
}
- PayServiceImpl
@Service
@Slf4j
public class PayServiceImpl implements IPayService {
@Override
public PayResponse create(Long orderId,BigDecimal amount) {
/*微信支付配置*/
WxPayConfig wxPayConfig =new WxPayConfig();
wxPayConfig.setAppId("xxx");//申请的公众号id
wxPayConfig.setMchId("xxx");//商户id
wxPayConfig.setMchKey("xxx");//商户秘钥
wxPayConfig.setNotifyUrl("xxx");//通知地址,该地址需要公网能够访问。发起支付后,微信会返回一些信息,该地址是用于接收微信的异步通知
/*支付类*/
BestPayServiceImpl bestPayService=new BestPayServiceImpl();
bestPayService.setWxPayConfig(wxPayConfig);
/*发起支付*/
PayRequest payRequest = new PayRequest();
payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_NATIVE);//支付方式
payRequest.setOrderId("23423947293847293842");//订单号
payRequest.setOrderAmount(0.01);//订单金额
payRequest.setOrderName("8804022-全世界最蠢的猪");//订单名称
PayResponse response = bestPayService.pay(payRequest);//微信返回信息
log.info("response={}",response);//用日志的方式打印通知信息
return response;
}
}
controller层
@Controller
@RequestMapping("/pay")
public class PayController {
@Autowired
private IPayService payService;
@GetMapping("/create")
//@RequestParam("orderId") Long orderId 用于接收参数:订单号
//ModelAndView 视图
public ModelAndView create(@RequestParam("orderId") Long orderId,
@RequestParam("amount") BigDecimal amount){
PayResponse response = payService.create(orderId, amount);
Map<String,String> map=new HashMap<>();
map.put("codeUrl",response.getCodeUrl());//将获得的codeUrl放入map中
return new ModelAndView("create",map);//携带数据map到create.ftl页面
}
}
在resources ->templatess里创建create.ftl文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>支付</title>
</head>
<body>
<div id="myQrcode"></div>
<script src="https://cdn.bootcss.com/jquery/1.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<script>
jQuery('#myQrcode').qrcode({
text: "${codeUrl}"
});
</script>
</body>
</html>
运行结果
微信扫码可以支付0.01元