本文目录前置条件创建支付订单响应下一步

快速开始 ​

几分钟内创建您的第一笔支付订单。

前置条件 ​

  • 拥有 Access Key ID 和 Secret Key 的 HashNut 商户账号
  • 已部署的 Splitter Address(分账钱包合约)

创建支付订单 ​

go
package main

import (
    "fmt"
    "log"

    hashnut "github.com/hashnut/hashnut-go-sdk"
    "github.com/hashnut/hashnut-go-sdk/model"
)

func main() {
    client := hashnut.NewClient("your-secret-key", true) // true = testnet

    order, err := client.CreateOrder(&model.CreateOrderRequest{
        AccessKeyID:     "your-access-key-id",
        MerchantOrderID: "ORDER-001",
        BlockChain:       "POLYGON",
        TokenSymbol:        "usdt",
        Amount:          "10.00",
        SplitterAddress: "0xYourSplitWalletAddress",
        Subject:         "Test Product",
        ExpireDuration:  600,
    })
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Order ID:        %s\n", order.PayOrderID)
    fmt.Printf("Receipt Address: %s\n", order.ReceiptAddress)
    fmt.Printf("Payment Page:    %s\n", order.PayURL)
}
java
import java.net.URI;
import java.net.http.*;
import java.time.Instant;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.UUID;

public class CreateOrder {
    static final String SECRET_KEY = "your-secret-key";
    static final String BASE_URL = "https://testnet.hashnut.io/api/v4.0.0";

    public static void main(String[] args) throws Exception {
        String uuid = UUID.randomUUID().toString();
        String timestamp = String.valueOf(Instant.now().toEpochMilli());
        String body = """
            {
              "accessKeyId": "your-access-key-id",
              "merchantOrderId": "ORDER-001",
              "blockChain": "POLYGON",
              "tokenSymbol": "usdt",
              "amount": "10.00",
              "splitterAddress": "0xYourSplitWalletAddress",
              "subject": "Test Product",
              "expireDuration": 600
            }""";

        // HMAC-SHA256 signing
        String message = uuid + timestamp + body;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(SECRET_KEY.getBytes(), "HmacSHA256"));
        String sign = Base64.getEncoder().encodeToString(mac.doFinal(message.getBytes()));

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(BASE_URL + "/api/orders/create"))
            .header("Content-Type", "application/json")
            .header("hashnut-request-uuid", uuid)
            .header("hashnut-request-timestamp", timestamp)
            .header("hashnut-request-sign", sign)
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();

        HttpResponse<String> response = HttpClient.newHttpClient()
            .send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
python
import hmac, hashlib, base64, json, time, uuid
import requests

SECRET_KEY = "your-secret-key"
BASE_URL = "https://testnet.hashnut.io/api/v4.0.0"

body = json.dumps({
    "accessKeyId": "your-access-key-id",
    "merchantOrderId": "ORDER-001",
    "blockChain": "POLYGON",
    "tokenSymbol": "usdt",
    "amount": "10.00",
    "splitterAddress": "0xYourSplitWalletAddress",
    "subject": "Test Product",
    "expireDuration": 600,
}, separators=(",", ":"))

# HMAC-SHA256 signing
req_uuid = str(uuid.uuid4())
timestamp = str(int(time.time() * 1000))
message = req_uuid + timestamp + body
signature = base64.b64encode(
    hmac.new(SECRET_KEY.encode(), message.encode(), hashlib.sha256).digest()
).decode()

resp = requests.post(
    f"{BASE_URL}/api/orders/create",
    headers={
        "Content-Type": "application/json",
        "hashnut-request-uuid": req_uuid,
        "hashnut-request-timestamp": timestamp,
        "hashnut-request-sign": signature,
    },
    data=body,
)

order = resp.json()["data"]
print(f"Order ID:        {order['payOrderId']}")
print(f"Receipt Address: {order['receiptAddress']}")
javascript
const crypto = require("crypto");

const SECRET_KEY = "your-secret-key";
const BASE_URL = "https://testnet.hashnut.io/api/v4.0.0";

const body = JSON.stringify({
  accessKeyId: "your-access-key-id",
  merchantOrderId: "ORDER-001",
  blockChain: "POLYGON",
  tokenSymbol: "usdt",
  amount: "10.00",
  splitterAddress: "0xYourSplitWalletAddress",
  subject: "Test Product",
  expireDuration: 600,
});

// HMAC-SHA256 signing
const uuid = Date.now().toString() + Math.random().toString(36).slice(2);
const timestamp = Date.now().toString();
const message = uuid + timestamp + body;
const signature = crypto
  .createHmac("sha256", SECRET_KEY)
  .update(message)
  .digest("base64");

fetch(`${BASE_URL}/api/orders/create`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "hashnut-request-uuid": uuid,
    "hashnut-request-timestamp": timestamp,
    "hashnut-request-sign": signature,
  },
  body,
})
  .then((res) => res.json())
  .then((data) => {
    console.log("Order ID:", data.data.payOrderId);
    console.log("Receipt Address:", data.data.receiptAddress);
  });

响应 ​

json
{
  "code": 0,
  "data": {
    "payOrderId": "01J5X9NDEKTSV4RRFFQ69G5FAV",
    "merchantOrderId": "ORDER-001",
    "receiptAddress": "0x1234...5678",
    "amount": "10.00",
    "accessSign": "3B08A1F2...",
    "state": 0
  }
}

下一步 ​

  • 支付流程 — 了解支付的完整生命周期
  • 通知 — 当订单状态变更时接收 Webhook 回调
  • 查询订单 — 轮询订单状态和交易详情