1.微信支付的流程
1.1业务说明
业务流程说明:
(1)商户后台系统根据用户选购的商品生成订单。
(2)用户确认支付后调用微信支付【统一下单API】生成预支付交易;
(3)微信支付系统收到请求后生成预支付交易单,并返回交易会话的二维码链接code_url。
(4)商户后台系统根据返回的code_url生成二维码。
(5)用户打开微信“扫一扫”扫描二维码,微信客户端将扫码内容发送到微信支付系统。
(6)微信支付系统收到客户端请求,验证链接有效性后发起用户支付,要求用户授权。
(7)用户在微信客户端输入密码,确认支付后,微信客户端提交授权。
(8)微信支付系统根据用户授权完成支付交易。
(9)微信支付系统完成支付交易后给微信客户端返回交易结果,并将交易结果通过短信、微信消息提示用户。微信客户端展示支付交易结果页面。
(10)微信支付系统通过发送异步消息通知商户后台系统支付结果。商户后台系统需回复接收情况,通知微信后台系统不再发送该单的支付通知。
(11)未收到支付通知的情况,商户后台系统调用【查询订单API】(查单实现可参考:支付回调和查单实现指引)。
(12)商户确认订单已支付后给用户发货。
微信支付开发的参考网址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
2.后端
2.1 导入相关依赖
<!-- 微信支付需要的依赖-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!-- java端发送请求:在java端模拟浏览器远程访问微信的接口-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
2.2 配置application.properties文件
server.port=9000
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/weixin?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=root
logging.level.com.wt.weixinpay.dao=debug
weixin.appid=wx8087d8149331d27c
weixin.mch_id=1532192611
weixin.api_key=Cc158380629071583806290715838062
2.3 引入httpclient的依赖类
package com.wt.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;
}
}
2.4 controller层
package com.wt.controller;
import com.wt.service.IOrderService;
import com.wt.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
/**
* <p>
* 订单 前端控制器
* </p>
*
* @author wt
* @since 2022-08-12
*/
@RestController
@RequestMapping("/wt/order")
public class OrderController {
@Autowired
private IOrderService orderService;
//通过订单好查询订单信息并返回二维码地址
@PostMapping("/payCha/{orderNo}")
public CommonResult payCha(@PathVariable String orderNo){
CommonResult result = orderService.selcPayCha(orderNo);
return result;
}
1.根据状态查询微信支付的情况,修改数据库状态
@PostMapping("/queryPayStatus/{orderNo}")
public CommonResult queryPayStatus(@PathVariable String orderNo){
System.out.println(orderNo);
CommonResult result = orderService.queryPayStatus(orderNo);
return result;
}
}
2.5 service层
package com.wt.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.wt.entity.Order;
import com.wt.mapper.OrderMapper;
import com.wt.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.utils.HttpClient;
import com.wt.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 订单 服务实现类
* </p>
*
* @author wt
* @since 2022-08-12
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
@Autowired
private OrderMapper orderMapper;
//引入application.properties中的内容,将其复制值给value,并由value赋给对应的变量
@Value("${weixin.appid}")
private String appId;
@Value("${weixin.mch_id}")
private String mchId;
@Value("${weixin.api_key}")
private String apiKey;
//根据订单号向微信端接口发送请求并返回二维码地址
@Override
public CommonResult selcPayCha(String orderNo) {
System.out.println(orderNo);
//1.根据订单号查询订单信息
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<String,String> map = new HashMap<>();
map.put("appid",appId);
map.put("mch_id",mchId);
map.put("nonce_str", WXPayUtil.generateNonceStr());
map.put("body",order.getCourseTitle());
map.put("out_trade_no",orderNo);
//map.put("total_fee",new BigDecimal(order.getTotalFee()).multiply(new BigDecimal(100)).longValue()+"");
map.put("total_fee",new BigDecimal(0.01).multiply(new BigDecimal(100)).longValue()+"");
System.out.println("111111");
map.put("spbill_create_ip","127.0.0.1"); //未来改为项目部署的ip
System.out.println("22222222");
map.put("notify_url","http:localhost:9000/pay/back");
map.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(map,apiKey));
//发送请求
client.post();
//获取请求的响应结果
String content = client.getContent();
System.out.println(content);
Map<String, String> maps = WXPayUtil.xmlToMap(content);
System.out.println("33333333333333");
System.out.println(maps.get("result_code"));
if (maps.get("result_code").equals("SUCCESS")){
Map<String,String> result = new HashMap<>();
result.put("codeUrl",maps.get("code_url"));
result.put("price",order.getTotalFee());
result.put("orderNo",orderNo);
return new CommonResult(2000,"二维码生成成功",result);
}
}catch (Exception e){
}
}
return new CommonResult(5000,"查询失败",123);
}
//查询订单状态
@Override
public CommonResult queryPayStatus(String orderNo) {
try{
//1.根据状态查询微信支付的情况
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();
Map<String,String> map = WXPayUtil.xmlToMap(content);
if (map.get("trade_state").equals("SUCCESS")){
//1.修改订单状态
Order order = new Order();
order.setStatus(1);
LocalDateTime now = LocalDateTime.now();
order.setGmtModified(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);
}
}
3.前端
3.1 main文件的配置
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import axios from "axios"
Vue.config.productionTip = false
//设置基础url路径
axios.defaults.baseURL="http://localhost:9000"
//把axios挂载到Vue对象中,以后在Vue对象中如果使用axios直接可以用$http名称($http可以随意替换)
Vue.prototype.$http=axios;
new Vue({
router,
render: h => h(App)
}).$mount('#app')
3.2 下载相应的vue-qr插件
npm install vue-qr --------借助vue-qr 可以把二维码地址转换为二维码图片
3.3 前端布局
<template>
<div id="app">
<el-button type="primary" @click="pay">支付</el-button>
<el-dialog
title="收银台"
:visible.sync="dialogVisible"
width="30%"
>
<div>
<p>微信支付{{payResult.price}} 元</p>
<div style="border: 1px solid #f3f3f3;width: 220px;padding: 10px;margin: 0px auto">
<!-- 使用vue-qr组件-->
<vue-qr
:logoSrc="require('@/assets/logo.png')"
:margin="0"
:size="200"
:text="payResult.codeUrl"
colorDark="green"
colorLight="#fff"
>
</vue-qr>
</div>
</div>
<el-divider></el-divider>
<div style="font-size: 13px">
提示:<br>
支付成功前请勿手动关闭页面
二维码两小时内有效,请及时扫码支付
</div>
</el-dialog>
</div>
</template>
<script>
//引入vue-qr组件
import vueQr from "vue-qr"
export default {
name: 'app',
components:{
//注册vue-qr组件
vueQr
},
data(){
return {
orderNo:"def097eeafe44d7b8df",
payResult:{
price:0,
//借助vue-qr 可以把二维码地址转换为二维码图片
codeUrl:"",
orderNo:""
},
dialogVisible:false,
timer1:""
}
},
created() {
},
methods:{
queryPayStatus(orderNo){
console.log("====================")
console.log(orderNo)
//根据订单号查询支付状态
this.$http.post("/wt/order/queryPayStatus/"+orderNo).then(result => {
if (result.data.code===2000){
//消除定时器
clearInterval(this.timer1);
this.timer1 = null;
this.$message.success("支付成功");
this.dialogVisible=false;
}
})
},
pay(){
this.dialogVisible = true
this.$http.post("/wt/order/payCha/"+this.orderNo).then(result=>{
if (result.data.code==2000){
this.payResult = result.data.data;
console.log(result)
//定时器,每3秒发送一次订单状态查询请求
this.timer1 = setInterval(()=>{
this.queryPayStatus(this.payResult.orderNo)
},3000)
}
})
}
}
}
</script>
<style>
</style>