diff --git a/ymt_v3/example_test.go b/ymt_v3/example_test.go new file mode 100644 index 0000000..7ffca0d --- /dev/null +++ b/ymt_v3/example_test.go @@ -0,0 +1,138 @@ +package ymt_v3 + +import ( + "context" + "fmt" + "testing" +) + +// 测试参数(请替换为实际值) +const ( + testAppID = "xxx" + testPrivateKey = `-----BEGIN PRIVATE KEY----- +xxx +-----END PRIVATE KEY-----` + testPublicKey = `-----BEGIN PUBLIC KEY----- +xxx +-----END PUBLIC KEY-----` + testKey = "xxxx" + testEncryptType = "aes" + testBaseURL = "https://gateway.dev.cdlsxd.cn" + testActivityNo = "xxxx" +) + +func TestClient_Order(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Order(context.Background(), &OrderRequest{ + OutBizNo: "order_001", + ActivityNo: testActivityNo, + Account: "18666666666", + NotifyURL: "https://notify.example.com/openapi", + }) + if err != nil { + t.Fatalf("Order failed: %v", err) + } + fmt.Printf("Order response: %+v\n", resp) +} + +func TestClient_Query(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Query(context.Background(), &QueryRequest{ + OutBizNo: "order_001", + }) + if err != nil { + t.Fatalf("Query failed: %v", err) + } + fmt.Printf("Query response: %+v\n", resp) +} + +func TestClient_Discard(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Discard(context.Background(), &DiscardRequest{ + TradeNo: "7251449503000383488", + }) + if err != nil { + t.Fatalf("Discard failed: %v", err) + } + fmt.Printf("Discard response: %+v\n", resp) +} + +func TestClient_BatchOrder(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.BatchOrder(context.Background(), &BatchOrderRequest{ + OutBizNo: "batch_001", + ActivityNo: testActivityNo, + Number: 10, + NotifyURL: "https://notify.example.com/openapi", + }) + if err != nil { + t.Fatalf("BatchOrder failed: %v", err) + } + fmt.Printf("BatchOrder response: %+v\n", resp) +} + +func TestClient_BatchQuery(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.BatchQuery(context.Background(), &BatchQueryRequest{ + TradeNo: "7251449503000383499", + }) + if err != nil { + t.Fatalf("BatchQuery failed: %v", err) + } + fmt.Printf("BatchQuery response: %+v\n", resp) +} + +func TestVerifyCallback(t *testing.T) { + // 模拟回调参数 + appID := "123456" + timestamp := "2026-06-22 15:30:00" + sign := "base64_encoded_signature" + data := `{"out_biz_no":"batch_001","trade_no":"7251449503000383499","status":"success","download_url":"https://oss.example.com/openapi_7251449503000383499.zip","zip_password":"123456"}` + + err := VerifyCallback(appID, timestamp, sign, data, testPublicKey, []byte(testKey), testEncryptType) + if err != nil { + t.Logf("VerifyCallback result: %v (expected if sign is invalid)", err) + } else { + t.Log("VerifyCallback success") + } +} \ No newline at end of file