前后端完全分离--微信支付

1.流程图

前后端完全分离--微信支付_第1张图片

微信支付开发文档地址:

 2.微信支付需要的依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.12.RELEASE
         
    
    com.qy151
    0813-wxzf
    0.0.1-SNAPSHOT
    0813-wxzf
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.2
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        



        
        
            com.github.wxpay
            wxpay-sdk
            0.0.3
        

        
        
            org.apache.httpcomponents
            httpclient
            4.5.3
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


3.创建一个Httpclient工具类-使用默认浏览器进行远程调用 

package com.qy151.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 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 param) {
      this.url = url;
      this.param = param;
   }
   public HttpClient(String url) {
      this.url = url;
   }
   public void setParameter(Map map) {
      param = map;
   }
   public void addParameter(String key, String value) {
      if (param == null)
         param = new HashMap();
      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 nvps = new LinkedList();
         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;
   }
}

4.创建实体类

package com.qy151.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
 * @BelongsProject: 0813-wxzf
 * @BelongsPackage: com.qy151.entity
 * @unthor : YSH
 * @date : 2022/8/12 21:46
 * @Description: TODO
 */
@Data
@TableName("t_order")
public class Order {

    @TableId(type = IdType.ASSIGN_ID)
    private String id;
    private String orderNo;
    private String courseId;
    private String courseTitle;
    private String courseCover;
    private String teacherName;
    private String memberId;
    private String nickname;
    private String mobile;
    private Double totalFee;
    private Integer payType;
    private Integer status;
    private Integer isDeleted;
    private String gmtCreate;
    private String gmtModified ;
}

5.后端

5.1解决跨域问题的配置类

package com.qy151.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
 
    // 当前跨域请求最大有效时长。这里默认1天
    private static final long MAX_AGE = 24 * 60 * 60;
 
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

5.2Controller层

package com.qy151.controller;

import com.qy151.service.OrderService;
import com.qy151.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: 0813-wxzf
 * @BelongsPackage: com.qy151.controller
 * @unthor : YSH
 * @date : 2022/8/12 21:29
 * @Description: TODO
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //创建订单信息
    @PostMapping("createNavite/{orderNo}")
    public CommonResult createNative(@PathVariable String orderNo){
        return orderService.createNative(orderNo);
    }



    //根据订单状态查询订单的支付情况
    @PostMapping("queryPayStatus/{orderNo}")
    public CommonResult queryPayStatus(@PathVariable String orderNo){
        return orderService.queryPayStatus(orderNo);
    }
}

5.3service层

package com.qy151.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.qy151.dao.OrderMapper;
import com.qy151.entity.Order;
import com.qy151.utils.HttpClient;
import com.qy151.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.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: 0813-wxzf
 * @BelongsPackage: com.qy151.service
 * @unthor : YSH
 * @date : 2022/8/12 21:34
 * @Description: TODO
 */
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Value("${weixin.appid}")
    private String appId;
    @Value("${weixin.mch_id}")
    private String mchId;
    @Value("${weixin.api_key}")
    private String apiKey;

    public CommonResult createNative(String orderNo) {
        //1.根据订单号查询出订单信息
        QueryWrapper wrapper = new QueryWrapper<>();;
        wrapper.eq("order_no",orderNo);
        wrapper.eq("status",0);
        Order order = orderMapper.selectOne(wrapper);
        if (order!=null){
            try {
                //设置请求的参数--格式为xml格式
                Map params = new HashMap<>();//请求参数
                //公众账号ID
                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);
                //支付金额
              //活数据  params.put("total_fee", new BigDecimal(order.getTotalFee()).multiply(new BigDecimal(100)).longValue() + "");

                //死数据
                params.put("total_fee", new BigDecimal(0.01).multiply(new BigDecimal(100)).longValue() + "");

                //终端IP
                params.put("spbill_create_ip", "127.0.0.1");
                //通知地址
                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();

                Map map = WXPayUtil.xmlToMap(content);
                if (map.get("result_code").equals("SUCCESS")){
                    Map result = new HashMap<>();
                    result.put("codeUrl",map.get("code_url"));
                    result.put("price",order.getTotalFee());
                    result.put("orderNo",orderNo);
                    return new CommonResult(2000,"生成二维码成功",result);
                }

            }catch (Exception e){

            }
        }
        return new CommonResult(5000,"订单失效",null);
    }

    public CommonResult queryPayStatus(String orderNo) {
        try {
            //1.根据订单状态查询微信的支付情况
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            Map 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 map = WXPayUtil.xmlToMap(content);
            if (map.get("trade_state").equals("SUCCESS")){
                //1.修改订单状态
                Order order = new Order();
                order.setStatus(1);
                order.setGmtModified(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                QueryWrapper wrapper = new QueryWrapper<>();
                wrapper.eq("order_no",orderNo);
                wrapper.eq("status",0);
                orderMapper.update(order,wrapper);


                //TODO 2.往支付记录中添加支付记录


                return new CommonResult(2000,"支付成功",null);
            }

        }catch (Exception e){
        }
        return new CommonResult(5000,"支付失败",null);
    }
}

5.4mapper层

package com.qy151.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qy151.entity.Order;
import org.apache.ibatis.annotations.Mapper;

/**
 * @BelongsProject: 0813-wxzf
 * @BelongsPackage: com.qy151.dao
 * @unthor : YSH
 * @date : 2022/8/12 21:41
 * @Description: TODO
 */
@Mapper
public interface OrderMapper extends BaseMapper {
}

5.5application配置文件



#数据源
server.port=9000
spring.datasource.url=jdbc:mysql://localhost:3306/weixin?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver









logging.level.com.qy151.weixinpay.dao=debug
# 微信的appid 商家id 密钥---申请你无法申请因为需要营业执照--这里一个老师的
weixin.appid=wx8087d8149331d27c
weixin.mch_id=1532192611
weixin.api_key=Cc158380629071583806290715838062

6.前端

前后端完全分离--微信支付_第2张图片

 






你可能感兴趣的:(Vue,微信)