Integrating Alipay Payments in Java: WAP and App Implementation Guide

1. Alipay Open Platform Setup

Begin by creating a Web/Mobile application within the Alipay Open Platform console. Once the application is created, configure the interface signature method. You will need to download the official Alipay Key Generator tool to generate your RSA2 key pair. Copy the generaetd Application Public Key and paste it into the platform's signature configuration to retrieve the Alipay Public Key. Finally, submit your application for review and wait for approval.

2. Project Dependency

Include the Alipay SDK in your project using Maven.

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.35.79.ALL</version>
</dependency>

3. Payment Configuration

Create a configuration class to store your Alipay credentials and endpoint settings. Ensure you correctly map the Merchant Private Key and the Alipay Public Key from the platform dashboard.

public class PaymentProperties {
    public static String APP_ID = "20210001...";
    public static String MERCHANT_PRIVATE_KEY = "MIIEvgIBADANBgkq...";
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkq...";
    
    // Must be a publicly accessible URL for asynchronous notifications
    public static String NOTIFY_URL = "https://yourdomain.com/api/payment/notify";
    // Can be a LAN address if the client is on the same network
    public static String RETURN_URL = "http://192.168.1.100:8080/payment/success";
    
    public static String SIGN_TYPE = "RSA2";
    public static String CHARSET = "UTF-8";
    public static String GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
}

4. WAP Payment Integration

For mobile browser payments (or WebView wrappers), the server generates an HTML form that automatically redirects the user to the Alipay payment page.

@PostMapping("/wap/pay")
@ResponseBody
public String initiateWapPayment(HttpServletRequest httpRequest) {
    AlipayClient alipayClient = new DefaultAlipayClient(
        PaymentProperties.GATEWAY_URL, 
        PaymentProperties.APP_ID, 
        PaymentProperties.MERCHANT_PRIVATE_KEY, 
        "json", 
        PaymentProperties.CHARSET, 
        PaymentProperties.ALIPAY_PUBLIC_KEY, 
        PaymentProperties.SIGN_TYPE
    );

    String tradeNo = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) 
                     + UUID.randomUUID().toString().substring(0, 8);
    
    AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
    request.setReturnUrl(PaymentProperties.RETURN_URL);
    request.setNotifyUrl(PaymentProperties.NOTIFY_URL);
    
    String bizContent = String.format(
        "{\"out_trade_no\":\"%s\", \"total_amount\":\"0.01\", \"subject\":\"WAP Order Payment\", \"product_code\":\"QUICK_WAP_PAY\"}",
        tradeNo
    );
    request.setBizContent(bizContent);

    try {
        return alipayClient.pageExecute(request).getBody();
    } catch (AlipayApiException e) {
        throw new RuntimeException("WAP payment initiation failed", e);
    }
}

5. App Payment Integration

For native mobile applications, the backend generates an order string (query parameters). The mobile client receives this string and uses the Alipay Client SDK to invoke the native Alipay app for payment.

@PostMapping("/app/pay")
@ResponseBody
public ApiResponse triggerAppPayment() {
    AlipayClient alipayClient = new DefaultAlipayClient(
        PaymentProperties.GATEWAY_URL, 
        PaymentProperties.APP_ID, 
        PaymentProperties.MERCHANT_PRIVATE_KEY, 
        "json", 
        PaymentProperties.CHARSET, 
        PaymentProperties.ALIPAY_PUBLIC_KEY, 
        PaymentProperties.SIGN_TYPE
    );

    String transactionId = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) 
                           + UUID.randomUUID().toString().substring(0, 8);
    
    AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
    AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
    model.setBody("Order Description");
    model.setSubject("App Payment Item");
    model.setOutTradeNo(transactionId);
    model.setTimeoutExpress("30m");
    model.setTotalAmount("0.01");
    model.setProductCode("QUICK_MSECURITY_PAY");
    
    request.setBizModel(model);
    request.setNotifyUrl(PaymentProperties.NOTIFY_URL);

    Map<String, Object> result = new HashMap<>();
    result.put("transactionId", transactionId);
    
    try {
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        result.put("orderString", response.getBody());
    } catch (AlipayApiException e) {
        throw new RuntimeException("App payment initiation failed", e);
    }
    return ApiResponse.success(result);
}

6. Asynchronous Notification Handling

Alipay sends payment result notifications to your configured NOTIFY_URL. This endpoint must be publicly accessible. Alipay will repeatedly send notifications until your server responds with the exact string "success". Always validate the payment status and amount within this callback.

@PostMapping("/notify")
public String handleAsyncNotification(HttpServletRequest request) {
    // Extract parameters from the incoming request
    Map<String, String> params = new HashMap<>();
    request.getParameterMap().forEach((key, values) -> {
        params.put(key, String.join(",", values));
    });

    // Process business logic: verify signature, check payment status, and validate amount
    boolean isPaymentVerified = orderService.processAlipayCallback(params);
    
    if (isPaymentVerified) {
        return "success";
    }
    return "fail";
}

Tags: java Alipay SDK Mobile Payment WAP Payment App Payment

Posted on Wed, 10 Jun 2026 16:57:53 +0000 by john-iom