diff --git a/intelligence_finance_v1_ga/example_test.go b/intelligence_finance_v1_ga/example_test.go new file mode 100644 index 0000000..5387dce --- /dev/null +++ b/intelligence_finance_v1_ga/example_test.go @@ -0,0 +1,259 @@ +package intelligence_finance_v1_ga + +import ( + "context" + "encoding/json" + "fmt" + "testing" +) + +// ExampleClient_SubmitInvoiceApply 演示提交订单开票申请。 +func ExampleClient_SubmitInvoiceApply() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &InvoiceApplyRequest{ + OrderID: "ORDER-20240101-001", + InvoiceType: 9, // 数电普票 + Purchaser: "某某科技有限公司", + TaxNum: "91330100XXXXXXXXXX", + Email: "finance@example.com", + Phone: "13800000000", + Products: []ProductItem{ + { + ProductName: "软件服务", + RevenueCode: "3040201000000000000", + AmountIncludeTax: 11300, + Quantity: 1, + TaxRate: 0.13, + TaxSign: 1, + }, + }, + } + + resp, err := client.SubmitInvoiceApply(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("status=%d errorMsg=%s\n", resp.Status, resp.ErrorMsg) + // Output: +} + +// ExampleClient_CreatePayment 演示创建付款单据。 +func ExampleClient_CreatePayment() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &CreatePaymentRequest{ + Code: "PAY-20240101-001", + UserID: "04221***", + Title: "供应商货款", + Amount: "11300.00", + Supplier: &Supplier{ + Name: "某某供应商", + }, + PaymentDetailList: []PaymentDetail{ + { + Amount: "11300.00", + Tax: "1300.00", + }, + }, + } + + resp, err := client.CreatePayment(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("code=%s\n", resp.Code) + // Output: +} + +// ExampleClient_CreateSupplier 演示创建供应商。 +func ExampleClient_CreateSupplier() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := []SupplierBizData{ + { + CorpID: "dingXXXXXX", + Creator: "04221***", + SupplierInfo: &SupplierInfo{ + Name: "某某供应商", + CorpID: "dingXXXXXX", + Creator: "04221***", + ContactName: "张三", + ContactTelephone: "18888888888", + InvoiceName: "某某供应商", + InvoiceTaxNo: "91330100XXXXXXXXXX", + }, + }, + } + dataBytes, _ := json.Marshal(bizData) + + req := &CreateSupplierRequest{ + InvokeID: "REQ-001", + UserID: "04221***", + Data: string(dataBytes), + BizType: BizTypeCreateSupplier, + } + + resp, err := client.CreateSupplier(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("bizType=%s data=%s\n", resp.BizType, resp.Data) + // Output: +} + +// ExampleClient_QuerySupplierByName 演示根据名称查询供应商。 +func ExampleClient_QuerySupplierByName() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + queries := []QueryByName{ + {CorpID: "dingXXXXXX", Name: "某某供应商"}, + } + dataBytes, _ := json.Marshal(queries) + + req := &QuerySupplierByNameRequest{ + InvokeID: "REQ-002", + UserID: "04221***", + Data: string(dataBytes), + BizType: BizTypeQuerySupplierByName, + } + + resp, err := client.QuerySupplierByName(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("success=%v result=%s\n", resp.Success, resp.Result) + // Output: +} + +// ExampleClient_UpdateSupplier 演示更新供应商。 +func ExampleClient_UpdateSupplier() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := UpdateSupplierBizData{ + SupplierID: "SUP_XXXXX", + Name: "某某供应商", + CorpID: "dingXXXXXX", + UserID: "04221***", + ContactName: "李四", + ContactTelephone: "18888888888", + AccountType: AccountTypeCorpBankCard, + } + dataBytes, _ := json.Marshal(bizData) + + req := &UpdateSupplierRequest{ + InvokeID: "REQ-003", + UserID: "04221***", + Data: string(dataBytes), + BizType: BizTypeUpdateSupplier, + } + + resp, err := client.UpdateSupplier(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("bizType=%s\n", resp.BizType) + // Output: +} + +// ExampleClient_QuerySupplierByUserDefineCode 演示根据用户自定义编码查询供应商。 +func ExampleClient_QuerySupplierByUserDefineCode() { + ctx := context.Background() + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := QueryByUserDefineCode{ + CorpID: "dingXXXXXX", + UserDefineCode: "SUP-CODE-001", + } + dataBytes, _ := json.Marshal(bizData) + + req := &QuerySupplierByUserDefineCodeRequest{ + InvokeID: "REQ-004", + UserID: "04221***", + Data: string(dataBytes), + BizType: BizTypeQuerySupplierByUserDefine, + } + + resp, err := client.QuerySupplierByUserDefineCode(ctx, req) + if err != nil { + fmt.Println("error:", err) + return + } + fmt.Printf("bizType=%s data=%s\n", resp.BizType, resp.Data) + // Output: +} + +// TestVerifyNotificationSignature 演示通知验签。 +func TestVerifyNotificationSignature(t *testing.T) { + verifyKey := "customer-verify-secret" + rawBody := []byte(`{"bizType":"payment","bizId":"PAY-001","data":"{}"}`) + sign := HmacSHA256Base64(verifyKey, string(rawBody)) + + ok, err := VerifyNotificationSignature(verifyKey, rawBody, sign) + if err != nil { + t.Fatalf("verify error: %v", err) + } + if !ok { + t.Fatal("signature mismatch") + } +} + +// TestParsePaymentNotify 演示解析支付完成通知。 +func TestParsePaymentNotify(t *testing.T) { + body := []byte(`{ + "code":"PAY-001", + "instanceId":"inst-001", + "corpId":"dingXXXXXX", + "paymentStatus":"SUCCESS", + "paymentTime":"2024-01-01 10:00:00", + "userId":"04221***", + "amount":"11300.00" + }`) + n, err := ParsePaymentNotify(body) + if err != nil { + t.Fatalf("parse error: %v", err) + } + if n.PaymentStatus != PaymentStatusSuccess { + t.Fatalf("unexpected status: %s", n.PaymentStatus) + } +} \ No newline at end of file