电商系统前后端开发(Vue+Springboot)(25) - 回应消息给微信后台

1 回应消息给微信后台

  • 支付结果通知,https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_7&index=8

  • Controller
@Controller
@RequestMapping("/wxpay")
public class WXPayController {

    @RequestMapping("/notify_url")
    public void getNotifyURL(HttpServletResponse response, HttpServletRequest request) throws IOException {

        // 获取微信发送来的请求
        ServletInputStream is = request.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while((len = is.read(b))!= -1){
            String str = new String(b, 0, len);
            System.out.print(str);
        }

        // 返回一个标准格式的回信
        response.getWriter().write("");
    }
}

2 微信支付整合到 springboot

  1. 整合 WXPay 的 pom 中的相关依赖到项目
  2. 把 sdk 中的类和接口引入进来
  3. 调整 sdk 相关包的路径

2.1 pom


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.16.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.tzb.wxpaygroupId>
    <artifactId>mywxpayartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mywxpayname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            
            <version>5.1.49version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
            <scope>truescope>
        dependency>


        
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.5.3version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.21version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
            <version>1.7.21version>
        dependency>

    dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>2.1.16.RELEASEversion>
            plugin>
        plugins>
    build>

project>

2.2 Zxing 二维码生成

 
        <dependency>
            <groupId>com.google.zxinggroupId>
            <artifactId>coreartifactId>
            <version>3.3.3version>
        dependency>

        <dependency>
            <groupId>com.google.zxinggroupId>
            <artifactId>javaseartifactId>
            <version>3.3.3version>
        dependency>

2.3 springboot 使用 Thymeleaf

 <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
dependency>

电商系统前后端开发(Vue+Springboot)(25) - 回应消息给微信后台_第1张图片


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>支付页面title>
head>
<body>
<h1>扫码支付h1>

<img th:src="@{/wxpay/doPay}"/>
body>
html>

2.4 完成支付业务

  • Controller
@Controller
@RequestMapping("/wxpay")
public class WXPayController {

    @RequestMapping("/showpay")
    public String showPay() {
        System.out.println("进入了 showPay");
        return "MyPay";

    }

    // 编写下单接口
    @RequestMapping("/doPay")
    public void doPay(HttpServletResponse response) {

        // 用于生成订单编号的随机数
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHssmm");
        String orderIdPrefix = sdf.format(date);

        // 设备id
        String sId = "1001";
        String orderId = orderIdPrefix + sId;

        // 创建配置类对象
        MyWXPayConfig config = new MyWXPayConfig();

        // 创建支付对象
        WXPay wxpay = null;
        try {
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "腾讯充值中心-QQ会员充值");
            data.put("out_trade_no", orderId);
            data.put("device_info", "");
            // 货币单位,分
            data.put("fee_type", "CNY");
            // 1 分
            data.put("total_fee", "1");

            // 此 ip 地址会被记录
            data.put("spbill_create_ip", "123.12.12.123");

            // 需要一个回调接口,获取此次微信支付的信息
            data.put("notify_url", "http://uyqi33.natappfree.cc/wxpay/notify_url");
            // 此处指定为扫码支付
            data.put("trade_type", "NATIVE");
            data.put("product_id", "12");

            // 向微信后台发起支付请求
            Map<String, String> resp = wxpay.unifiedOrder(data);
            // 响应数据中有而二维码链接,code_url
            System.out.println(resp);
            String code_url = resp.get("code_url");

            // 生成二维码
            HashMap<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            hints.put(EncodeHintType.MARGIN, 2);

            BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, 200, 200, hints);
            MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
            System.out.println("二维码创建完成");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/notify_url")
    public void getNotifyURL(HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 获取微信发送来的请求
        ServletInputStream is = request.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = is.read(b)) != -1) {
            String str = new String(b, 0, len);
            System.out.print(str);
        }
        // 返回一个标准格式的回信
        response.getWriter().write("");
    }


}

你可能感兴趣的:(#,springboot,微信支付)