diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..3f614ac --- /dev/null +++ b/example_test.go @@ -0,0 +1,185 @@ +package intelligence_finance_test + +import ( + "context" + "fmt" + "log" + + "intelligence_finance" +) + +// ExampleClient_CreateInvoice demonstrates how to create an invoice. +func ExampleClient_CreateInvoice() { + // Create a new client + client := intelligence_finance.NewClient( + "https://api.example.com", + "your-tenant-id", + "your-client-id", + "your-client-secret", + ) + + ctx := context.Background() + + // Build the invoice create request + req := &intelligence_finance.InvoiceCreateRequest{ + OrderID: "ORD-2024-001", + InvoiceType: 9, // 数电普票 + Purchaser: "某某科技有限公司", + TaxNum: "91110108MA01XXXXX", + Phone: "13800138000", + Email: "finance@example.com", + ApplyPerson: "张三", + Payee: "李四", + Reviewer: "王五", + Products: []intelligence_finance.InvoiceProduct{ + { + ProductName: "云服务器ECS", + RevenueCode: "1010101010101010101", + AmountIncludeTax: 1180.00, + Unit: "台", + Quantity: 1, + TaxRate: 0.06, + TaxSign: 1, + }, + }, + } + + resp, err := client.CreateInvoice(ctx, req) + if err != nil { + log.Fatalf("CreateInvoice failed: %v", err) + } + + fmt.Printf("Invoice status: %d\n", resp.Status) + for _, inv := range resp.DataList { + fmt.Printf("Invoice No: %s, Amount: %s\n", inv.InvoiceNo, inv.TotalIncludeTax) + } +} + +// ExampleClient_QueryInvoiceStatus demonstrates how to query invoice status. +func ExampleClient_QueryInvoiceStatus() { + client := intelligence_finance.NewClient( + "https://api.example.com", + "your-tenant-id", + "your-client-id", + "your-client-secret", + ) + + ctx := context.Background() + + req := &intelligence_finance.InvoiceStatusQueryRequest{ + OrderID: "ORD-2024-001", + } + + resp, err := client.QueryInvoiceStatus(ctx, req) + if err != nil { + log.Fatalf("QueryInvoiceStatus failed: %v", err) + } + + fmt.Printf("Status: %d, Message: %s\n", resp.Status, resp.Message) + for _, inv := range resp.Data { + fmt.Printf("Invoice No: %s\n", inv.InvoiceNo) + } +} + +// ExampleClient_CreatePaymentOrder demonstrates how to create a payment order. +func ExampleClient_CreatePaymentOrder() { + client := intelligence_finance.NewClient( + "https://api.example.com", + "your-tenant-id", + "your-client-id", + "your-client-secret", + ) + + ctx := context.Background() + + req := &intelligence_finance.CreatePaymentOrderRequest{ + Code: "PAY-2024-001", + UserID: "user-001", + Title: "采购付款-云服务器", + Amount: "1180.00", + Department: &intelligence_finance.Department{ + Name: "技术部", + }, + Supplier: &intelligence_finance.Supplier{ + Name: "某某云服务商", + }, + PaymentDetailList: []intelligence_finance.PaymentDetail{ + { + Amount: "1180.00", + }, + }, + } + + resp, err := client.CreatePaymentOrder(ctx, req) + if err != nil { + log.Fatalf("CreatePaymentOrder failed: %v", err) + } + + fmt.Printf("Payment order code: %s\n", resp.Code) +} + +// ExampleClient_QueryPaymentStatus demonstrates how to query payment status. +func ExampleClient_QueryPaymentStatus() { + client := intelligence_finance.NewClient( + "https://api.example.com", + "your-tenant-id", + "your-client-id", + "your-client-secret", + ) + + ctx := context.Background() + + req := &intelligence_finance.PaymentStatusQueryRequest{ + Code: "PAY-2024-001", + UserID: "user-001", + } + + resp, err := client.QueryPaymentStatus(ctx, req) + if err != nil { + log.Fatalf("QueryPaymentStatus failed: %v", err) + } + + fmt.Printf("Payment status: %s\n", resp.PaymentStatus) + fmt.Printf("Payment time: %s\n", resp.PaymentTime) +} + +// ExampleParsePaymentNotification demonstrates how to handle a payment notification +// callback from the platform. +func ExampleParsePaymentNotification() { + // This is the body received from the platform's callback + body := []byte(`{ + "code": "PAY-2024-001", + "instanceId": "inst-001", + "corpId": "corp-123", + "paymentStatus": "SUCCESS", + "paymentTime": "2024-01-15 10:30:00", + "userId": "user-001", + "amount": "1180.00" + }`) + + notification, err := intelligence_finance.ParsePaymentNotification(body) + if err != nil { + log.Fatalf("ParsePaymentNotification failed: %v", err) + } + + fmt.Printf("Payment code: %s, Status: %s\n", notification.Code, notification.PaymentStatus) + + // In a real HTTP handler, respond with "SUCCESS" or "FAILED" + // w.Write([]byte("SUCCESS")) +} + +// ExampleClient_WithCustomOptions demonstrates how to create a client with custom options. +func ExampleClient_WithCustomOptions() { + client := intelligence_finance.NewClient( + "https://api.example.com", + "your-tenant-id", + "your-client-id", + "your-client-secret", + intelligence_finance.WithInvoiceCreatePath("/custom/path/invoice/create"), + intelligence_finance.WithInvoiceStatusQueryPath("/custom/path/invoice/status"), + intelligence_finance.WithPaymentCreatePath("/custom/path/payment/create"), + intelligence_finance.WithPaymentStatusQueryPath("/custom/path/payment/status"), + ) + + _ = client // use the client as needed +} \ No newline at end of file