一、根据订单生成付款二维码
1.导入依赖和配置文件的配置
<!--微信依赖-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--java端发送请求-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!--mpy依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
配置文件:
server.port=9000
#数据库连接的信息
spring.datasource.password=
spring.datasource.username=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/weixin?serverTimezone=Asia/Shanghai
# 微信的appid 商家id 密钥---申请你无法申请因为需要营业执照--
weixin.appid=
weixin.mch_id=
weixin.api_key=
2.导入发送网络请求配置类
package com.gsh.system.utils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* http请求客户端
*
* @author 必须引入httpclient的依赖:在java端模拟浏览器的效果。
*
*/
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public HttpClient(String url, Map<String, String> param) {
this.url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public void setParameter(Map<String, String> map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap<String, String>();
param.put(key, value);
}
public void post() throws ClientProtocolException, IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws ClientProtocolException, IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws ClientProtocolException, IOException {
if (param != null) {
StringBuilder url = new StringBuilder(this.url);
boolean isFirst = true;
for (String key : param.keySet()) {
if (isFirst)
url.append("?");
else
url.append("&");
url.append(key).append("=").append(param.get(key));
}
this.url = url.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
for (String key : param.keySet())
nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
}
if (xmlParam != null) {
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http) throws ClientProtocolException,
IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null)
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// 响应内容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
}
3.controller 层
@PostMapping("/createNative/{orderNo}")
public CommonResult createNative(@PathVariable String orderNo){
return orderService.createNative(orderNo);
}
4.Service层业务代码
获取配置文件的信息
//获取配置文件里的appid
@Value("${weixin.appid}")
private String appId;
//获取配置文件里的mch_id
@Value("${weixin.mch_id}")
private String mchId;
//获取配置文件里的apiKey
@Value("${weixin.api_key}")
private String apiKey;
public CommonResult createNative(String orderNo) {
QueryWrapper<Order>wrapper=new QueryWrapper<>();
wrapper.eq("order_no",orderNo);
wrapper.eq("status",0);
Order order = orderMapper.selectOne(wrapper);
System.out.println(order);
if(order!=null){
try{
//设置请求参数----需要的格式为.xml格式
//我们使用map存放再转为xml
Map<String,String> params=new HashMap<>();//请求参数
params.put("appid",appId);
params.put("mch_id",mchId);
params.put("nonce_str", WXPayUtil.generateNonceStr());
params.put("body",order.getCourseTitle());
params.put("out_trade_no",orderNo);
BigDecimal totalFee = order.getTotalFee();
//double会丢失精度所以我们选择BigDecimal
//params.put("total_fee",totalFee.multiply(new BigDecimal(100)).longValue()+"");
//0.01将来要缓存真实的金额
params.put("total_fee",new BigDecimal(0.01).multiply(new BigDecimal(100)).longValue()+"");
params.put("spbill_create_ip","127.0.0.1");//未来写成项目部署的ip
params.put("notify_url","http://localhost:9000/pay/back");
params.put("trade_type","NATIVE");
//创建HttpClient对象 远程调用
HttpClient client=new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
//支持https协议
client.setHttps(true);
client.setXmlParam(WXPayUtil.generateSignedXml(params,apiKey));
//设置请求方式
client.post();
String content = client.getContent();
System.out.println(content);
//将返回结果的xml转换为Map
Map<String, String> map = WXPayUtil.xmlToMap(content);
if(map.get("result_code").equals("SUCCESS")){
Map<String,Object> result=new HashMap<>();
result.put("codeUrl",map.get("code_url"));//获取生成二维码地址
result.put("price",totalFee);//获取商品价格
result.put("orderNo",orderNo);//获取订单编号
return new CommonResult(2000,"生成二维码",result);
}
}catch (Exception e){
e.printStackTrace();
}
}
return new CommonResult(5000,"订单失效",null);
}
5.前端二维码的展示
<el-button type="primary" @click="pay">支付</el-button>
<el-dialog
title="收银台"
:visible.sync="dialogVisible"
width="30%">
<div style="text-align: center">
<p>微信支付{{payResult.price}}元</p>
<div style="border: 1px solid #f3f3f3;width: 220px;padding: 10px;margin: 0px auto">
<vue-qr
:text="payResult.codeUrl"
:margin="0"
colorDark="green"
:logoSrc="require('@/assets/logo.png')"
colorLight="#fff"
:size="200">
</vue-qr>
</div>
</div>
<el-divider></el-divider>
<div style="font-size: 13px">
提示:<br>
支付成功前请勿手动关闭页面<br>
二维码两小时有效,请及时扫码支付<br>
</div>
</el-dialog>
6.data数据的存放
data(){
return{
orderNo:"e334ce2a6b1d4bc6949",
dialogVisible:false,
payResult:{
price:0,
codeUrl:"",
orderNo:"",
},
timer1:""
}
},
7.前端发送的请求方法
pay(){
this.dialogVisible=true;
this.$http.post("/order/createNative/"+this.orderNo).then(result=>{
if (result.data.code===2000) {
this.payResult = result.data.data
//设置一个定时任务,每隔3秒调用一次
this.timer1=setInterval(()=>{
this.queryPayStatus(this.payResult.orderNo)
},3000)
}
})
},
二、前端调用订单状态查询的方法
//根据订单号查询支付状态
queryPayStatus(orderNo){
this.$http.post("/order/queryPayStatus/"+this.orderNo).then(result=>{
console.log(result.data.code)
if(result.data.code===2000){
console.log("****************************")
//消除定时器
clearInterval(this.timer1)
this.timer1=null;
this.$message.success("支付成功!")
this.dialogVisible=false;
}
})
},
三、后台查询订单状态
1.controller层
@PostMapping("/queryPayStatus/{orderNo}")
public CommonResult queryPayStatus(@PathVariable String orderNo){
return orderService.queryPayStatus(orderNo);
}
2.Service业务层
public CommonResult queryPayStatus(String orderNo) {
try {
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
Map<String,String> params=new HashMap<>();
params.put("appid",appId);
params.put("mch_id",mchId);
params.put("out_trade_no",orderNo);
params.put("nonce_str",WXPayUtil.generateNonceStr());
client.setHttps(true);
//根据订单状态查询微信的支付情况
client.setXmlParam(WXPayUtil.generateSignedXml(params,apiKey));
client.post();
String content = client.getContent();
System.out.println(content);
//将返回的xml内容转为map内容
Map<String, String> map = WXPayUtil.xmlToMap(content);
//修改订单状态
if(map.get("trade_state").equals("SUCCESS")) {
Order order = new Order();
order.setStatus(1);
order.setGmtModified(LocalDateTime.now());
QueryWrapper<Order> wrapper=new QueryWrapper<>();
wrapper.eq("order_no",orderNo);
wrapper.eq("status",0);
orderMapper.update(order,wrapper);
return new CommonResult(2000,"支付成功!",null);
}
} catch (Exception e) {
}
return new CommonResult(5000,"支付失败!",null);
}
该博客介绍了如何在后端通过Java实现微信支付的二维码生成,并结合前端展示二维码,以及订单状态的查询。首先,配置了相关依赖并设置了微信支付所需的参数。接着,展示了如何使用HttpClient发送HTTP请求,包括POST、PUT和GET操作。然后,在Controller和Service层处理订单创建及查询支付状态的逻辑。最后,前端通过Vue.js展示支付二维码,并监听支付状态的变化,实现支付成功的通知。
2164

被折叠的 条评论
为什么被折叠?



