赞
踩
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.1.0</version>
</dependency>
获取WxPayService
public static WxPayService getPayService() {
WxPayService wxPayService = new WxPayServiceImpl();
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId("appid");
payConfig.setMchId("商户号");
payConfig.setMchKey("API3密钥");
payConfig.setKeyPath("证书秘钥文件地址");
// 可以指定是否使用沙箱环境
payConfig.setUseSandboxEnv(false);
wxPayService.setConfig(payConfig);
return wxPayService;
}
获取支付地址code_url
public String getPayUrl (Order order) { WxPayService wxPayService = WxPayConfiguration.getPayService(); WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); orderRequest.setOutTradeNo("订单号"); orderRequest.setTotalFee(金额); orderRequest.setSpbillCreateIp("IP"); orderRequest.setNotifyUrl("支付回调地址"); orderRequest.setBody("说明"); orderRequest.setAttach("附加内容"); orderRequest.setTradeType("NATIVE");//交易类型 orderRequest.setProductId(order.getZforderId()); try { WxPayNativeOrderResult wxPayNativeOrderResult = (WxPayNativeOrderResult) wxPayService.createOrder(orderRequest); return wxPayNativeOrderResult.getCodeUrl(); } catch (WxPayException e) { e.printStackTrace(); return null; } }
支付成功回调
@RequestMapping(value = "/pay/notify") public String renotify(@RequestBody String xmlData) { System.err.println(xmlData); try { WxPayService wxPayService = WxPayConfiguration.getPayService(); WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData); String orderId = notifyResult.getOutTradeNo();//拿到订单号获取订单 Order orderInfo = orderService.getOne(new QueryWrapper<Order>().eq("zforder_id", orderId)); if(orderInfo == null) { return WxPayNotifyResponse.success("处理成功!"); } orderService.paySuccess(orderInfo);//处理业务逻辑 return WxPayNotifyResponse.success("处理成功!"); } catch (WxPayException e) { e.printStackTrace(); return WxPayNotifyResponse.fail(e.getMessage()); } }
前端显示支付二维码
<div class="pay-wrap"> <canvas id="QRCode_header"></canvas> </div> npm install qrcode --save-dev import QRCode from "qrcode"; getQRCode() { //生成的二维码为URL地址js let opts = { errorCorrectionLevel: "H", //容错级别 type: "image/png", //生成的二维码类型 quality: 0.3, //二维码质量 margin: 0, //二维码留白边距 width: 180, //宽 height: 180, //高 text: "https://www.baidu.com/", //二维码内容 color: { dark: "#333333", //前景色 light: "#fff", //背景色 }, }; let msg = document.getElementById("QRCode_header"); // 将获取到的数据(val)画到msg(canvas)上。。。this.payQrcode后台返回的支付地址 QRCode.toCanvas(msg, this.payQrcode, opts, function (error) { if (error) { console.log("二维码加载失败", error); this.$message.error("二维码加载失败"); } }); console.log("画二维码") },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。