微信扫码支付及回调
## pom包
```xml <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-pay</artifactId> <version>4.0.0</version> </dependency> ```
## 配置
## 配置文件
获取配置
```java package com.zt.edu.userapi.weChat.config;
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
/** * @author <a href="https://github.com/binarywang">Binary Wang</a> */ @ConfigurationProperties(prefix = "wx.pay") @Data public class WxMaProperty {
/** * 公众账号ID */ private String appid;
/** * 支付通知地址 */ private String notifyUrl;
/** * 退款通知地址 */ private String refundUrl;
/** * 支付key */ private String key;
/** * 支付key */ private String keyPath;
}
```
配置文件
~~~java package com.zt.edu.userapi.weChat.config;
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map;
/** * @author hwk * @Description 支付配置 * @date 2021/3/22 14:09 **/ @Configuration @ConditionalOnClass(WxPayService.class) @EnableConfigurationProperties(WxMaProperty.class) public class WxPayConfiguration {
@Resource WxMaProperty wxMaProperty;
private static Map<String, WxPayService> wxPayServices = new HashMap<>();
@PostConstruct public void init() { //1、赋值wxPayServices WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(StringUtils.trimToNull(wxMaProperty.getAppid())); payConfig.setMchId(StringUtils.trimToNull(wxMaProperty.getMchId())); payConfig.setMchKey(StringUtils.trimToNull(wxMaProperty.getKey())); payConfig.setKeyPath(StringUtils.trimToNull(wxMaProperty.getKeyPath())); // 可以指定是否使用沙箱环境 payConfig.setUseSandboxEnv(false);
WxPayService wxPayService = new WxPayServiceImpl(); wxPayService.setConfig(payConfig); wxPayServices.put(wxMaProperty.getAppid(),wxPayService); }
} ~~~
## 示例
~~~java package com.zt.edu.userapi.weChat.service.impl;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; import com.zt.edu.userapi.dto.ZtbOrderDTO; import com.zt.edu.userapi.service.ZtbPayResultService; import com.zt.edu.userapi.weChat.config.WxMaProperty; import com.zt.edu.userapi.weChat.config.WxPayConfiguration; import com.zt.edu.userapi.weChat.service.WeChatPayService; import com.zt.edu.userapi.weChat.util.WXPayUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import java.math.BigDecimal;
@Service @Slf4j public class WeChatPayServiceImpl implements WeChatPayService {
@Resource private WxMaProperty wxMaProperty;
@Resource private WxPayConfiguration wxPayConfiguration;
@Resource private ZtbPayResultService ztbPayResultService;
@Resource private WxMaProperty wxMaProperty;
统一下单后生成二维码
package com.zt.edu.userapi.util; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; /** * 生成二维码 */ public class QRCodeUtil { /** * * @param response * @param codeUrl 需要转二维码的URL */ public static void createQrCode(HttpServletResponse response,String codeUrl) { try { //生成二维码配置 Map<EncodeHintType,Object> hints = new HashMap<>(); //设置纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //设置编码类型 hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); //构造图片对象 BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE,400,400,hints); //输出流 OutputStream out = response.getOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix,"png",out); } catch (Exception e) { e.printStackTrace(); } } }
代码示例