Download the official demo of the computer website:

Suggestions: novice access to pay for the first time or download to see, understand, readme. TXT good look, including later to see the third party this file is best to browse, or easy to step on the hole below there will be special SDK access guide

Download: docs.open.alipay.com/270/106291/

Ali source code main Demo

package com.alipay.config; /* * * Class name: AlipayConfig * Function: Basic configuration class * Details: Set account information and return path * Modified date: 2017-04-05 * Description: * The following code is provided as a sample code for merchant testing purposes only. Merchant can write it according to the needs of their website and according to the technical documentation. It is not necessary to use this code. * This code is only for learning and studying alipay interface, just to provide a reference. * / public class AlipayConfig {/ / left left left left left left left left left left down down down down down down down down down here, please configure your basic information left left left left left left left left left left down down down down down down down down down left left left left left/down down down down down/application ID, your APPID, Public static String app_id = "2016102400748331"; public static String app_id = "2016102400748331"; Public static String merchant_private_key = ""; / / alipay public key, check the address: https://openhome.alipay.com/platform/keyManage.htm under the corresponding APPID alipay public key. public static String alipay_public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjC qjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB\n"; // The server asynchronous notification page path must be in the format of http://. Custom parameters like id=123, Public static String notify_url = "http://project public network access address /alipay.trade.page. Pay-java-utf-8 /notify_url.jsp"; The page path is in the format of http://and cannot be added with?. Custom parameters like id=123, Public static String return_url = "http://public network access address /alipay.trade.page. Pay-java-utf-8 /return_url "; Public static String sign_type = "RSA2"; Public static String charset = "utF-8 "; / / alipay gateway public static String gatewayUrl = "https://openapi.alipay.com/gateway.do"; Public static String log_path = "C:\\"; }Copy the code

 

Now officially register the ant developer account

Register at open.alipay.com

Select the following

Set the APPID and RAS key

The application of RAS in Alipay payment (asymmetric impersonation, the public key is used to generate alipay key [transfer data], and the private key is used to decrypt data in your program)

Set the key

 

 

Then download the key generation tool below

 

The third party SDK

<dependency>
    <groupId>net.guerlab</groupId>
    <artifactId>sdk-alipay-starter</artifactId>
    <version>2.1.0</version>
</dependency>
Copy the code

Then the generated public key to the website above paste, obtain alipay public key, alipay public key replacement procedures in the public key, you generate the private key to replace the private key in the code

The sample

Spring: MVC: view: # prefix: /WEB-INF/ JSP / # suffix:.jsp server: port: 9999 appID: wx60c1c374fd1329a2 secret: b6e4ebc42a1fa92d1ea7ea440e52d648 redirectUri: http://moran1012.natapp1.cc/callback # # # to generate WeChat authorizedUrl authorization: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsap I_userinfo &state= state #wechat_redirect ### After obtaining code, request the following link to obtain access_token access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code Snsapi_userinfo = snsapi_userinfo = snsapi_userinfo https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN alipay: Dev: true # signature algorithm RSA2 RSA sign-type: RSA2 # application ID app-id: 2012101200736715 # Application private key: MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQDOP2FcM6sWiapQQqSBE4Y3OvXb6r0WVhmuZN9N7LdrQRV516Kxg4OL7goMei7i1gzY3bec qhiVYdRG+q0WhZ3K0i8EecpUeTUG7avxMYFsc9CoQNd/IxRXy+qRrg6Y7QAvuwhSakse+k/3+VUVto/i3wIY+Satph//iIzawKqMRZxZbhX7SjvK4Iaajitv qd3NcWhmh179XxkBA3ySQoWXLNO2FFs8sdmpRVV5MweZBJVaGwW2OQpAMs4tkpdrWf2m8iFUanIRW/6GeApt58tlPgkkt3TyP9rAQPLY+fKxScbNaPbvB9E8 ZGkKoaA9/TJZPwlKxtSF1x/k1pEAp5m1AgMBAAECgf8t6+7BmL9/RSemty/pjW9U3MvhK2Lulj0hl1QhBZPRTax6dIjE/YNMpLOYE5xc5Uv2/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fjsQnyaxV8A8fgQNAUv5 Q7rU zrgQM8E4MXP99bQNOJJgAjm7 + dpRZ/GJL9SiAHkJL5h2McSNCh # alipay alipay public key - public - key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhWdnts9UIBLSwIin1IToaCXc5xV2vX6BhOFms6K7pbzNVFCQ3tgnqrPbpjyI6ZUTwPTbhu3u+aaf bC1hOQAsciBALxTP/pDe+AOPDoUo5Yj9D+Io+Q6d/sEDtsJQuaPpWMET0qM4Nngn1iroJYef4CbenjFMZ4IYzciBZlNgVzSgf6ZOvF9C7tAwpaURFs2r2e8H xoyUrv/T7oVU5Ws/a70FpQB4JuirKIeTLEE4fia9IjMOOmicXcFlpR0ht/m8ggAdr5o+hl+vU1EKcTUJKCogRdlt29DZQ2RLutr0tIaFAJ2kzoocLtQW7AyI 8TnHZFDFqJiBap1GsQgltMGWBwIDAQABCopy the code

Payment code

/** * @ClassName AliPayController * @Description TODO * @Author moran * @Date 2020/4/21 16:04 **/ @Slf4j @RestController  @RequestMapping("/open/api/pay") public class AliPayController { @Autowired private AlipayClient client; @Autowired private AlipayProperties properties; private static final String REDIRECT_WFWZS_URL = "http://moran1012.natapp1.cc/return_url.jsp"; @GetMapping("/submit_pay") public void submitPay(HttpServletResponse response ) throws AlipayApiException, IOException { JSONObject data = new JSONObject(); String orderNo = System.currentTimeMillis()+ ""; data.put("out_trade_no",orderNo ); // Data. put("total_amount", "0.01"); // Order amount data.put("subject", "test alipay order "); // Order title data.put("product_code", "FAST_INSTANT_TRADE_PAY"); / / product code, PC pay FAST_INSTANT_TRADE_PAY, AlipayTradePagePayRequest requestAli = new AlipayTradePagePayRequest (); requestAli.setNotifyUrl("http://moran1012.natapp1.cc/open/api/pay/notify_ali"); Requestali.setbizcontent (data.tojsonString ()); // Service parameter String returnUrl = REDIRECT_WFWZS_URL + "? orderNo=" + orderNo; requestAli.setReturnUrl(returnUrl); String from = client.pageExecute(requestAli).getBody(); response.setContentType("text/html; charset=UTF-8"); response.getWriter().write(from); PostMapping("/notify_ali") public void notifyAli(HttpServletRequest Request, Throws Exception {log.info(" paypal async notification data: {}", Servletutil.getParamMap (Request)); if (! Alipayutils.rsacheck (properties, request.getParameterMap())) {log.error(" alipay asynchronous notification failed to check "); response.getWriter().write("fail"); response.getWriter().flush(); response.getWriter().close(); } else { String out_trade_no = request.getParameter("out_trade_no"); // Get the merchant order number in the request parameter log.info(" Alipay asynchronous notification check success, order number: {}", out_trade_no); String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8"); // Trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8"); if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) { // TRADE_FINISHED After the refund date exceeds the refundable period (such as three months refundable), Alipay system will send the trade status notification // TRADE_SUCCESS after the payment is completed, Alipay system will send the trade status notification // Determine whether the order has been processed on the merchant's website // If not, Find the details of the order in the order system of the merchant website according to the order number (out_trade_no) and execute the merchant's business program. response.getWriter().flush(); response.getWriter().close(); }}}Copy the code

The front page requests payment

< script SRC = "https://code.jquery.com/jquery-3.1.1.min.js" > < / script > < body > < button onclick = "ww ()" > pay < / button > < / body > <script type="text/javascript"> function pay(){ $.ajax({ url: ".. /open/ API /pay/submit_pay", type: "get", success: function(r) {$("body").append(r); } }) } </script>Copy the code