From e51430512fa1f9e5c5c0a0f6e2ef5049b0c6cf22 Mon Sep 17 00:00:00 2001 From: martin <1047853491@qq.com> Date: Fri, 10 Jul 2020 14:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=89=E9=92=89=E5=BA=94=E7=94=A8=E5=86=85?= =?UTF-8?q?=E8=B4=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/api_auth_caller_test.go | 2 +- sdk/api_order_caller.go | 98 ++++++++++++++++++++++++++++++++++++ sdk/api_order_caller_test.go | 41 +++++++++++++++ sdk/api_order_models.go | 46 +++++++++++++++++ sdk/sdk.go | 4 ++ 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 sdk/api_order_caller.go create mode 100644 sdk/api_order_caller_test.go create mode 100644 sdk/api_order_models.go diff --git a/sdk/api_auth_caller_test.go b/sdk/api_auth_caller_test.go index 59ab59c..715f344 100644 --- a/sdk/api_auth_caller_test.go +++ b/sdk/api_auth_caller_test.go @@ -11,7 +11,7 @@ import ( func CreateCorp() *Corp { os.Setenv("SUITE_KEY", "suiteocpiljyoalvbhrbi") os.Setenv("SUITE_SECRET", "d1XKtyVpocDrOVJrDqPfqysmLGX7pinWS7iA8l5T7OWPd8aWZWNRfXEJrHoyb5Ng") - return NewCorp("xxx", "ding79b4e083c47d2808f2c783f7214b6d69") + return NewCorp("xxx", "dingf877c5d6d03a678aa1320dcb25e91351") } //Create Client just for test diff --git a/sdk/api_order_caller.go b/sdk/api_order_caller.go new file mode 100644 index 0000000..11bad48 --- /dev/null +++ b/sdk/api_order_caller.go @@ -0,0 +1,98 @@ +package sdk + +import ( + "github.com/polaris-team/dingtalk-sdk-golang/encrypt" + "github.com/polaris-team/dingtalk-sdk-golang/http" + "github.com/polaris-team/dingtalk-sdk-golang/json" + "strconv" +) + +//内购商品订单处理完成 +//doc:https://ding-doc.dingtalk.com/doc#/serverapi3/lg1nb7/NqyuB +func (client *DingTalkClient) OrderFinish(orderId int64) (*BaseResp, error) { + params := map[string]string{ + "access_token": client.AccessToken, + "biz_order_id": strconv.FormatInt(orderId, 10), + } + body, err := http.Post("https://oapi.dingtalk.com/topapi/appstore/internal/order/finish", params, "") + resp := BaseResp{} + if err != nil { + return nil, err + } + json.FromJson(body, &resp) + return &resp, err +} + +//获取内购商品SKU页面地址 +//doc:https://ding-doc.dingtalk.com/doc#/serverapi3/lg1nb7/Uxjfs +func (client *DingTalkClient) GetSkuPage(goodsCode string, callBackPage string) (GetSkuPageResp, error) { + params := map[string]string{ + "access_token": client.AccessToken, + "goods_code": goodsCode, + } + if callBackPage != "" { + params["callback_page"] = encrypt.URLEncode(callBackPage) + } + body, err := http.Get("https://oapi.dingtalk.com/topapi/appstore/internal/skupage/get", params) + resp := GetSkuPageResp{} + if err != nil { + return resp, err + } + json.FromJson(body, &resp) + return resp, err +} + +//获取内购订单信息 +//doc:https://ding-doc.dingtalk.com/doc#/serverapi3/lg1nb7/MgHqh +func (client *DingTalkClient) GetOrderInfo(orderId int64) (GetOrderInfoResp, error) { + params := map[string]string{ + "access_token": client.AccessToken, + "biz_order_id": strconv.FormatInt(orderId, 10), + } + body, err := http.Get("https://oapi.dingtalk.com/topapi/appstore/internal/order/get", params) + resp := GetOrderInfoResp{} + if err != nil { + return resp, err + } + json.FromJson(body, &resp) + return resp, err +} + +//应用内购商品核销 +//doc:https://ding-doc.dingtalk.com/doc#/serverapi3/lg1nb7/XbiVN +func (client *DingTalkClient) OrderConsume(orderId int64, requestId string, quantity int, userId string) (*BaseResp, error) { + params := map[string]string{ + "access_token": client.AccessToken, + "biz_order_id": strconv.FormatInt(orderId, 10), + "request_id": requestId, + "quantity": strconv.Itoa(quantity), + "userid": userId, + } + body, err := http.Post("https://oapi.dingtalk.com/topapi/appstore/internal/order/consume", params, "") + resp := BaseResp{} + if err != nil { + return nil, err + } + json.FromJson(body, &resp) + return &resp, err +} + +//获取未处理的已支付订单 +//doc:https://ding-doc.dingtalk.com/doc#/serverapi3/lg1nb7/2abS0 +func (client *DingTalkClient) GetUnfinishOrderList(itemCode string, page int64, size int64) (GetUnfinishOrderListResp, error) { + params := map[string]string{ + "access_token": client.AccessToken, + "page": strconv.FormatInt(page, 10), + "page_size": strconv.FormatInt(size, 10), + } + if itemCode != "" { + params["item_code"] = itemCode + } + body, err := http.Get("https://oapi.dingtalk.com/topapi/appstore/internal/unfinishedorder/list", params) + resp := GetUnfinishOrderListResp{} + if err != nil { + return resp, err + } + json.FromJson(body, &resp) + return resp, err +} diff --git a/sdk/api_order_caller_test.go b/sdk/api_order_caller_test.go new file mode 100644 index 0000000..61de49c --- /dev/null +++ b/sdk/api_order_caller_test.go @@ -0,0 +1,41 @@ +package sdk + +import ( + "github.com/polaris-team/dingtalk-sdk-golang/json" + "testing" +) + +func TestDingTalkClient_GetOrderInfo(t *testing.T) { + client := CreateClient() + res, err := client.GetOrderInfo(50010505110088) + t.Log(err) + t.Log(res) +} + +func TestDingTalkClient_GetSkuPage(t *testing.T) { + client := CreateClient() + res, err := client.GetSkuPage("FW_GOODS_1111", "https://apptest.bjx.cloud") + t.Log(err) + t.Log(res) +} + +func TestDingTalkClient_OrderFinish(t *testing.T) { + client := CreateClient() + res, err := client.OrderFinish(50010505110088) + t.Log(err) + t.Log(res) +} + +func TestDingTalkClient_OrderConsume(t *testing.T) { + client := CreateClient() + res, err := client.OrderConsume(50010505110088, "1199291922", 1, "12") + t.Log(err) + t.Log(res) +} + +func TestDingTalkClient_GetUnfinishOrderList(t *testing.T) { + client := CreateClient() + res, err := client.GetUnfinishOrderList("", 1, 10) + t.Log(err) + t.Log(json.ToJson(res)) +} diff --git a/sdk/api_order_models.go b/sdk/api_order_models.go new file mode 100644 index 0000000..10b15ff --- /dev/null +++ b/sdk/api_order_models.go @@ -0,0 +1,46 @@ +package sdk + +type GetSkuPageResp struct { + BaseResp + Result string `json:"result"` +} + +type GetOrderInfoResp struct { + BaseResp + Result OrderInfo `json:"result"` +} + +type OrderInfo struct { + CreateTimestamp int64 `json:"create_timestamp"` + StartTimestamp int64 `json:"start_timestamp"` + EndTimestamp int64 `json:"end_timestamp"` + PaidTimestamp int64 `json:"paid_timestamp"` + Quantity int `json:"quantity"` + Status int `json:"status"` + TotalActualPayFee int64 `json:"total_actual_pay_fee"` + GoodsCode string `json:"goods_code"` + ItemCode string `json:"item_code"` + CorpId string `json:"corp_id"` + BizOrderId int64 `json:"biz_order_id"` +} + +type GetUnfinishOrderListResp struct { + BaseResp + Result UnfinishOrderList `json:"result"` +} + +type UnfinishOrderList struct { + Items UnfinishOrderInfo `json:"items"` +} + +type UnfinishOrderInfo struct { + BizOrderId int64 `json:"biz_order_id"` + corpId string `json:"corp_id"` + CreateTimestamp int64 `json:"create_timestamp"` + GoodsCode string `json:"goods_code"` + ItemCode string `json:"item_code"` + PaidTimestamp int64 `json:"paid_timestamp"` + Quantity int `json:"quantity"` + Status int `json:"status"` + TotalActualPayFee int64 `json:"total_actual_pay_fee"` +} diff --git a/sdk/sdk.go b/sdk/sdk.go index af65a5f..c9788b3 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -1,6 +1,7 @@ package sdk import ( + "errors" "github.com/polaris-team/dingtalk-sdk-golang/encrypt" "github.com/polaris-team/dingtalk-sdk-golang/http" "os" @@ -107,6 +108,9 @@ func (corp *Corp) CreateDingTalkClient() (*DingTalkClient, error) { if err != nil { return nil, err } + if authInfo.ErrCode > 0 { + return nil, errors.New(authInfo.ErrMsg) + } return NewDingTalkClient(tokenInfo.AccessToken, authInfo.AuthInfo.Agent[0].AgentId), nil }