Backend Development 10 min read

Integrating Alipay Payment in a Spring Boot Application Using the Java SDK and Ngrok Tunneling

This guide explains how to configure the Alipay sandbox, set up an Ngrok tunnel for external callbacks, add the Alipay Java SDK dependency, create Spring Boot configuration and controller classes, implement the payment flow and callback handling, and troubleshoot common initialization errors.

Architecture Digest
Architecture Digest
Architecture Digest
Integrating Alipay Payment in a Spring Boot Application Using the Java SDK and Ngrok Tunneling

The tutorial starts by requiring an Alipay sandbox account and provides the sandbox URL (https://openhome.alipay.com/platform/appDaily.htm) along with the necessary credentials: public/private keys, gateway URL, APPID, and SDK version.

Because the service runs locally, an internal‑network‑penetration tool (e.g., Ngrok) is used to expose the local 9090 port to the public internet, allowing Alipay to send asynchronous notifications back to the application.

Dependency – Add the Alipay Java SDK to the Maven pom:

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

Configuration – Create a YAML/Properties section for the Alipay settings (port, appId, appPrivateKey, alipayPublicKey, notifyUrl) and a Spring @ConfigurationProperties class AliPayConfig that holds these fields.

In the AliPayConfig class, a @PostConstruct init() method builds a Config object and calls Factory.setOptions(config) to initialise the SDK globally.

Payment Controller – A Spring @RestController named AliPayController injects AliPayConfig and a DAO for orders. The /pay endpoint (GET for demo) creates a DefaultAlipayClient , builds an AlipayTradePagePayRequest , sets the business JSON (out_trade_no, total_amount, subject, body, product_code), and writes the generated HTML form to the HTTP response.

Callback handling – A POST endpoint /notify reads all request parameters, verifies the signature with Factory.Payment.Common().verifyNotify(params) , checks trade_status == "TRADE_SUCCESS" , logs transaction details, and updates the order status in the database.

Common error – If the SDK is not initialised, a NullPointerException occurs when calling Context.getCertEnvironment() . The fix is to ensure the init() method in AliPayConfig runs, setting protocol, gatewayHost, signType, appId, merchantPrivateKey, alipayPublicKey, and notifyUrl before any API call.

Finally, the article shows how to test the flow by visiting http://localhost:9090/alipay/pay?subject=TestProduct&total_amount=1000 , completing the sandbox login, and observing the Alipay success page and callback data.

backendJavaSDKSpring BootPayment IntegrationAlipaynetwork tunneling
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.