This commit is contained in:
parent
f352a4349f
commit
f6e56fe462
|
|
@ -122,18 +122,18 @@ func (c *Client) doRequest(ctx context.Context, path string, request interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析通用响应
|
// 解析通用响应
|
||||||
var commonResp CommonResponse
|
//var commonResp CommonResponse
|
||||||
if err := json.Unmarshal(respBody, &commonResp); err != nil {
|
//if err := json.Unmarshal(respBody, &commonResp); err != nil {
|
||||||
return NewSDKError(ErrCodeUnknown, "响应解析失败", err)
|
// return NewSDKError(ErrCodeUnknown, "响应解析失败", err)
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
if commonResp.Code != 0 {
|
//if commonResp.Code != 0 {
|
||||||
return NewSDKError(commonResp.Code, commonResp.Msg, nil)
|
// return NewSDKError(commonResp.Code, commonResp.Msg, nil)
|
||||||
}
|
//}
|
||||||
|
|
||||||
// 如果response不为nil,将commonResp.Data反序列化到response
|
// 如果response不为nil,将commonResp.Data反序列化到response
|
||||||
if response != nil {
|
if response != nil {
|
||||||
if err := json.Unmarshal(commonResp.Data, response); err != nil {
|
if err := json.Unmarshal(respBody, response); err != nil {
|
||||||
return NewSDKError(ErrCodeUnknown, "响应数据解析失败", err)
|
return NewSDKError(ErrCodeUnknown, "响应数据解析失败", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -148,7 +148,7 @@ func (c *Client) doRequest(ctx context.Context, path string, request interface{}
|
||||||
// CreateInvoice 提交订单开票申请
|
// CreateInvoice 提交订单开票申请
|
||||||
// 文档:接口1 - 提交订单开票申请
|
// 文档:接口1 - 提交订单开票申请
|
||||||
func (c *Client) CreateInvoice(ctx context.Context, req *CreateInvoiceRequest) (*CreateInvoiceResponse, error) {
|
func (c *Client) CreateInvoice(ctx context.Context, req *CreateInvoiceRequest) (*CreateInvoiceResponse, error) {
|
||||||
path := "/api/v1/invoice/create"
|
path := "OZu"
|
||||||
var resp CreateInvoiceResponse
|
var resp CreateInvoiceResponse
|
||||||
if err := c.doRequest(ctx, path, req, &resp); err != nil {
|
if err := c.doRequest(ctx, path, req, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -163,7 +163,7 @@ func (c *Client) CreateInvoice(ctx context.Context, req *CreateInvoiceRequest) (
|
||||||
// QueryInvoiceStatus 查询订单开票状态
|
// QueryInvoiceStatus 查询订单开票状态
|
||||||
// 文档:接口2 - 查询订单开票状态
|
// 文档:接口2 - 查询订单开票状态
|
||||||
func (c *Client) QueryInvoiceStatus(ctx context.Context, req *QueryInvoiceStatusRequest) (*QueryInvoiceStatusResponse, error) {
|
func (c *Client) QueryInvoiceStatus(ctx context.Context, req *QueryInvoiceStatusRequest) (*QueryInvoiceStatusResponse, error) {
|
||||||
path := "/api/v1/invoice/query"
|
path := "OZv"
|
||||||
var resp QueryInvoiceStatusResponse
|
var resp QueryInvoiceStatusResponse
|
||||||
if err := c.doRequest(ctx, path, req, &resp); err != nil {
|
if err := c.doRequest(ctx, path, req, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
// GenerateTimestamp 生成秒级时间戳字符串
|
// GenerateTimestamp 生成秒级时间戳字符串
|
||||||
func GenerateTimestamp() string {
|
func GenerateTimestamp() string {
|
||||||
return fmt.Sprintf("%d", time.Now().Unix())
|
return fmt.Sprintf("%d", time.Now().UnixMilli())
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateNonce 生成指定长度的加密安全随机字符串
|
// GenerateNonce 生成指定长度的加密安全随机字符串
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,18 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ApiBaseURL = "https://bfl.jcsk100.com/webhook/"
|
||||||
|
tenantID = "142"
|
||||||
|
clientID = "webhook_lsxd"
|
||||||
|
clientSecret = "wNKmz2VrSTawWLG5CoYNP9HLbQ6I4Cei"
|
||||||
|
)
|
||||||
|
|
||||||
// ExampleNewClient 展示了如何创建SDK客户端
|
// ExampleNewClient 展示了如何创建SDK客户端
|
||||||
func ExampleNewClient() {
|
func ExampleNewClient() {
|
||||||
// 创建客户端
|
// 创建客户端
|
||||||
|
|
@ -24,21 +33,22 @@ func ExampleNewClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleClient_CreateInvoice 展示了如何提交订单开票申请
|
// ExampleClient_CreateInvoice 展示了如何提交订单开票申请
|
||||||
func ExampleClient_CreateInvoice() {
|
func Test_ExampleClient_CreateInvoice(t *testing.T) {
|
||||||
client := NewClient(
|
client := NewClient(
|
||||||
"https://api.example.com",
|
ApiBaseURL,
|
||||||
"your-tenant-id",
|
tenantID,
|
||||||
"dd-ai-table",
|
clientID,
|
||||||
"your-client-secret",
|
clientSecret,
|
||||||
|
WithDebug(true),
|
||||||
)
|
)
|
||||||
|
|
||||||
req := &CreateInvoiceRequest{
|
req := &CreateInvoiceRequest{
|
||||||
OrderID: "ORDER20240101001",
|
OrderID: "testOrder" + strconv.FormatInt(time.Now().Unix(), 10),
|
||||||
InvoiceType: 9, // 数电普票
|
InvoiceType: 9, // 数电普票
|
||||||
Purchaser: "测试购买方有限公司",
|
Purchaser: "测试购买方有限公司",
|
||||||
TaxNum: "91110108MA12345678",
|
TaxNum: "91110108MA12345678",
|
||||||
Phone: "13800138000",
|
Phone: "13800138000",
|
||||||
Email: "test@example.com",
|
Email: "",
|
||||||
Products: []InvoiceProduct{
|
Products: []InvoiceProduct{
|
||||||
{
|
{
|
||||||
ProductName: "办公用品",
|
ProductName: "办公用品",
|
||||||
|
|
@ -61,23 +71,26 @@ func ExampleClient_CreateInvoice() {
|
||||||
log.Fatalf("创建开票申请失败: %v", err)
|
log.Fatalf("创建开票申请失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("开票状态: %d, 错误信息: %s\n", resp.Status, resp.ErrorMsg)
|
fmt.Printf("开票状态: %d, 错误信息: %s\n", resp.Code, resp.Msg)
|
||||||
for i, inv := range resp.DataList {
|
if resp.Data == "true" {
|
||||||
fmt.Printf("发票%d: 代码=%s, 号码=%s, 金额(含税)=%s\n", i+1, inv.InvoiceCode, inv.InvoiceNo, inv.TotalIncludeTax)
|
fmt.Printf("开票成功")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("开票失败")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleClient_QueryInvoiceStatus 展示了如何查询开票状态
|
// ExampleClient_QueryInvoiceStatus 展示了如何查询开票状态
|
||||||
func ExampleClient_QueryInvoiceStatus() {
|
func Test_ExampleClient_QueryInvoiceStatus(t *testing.T) {
|
||||||
client := NewClient(
|
client := NewClient(
|
||||||
"https://api.example.com",
|
ApiBaseURL,
|
||||||
"your-tenant-id",
|
tenantID,
|
||||||
"dd-ai-table",
|
clientID,
|
||||||
"your-client-secret",
|
clientSecret,
|
||||||
|
WithDebug(true),
|
||||||
)
|
)
|
||||||
|
|
||||||
req := &QueryInvoiceStatusRequest{
|
req := &QueryInvoiceStatusRequest{
|
||||||
OrderID: "ORDER20240101001",
|
OrderID: "testOrder1787018268",
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.QueryInvoiceStatus(context.Background(), req)
|
resp, err := client.QueryInvoiceStatus(context.Background(), req)
|
||||||
|
|
@ -85,7 +98,11 @@ func ExampleClient_QueryInvoiceStatus() {
|
||||||
log.Fatalf("查询开票状态失败: %v", err)
|
log.Fatalf("查询开票状态失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("开票状态: %d, 描述: %s\n", resp.Status, resp.Message)
|
fmt.Printf("开票状态查询: %d, 错误信息: %s,当前状态:%s\n", resp.Code, resp.Msg, resp.Data.Message)
|
||||||
|
if resp.Data.InvoiceDateList != nil {
|
||||||
|
invoce, _ := json.Marshal(resp.Data.InvoiceDateList)
|
||||||
|
fmt.Printf(string(invoce))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleClient_CreatePayment 展示了如何创建付款单据
|
// ExampleClient_CreatePayment 展示了如何创建付款单据
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,13 @@ type InvoiceProduct struct {
|
||||||
|
|
||||||
// CreateInvoiceResponse 提交订单开票申请响应
|
// CreateInvoiceResponse 提交订单开票申请响应
|
||||||
type CreateInvoiceResponse struct {
|
type CreateInvoiceResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data string `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开票回调
|
||||||
|
type CreateInvoiceNotify struct {
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
ErrorMsg string `json:"errorMsg,omitempty"`
|
ErrorMsg string `json:"errorMsg,omitempty"`
|
||||||
DataList []InvoiceData `json:"dataList"`
|
DataList []InvoiceData `json:"dataList"`
|
||||||
|
|
@ -138,11 +145,14 @@ type QueryInvoiceStatusRequest struct {
|
||||||
OrderID string `json:"orderId"`
|
OrderID string `json:"orderId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryInvoiceStatusResponse 查询订单开票状态响应
|
|
||||||
type QueryInvoiceStatusResponse struct {
|
type QueryInvoiceStatusResponse struct {
|
||||||
Status int `json:"status"`
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data struct {
|
||||||
|
InvoiceDateList []interface{} `json:"invoiceDateList"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Data []InvoiceData `json:"data"`
|
Status int `json:"status"`
|
||||||
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue