Java calls the latest version of Alipay interface

The latest version of the Java Alipay interface!

Technology: Java+SpringBoot+Maven+Sandbox Test

Time: Released on March 1, 2022

  1. There is not much introduction to adding Web modules to new SpringBoot, and it will definitely be

  2. Add Alipay dependency

    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>4.22.32.ALL</version>
    </dependency>
    
  3. Start configuring the sandbox

    Address: https://open.alipay.com/platform/appDaily.htm

    In the old version, the public key and private key need to be automatically generated, but the new version has already generated it for you, and you can use it directly...

    Sandbox Introduction:

insert image description here

Click on the public key mode to view , which is used later in the parameters

insert image description here

insert image description here

insert image description here
An account is a buyer and a seller is relatively simple , so I will introduce it here, and start to code below

  1. Simulate a page to facilitate POST requests, the code is as follows

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>支付宝支付测试</title>
        </head>
        <body>
            <h1>商品购买页面</h1>
            <form action="/buy" method="post">
                <p>商品单号:<input type="text" name="out_trade_no"></p>
                <p>商品名称:<input type="text" name="subject"></p>
                <p>付款金额:<input type="text" name="total_amount"></p>
                <p>商品描述:<input type="text" name="body"></p>
                <p>支付超时时间:<input type="text" name="timeout_express"></p>
                <p>销售产品码:<input type="text" name="product_code"></p>
                <p><input type="submit" value="提交"></p>
            </form>
        </body>
    </html>
    
  2. Start to do some basic configuration for the transaction. The following config classes are all annotated, so I won’t introduce them one by one. Most of them are the parameters used when introducing the sandbox above.

    I will jump to Baidu if the payment is successful here, you can according to your needs

    If you don't like to use Java classes, you can also use configuration files. After that, the IO reading is the same. The code is as follows

    /**
     * @author: ZhouXK
     * @date: 2022/3/1-17:06
     * @describeL: 支付宝接口参数
     */
    public class AliPayConfig {
          
          
    	/**
    	 * 商户APPId
    	 */
    	public static String APPID = "2021000119626311";
    	/**
    	 * 私钥
    	 */
    	public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC78A16/43Px6/HdN4s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vc5TPRVBQaOi5JSYm4jHemvvDlf359LI7ip7QAJ1uTxwsPencGMvw3naOGOYedjXFqSgnL68CAVVn5g6RUa2FbIZNYixAK9cCKMfE2jdYKFbzvTwl8+Oi0d7AoGAaTJ3WXNbfp/F+koAsiKSkAzGO3EOrgYaIXYycjDlyVm/uaM55sO/E8QIAIYVxy5D1OAWOgLAyCP/VArhU3/NdSUbhgbCXkGW0o+XCrZPq/i+WfDjcooTFubQyqVomy9DcYeRU+wJGXFqVW+yJTHB0Gzt2jer9bU54y7X/TmD4AMCgYEA3L3SYNFh9txoOd7dtJaBzj52Eiu6LOWGV2wQLYoiKLfaoa7+RUjDK6CWs6LxVd6aKtjOchxNA052Feu1XyMNKNlbfT5HPpOrGcoLCn0qHYX/D3/jiftECGz7M6Sp9jvLXamiIMhGQCm8Dnv9PCA0DIXdsQJ4ldRvZ5RbvnT244c=";
    	/**
    	 * 异步通知URL
    	 */
    	public static String NOTIFY_URL = "https://www.baidu.com/";
    	/**
    	 * 同步通知URL
    	 */
    	public static String RETURN_URL = "https://www.baidu.com/";
    	/**
    	 * 请求网关地址
    	 */
    	public static String URL = "https://openapi.alipaydev.com/gateway.do";
    	/**
    	 * 编码格式
    	 */
    	public static String CHARSET = "UTF-8";
    	/**
    	 * 返回格式
    	 */
    	public static String FORMAT = "json";
    	/**
    	 * 支付宝公钥
    	 */
    	public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlhRcorwMswcYN61r8AY7/qX6vASrDKbr1N4GvXUV1z7kIJvMNCiNrbO90pqr788gqf7TJVIRjWjgvVhTZiYWUBveKeho2C4XnZXFk2d6QY2jOBpWCXsjC+5ggp4njDdwj5thD2ujo7/reepgpC+KHHaPJ5iZ4JTifDHE4qJ1KBJNIikmNokQIT3YNKpdYSD8eRhQqi2lXOo1Fj7IuKESMGdG+JHo1p+s0Z3XTdVkuWlNxxClO0WxU89Vq+0FRbSh4R2jaC+LiKwtwHVUnYkbSgmToukb4vBjjmB2HbWYKbKJw018RMiDhziRVYjupF6wBPyfUuSkubCj4//yYgi8xwIDAQAB";
    	/**
    	 * 加密方式
    	 */
    	public static String SIGNTYPE = "RSA2";
    }
    
  3. Next, write the Controller layer

    /**
     * @author: ZhouXK
     * @date: 2022/3/1-17:19
     * @describeL: 支付宝支付接口
     */
    @Controller
    public class ShopAlipayController {
          
          
    
        @PostMapping("/buy")
        @ResponseBody
        //这个接收的参数就是前端页面那几个参数,这是MVC的东西,如果可以我建议你封装一个DTO对象啥的...
        public String test(HttpServletResponse response, String out_trade_no, String subject, String total_amount, String body, String timeout_express, String product_code) throws IOException {
          
          
            /**
    		 * 支付宝签名方式,参数讲解,跟配置类里面的参数是照应的,如果不想要配置类,直接把值写到这里面也ok
    		 * 1.请求网关
    		 * 2.收款人Id
    		 * 3.支付宝私钥
    		 * 4.返回格式
    		 * 5.编码格式
    		 * 6.支付宝公钥
    		 * 7.加密方式
    		 */
            AlipayClient alipayClient = new DefaultAlipayClient(
                AliPayConfig.URL,
                AliPayConfig.APPID,
                AliPayConfig.RSA_PRIVATE_KEY,
                AliPayConfig.FORMAT,
                AliPayConfig.CHARSET,
                AliPayConfig.ALIPAY_PUBLIC_KEY,
                AliPayConfig.SIGNTYPE
            );
            //创建Request请求,这个是支付宝提供的
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            // 封装参数
            AlipayTradeWapPayModel payModel = new AlipayTradeWapPayModel();
            payModel.setOutTradeNo(out_trade_no);
            payModel.setSubject(subject);
            payModel.setTotalAmount(total_amount);
            payModel.setBody(body);
            payModel.setTimeoutExpress(timeout_express);
            payModel.setProductCode(product_code);
            request.setBizModel(payModel);
            //设置异步地址
            request.setNotifyUrl(AliPayConfig.NOTIFY_URL);
            //设置同步回调地址
            request.setReturnUrl(AliPayConfig.RETURN_URL);
            //生成表单
            String form = null;
            try {
          
          
                form = alipayClient.pageExecute(request).getBody();
            } catch (AlipayApiException e) {
          
          
                e.printStackTrace();
            }
            return form;
        }
    }
    

    Then request localhost:8080 to jump to index.html by default, fill in the parameters casually, and the request is over

insert image description here

Browser payment, it is also possible to use a sandbox account, or directly download a sandbox version of the APP

insert image description here

The rest is to jump to Baidu after successful login and payment... The tutorial is over when you get here, come on, be a worker!

Guess you like

Origin blog.csdn.net/weixin_53059814/article/details/123214776