diff --git a/intelligence_finance_v1/example_test.go b/intelligence_finance_v1/example_test.go new file mode 100644 index 0000000..799377e --- /dev/null +++ b/intelligence_finance_v1/example_test.go @@ -0,0 +1,334 @@ +package intelligence_finance_v1 + +import ( + "context" + "encoding/json" + "fmt" + "log" + "time" +) + +// ExampleNewClient 展示了如何创建SDK客户端 +func ExampleNewClient() { + // 创建客户端 + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + WithTimeout(30*time.Second), + WithDebug(true), + ) + _ = client + fmt.Println("Client created successfully") +} + +// ExampleClient_CreateInvoice 展示了如何提交订单开票申请 +func ExampleClient_CreateInvoice() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &CreateInvoiceRequest{ + OrderID: "ORDER20240101001", + InvoiceType: 9, // 数电普票 + Purchaser: "测试购买方有限公司", + TaxNum: "91110108MA12345678", + Phone: "13800138000", + Email: "test@example.com", + Products: []InvoiceProduct{ + { + ProductName: "办公用品", + RevenueCode: "1090515010000000000", + AmountIncludeTax: 1130.00, + Quantity: 10, + Unit: "个", + TaxRate: 0.13, + TaxSign: 1, + Discount: 0, + }, + }, + ApplyPerson: "张三", + Payee: "李四", + Reviewer: "王五", + } + + resp, err := client.CreateInvoice(context.Background(), req) + if err != nil { + log.Fatalf("创建开票申请失败: %v", err) + } + + fmt.Printf("开票状态: %d, 错误信息: %s\n", resp.Status, resp.ErrorMsg) + for i, inv := range resp.DataList { + fmt.Printf("发票%d: 代码=%s, 号码=%s, 金额(含税)=%s\n", i+1, inv.InvoiceCode, inv.InvoiceNo, inv.TotalIncludeTax) + } +} + +// ExampleClient_QueryInvoiceStatus 展示了如何查询开票状态 +func ExampleClient_QueryInvoiceStatus() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &QueryInvoiceStatusRequest{ + OrderID: "ORDER20240101001", + } + + resp, err := client.QueryInvoiceStatus(context.Background(), req) + if err != nil { + log.Fatalf("查询开票状态失败: %v", err) + } + + fmt.Printf("开票状态: %d, 描述: %s\n", resp.Status, resp.Message) +} + +// ExampleClient_CreatePayment 展示了如何创建付款单据 +func ExampleClient_CreatePayment() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &CreatePaymentRequest{ + Code: "PAY20240101001", + UserID: "user123", + Title: "采购付款-2024年1月", + Amount: "50000.00", + Supplier: &Supplier{ + Name: "供应商A", + }, + Department: &Department{ + Name: "财务部", + }, + Company: &Company{ + Name: "XX科技有限公司", + }, + PaymentDetailList: []PaymentDetail{ + { + Amount: "50000.00", + Remark: "1月采购货款", + }, + }, + } + + resp, err := client.CreatePayment(context.Background(), req) + if err != nil { + log.Fatalf("创建付款单据失败: %v", err) + } + + fmt.Printf("付款单据编码: %s\n", resp.Code) +} + +// ExampleHandlePaymentNotification 展示了如何处理支付完成通知 +func ExampleHandlePaymentNotification() { + // 模拟平台回调的请求体(符合NotificationRequest格式) + notificationBody := `{ + "bizType": "payment_notification", + "bizId": "instance_001", + "data": "{\"code\":\"PAY20240101001\",\"instanceId\":\"instance_001\",\"corpId\":\"dingXXXXXX\",\"paymentStatus\":\"SUCCESS\",\"paymentTime\":\"2024-01-15 10:30:00\",\"userId\":\"user123\",\"amount\":\"50000.00\",\"source\":\"openapi\"}" + }` + + result, err := HandlePaymentNotification([]byte(notificationBody)) + if err != nil { + log.Printf("处理通知失败: %v", err) + } + + fmt.Printf("通知处理结果: %s\n", result) + + // 实际业务中,可以在此处解析通知数据并执行后续操作 + // 解析外层 + var notificationReq NotificationRequest + json.Unmarshal([]byte(notificationBody), ¬ificationReq) + var paymentNotification PaymentNotification + json.Unmarshal([]byte(notificationReq.Data), &paymentNotification) + fmt.Printf("收到支付通知: 单据编码=%s, 支付状态=%s\n", paymentNotification.Code, paymentNotification.PaymentStatus) +} + +// ExampleClient_QueryPaymentStatus 展示了如何查询支付状态 +func ExampleClient_QueryPaymentStatus() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + req := &QueryPaymentStatusRequest{ + Code: "PAY20240101001", + UserID: "user123", + } + + resp, err := client.QueryPaymentStatus(context.Background(), req) + if err != nil { + log.Fatalf("查询支付状态失败: %v", err) + } + + fmt.Printf("支付状态: %s, 支付时间: %s\n", resp.PaymentStatus, resp.PaymentTime) +} + +// ExampleClient_CreateSupplier 展示了如何创建供应商 +func ExampleClient_CreateSupplier() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := CreateSupplierBizData{ + CorpID: "dingXXXXXX", + Creator: "user123", + SupplierInfo: &SupplierInfo{ + Name: "测试供应商有限公司", + CorpID: "dingXXXXXX", + Creator: "user123", + UserDefineCode: "SUP-001", + ContactName: "张三", + ContactTelephone: "13800138000", + ContactEmail: "contact@supplier.com", + ContactAddress: "北京市朝阳区XX路XX号", + }, + } + + bizDataBytes, _ := json.Marshal(bizData) + + req := &CreateSupplierRequest{ + InvokeID: "invoke_001", + UserID: "user123", + Data: string(bizDataBytes), + BizType: "create_supplier", + } + + resp, err := client.CreateSupplier(context.Background(), req) + if err != nil { + log.Fatalf("创建供应商失败: %v", err) + } + + fmt.Printf("创建供应商响应: invokeId=%s, bizType=%s\n", resp.InvokeID, resp.BizType) + // 如果需要解析响应数据,可以进一步处理 resp.Data + var responseData CreateSupplierResponseData + if err := json.Unmarshal(resp.Data, &responseData); err == nil { + fmt.Printf("响应数据: success=%v, result=%s\n", responseData.Success, responseData.Result) + } +} + +// ExampleClient_QuerySupplierByName 展示了如何根据名称查询供应商 +func ExampleClient_QuerySupplierByName() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := []QuerySupplierByNameBizData{ + { + CorpID: "dingXXXXXX", + Name: "测试供应商有限公司", + }, + } + + bizDataBytes, _ := json.Marshal(bizData) + + req := &QuerySupplierByNameRequest{ + InvokeID: "invoke_002", + UserID: "user123", + Data: string(bizDataBytes), + BizType: "query_supplier_by_name", + } + + resp, err := client.QuerySupplierByName(context.Background(), req) + if err != nil { + log.Fatalf("查询供应商失败: %v", err) + } + + fmt.Printf("查询结果: success=%v, result=%s\n", resp.Success, resp.Result) + // 解析响应数据 + var results []QuerySupplierByNameResult + if err := json.Unmarshal(resp.Data, &results); err == nil { + for _, r := range results { + fmt.Printf("供应商: %s, ID: %s\n", r.SupplierName, r.SupplierID) + } + } +} + +// ExampleClient_UpdateSupplier 展示了如何更新供应商 +func ExampleClient_UpdateSupplier() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := UpdateSupplierBizData{ + SupplierID: "SUP_XXXXX", + Name: "更新后的供应商名称", + CorpID: "dingXXXXXX", + UserID: "user123", + ContactName: "李四", + ContactTelephone: "13900139000", + ContactEmail: "new@supplier.com", + Description: "长期合作供应商", + } + + bizDataBytes, _ := json.Marshal(bizData) + + req := &UpdateSupplierRequest{ + InvokeID: "invoke_003", + UserID: "user123", + Data: string(bizDataBytes), + BizType: "update_supplier", + } + + resp, err := client.UpdateSupplier(context.Background(), req) + if err != nil { + log.Fatalf("更新供应商失败: %v", err) + } + + fmt.Printf("更新供应商响应: invokeId=%s, bizType=%s\n", resp.InvokeID, resp.BizType) +} + +// ExampleClient_QuerySupplierByUserDefineCode 展示了如何根据自定义编码查询供应商 +func ExampleClient_QuerySupplierByUserDefineCode() { + client := NewClient( + "https://api.example.com", + "your-tenant-id", + "dd-ai-table", + "your-client-secret", + ) + + bizData := QuerySupplierByUserDefineCodeBizData{ + CorpID: "dingXXXXXX", + UserDefineCode: "SUP-001", + } + + bizDataBytes, _ := json.Marshal(bizData) + + req := &QuerySupplierByUserDefineCodeRequest{ + InvokeID: "invoke_004", + UserID: "user123", + Data: string(bizDataBytes), + BizType: "query_supplier_by_userDefineCode", + } + + resp, err := client.QuerySupplierByUserDefineCode(context.Background(), req) + if err != nil { + log.Fatalf("查询供应商失败: %v", err) + } + + fmt.Printf("查询供应商响应: invokeId=%s, bizType=%s\n", resp.InvokeID, resp.BizType) + // 解析响应数据 + var responseData QuerySupplierByUserDefineCodeResponseData + if err := json.Unmarshal(resp.Data, &responseData); err == nil { + fmt.Printf("响应数据: success=%v\n", responseData.Success) + } +} \ No newline at end of file