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

View File

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

View File

@ -1,11 +1,16 @@
package card
type Code string
import "encoding/json"
const CodeSuccess Code = "0000"
const RechargeCodeSuccess Code = "2000"
type Code json.Number
const (
CodeSuccess Code = "2000"
CodeSuccess2 Code = "0000"
)
var ResMessageMap = map[Code]string{
CodeSuccess: "成功",
"1000": "Ip limit(ip未绑定或绑定失败)",
"1001": "Missing parameters(参数异常)",
"1002": "Invalid merchant(无效商户信息)",
@ -22,17 +27,14 @@ var ResMessageMap = map[Code]string{
"1999": "unknown error(异常错误,建议人工处理或查询订单状态)",
}
func (c Code) IsRechargeSuccess() bool {
return c == RechargeCodeSuccess
func (c Code) IsSuccess() bool {
// 根据沟通对接,两种都是请求成功
return c == CodeSuccess || c == CodeSuccess2
}
func (c Code) GetMessage() string {
if message, ok := ResMessageMap[c]; ok {
return message
}
return ""
}
func (c Code) IsSuccess() bool {
return c == CodeSuccess
return "未知错误,请联系平台"
}

View File

@ -3,7 +3,6 @@ package card
import (
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/alipay/vo"
"github.com/go-playground/validator/v10"
)
@ -18,8 +17,9 @@ type Order struct {
}
type OrderResp struct {
Code vo.Code `json:"code"`
Code json.Number `json:"code"`
Message string `json:"message"`
TradeNo string `json:"tradeNo"`
}
type Query struct {
@ -29,7 +29,7 @@ type Query struct {
}
type QueryResp struct {
Code vo.Code `json:"code"`
Code json.Number `json:"code"`
Status OrderStatus `json:"status"`
Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"`
@ -97,3 +97,13 @@ func (o *Notify) Validate() error {
}
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

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

View File

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

View File

@ -18,7 +18,7 @@ type Order struct {
}
type OrderResp struct {
Code Code `json:"code"`
Code json.Number `json:"code"`
Message string `json:"message"`
TradeNo string `json:"tradeNo"`
}
@ -30,7 +30,7 @@ type Query struct {
}
type QueryResp struct {
Code Code `json:"code"`
Code json.Number `json:"code"`
Status OrderStatus `json:"status"`
Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"`
@ -97,3 +97,13 @@ func (o *Notify) Validate() error {
}
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/hashicorp/go-plugin v1.6.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/protobuf v1.34.2
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // 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/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // 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/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // 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/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=