1.生成二维码
导入谷歌二维码maven依赖
<!-- 谷歌二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 微信支付SDK -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--使用httpclient需要的maven依赖 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
生成一个JSON格式的支付串 使用fastjson
例:
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","贺庆玉")
jsonObject.put("age","20")
//JSON对象转JSON格式支付串
String jsosntring = jsonObject.toString();
创建一个矩阵对象
//设置二维码宽高
int width = 100;
int height = 100;
//创建Map集合
Map<EncodeHintType,Object> map = new HashMap<EncodeHintType,Object>();
//key:指定一个支付集
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//创建矩阵对象 key 格式 宽 高
BitMatrix bitmatrix = new
MultiFormatWriter().encode(jsonString,BarcodeFormat.QR_CODE,width,height,map);
将矩阵对象生成一个二维码图片
//创建路径对象
//生成路径
String filePath = "D://";
String fileName = "QRCode.png";
//java.nio.file
Path path = FileSystems.getDefault().getPath(filePath,fileName)
//将矩阵对象生成一个二维码图片 矩阵对象 图片格式 路径对象
MatrixToImageWriter.writeToPath(bitmatrix,"png",path)
地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=2_2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gCG9uoDH-1598944723995)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\image-20200623110657149.png)]
@Controller
public class WXpayController{
@RequestMApping(value="/api/wxpay")
public @ResponseBody Object wxpay(HttpServletRequest request,
@RequestParam(value = "out_trade_no",required=true) String out_trade_no,
@RequestParam(value = "total_free",required=true) String total_free, @RequestParam(value = "boby",required=true) String boby){
Map<String,String> requestDataMap = new HashMap<String,String>();
requestDataMap.put("appid",""); //应用ID
requestDataMap.put("mch_id",""); //商户号
//生成随机字符串
String noncestr = WXPayUtil.generateNonceStr();
requestDataMap.put("nonce_str",""); //随机字符串
requestDataMap.put("body",boby); //商品描述
requestDataMap.put("out_trade_no",out_trade_no); //商品订单号
requestDataMap.put("total_fee",total_free); //金额
InetAddress localHst = InetAddress.getLocalHost();
String hostAddress = localHost.getHostAddress();
requestDataMap.put("spbill_create_ip",hostAddress); //IP终端
requestDataMap.put("notify_url","http://localhost:8081/api/pay/wxpayNotity"); //通知地址
requestDataMap.put("trade_type","NATIVE "); //商品类型
requestDataMap.put("product_id",out_trade_no); //商品ID
/**生成签名*/ //你自己的key
String signValue = WXPayUtil.generateSignaturesDataMap(requestDataMap,""key"");
requestDataMap.put("sign",signValue); //签名
/**将参数为map类型的转为xml*/
String requestDataXml = WXPayUtil.mapToXml(requestDataMap);
//调用pay工程的微信支付接口 发送调用统一下单API接口,响应的是xml格式的结果
String responseDataXml = HttpClientUtils.doPostByXml("https://api.mch.weixin.qq.com/pay/unifiedorder",requestDataXml)
return responseDataXml;
}
}
/**
*请求格式为XML的Post请求
*/
public static String doPostByXml(String url, String requestDataXml){
CloseableHttpClient httpClient = null;
CloseableHttpResPonse httpResponse = null;
//创建HTTPClient连接对象
httpClient = HttpClients.createDefault();
//创建post请求连接对象
HttpPost httpPost = new HttpPost(url);
//创建连接请求参数对象,并设置连接参数
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(15000)//连接服务器主机超时时间
.setConnectionRequesTimeout(60000)//连接请求超时时间
.setSocketTimeout(60000) //设置读取响应数据超时时间
.build();
//为httpPost请求设置参数
httpPost.setConfig(requestConfig);
//将上传参数放到Entity属性中
httpPost.setEntity(new StringEntity(requestDataXml,"UTF-8"));
//添加头信息
httpPost.addHeader("Content-Type","text/xml");
String result = "";
try{
//发送请求
httpResponse = httpClient.execute(httpPost);
//从响应对象中获取返回内容
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity,"UTF-8");
}catch(IOException e){
e.printStackTrace();
}
return result;
}