Skip to content

Java 集成

請替換示例憑證

下方代碼示例使用了占位值。您必須將它們替換為您的實際憑證:

  • "your-api-key" → 從商戶後台獲取的實際 API Key
  • "your-api-secret" → 從商戶後台獲取的實際 API Secret
  • "1000" → 註冊時獲得的實際 Merchant ID

依賴項 (Maven)

xml
<dependencies>
    <!-- Jackson for JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version>
    </dependency>
</dependencies>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

快速開始

java
// 創建客戶端
StablePayClient client = new StablePayClient(
    "https://api.paystablecoin.global",
    "1000",  // 商戶 ID
    "your-api-key",
    "your-api-secret"
);

// 創建訂單
Map<String, Object> orderRequest = new HashMap<>();
orderRequest.put("merchantOrderId", "M_ORD_" + System.currentTimeMillis());
orderRequest.put("orderAmount", Map.of(
    "value", "99.99",
    "currency", "USD"
));
orderRequest.put("expiresAt", "2025-12-31T23:59:59Z");
orderRequest.put("returnUrl", "https://yoursite.com/payment/success");
orderRequest.put("paymentMethod", Map.of(
    "supportedCurrencies", List.of("USDT", "USDC"),
    "preferredChain", "TRON"
));

String response = client.createOrder(orderRequest);
System.out.println(response);

// 查詢訂單
String queryResult = client.queryOrder("M_ORD_" + System.currentTimeMillis());
System.out.println(queryResult);

完整示例

完整的 SDK 實現請參見 examples/java/StablePayClient.java

主要特性:

  • 自動簽名計算
  • 支持 POST (創建) 和 GET (查詢) 操作
  • 內置錯誤處理
  • 使用 Java 11+ HttpClient

運行示例:

bash
cd examples/java
mvn clean compile
mvn exec:java -Dexec.mainClass="com.stablepay.merchant.sdk.StablePayClient"

相關文檔

Released under the MIT License.