2021-02-11-JavaWeb website access Alipay payment

Preface

  • Today I will share with you how to access Alipay payment, although it may not be necessary, after all, a larger company will not allow an ordinary employee to write a financial module, but what if you go to a small company? Haha, just kidding
  • This blog will list the steps to access Alipay. If you don’t understand, you can ask me or Baidu.

Preliminary steps

  • To Alipay open platform to register an account
  • Go to the document center to find the payment document of the computer website , click to open it and see the quick access

Access

pom.xml

  • Click on the link in the figure to go in and see the link that maven depends on, just go in and copyList item
  • The dependency I used before
19年11月使用的依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>3.7.110.ALL</version>
</dependency>

Get a test account in a sandbox environment, and the account that pays for it

Insert picture description here

  • Some information that the controller needs to fill in and the test account of the scan code payment are all in the picture.
    Insert picture description here

The public key private key can be generated by the following tool

  • Link: https://pan.baidu.com/s/1GNB1V3-cUNTAlvHKl0_x8w
    Extraction code: 2dqo
  • You can also download it in the document center by yourself

Insert picture description here

Controller layer

  • After completing the above steps, you must write code in the Controller layer of your project.
  • The following controller is copied directly from the access document, and then some information and public key of my project are filled in
  • If you don’t understand, ask me or read the access documentation. The access documentation is very detailed, much better than WeChat
@RequestMapping(value = "/pay")
    public void pay(HttpServletResponse httpResponse, String oid) throws Exception {
        // 1.获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(

                // 支付接口url
                //https://openapi.alipaydev.com/gateway.do
                "https://openapi.alipaydev.com/gateway.do",
                // APPI
                "2016100656227",
                // 商家的私钥
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCuRzePWKineX1AKR5780URiwxC3ObTQJfBtSCsQxLs+xmYPQS0YBQX/eIGkKB14tPXrVt8zvssNkcN7GaaX82b3CvOX0azZHL9lZAyDjPUkhlVp0SnyP2pO75EiMw+Ldy8pDPyzTcCERaTbZWn829JsW1urR/6zUk7waAb7QTB3G2RABaH7jNfihV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gZqfwNNQ5qmOFeJM4PKRx6QP4o",

                // 格式化编码
                "JSON",

                // 编码格式
                "utf-8",

                // 支付宝的公钥(在沙箱环境中可以获取)
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArC+n7jVK2EtqhtynNGDxdFVYrwT8jP2gu3VsRu7CSfdsAfuDTrwmeuKeaQCBQ19gSAslwTzfip8ebIsiA6jnhRrpNbd+c3FxBxHR6tB6pG2tvBPB6EtQAStRTjPWkiTF1laTbcynuKsiW8X4dgX92PxPfW/Fx8bHgDgxrEBhrURUtZHuQh6VBdUyLHAnpy2DU5K1gw3s8anGoOMnc4tFsXW4yDEZQrspxbhtLiU7tWc4fvd4GEQ7Ye7OoPIWtUZq1iyra8TMtT2KIhyYRqj+1K21HyMsJkmf0cvwIDAQAB",

                // 签名的算法
                "RSA2");

        // 2.创建API对应的request
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://localhost:8081/"); // 用户支付完比后跳转到页面,同步
        // 用户支付成功异步通知的请求
        alipayRequest.setNotifyUrl("http://8srwgj.natappfree.cc/updateOrder");//在公共参数中设置回跳和通知地址


        Order order = orderService.getOrderById(oid);

        // 订单的信息都是封装到下面的
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":" + oid + "," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":" + order.getTotalPrice() + "," +
                "    \"subject\":\"Iphone6 16G\"," +
                "    \"body\":\"Iphone6 16G\"," +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\"2088511833207846\"" +
                "    }" +
                "  }");//填充业务参数


        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + "utf-8");
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
  • Pay attention to the two links
  • alipayRequest.setReturnUrl("http://localhost:8081/");// The user jumps to the page after the payment is completed, synchronously, it is sent by the browser, so you can use localhost
    //
    AlipayRequest for asynchronous notification of successful payment . setNotifyUrl("http://w5g9y4.natappfree.cc/updateOrder");//Set the return jump and notification address in the public parameters
  • Both links are redirected by Alipay. The first link is the page to which the payment is successful, and it is synchronized. You can specify your homepage or payment success page, and the second link is to jump after the payment is successful. Go to an asynchronous request sent before the first link. This request can be sent to the controller you want to jump to to do some operations, such as changing the unpaid status of the order to the paid status

Mock payment

  • Web version payment

Insert picture description here

  • You can also click the link in the picture to download a mobile phone simulation Alipay app payment

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113779086