Java SDK
商数通开放平台 Java SDK,帮助您快速集成 API 调用能力。
环境要求
- JDK 8+
- Maven 或 Gradle
安装
Maven
xml
<dependency>
<groupId>com.example</groupId>
<artifactId>merchant-flow-sdk</artifactId>
<version>1.0.0</version>
</dependency>Gradle
groovy
implementation 'com.example:merchant-flow-sdk:1.0.0'快速开始
初始化客户端
java
import com.example.merchantflow.MerchantFlowClient;
import com.example.merchantflow.Config;
Config config = Config.builder()
.appKey("your_app_key")
.appSecret("your_app_secret")
.baseUrl("https://openapi.example.com")
.build();
MerchantFlowClient client = new MerchantFlowClient(config);调用 API
java
// 查询商户列表
MerchantListRequest request = MerchantListRequest.builder()
.page(1)
.size(10)
.build();
MerchantListResponse response = client.merchants().list(request);
if (response.isSuccess()) {
List<Merchant> merchants = response.getData().getList();
int total = response.getData().getTotal();
}支付收银台
java
// 创建收银台会话
CreateCashierRequest request = CreateCashierRequest.builder()
.merchantId(1001L)
.outTradeNo("ORDER20260310001")
.totalAmount(29900)
.subject("深层清洁护理服务")
.notifyUrl("https://app.example.com/pay/notify")
.returnUrl("https://app.example.com/pay/result")
.build();
CreateCashierResponse response = client.payment().createCashier(request);
if (response.isSuccess()) {
String cashierUrl = response.getData().getCashierUrl();
// 引导用户跳转到收银台
}查询支付结果
java
PaymentQueryRequest request = PaymentQueryRequest.builder()
.tradeNo("10086")
.build();
PaymentQueryResponse response = client.payment().query(request);
if (response.isSuccess()) {
String status = response.getData().getTradeStatus();
if ("SUCCESS".equals(status)) {
// 支付成功
}
}异常处理
java
try {
MerchantListResponse response = client.merchants().list(request);
} catch (MerchantFlowException e) {
// API 返回的业务错误
System.err.println("Error code: " + e.getCode());
System.err.println("Error message: " + e.getMessage());
} catch (MerchantFlowApiException e) {
// 网络或 HTTP 错误
System.err.println("HTTP error: " + e.getMessage());
}沙箱环境
java
Config config = Config.builder()
.appKey("your_sandbox_app_key")
.appSecret("your_sandbox_app_secret")
.baseUrl("https://sandbox-openapi.example.com")
.sandbox(true)
.build();配置项
| 配置项 | 说明 | 默认值 |
|---|---|---|
appKey | 应用 AppKey | 必填 |
appSecret | 应用 AppSecret | 必填 |
baseUrl | API 地址 | 生产环境 |
connectTimeout | 连接超时(毫秒) | 5000 |
readTimeout | 读取超时(毫秒) | 10000 |
sandbox | 是否沙箱环境 | false |