添加文件: intelligence_finance_v1/example_test.go
This commit is contained in:
parent
5ff9752895
commit
72c88ca004
|
|
@ -0,0 +1,221 @@
|
||||||
|
package intelligence_finance_v1_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"intelligence_finance_v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExampleClient_CreateInvoice 演示如何提交订单开票请求
|
||||||
|
func ExampleClient_CreateInvoice() {
|
||||||
|
// 创建客户端
|
||||||
|
client := intelligence_finance_v1.NewClient(
|
||||||
|
"https://api.example.com", // 平台基础地址
|
||||||
|
"your-tenant-id", // 租户ID
|
||||||
|
"your-client-id", // 应用标识
|
||||||
|
"your-client-secret", // 密钥
|
||||||
|
)
|
||||||
|
|
||||||
|
// 构建开票请求
|
||||||
|
req := &intelligence_finance_v1.InvoiceRequest{
|
||||||
|
OrderID: "ORDER-2024-001",
|
||||||
|
InvoiceType: intelligence_finance_v1.InvoiceTypeDigitalNormal, // 数电普票
|
||||||
|
Purchaser: "测试购方企业",
|
||||||
|
Taxnum: "91110000000000000X",
|
||||||
|
Products: []intelligence_finance_v1.ProductItem{
|
||||||
|
{
|
||||||
|
ProductName: "测试商品",
|
||||||
|
RevenueCode: "1090101000000000000",
|
||||||
|
AmountIncludeTax: "1130.00",
|
||||||
|
Quantity: "1",
|
||||||
|
Unit: "个",
|
||||||
|
TaxSign: 1,
|
||||||
|
TaxRate: "0.13",
|
||||||
|
Discount: "0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Email: "test@example.com",
|
||||||
|
Phone: "13800138000",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用接口
|
||||||
|
ctx := context.Background()
|
||||||
|
resp, err := client.CreateInvoice(ctx, "/api/invoice/create", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("创建发票失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("开票状态: %d\n", resp.Status)
|
||||||
|
if resp.Status == intelligence_finance_v1.InvoiceStatusSuccess {
|
||||||
|
for _, data := range resp.DataList {
|
||||||
|
fmt.Printf("发票号码: %s, 发票代码: %s\n", data.InvoiceNo, data.InvoiceCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExampleClient_QueryInvoiceStatus 演示如何查询开票状态
|
||||||
|
func ExampleClient_QueryInvoiceStatus() {
|
||||||
|
client := intelligence_finance_v1.NewClient(
|
||||||
|
"https://api.example.com",
|
||||||
|
"your-tenant-id",
|
||||||
|
"your-client-id",
|
||||||
|
"your-client-secret",
|
||||||
|
)
|
||||||
|
|
||||||
|
req := &intelligence_finance_v1.InvoiceStatusQueryRequest{
|
||||||
|
OrderID: "ORDER-2024-001",
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
resp, err := client.QueryInvoiceStatus(ctx, "/api/invoice/status", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("查询开票状态失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("状态: %d, 描述: %s\n", resp.Status, resp.Message)
|
||||||
|
for _, data := range resp.Data {
|
||||||
|
fmt.Printf("发票号码: %s, 发票状态: %s\n", data.InvoiceNo, data.InvoiceStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExampleClient_CreatePaymentDocument 演示如何创建付款单据
|
||||||
|
func ExampleClient_CreatePaymentDocument() {
|
||||||
|
client := intelligence_finance_v1.NewClient(
|
||||||
|
"https://api.example.com",
|
||||||
|
"your-tenant-id",
|
||||||
|
"your-client-id",
|
||||||
|
"your-client-secret",
|
||||||
|
)
|
||||||
|
|
||||||
|
needPayment := true
|
||||||
|
req := &intelligence_finance_v1.PaymentDocumentRequest{
|
||||||
|
Code: "PAY-2024-001",
|
||||||
|
UserID: "user123",
|
||||||
|
Title: "测试付款单",
|
||||||
|
Amount: "10000.00",
|
||||||
|
Supplier: &intelligence_finance_v1.Supplier{
|
||||||
|
Name: "测试供应商",
|
||||||
|
},
|
||||||
|
Company: &intelligence_finance_v1.Company{
|
||||||
|
Name: "测试企业",
|
||||||
|
},
|
||||||
|
Department: &intelligence_finance_v1.Department{
|
||||||
|
Name: "财务部",
|
||||||
|
},
|
||||||
|
NeedPayment: &needPayment,
|
||||||
|
PaymentDetailList: []intelligence_finance_v1.PaymentDetail{
|
||||||
|
{
|
||||||
|
Amount: "10000.00",
|
||||||
|
Remark: "货款",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
resp, err := client.CreatePaymentDocument(ctx, "/api/payment/create", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("创建付款单据失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("付款单据创建成功,单据编码: %s\n", resp.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExampleClient_QueryPaymentStatus 演示如何查询支付状态
|
||||||
|
func ExampleClient_QueryPaymentStatus() {
|
||||||
|
client := intelligence_finance_v1.NewClient(
|
||||||
|
"https://api.example.com",
|
||||||
|
"your-tenant-id",
|
||||||
|
"your-client-id",
|
||||||
|
"your-client-secret",
|
||||||
|
)
|
||||||
|
|
||||||
|
req := &intelligence_finance_v1.PaymentStatusQueryRequest{
|
||||||
|
Code: "PAY-2024-001",
|
||||||
|
UserID: "user123",
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
resp, err := client.QueryPaymentStatus(ctx, "/api/payment/status", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("查询支付状态失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("支付状态: %s, 支付时间: %s\n", resp.PaymentStatus, resp.PaymentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example_HandlePaymentNotification 演示如何处理平台支付完成回调
|
||||||
|
func Example_HandlePaymentNotification() {
|
||||||
|
// 这是一个 HTTP handler 示例,展示如何处理平台回调
|
||||||
|
http.HandleFunc("/callback/payment", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// 解析通知数据
|
||||||
|
notification, err := intelligence_finance_v1.ParsePaymentNotification(r)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("解析通知失败: %v", err)
|
||||||
|
intelligence_finance_v1.WriteNotificationResponse(w, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据支付状态处理业务逻辑
|
||||||
|
switch notification.PaymentStatus {
|
||||||
|
case intelligence_finance_v1.PaymentStatusSuccess:
|
||||||
|
log.Printf("支付成功: 单据 %s, 金额 %s, 时间 %s",
|
||||||
|
notification.Code, notification.Amount, notification.PaymentTime)
|
||||||
|
// 更新订单状态等业务处理...
|
||||||
|
case intelligence_finance_v1.PaymentStatusFail:
|
||||||
|
log.Printf("支付失败: 单据 %s, 原因: %s",
|
||||||
|
notification.Code, notification.FailReason)
|
||||||
|
case intelligence_finance_v1.PaymentStatusTerminate:
|
||||||
|
log.Printf("支付已取消: 单据 %s", notification.Code)
|
||||||
|
default:
|
||||||
|
log.Printf("支付状态: %s, 单据 %s", notification.PaymentStatus, notification.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回成功响应
|
||||||
|
intelligence_finance_v1.WriteNotificationResponse(w, true)
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Println("回调服务启动在 :8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example_VerifySignature 演示签名生成和验证
|
||||||
|
func Example_VerifySignature() {
|
||||||
|
secret := "your-client-secret"
|
||||||
|
timestamp := intelligence_finance_v1.GenerateTimestamp()
|
||||||
|
nonce, _ := intelligence_finance_v1.GenerateNonce(16)
|
||||||
|
|
||||||
|
// 生成签名
|
||||||
|
data := timestamp + nonce
|
||||||
|
signature := intelligence_finance_v1.HmacSHA256Sign(secret, data)
|
||||||
|
fmt.Printf("签名: %s\n", signature)
|
||||||
|
|
||||||
|
// 验证签名
|
||||||
|
valid := intelligence_finance_v1.HmacSHA256Verify(secret, data, signature)
|
||||||
|
fmt.Printf("签名验证: %v\n", valid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example_DingTalkClient 演示钉钉AI表格客户端的使用
|
||||||
|
func Example_DingTalkClient() {
|
||||||
|
// 钉钉AI表格的 client-id 固定为 "dd-ai-table"
|
||||||
|
// 签名由AI表格自动完成,但 SDK 仍会生成签名请求头
|
||||||
|
client := intelligence_finance_v1.NewDingTalkClient(
|
||||||
|
"https://api.example.com",
|
||||||
|
"your-tenant-id",
|
||||||
|
"your-app-secret", // 在钉钉AI表格中配置为 APPSecret
|
||||||
|
)
|
||||||
|
|
||||||
|
req := &intelligence_finance_v1.InvoiceStatusQueryRequest{
|
||||||
|
OrderID: "ORDER-2024-001",
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
resp, err := client.QueryInvoiceStatus(ctx, "/api/invoice/status", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("查询失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("开票状态: %d\n", resp.Status)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue