introduction
In today's digital business environment, online payment has become the mainstream method of transactions. As the two mainstream third-party payment platforms in China, WeChat Pay and Alipay Pay have a huge user base. For Java backend developers, it is a common requirement to connect with these two payment platforms. This article will introduce in detail how the Java backend calls WeChat Pay and Alipay payment, covering basic concepts, configuration steps, code examples, and precautions.
1. WeChat Payment
1.1 Basic Concept
WeChat Pay is a convenient payment method launched by Tencent, which supports multiple payment scenarios, such as scanning code payment, JSAPI payment, APP payment, etc. Calling WeChat Pay on the Java backend mainly completes the payment process by calling the API interface provided by WeChat Pay.
1.2 Preparation
-
Register a WeChat Pay Merchant Number: Log in to the WeChat Pay Merchant Platform (WeChat Pay - China's leading third-party payment platform | WeChat Pay provides a safe and fast payment method) Register and get the merchant number (
mch_id
)。 - Configure API keys: Set up API keys on the merchant platform for signature verification.
-
Obtain a certificate: Download the merchant certificate (
apiclient_cert.p12
), used for secure communication.
1.3 Configuration Dependencies
existAdd the following dependencies to:
<dependency> <groupId></groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
1.4 Code example: Scan the code to pay
import ; import ; import ; import ; import ; import ; public class WechatPayExample { public static void main(String[] args) throws Exception { // Implement WXPayConfig interface WXPayConfig config = new WXPayConfig() { @Override public String getAppID() { return "your_app_id"; } @Override public String getMchID() { return "your_mch_id"; } @Override public String getKey() { return "your_api_key"; } @Override public InputStream getCertStream() { // Load the merchant certificate return ("/apiclient_cert.p12"); } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } }; WXPay wxpay = new WXPay(config, .MD5); // Assembly request parameters Map<String, String> data = new HashMap<>(); ("body", "Product Description"); ("out_trade_no", "20250225001"); ("total_fee", "1"); ("spbill_create_ip", "127.0.0.1"); ("notify_url", "/notify"); ("trade_type", "NATIVE"); // Call unified single interface Map<String, String> resp = (data); (resp); if ("SUCCESS".equals(("return_code")) && "SUCCESS".equals(("result_code"))) { String codeUrl = ("code_url"); ("The generated QR code link: " + codeUrl); } } }
1.5 Code Explanation
-
accomplish
WXPayConfig
Interface: This interface is used to configure basic information of WeChat payment, such asapp_id
、mch_id
、api_key
wait. -
Create
WXPay
Object: Create using configuration informationWXPay
Object, specify the signature type to MD5. - Assembly request parameters: Assemble and unify the request parameters of the single interface according to business needs, such as product description, order number, amount, etc.
-
Call unified single interface: Call
unifiedOrder
Method initiates a unified order request and returns payment result information. -
Get the QR code link: If the order is successfully placed, get it from the return result
code_url
, used to generate payment QR code.
2. Alipay payment
2.1 Basic Concept
Alipay Payment is a payment platform under Ant Financial, and also supports multiple payment scenarios, such as scanning code payment, computer website payment, mobile website payment, etc. Java backend calls to Alipay payment is also implemented by calling the API interface provided by Alipay.
2.2 Preparation
-
Register an Alipay Open Platform Account: Log in to the Alipay Open Platform (Alipay Open Platform) Register, create an app and get the app ID (
app_id
)。 - Configuration key: Generate the application private key and public key, and upload the public key to the Alipay open platform.
- Configure payment parameters: Configure payment callback addresses and other parameters on the open platform.
2.3 Configuration dependencies
existAdd the following dependencies to:
<dependency> <groupId></groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.10.</version> </dependency>
2.4 Code example: Computer website payment
import ; import ; import ; import ; public class AlipayExample { public static void main(String[] args) { // Alipay gateway String gatewayUrl = "/"; // Application ID String appId = "your_app_id"; // Apply private key String privateKey = "your_private_key"; // Alipay public key String alipayPublicKey = "your_alipay_public_key"; // Character encoding format String charset = "UTF-8"; // Signature method String signType = "RSA2"; // Payment successful callback address String returnUrl = "/return"; // Payment result notification address String notifyUrl = "/notify"; // Create an AlipayClient object AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, "json", charset, alipayPublicKey, signType); // Create AlipayTradePagePayRequest object AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); (returnUrl); (notifyUrl); // Assembly request parameters ("{\"out_trade_no\":\"20250225002\"," + "\"total_amount\":\"0.01\"," + "\"subject\":\"Product Name\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); try { // Call the payment interface String result = (alipayRequest).getBody(); (result); } catch (AlipayApiException e) { (); } } }
2.5 Code Explanation
-
Create
AlipayClient
Object: Create using Alipay gateway, application ID, application private key, Alipay public key and other informationAlipayClient
Object. -
Create
AlipayTradePagePayRequest
Object: Set the payment success callback address and payment result notification address. - Assembly request parameters: Assemble the business parameters of payment requests according to business needs, such as order number, amount, product name, etc.
-
Calling the payment interface: Call
pageExecute
Method initiates a payment request and returns the HTML code of the payment page.
3. Things to note
- Signature verification: During the interaction with the payment platform, it is necessary to ensure the correctness of the signature and prevent data from being tampered with.
- Callback processing: After payment is completed, the payment platform will send a notification to the callback address, and the callback information must be processed correctly and the order status must be updated.
- Exception handling: When calling the payment interface, possible exceptions must be captured and processed to ensure the stability of the system.
4. Summary
Through the above steps and code examples, we can see the basic process of calling WeChat payment and Alipay payment on the Java backend. In actual development, appropriate payment scenarios and interfaces should be selected according to specific business needs, and pay attention to payment security and data accuracy. I hope this article can help you successfully connect Java backend with WeChat Pay and Alipay Payment.
This is the article about Java backend calling WeChat Pay and Alipay payment. For more related content on Java backend calling WeChat and Alipay payment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!