Preparatory work

1. Create the app and get the APPID (unique identifier of the app)

2. Signing function

3. Configure the key

Alipay open platform configuration secret key

4. Access scheme: The server integrates SDK access and payment

  • The client SDK
    • The client SDK needs to be integrated into the merchant’s own APP to evoke alipay APP and send transaction data, and obtain payment results when Alipay APP returns to merchant APP
  • The service side the SDK
    • The server SDK requires the merchant to integrate into its server system to assist in parsing and verifying the payment results and asynchronous notifications returned by the client synchronously

In actual combat

1. Configuration and dependency

  • 1.1 depend on
  <dependency>
       <groupId>com.alipay</groupId>
       <artifactId>alipay-java-sdk</artifactId>
       <version>1.0. 0.RELEASE</version>
  </dependency>

 <dependency>
     <groupId>com.alipay</groupId>
     <artifactId>alipay-trade-sdk</artifactId>
     <version>1.0. 0.RELEASE</version>
</dependency>
Copy the code
  • 1.2 configuration

  • 1.2.1 Initializing the instance required by alipay SDK

@Configuration
public class AliPayConfig {

	// App payment configuration
    @Bean
    public AlipayClient alipayClient(a){
        return new DefaultAlipayClient(
                AliPayProperty.SERVER_URL,
                AliPayProperty.APP_ID,
                AliPayProperty.APP_PRIVATE_KEY,
                AliPayProperty.FORMAT,
                AliPayProperty.CHARSET,
                AliPayProperty.ALIPAY_PUBLIC_KEY,
                AliPayProperty.SIGN_TYPE);
    }
    
    // Scan the payment configuration
    @Bean
    public AlipayTradeService alipayTradeService(a){
        return newAlipayTradeServiceImpl.ClientBuilder() .setAppid(AliPayProperty.APP_ID) .setAlipayPublicKey(AliPayProperty.ALIPAY_PUBLIC_KEY) .setCharset(AliPayProperty.CHARSET) .setFormat(AliPayProperty.FORMAT) .setPrivateKey(AliPayProperty.APP_PRIVATE_KEY) .setSignType(AliPayProperty.SIGN_TYPE) .setGatewayUrl(AliPayProperty.SERVER_URL).build(); }}Copy the code
  • 1.2.2 Parameters obtained from alipay’s preparatory work

2. Request parameters and return values

  • Request parameters
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AliPayCommand {
    private Integer fee;// The order amount unit is divided into ---- Alipay interface amount unit is unified into yuan
    private String operatorId = "merengues";// Operator id
    private String storeId = "000001";// Store id
    private String title;/ / title
    private String remark;/ / note
}
Copy the code
  • App payment interface return value
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class AliPayTradeAppPayResponseDto {
    private String body;

    public AliPayTradeAppPayResponseDto(AlipayTradeAppPayResponse response) {
        this.setBody(response.getBody()); }}Copy the code

3. Code

3.1 Business processing and Alipay SDK invocation

 public AliPayTradeAppPayResponseDto generateAppPayInfo(AliPayCommand command) {
        AlipayTradeAppPayRequest request = AliPayRequestBuilder.generateAliPayAppRequest(command,aliPayAppCallBack);
        AlipayTradeAppPayResponse response = null;
        try {
            response = alipayClient.sdkExecute(request);
            response.setOutTradeNo(((AlipayTradeAppPayModel)request.getBizModel()).getOutTradeNo());
            createPayRecord(command, response.getOutTradeNo(),response.getTradeNo(),PayType.ALI, command.getPayChannel());
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(! response.isSuccess()) {return null;
        }
        return new AliPayTradeAppPayResponseDto(response);
   }
    
Copy the code

3.2 SDK interface request parameter construction

public static AlipayTradeAppPayRequest generateAliPayAppRequest(AliPayCommand command, String notifyUrl){
    AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
    request.setNotifyUrl(notifyUrl);// Notify public parameter. After alipay APP makes payment, Alipay asynchronously invokes the URL of its own service to process the business after successful payment
    // Non-public parameters are wrapped in bizModel
    AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
    model.setBody(command.getRemark());
    model.setSubject(command.getTitle());/ / will be selected
    model.setOutTradeNo(generateTradeId());// Specifies the id of the generated order
    model.setTimeExpire(DateUtil.obtainAliPayTimeExpire());
    Float floatFee = Float.valueOf(command.getFee().toString()) / 100;
    model.setTotalAmount(floatFee.toString());/ / will be selected
    model.setProductCode(AliPayProperty.MODEL_PRODUCT_CODE);
    request.setBizModel(model);
    return request;
}
Copy the code

conclusion

  • The difficulty of alipay interface interconnection lies in the preparatory work, such as creating applications to obtain the secret key public key of Alipay and signing contracts. The later business code is relatively simple and easy to understand, and it is necessary to understand how different parameters are constructed when constructing SDk request parameters. Finally, alipay APP payment function document is completed, I hope it will be helpful for you to develop code farming!