From b12825f83b89b37b966acc066a124c24f656e045 Mon Sep 17 00:00:00 2001 From: markwang <2951177317@qq.com> Date: Wed, 21 Jun 2023 17:31:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1-?= =?UTF-8?q?=E7=94=B5=E5=AD=90=E5=8F=91=E7=A5=A8=20(#695)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: markwang --- work/invoice/client.go | 17 ++++ work/invoice/invoice.go | 191 ++++++++++++++++++++++++++++++++++++++++ work/work.go | 6 ++ 3 files changed, 214 insertions(+) create mode 100644 work/invoice/client.go create mode 100644 work/invoice/invoice.go diff --git a/work/invoice/client.go b/work/invoice/client.go new file mode 100644 index 0000000..c5dde0d --- /dev/null +++ b/work/invoice/client.go @@ -0,0 +1,17 @@ +package invoice + +import ( + "github.com/silenceper/wechat/v2/work/context" +) + +// Client 电子发票接口实例 +type Client struct { + *context.Context +} + +// NewClient 初始化实例 +func NewClient(ctx *context.Context) *Client { + return &Client{ + ctx, + } +} diff --git a/work/invoice/invoice.go b/work/invoice/invoice.go new file mode 100644 index 0000000..b72754d --- /dev/null +++ b/work/invoice/invoice.go @@ -0,0 +1,191 @@ +package invoice + +import ( + "fmt" + + "github.com/silenceper/wechat/v2/util" +) + +const ( + // getInvoiceInfoURL 查询电子发票 + getInvoiceInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/getinvoiceinfo?access_token=%s" + // updateInvoiceStatusURL 更新发票状态 + updateInvoiceStatusURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/updateinvoicestatus?access_token=%s" + // updateStatusBatchURL 批量更新发票状态 + updateStatusBatchURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/updatestatusbatch?access_token=%s" + // getInvoiceInfoBatchURL 批量查询电子发票 + getInvoiceInfoBatchURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/getinvoiceinfobatch?access_token=%s" +) + +// GetInvoiceInfoRequest 查询电子发票请求 +type GetInvoiceInfoRequest struct { + CardID string `json:"card_id"` + EncryptCode string `json:"encrypt_code"` +} + +// GetInvoiceInfoResponse 查询电子发票响应 +type GetInvoiceInfoResponse struct { + util.CommonError + CardID string `json:"card_id"` + BeginTime int64 `json:"begin_time"` + EndTime int64 `json:"end_time"` + OpenID string `json:"openid"` + Type string `json:"type"` + Payee string `json:"payee"` + Detail string `json:"detail"` + UserInfo UserInfo `json:"user_info"` +} + +// UserInfo 发票的用户信息 +type UserInfo struct { + Fee int64 `json:"fee"` + Title string `json:"title"` + BillingTime int64 `json:"billing_time"` + BillingNo string `json:"billing_no"` + BillingCode string `json:"billing_code"` + Info []Info `json:"info"` + FeeWithoutTax int64 `json:"fee_without_tax"` + Tax int64 `json:"tax"` + Detail string `json:"detail"` + PdfURL string `json:"pdf_url"` + TripPdfURL string `json:"trip_pdf_url"` + ReimburseStatus string `json:"reimburse_status"` + CheckCode string `json:"check_code"` + BuyerNumber string `json:"buyer_number"` + BuyerAddressAndPhone string `json:"buyer_address_and_phone"` + BuyerBankAccount string `json:"buyer_bank_account"` + SellerNumber string `json:"seller_number"` + SellerAddressAndPhone string `json:"seller_address_and_phone"` + SellerBankAccount string `json:"seller_bank_account"` + Remarks string `json:"remarks"` + Cashier string `json:"cashier"` + Maker string `json:"maker"` +} + +// Info 商品信息结构 +type Info struct { + Name string `json:"name"` + Num int64 `json:"num"` + Unit string `json:"unit"` + Fee int64 `json:"fee"` + Price int64 `json:"price"` +} + +// GetInvoiceInfo 查询电子发票 +// see https://developer.work.weixin.qq.com/document/path/90284 +func (r *Client) GetInvoiceInfo(req *GetInvoiceInfoRequest) (*GetInvoiceInfoResponse, error) { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return nil, err + } + var response []byte + if response, err = util.PostJSON(fmt.Sprintf(getInvoiceInfoURL, accessToken), req); err != nil { + return nil, err + } + result := &GetInvoiceInfoResponse{} + if err = util.DecodeWithError(response, result, "GetInvoiceInfo"); err != nil { + return nil, err + } + return result, nil +} + +// UpdateInvoiceStatusRequest 更新发票状态请求 +type UpdateInvoiceStatusRequest struct { + CardID string `json:"card_id"` + EncryptCode string `json:"encrypt_code"` + ReimburseStatus string `json:"reimburse_status"` +} + +// UpdateInvoiceStatus 更新发票状态 +// see https://developer.work.weixin.qq.com/document/path/90285 +func (r *Client) UpdateInvoiceStatus(req *UpdateInvoiceStatusRequest) error { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return err + } + var response []byte + if response, err = util.PostJSON(fmt.Sprintf(updateInvoiceStatusURL, accessToken), req); err != nil { + return err + } + return util.DecodeWithCommonError(response, "UpdateInvoiceStatus") +} + +// UpdateStatusBatchRequest 批量更新发票状态 +type UpdateStatusBatchRequest struct { + OpenID string `json:"openid"` + ReimburseStatus string `json:"reimburse_status"` + InvoiceList []Invoice `json:"invoice_list"` +} + +// Invoice 发票卡券 +type Invoice struct { + CardID string `json:"card_id"` + EncryptCode string `json:"encrypt_code"` +} + +// UpdateStatusBatch 批量更新发票状态 +// see https://developer.work.weixin.qq.com/document/path/90286 +func (r *Client) UpdateStatusBatch(req *UpdateStatusBatchRequest) error { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return err + } + var response []byte + if response, err = util.PostJSON(fmt.Sprintf(updateStatusBatchURL, accessToken), req); err != nil { + return err + } + return util.DecodeWithCommonError(response, "UpdateStatusBatch") +} + +// GetInvoiceInfoBatchRequest 批量查询电子发票请求 +type GetInvoiceInfoBatchRequest struct { + ItemList []Invoice `json:"item_list"` +} + +// GetInvoiceInfoBatchResponse 批量查询电子发票响应 +type GetInvoiceInfoBatchResponse struct { + util.CommonError + ItemList []Item `json:"item_list"` +} + +// Item 电子发票的结构化信息 +type Item struct { + CardID string `json:"card_id"` + BeginTime int64 `json:"begin_time"` + EndTime int64 `json:"end_time"` + OpenID string `json:"openid"` + Type string `json:"type"` + Payee string `json:"payee"` + Detail string `json:"detail"` + UserInfo UserInfo `json:"user_info"` +} + +// GetInvoiceInfoBatch 批量查询电子发票 +// see https://developer.work.weixin.qq.com/document/path/90287 +func (r *Client) GetInvoiceInfoBatch(req *GetInvoiceInfoBatchRequest) (*GetInvoiceInfoBatchResponse, error) { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return nil, err + } + var response []byte + if response, err = util.PostJSON(fmt.Sprintf(getInvoiceInfoBatchURL, accessToken), req); err != nil { + return nil, err + } + result := &GetInvoiceInfoBatchResponse{} + if err = util.DecodeWithError(response, result, "GetInvoiceInfoBatch"); err != nil { + return nil, err + } + return result, nil +} diff --git a/work/work.go b/work/work.go index d394658..286dce4 100644 --- a/work/work.go +++ b/work/work.go @@ -7,6 +7,7 @@ import ( "github.com/silenceper/wechat/v2/work/config" "github.com/silenceper/wechat/v2/work/context" "github.com/silenceper/wechat/v2/work/externalcontact" + "github.com/silenceper/wechat/v2/work/invoice" "github.com/silenceper/wechat/v2/work/kf" "github.com/silenceper/wechat/v2/work/material" "github.com/silenceper/wechat/v2/work/message" @@ -79,3 +80,8 @@ func (wk *Work) GetMessage() *message.Client { func (wk *Work) GetAppChat() *appchat.Client { return appchat.NewClient(wk.ctx) } + +// GetInvoice get invoice +func (wk *Work) GetInvoice() *invoice.Client { + return invoice.NewClient(wk.ctx) +}