springboot微信支付,支付二维码生成
时间: 2023-10-19 21:34:07 浏览: 171
要生成微信支付二维码,可以使用微信支付官方提供的接口,以下是基于 Spring Boot 的简单实现步骤:
1. 引入相关依赖
```xml
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>3.0.9</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
```
2. 配置微信支付相关参数
```java
@Configuration
public class WxPayConfig {
// 微信支付分配的商户号
@Value("${wxpay.mchId}")
private String mchId;
// 微信支付分配的终端设备号
@Value("${wxpay.deviceInfo}")
private String deviceInfo;
// 微信支付分配的公众账号ID
@Value("${wxpay.appId}")
private String appId;
// 微信支付分配的商户密钥
@Value("${wxpay.key}")
private String key;
// 微信支付异步通知地址
@Value("${wxpay.notifyUrl}")
private String notifyUrl;
// 微信支付统一下单接口地址
@Value("${wxpay.unifiedOrderUrl}")
private String unifiedOrderUrl;
// 微信支付查询订单接口地址
@Value("${wxpay.orderQueryUrl}")
private String orderQueryUrl;
// 微信支付关闭订单接口地址
@Value("${wxpay.closeOrderUrl}")
private String closeOrderUrl;
// 微信支付申请退款接口地址
@Value("${wxpay.refundUrl}")
private String refundUrl;
// 微信支付查询退款接口地址
@Value("${wxpay.refundQueryUrl}")
private String refundQueryUrl;
// 微信支付下载对账单接口地址
@Value("${wxpay.downloadBillUrl}")
private String downloadBillUrl;
// 微信支付交易保障接口地址
@Value("${wxpay.reportUrl}")
private String reportUrl;
// 签名类型
private final String SIGN_TYPE = "MD5";
// 连接超时时间
private final int CONNECT_TIMEOUT = 5000;
// 读取超时时间
private final int READ_TIMEOUT = 10000;
// 微信支付API证书路径
@Value("${wxpay.certPath}")
private String certPath;
// 微信支付API证书密码
@Value("${wxpay.certPassword}")
private String certPassword;
/**
* 初始化微信支付配置
*/
@Bean
public WXPay wxPay() throws Exception {
WXPayConfigImpl config = new WXPayConfigImpl();
config.setAppID(appId);
config.setMchID(mchId);
config.setKey(key);
config.setDeviceInfo(deviceInfo);
config.setSignType(SIGN_TYPE);
config.setUnifiedOrderUrl(unifiedOrderUrl);
config.setOrderQueryUrl(orderQueryUrl);
config.setCloseOrderUrl(closeOrderUrl);
config.setRefundUrl(refundUrl);
config.setRefundQueryUrl(refundQueryUrl);
config.setDownloadBillUrl(downloadBillUrl);
config.setReportUrl(reportUrl);
return new WXPay(config, notifyUrl, true, true);
}
/**
* 微信支付配置类
*/
class WXPayConfigImpl extends WXPayConfig {
private String appId;
private String mchId;
private String key;
private String deviceInfo;
private String signType;
private String unifiedOrderUrl;
private String orderQueryUrl;
private String closeOrderUrl;
private String refundUrl;
private String refundQueryUrl;
private String downloadBillUrl;
private String reportUrl;
public void setAppID(String appId) {
this.appId = appId;
}
public void setMchID(String mchId) {
this.mchId = mchId;
}
public void setKey(String key) {
this.key = key;
}
public void setDeviceInfo(String deviceInfo) {
this.deviceInfo = deviceInfo;
}
public void setSignType(String signType) {
this.signType = signType;
}
public void setUnifiedOrderUrl(String unifiedOrderUrl) {
this.unifiedOrderUrl = unifiedOrderUrl;
}
public void setOrderQueryUrl(String orderQueryUrl) {
this.orderQueryUrl = orderQueryUrl;
}
public void setCloseOrderUrl(String closeOrderUrl) {
this.closeOrderUrl = closeOrderUrl;
}
public void setRefundUrl(String refundUrl) {
this.refundUrl = refundUrl;
}
public void setRefundQueryUrl(String refundQueryUrl) {
this.refundQueryUrl = refundQueryUrl;
}
public void setDownloadBillUrl(String downloadBillUrl) {
this.downloadBillUrl = downloadBillUrl;
}
public void setReportUrl(String reportUrl) {
this.reportUrl = reportUrl;
}
@Override
public String getAppID() {
return appId;
}
@Override
public String getMchID() {
return mchId;
}
@Override
public String getKey() {
return key;
}
@Override
public InputStream getCertStream() {
try {
return new FileInputStream(new File(certPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public int getHttpConnectTimeoutMs() {
return CONNECT_TIMEOUT;
}
@Override
public int getHttpReadTimeoutMs() {
return READ_TIMEOUT;
}
@Override
public String getDeviceInfo() {
return deviceInfo;
}
@Override
public String getSignType() {
return signType;
}
@Override
public String getUnifiedOrderUrl() {
return unifiedOrderUrl;
}
@Override
public String getOrderQueryUrl() {
return orderQueryUrl;
}
@Override
public String getCloseOrderUrl() {
return closeOrderUrl;
}
@Override
public String getRefundUrl() {
return refundUrl;
}
@Override
public String getRefundQueryUrl() {
return refundQueryUrl;
}
@Override
public String getDownloadBillUrl() {
return downloadBillUrl;
}
@Override
public String getReportUrl() {
return reportUrl;
}
}
}
```
3. 调用微信支付统一下单接口,获取支付二维码
```java
@RestController
@RequestMapping("/wxpay")
public class WxPayController {
@Autowired
private WXPay wxPay;
/**
* 生成微信支付二维码
*
* @param orderId 订单ID
* @param amount 支付金额
* @return 二维码图片Base64编码字符串
*/
@GetMapping("/qrcode")
public String generateQrCode(@RequestParam("order_id") String orderId, @RequestParam("amount") int amount) {
try {
Map<String, String> data = new HashMap<>();
data.put("body", "xxxx"); // 商品描述
data.put("out_trade_no", orderId); // 订单号
data.put("total_fee", String.valueOf(amount)); // 支付金额,单位为分
data.put("spbill_create_ip", "127.0.0.1"); // 发起支付的客户端IP
data.put("notify_url", "http://localhost/wxpay/notify"); // 支付成功的回调地址
data.put("trade_type", "NATIVE"); // 交易类型,JSAPI:公众号支付;NATIVE:扫码支付;APP:APP支付
data.put("product_id", orderId); // 商品ID,trade_type=NATIVE时必填
// 调用微信支付统一下单接口
Map<String, String> result = wxPay.unifiedOrder(data);
String return_code = result.get("return_code");
String result_code = result.get("result_code");
if ("SUCCESS".equals(return_code) && "SUCCESS".equals(result_code)) {
String codeUrl = result.get("code_url");
// 生成二维码
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 300, 300);
MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
byte[] bytes = outputStream.toByteArray();
return Base64.getEncoder().encodeToString(bytes);
} else {
String err_code_des = result.get("err_code_des");
throw new RuntimeException(err_code_des);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
```
以上就是基于 Spring Boot 的微信支付二维码生成的简单实现了。需要注意的是,微信支付二维码的有效期为2小时,超过时间后需要重新生成。
阅读全文