How does java borrow a third party to quickly develop wechat payment function (v3)

Posted by sriphp on Wed, 22 Dec 2021 03:37:53 +0100

Data required for wechat payment

Official account / merchant number

  • The official account number of WeChat is selected, service number or enterprise number is selected. Subscription number does not support WeChat payment function.
  • Wechat merchant account application (to be reviewed)

Above can get WeChat's official account number appid and merchant's mchid.

  • v3 certificate

https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml
Wechat provides a wechat payment platform certificate download tool to download the certificate according to the required merchant number and company name (wechat payment API certificate is required for red envelope sending / enterprise payment / refund and other operations)

The above certificates can be obtained

  • api secret key and apiv3 secret key (in the download platform certificate interface and callback notification of wechat payment apiv3, in order to prevent the message from being maliciously tampered by others, the server will encrypt the data. After receiving the message, the merchant shall decrypt the plaintext, and the key used in the decryption process is the apiv3 key)

Log in to wechat payment merchant platform and enter [account center] - [account settings] - [API security] - [APIv3 key] to set
api secret key and apiv3 secret key can be obtained above

maven, project configuration, configuration file

Note: all implementations of the project are based on the IJpay demo springboot project of IJpay source code

resources

Create a cert file and put the three downloaded certificate files under cert

pom.xml

The dependencies that have been applied to the production environment in my project (including eureka, oauth2, etc.) are attached below

<dependencies>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--         sqlserver -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/sqljdbc4-4.0.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-dts</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.dachao.parameter</groupId>
            <artifactId>parameter</artifactId>
            <version>1.0.1-SNAPSHOT</version>
        </dependency>
        <!--        // Expansion pack -- >
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--SpringBoot integrate redis rely on-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>


        <!-- IJPay -->
        <dependency>
            <groupId>com.github.javen205</groupId>
            <artifactId>IJPay-All</artifactId>
            <version>${ijapy.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.xkzhangsan</groupId>
            <artifactId>xk-time</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.youxuewang</groupId>
            <artifactId>yxwApi</artifactId>
            <version>1.1.0-SNAPSHOT</version>
        </dependency>
        <!-- hutool tool -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier><!-- jdk edition -->
        </dependency>
    </dependencies>

IJPay source code

https://gitee.com/javen205/IJPay
You can download the source code and read it to understand the implementation principle. It encapsulates the api for calling wechat. You need to download it and paste part of the code from the source code for modification

First, complete the preparations, and the wxpay in the source code ijpay demo springboot_ v3. Put properties under resources in your project

Domain is the domain name authorized by wechat. Please refer to the official document for details Wechat payment development document

v3.appId=official account id
v3.keyPath=key.pem(resources Under file cert The absolute path of the file. It is recommended to access it under the server folder after local completion)
v3.certPath=cert.pem ((above)
v3.certP12Path=cert.p12((above)
v3.platformCertPath=wx_cert.pem(call WxPayV3Controller Medium get Interface generation)
v3.mchId=Merchant id
v3.apiKey3= custom apiv3 Secret key
v3.apiKey= custom apiv Secret key
v3.domain= Wechat authorized domain name

Configure the appid, mchid and other data obtained above here and bring WxPayV3Bean

code implementation

WxPayV3Controller

  1. Generate platform cert pem

The source code under com ijpay. demo. controller. wxpay. Copy the wxpayv3controller file to its own project, and configure the dependency to make the project run
Call the get method to generate platformcert PEM file.

JSAPI (official account) Order

Take any openid and call the / v3/jsApiPay interface under the project
The returned results are official documents( jaspi orders )Send a description and return the results

The following is the return result of self adjustment of my production environment.

The data returned by this interface can make the foreground call evoke payment jsapi evoke payment

Topics: Java Maven Spring Boot Spring Cloud wechat