Skip to content

Commit

Permalink
钉钉应用内购
Browse files Browse the repository at this point in the history
  • Loading branch information
mengfengkong committed Jul 10, 2020
1 parent 453d666 commit e514305
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sdk/api_auth_caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions sdk/api_order_caller.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions sdk/api_order_caller_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
46 changes: 46 additions & 0 deletions sdk/api_order_models.go
Original file line number Diff line number Diff line change
@@ -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"`
}
4 changes: 4 additions & 0 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit e514305

Please sign in to comment.