Haven't you mastered the WeChat payment interface test yet?

Native payment document: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1

(The actual development must be read below)

==============================================================================

WeChat payment application (understand)

Step 1: Register an official account (type must be: service account)

Please select the following subject registration according to the type of business license: individual industrial and commercial households | enterprises / companies | government | media | other types.

Step 2: Authenticate the official account

You can apply for WeChat payment only after the official account is authenticated. Authentication fee: 300 yuan/time.

Step 3: Submit information to apply for WeChat payment

Log in to the public platform, click on the left menu [WeChat Pay], start to fill in the information and wait for review. The review time is within 1-5 working days.

Step 4: Successfully open an account, log in to the merchant platform for verification

After the information is approved, please log in to the contact's mailbox to check the merchant number and password, and log in to the merchant platform to fill in the small amount of funds provided by Tenpay to complete the account verification.

Step 5: Sign the agreement online

This agreement is an online electronic agreement, transactions and fund settlement can only be carried out after signing, and it will take effect immediately after signing.

After completing the above steps, you can get the account and key used to call the API

Really test, therefore, the interface we write, the money will be given to the corresponding official account. The money must be set to 1 cent.

appid: The unique identifier of WeChat public account or open platform APP wx8397f8696b538317

mch_id: Merchant number 14734268802

key: Merchant key T6m9iK73b0kn9g5v426MKfHQH7X8rKwb

WeChat payment development documentation and SDK
online WeChat payment development documentation:

https://pay.weixin.qq.com/wiki/doc/api/index.html

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

The overall idea of ​​calling the WeChat payment interface:

Assemble the parameters according to the API requirements and send (POST) to the WeChat payment interface (URL) in XML, and the WeChat payment interface also responds in XML. The program generates a QR code or judges the status of the order based on the returned result (including the payment URL).

We unzip the SDK downloaded from the official website and install it to the local warehouse

Corresponding methods are provided under the com.github.wxpay.sdk.WXPay class:

Insert picture description here

Actual development

1. Create a maven project for wxpay_test

Insert picture description here

2. Import the corresponding dependencies

<dependencies>
        <dependency>
            <groupId>com.github.wxpay</groupId>
            <artifactId>wxpay-sdk</artifactId>
            <version>3.0.9</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

3. Create configuration class Note: The registration must be com.github.wxpay.sdk

The APP uniquely identifies the merchant id and the merchant key both require us to apply for an official account. The following certified ones can be used

package com.github.wxpay.sdk;

import java.io.InputStream;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2020/3/6 16:46
 */
public class MyConfig extends WXPayConfig{
    
    

    /**
     * appid:微信公众账号或开放平台APP的唯一标识  wx8397f8696b538317
     *
     * mch_id:商户号  1473426802
     *
     * key:商户密钥   T6m9iK73b0kn9g5v426MKfHQH7X8rKwb
     * @return
     */
    @Override
    String getAppID() {
    
    
        return "wx8397f8696b538317";  // app的唯一表示
    }

    @Override
    String getMchID() {
    
    
        return "1473426802";  // 商户号
    }

    @Override
    String getKey() {
    
    
        return "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb";  // 商户秘钥
    }

    @Override
    InputStream getCertStream() {
    
    
        return null;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
    
    
        return new IWXPayDomain() {
    
    
            @Override
            public void report(String s, long l, Exception e) {
    
    

            }

            @Override
            public DomainInfo getDomain(WXPayConfig wxPayConfig) {
    
    

                 // 微信支付接口 URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
                return new DomainInfo("api.mch.weixin.qq.com", true);
            }
        };
    }
}

4. Write a test class

package javaclimber.test;

import com.github.wxpay.sdk.MyConfig;
import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2020/3/6 16:53
 */
public class PayTest {
    
    
    public static void main(String[] args) throws Exception {
    
    

        // 1.1 创建配置类
        MyConfig myConfig=new MyConfig();
        // 1. 创建一个微信客户端
        WXPay wxPay=new WXPay(myConfig);

        // 根据官方文档 必填值封装数据
        // 文档地址  https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
        Map<String,String> map = new HashMap<>();
        map.put("body", "JavaClimber");   // 商品描述
        map.put("out_trade_no", "18003540314"); // 商户订单ID
        map.put("total_fee", "1");          //标价金额 单位是分,代表1分钱。 (学习中不要设置别的值,毕竟都是钱)
        map.put("spbill_create_ip", "127.0.0.1");  //公网IP 学习中没有就写本地
        map.put("notify_url", "https://blog.csdn.net/qq_37126480");  // 通知地址 回调地址
        map.put("trade_type", "NATIVE");  // 交易类型(详情见官方文档)

        // URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
        // 2. 进行支付
        Map<String, String> resultMap = wxPay.unifiedOrder(map);
        // 3. 返回结果
        // code_url=weixin://wxpay/bizpayurl?pr=f21q7E6 返回结果里用这个code_url的值 生成二维码就可以了。
        System.out.println(resultMap);
    }

}

The final result is as follows

{
    
    nonce_str=mzLTeHCgd3hJgI1U, code_url=weixin://wxpay/bizpayurl?pr=MGLlqLe, appid=wx8397f8696b538317, sign=745CCD2A8E8CA787EFA4E2A86C394F01BD53971E0F8731E4D9C52EAA30D62847, trade_type=NATIVE, return_msg=OK, result_code=SUCCESS, mch_id=1473426802, return_code=SUCCESS, prepay_id=wx0619414423579251a87d801e1436394200}

Note that success must have the following two parameters

result_code=SUCCESS return_code=SUCCESS

5. Test the QR code

From the above return value, we find out code_url=weixin://wxpay/bizpayurl?pr=MGLlqLe
just need its value weixin://wxpay/bizpayurl?pr=MGLlqLe to convert it to any webpage that generates a QR code on the Internet. Become a QR code for WeChat receipt Money is what we stipulate in the code.

Guess you like

Origin blog.csdn.net/qq_37126480/article/details/104702982
Recommended