This commit is contained in:
李子铭 2024-11-15 10:34:36 +08:00
parent e0985d04af
commit 5e71fabbf9
10 changed files with 87 additions and 44 deletions

View File

@ -3,7 +3,6 @@ package card
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/dctw/v1/api" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/api"
"gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core"
"io/ioutil" "io/ioutil"
@ -34,7 +33,6 @@ func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println("result:", string(result))
var response *QueryResp var response *QueryResp
if err = json.Unmarshal(result, &response); err != nil { if err = json.Unmarshal(result, &response); err != nil {
return nil, err return nil, err

View File

@ -4,12 +4,13 @@ import (
"bytes" "bytes"
"context" "context"
"gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core" "gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core"
"github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"testing" "testing"
) )
func TestCardOrder(t *testing.T) { func TestCard_Order(t *testing.T) {
server, err := core.NewDctWServer( server, err := core.NewDctWServer(
&core.DctWConfig{ &core.DctWConfig{
AppId: "1", AppId: "1",
@ -24,9 +25,9 @@ func TestCardOrder(t *testing.T) {
a := &Card{DctWServer: server} a := &Card{DctWServer: server}
req := &Order{ req := &Order{
Number: 1, Number: 1,
MerchantId: "101", MerchantId: "23329",
OutTradeNo: "test_1_zltx_4", OutTradeNo: "test_1_zltx_4",
ProductId: "106", ProductId: "222",
Mobile: "18666666666", Mobile: "18666666666",
NotifyUrl: "http://test.openapi.1688sup.cn", NotifyUrl: "http://test.openapi.1688sup.cn",
Version: "1.0", Version: "1.0",
@ -36,6 +37,10 @@ func TestCardOrder(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if !resp.GetCode().IsSuccess() {
t.Error(resp.Message)
return
}
t.Log(resp) t.Log(resp)
} }
@ -50,7 +55,7 @@ func TestCard_Query(t *testing.T) {
} }
a := &Card{DctWServer: server} a := &Card{DctWServer: server}
req := &Query{ req := &Query{
MerchantId: "101", MerchantId: "23329",
OutTradeNo: "test_1_zltx_4", OutTradeNo: "test_1_zltx_4",
Version: "1.0", Version: "1.0",
} }
@ -59,7 +64,12 @@ func TestCard_Query(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if !resp.GetCode().IsSuccess() {
t.Error(resp.Message)
return
}
t.Log(resp) t.Log(resp)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
} }
func TestCard_Notify(t *testing.T) { func TestCard_Notify(t *testing.T) {
@ -89,4 +99,5 @@ func TestCard_Notify(t *testing.T) {
return return
} }
t.Log(resp) t.Log(resp)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
} }

View File

@ -1,38 +1,40 @@
package card package card
type Code string import "encoding/json"
const CodeSuccess Code = "0000" type Code json.Number
const RechargeCodeSuccess Code = "2000"
const (
CodeSuccess Code = "2000"
CodeSuccess2 Code = "0000"
)
var ResMessageMap = map[Code]string{ var ResMessageMap = map[Code]string{
"1000": "Ip limit(ip未绑定或绑定失败)", CodeSuccess: "成功",
"1001": "Missing parameters(参数异常)", "1000": "Ip limit(ip未绑定或绑定失败)",
"1002": "Invalid merchant(无效商户信息)", "1001": "Missing parameters(参数异常)",
"1003": "Invalid signature(签名校验失败)", "1002": "Invalid merchant(无效商户信息)",
"1004": "Request expiration(请求时间过期)", "1003": "Invalid signature(签名校验失败)",
"1005": "Order repeat(订单重复)", "1004": "Request expiration(请求时间过期)",
"1006": "Invalid item(商品未开通)", "1005": "Order repeat(订单重复)",
"1007": "Item price invalid(商品价格无效)", "1006": "Invalid item(商品未开通)",
"1008": "Insufficient Balance(余额不足)", "1007": "Item price invalid(商品价格无效)",
"1009": "Interface adjustment(商品映射无效)", "1008": "Insufficient Balance(余额不足)",
"1010": "Interface price adjustment(映射价格无效)", "1009": "Interface adjustment(商品映射无效)",
"1011": "Account format matching(充值账号格式不匹配)", "1010": "Interface price adjustment(映射价格无效)",
"1012": "no order(无订单信息)", "1011": "Account format matching(充值账号格式不匹配)",
"1999": "unknown error(异常错误,建议人工处理或查询订单状态)", "1012": "no order(无订单信息)",
"1999": "unknown error(异常错误,建议人工处理或查询订单状态)",
} }
func (c Code) IsRechargeSuccess() bool { func (c Code) IsSuccess() bool {
return c == RechargeCodeSuccess // 根据沟通对接,两种都是请求成功
return c == CodeSuccess || c == CodeSuccess2
} }
func (c Code) GetMessage() string { func (c Code) GetMessage() string {
if message, ok := ResMessageMap[c]; ok { if message, ok := ResMessageMap[c]; ok {
return message return message
} }
return "" return "未知错误,请联系平台"
}
func (c Code) IsSuccess() bool {
return c == CodeSuccess
} }

View File

@ -3,7 +3,6 @@ package card
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitea.cdlsxd.cn/sdk/plugin/alipay/vo"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
) )
@ -18,8 +17,9 @@ type Order struct {
} }
type OrderResp struct { type OrderResp struct {
Code vo.Code `json:"code"` Code json.Number `json:"code"`
Message string `json:"message"` Message string `json:"message"`
TradeNo string `json:"tradeNo"`
} }
type Query struct { type Query struct {
@ -29,7 +29,7 @@ type Query struct {
} }
type QueryResp struct { type QueryResp struct {
Code vo.Code `json:"code"` Code json.Number `json:"code"`
Status OrderStatus `json:"status"` Status OrderStatus `json:"status"`
Message string `json:"message"` Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"` OutTradeNo string `json:"outTradeNo"`
@ -97,3 +97,13 @@ func (o *Notify) Validate() error {
} }
return nil return nil
} }
func (o *OrderResp) GetCode() Code {
// 不能直接定义值对象因为单独定义类型type code json.Number类型无法jsonUnmarshal
return Code(o.Code)
}
func (o *QueryResp) GetCode() Code {
// 不能直接定义值对象因为单独定义类型type code json.Number类型无法jsonUnmarshal
return Code(o.Code)
}

View File

@ -5,7 +5,8 @@ import "encoding/json"
type Code json.Number type Code json.Number
const ( const (
CodeSuccess Code = "2000" CodeSuccess Code = "2000"
CodeSuccess2 Code = "0000"
) )
var ResMessageMap = map[Code]string{ var ResMessageMap = map[Code]string{
@ -27,7 +28,8 @@ var ResMessageMap = map[Code]string{
} }
func (c Code) IsSuccess() bool { func (c Code) IsSuccess() bool {
return c == CodeSuccess // 根据沟通对接,两种都是请求成功
return c == CodeSuccess || c == CodeSuccess2
} }
func (c Code) GetMessage() string { func (c Code) GetMessage() string {

View File

@ -26,7 +26,7 @@ func TestDirect_Order(t *testing.T) {
req := &Order{ req := &Order{
Number: 1, Number: 1,
MerchantId: "23329", MerchantId: "23329",
OutTradeNo: "test_1_zltx_6", OutTradeNo: "test_1_zltx_7",
ProductId: "106", ProductId: "106",
AccountType: 1, AccountType: 1,
RechargeAccount: "18666666666", RechargeAccount: "18666666666",
@ -38,7 +38,7 @@ func TestDirect_Order(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if !resp.Code.IsSuccess() { if !resp.GetCode().IsSuccess() {
t.Error(resp.Message) t.Error(resp.Message)
return return
} }
@ -56,8 +56,8 @@ func TestDirect_Query(t *testing.T) {
} }
a := &Direct{DctWServer: server} a := &Direct{DctWServer: server}
req := &Query{ req := &Query{
MerchantId: "106", MerchantId: "23329",
OutTradeNo: "test_1_zltx_4", OutTradeNo: "test_1_zltx_7",
Version: "1.0", Version: "1.0",
} }
resp, err := a.Query(context.Background(), req) resp, err := a.Query(context.Background(), req)
@ -65,6 +65,10 @@ func TestDirect_Query(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if !resp.GetCode().IsSuccess() {
t.Error(resp.Message)
return
}
t.Log(resp) t.Log(resp)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
} }

View File

@ -18,9 +18,9 @@ type Order struct {
} }
type OrderResp struct { type OrderResp struct {
Code Code `json:"code"` Code json.Number `json:"code"`
Message string `json:"message"` Message string `json:"message"`
TradeNo string `json:"tradeNo"` TradeNo string `json:"tradeNo"`
} }
type Query struct { type Query struct {
@ -30,7 +30,7 @@ type Query struct {
} }
type QueryResp struct { type QueryResp struct {
Code Code `json:"code"` Code json.Number `json:"code"`
Status OrderStatus `json:"status"` Status OrderStatus `json:"status"`
Message string `json:"message"` Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"` OutTradeNo string `json:"outTradeNo"`
@ -97,3 +97,13 @@ func (o *Notify) Validate() error {
} }
return nil return nil
} }
func (o *OrderResp) GetCode() Code {
// 不能直接定义值对象因为单独定义类型type code json.Number类型无法jsonUnmarshal
return Code(o.Code)
}
func (o *QueryResp) GetCode() Code {
// 不能直接定义值对象因为单独定义类型type code json.Number类型无法jsonUnmarshal
return Code(o.Code)
}

4
go.mod
View File

@ -7,11 +7,13 @@ require (
github.com/go-playground/validator/v10 v10.22.0 github.com/go-playground/validator/v10 v10.22.0
github.com/hashicorp/go-plugin v1.6.1 github.com/hashicorp/go-plugin v1.6.1
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.64.0 google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect github.com/fatih/color v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
@ -24,9 +26,11 @@ require (
github.com/mattn/go-isatty v0.0.10 // indirect github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
github.com/oklog/run v1.0.0 // indirect github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.25.0 // indirect golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

2
go.sum
View File

@ -62,5 +62,7 @@ google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=