1. Preparation
Pay treasure to develop API (mobile web payment) : docs.open.alipay.com/api_1/alipa…
1.1. Log in to ant Financial’s open platform
Open.alipay.com/platform/ma…
2.2 Choose developer Center – website & Mobile application
1.3. Select payment access
1.4 create an application and enter a name (select web Application)
There are certain naming requirements, you can view the naming specification:docs.open.alipay.com/200/105319
1.5. The vm is created successfully
1.6. Download the signature tool at:docs.open.alipay.com/291/105971/To download Windows, click RSA Signature Verification Tool
1.7. Click Generate Secret key to generate merchant private key and application secret key, which will be saved as text (TXT)
1.8 Return to the open platform application Improvement page, click Set Application Public Key, receive the verification code, enter the merchant application public key generated in the previous step, and click OK. Then the corresponding alipay public key will be generated. The merchant application public key and Alipay public key should be clearly separated and used in the development codeAlipay public key and merchant application private key.
You can set it directly in this url:Openhome.alipay.com/platform/ke…
1.9. Submit the application for review. One day after the review is completed, the application will be displayed online. After that, you can start backend code development. (The formal project requires the merchant to sign)
2. Code development
2.1 Introducing the Java SDK Package
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.726..ALL</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.11.</version>
</dependency>
Copy the code
2.2 Developing configuration classes
package com.shop.constants;
/** * Class name: AlipayConfig * Function: Basic configuration class * Details: Set account information and return path * Modified date: 2019-04-30 * 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 {
//↓↓↓↓↓↓ Enter your basic information here
// Application ID, your APPID, receivables account is your APPID corresponding to alipay account
public static String app_id = "20160 * * * * * *";
// Merchant private key, your PKCS8 format RSA2 private key
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 = "* * * * * * * *";
// The server asynchronous notification page path must be in the format of http://. User-defined parameters, such as ID =123, must be accessible from the Internet
public static String notify_url = "https://www.alipay.com";
The page path is in the format of http://and cannot be added with?. User-defined parameters such as ID =123 can be entered in the sandbox environment
public static String return_url = "http://localhost:8082/web/pay/callback";
// Signature mode
public static String sign_type = "RSA2";
// Character encoding format
public static String charset = "utf-8";
// Alipay gateway
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// Sales product code, the product code signed by merchants and Alipay, the default value is QUICK_WAP_WAY
public static String productCode ="QUICK_WAP_WAY";
// Please configure your basic information here
}
Copy the code
2.3 Developing tool classes
Parameters for details please see here: docs.open.alipay.com/204/105301/
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.shop.constants.AlipayConfig;
import com.shop.constants.BakConstants;
public class PayUtil {
/ * * *@Title: alipay
* @Description: TODO(pay) *@paramOutTradeNo Merchant order number, which is the unique order number in the order system of merchant website. The corresponding payment record orderNo * must be filled in@paramTotalAmount Payment amount, mandatory *@paramThe subject theme *@paramThe product description can be blank *@param: @return
* @param: @throws Exception
* @return: String
* @throws* /
public static String alipay(String outTradeNo,String totalAmount,String subject,String body) throws Exception{
// Get the initialized AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
AlipayConfig.sign_type);
// The latest payment time allowed for this order
String timeout=30m;
// Set the request parameters
String content = "{\"out_trade_no\":\""+ outTradeNo +"\","
+ "\"total_amount\":\""+ totalAmount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"timeout_express\":\""+ timeout +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\""+AlipayConfig.productCode+"\"}";
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
alipayRequest.setBizContent(content);
/ / request
String result="";
result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}
/** * Alipay refund interface *@param outTradeNo
* @param tradeNo
* @param refundAmount
* @param refundReason
* @paramOut_request_no Specifies a single refund request. Multiple refunds for the same transaction must be guaranteed to be unique. If a partial refund is required, this parameter is mandatory@return* /
public static String aliRefund(String outTradeNo,String tradeNo,String refundAmount,String refundReason,String out_request_no) {
// Get the initialized AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
// Set the request parameters
AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
try {
alipayRequest.setBizContent("{\"out_trade_no\":\""+ outTradeNo +"\","
+ "\"trade_no\":\""+ tradeNo +"\","
+ "\"refund_amount\":\""+ refundAmount +"\","
+ "\"refund_reason\":\""+ refundReason +"\","
+ "\"out_request_no\":\""+ out_request_no +"\"}");
/ / request
String result;
/ / request
result = alipayClient.execute(alipayRequest).getBody();
System.out.println("*********************\n returns:"+result);
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null; }}/** * Alipay visa check method *@param req
* @return* /
public static boolean checkSign(HttpServletRequest req) {
Map<String, String[]> requestMap = req.getParameterMap();
Map<String, String> paramsMap = new HashMap<>();
requestMap.forEach((key, values) -> {
String strs = "";
for(String value : values) {
strs = strs + value;
}
System.out.println(("The key value is"+key+"Value for:"+strs));
paramsMap.put(key, strs);
});
// Invoke the SDK to verify the signature
try {
return AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("********************* failed to check ********************");
return false; }}}Copy the code
2.4. At this point, the Alipay login page can be opened. Alipay client can be opened on the mobile phone
2.5. Callback method and return method of payment completion.
The method of the callback is that under the AlipayConfig class there is a return_URL that is the address of the application callback
2.6 Callback receiving parameters:
import java.math.BigDecimal;
public class AlipayNotifyParam {
private String charset;// Encoding format
private String out_trade_no;// Merchant website unique order number
private String trade_no;// Transaction serial number of this transaction in alipay system. The longest 64
private String method;
private BigDecimal total_amount;// Total fund of this order
private String sign;// Signature result
private String auth_app_id;
private String app_id;// The app Id assigned by Alipay to the developer
private String sign_type;// Signature type
private String seller_id;// The unique alipay user number corresponding to the alipay account. A pure 16-bit number beginning with 2088
private String timestamp;/ / time
//get and set are omitted
}
Copy the code
3. Sandbox environment
In the development center – “R&D Services -” sandbox
The sandbox:docs.open.alipay.com/200/105311/
Simply speaking, it is the alipay test environment, with a sandbox account: buyer account and a seller account, which can conduct simulated payment
Finally finish a series of order operations. The payment completion return page can be the order Details page.
If you don’t know anything, please leave a message in the lower level. If you see it, you will get back to us as soon as possible. Thank you