springboot微信支付,支付二维码生成

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1 微信扫码支付开发者文档,里面会有支付流程的教程

接口链接

首先,我们需要符合微信平台的要求,通过接口连接,返回一下该数据,数据类型为xml格式,我们一般获取code_url的链接即可
{nonce_str=xBkIJKHN7wGdjbgn, code_url= weixin://wxpay/bizpayurl?pr=NbLYUB0, appid=wx5beac15ca207c40c, sign=2D2972919C12A6C5628198AB0B906D74, trade_type=NATIVE, return_msg=OK, result_code=SUCCESS, mch_id=1503809911, return_code=SUCCESS, prepay_id=wx311714159734431013de08ba0340929716}
 
接下来需要导入谷歌
依赖
                <!-- google二维码生成包 -->
                <dependency>
                    <groupId>com.google.zxing</groupId>
                    <artifactId>javase</artifactId>
                    <version>3.3.0</version>
                </dependency>
                        
                <dependency>
                    <groupId>com.google.zxing</groupId>
                    <artifactId>core</artifactId>
                    <version>2.0</version>
                </dependency>
 
导入之后,我们就可以使用它的api了.
接下来://生成二维码配置
Map<EncodeHintType, Object> hints = new HashMap<>();
//设置纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
//编码类型
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
 
将我们获取的code_url(前文已提到的微信文档中,我们请求成功会返回一个code_url,这是关键,有了它才能生成二维码,所以请仔细阅读微信文档),生成二维码,并且条码类型设置为QR_CODE(二维码),宽高为400
BitMatrix bitMatrix = new MultiFormatWriter().encode(code_URl, BarcodeFormat.QR_CODE, 400, 400);
 
通过响应输出流,输出二维码
//响应用输出流输出
OutputStream outputStream = response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix,"png",outputStream);

猜你喜欢

转载自www.cnblogs.com/zexin/p/10206289.html