This commit is contained in:
李子铭 2024-12-10 14:10:42 +08:00
parent c5c809a019
commit d28961f0ba
6 changed files with 37 additions and 22 deletions

View File

@ -25,5 +25,8 @@ type QueryResp struct {
}
func (o *QueryResp) GetMsg() string {
if o.Code.IsSuccess() {
return o.Msg
}
return fmt.Sprintf("Msg:[%s],SubMsg:[%s],自定处理msg:[%s]", o.Msg, o.SubMsg, o.SubCode.GetMsg())
}

View File

@ -106,6 +106,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*po.OrderReq, error) {
if err != nil {
return nil, err
}
return &po.OrderReq{
Cmd: vo.OrderCmd,
AccessId: vo.AccessId,
@ -121,7 +122,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*po.OrderReq, error) {
}, nil
}
func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse {
func orderResp(request *proto.OrderRequest, resp po.OrderResp) (*proto.OrderResponse, error) {
data, _ := json.Marshal(resp)
result := &proto.Result{
OrderNo: request.Order.OrderNo,
@ -135,22 +136,24 @@ func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderRespo
} else {
result.Status = proto.Status_ING
}
return &proto.OrderResponse{Result: result}
return &proto.OrderResponse{Result: result}, nil
}
func queryReq(in *proto.QueryRequest, chNlId string) *po.QueryReq {
func queryReq(in *proto.QueryRequest, chNlId string) (*po.QueryReq, error) {
type OrderExtra struct {
OrigDate string `json:"origDate"`
}
var e OrderExtra
_ = json.Unmarshal(in.Order.Extra, &e)
if err := json.Unmarshal(in.Order.Extra, &e); err != nil {
return nil, err
}
return &po.QueryReq{
ChNlId: chNlId,
Cmd: vo.QueryCmd,
OrigQid: in.Order.OrderNo,
OrigDate: e.OrigDate,
TraceId: in.Order.OrderNo,
}
}, nil
}
func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse {
@ -165,11 +168,15 @@ func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryRespo
return &proto.QueryResponse{Result: result}
}
func notifyReq(in *proto.NotifyRequest) *po.Notify {
func notifyReq(in *proto.NotifyRequest) (*po.Notify, error) {
var h po.Headers
_ = json.Unmarshal(in.Headers, &h)
if err := json.Unmarshal(in.Headers, &h); err != nil {
return nil, err
}
var body po.Body
_ = json.NewDecoder(strings.NewReader(string(in.Body))).Decode(&body)
if err := json.NewDecoder(strings.NewReader(string(in.Body))).Decode(&body); err != nil {
return nil, err
}
body.CouponNum = union_pay.ConvertToInt(body.CouponNum)
body.OrderAt = union_pay.ConvertToInt(body.OrderAt)
body.DiscountAt = union_pay.ConvertToInt(body.DiscountAt)
@ -180,7 +187,7 @@ func notifyReq(in *proto.NotifyRequest) *po.Notify {
return &po.Notify{
Headers: &h,
Body: &body,
}
}, nil
}
func notifyResp(request *proto.NotifyRequest, n *po.Notify) (*proto.NotifyResponse, error) {

View File

@ -54,7 +54,7 @@ func (p *UnionPayCpnService) Order(ctx context.Context, request *proto.OrderRequ
return nil, proto.ErrorResponseFail(err.Error())
}
return orderResp(request, response), nil
return orderResp(request, response)
}
func (p *UnionPayCpnService) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
@ -62,7 +62,10 @@ func (p *UnionPayCpnService) Query(ctx context.Context, request *proto.QueryRequ
if err != nil {
return nil, err
}
req := queryReq(request, c.ChNlId)
req, err := queryReq(request, c.ChNlId)
if err != nil {
return nil, proto.ErrorParamFail(err.Error())
}
if err = req.Validate(); err != nil {
return nil, err
}
@ -81,8 +84,12 @@ func (p *UnionPayCpnService) Query(ctx context.Context, request *proto.QueryRequ
}
func (p *UnionPayCpnService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
req := notifyReq(request)
if err := req.Validate(); err != nil {
req, err := notifyReq(request)
if err != nil {
return nil, proto.ErrorParamFail(err.Error())
}
if err = req.Validate(); err != nil {
return nil, err
}

View File

@ -73,7 +73,7 @@ func TestQuery(t *testing.T) {
t.Errorf("Query() error = %v", err)
return
}
fmt.Printf("%+v \n", got)
t.Logf("%+v \n", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}

View File

@ -32,11 +32,8 @@ func (o OperaSt) GetText() string {
}
func (o OperaSt) GetOrderStatus() proto.Status {
if len(o) == 0 {
return proto.Status_INVALID
}
if resultStatus, ok := queryOrderStatusMap[o]; ok {
return resultStatus
}
return proto.Status_FAIL
return proto.Status_INVALID
}

View File

@ -56,7 +56,7 @@ func Put(ctx context.Context, url string, body []byte, options ...RequestOption)
// Request 封装的HTTP请求函数使用选项模式进行配置
func Request(_ context.Context, method, url string, body []byte, options ...RequestOption) (http.Header, []byte, error) {
// 设置默认选项
defaultOptions := &RequestOptions{
o := &RequestOptions{
Headers: http.Header{
"Content-Type": []string{"application/json"},
},
@ -69,16 +69,17 @@ func Request(_ context.Context, method, url string, body []byte, options ...Requ
// 根据传入的选项更新默认选项
for _, option := range options {
option(defaultOptions)
option(o)
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, nil, fmt.Errorf("创建HTTP请求失败: %w", err)
}
req.Header = o.Headers
httpClient := &http.Client{
Timeout: defaultOptions.Timeout,
Timeout: o.Timeout,
}
resp, err := httpClient.Do(req)
if err != nil {
@ -91,7 +92,7 @@ func Request(_ context.Context, method, url string, body []byte, options ...Requ
return nil, nil, fmt.Errorf("读取响应体失败: %w", err)
}
if !defaultOptions.StatusCodeFunc(resp.StatusCode) {
if !o.StatusCodeFunc(resp.StatusCode) {
return nil, nil, fmt.Errorf("请求异常:%s", resp.Status)
}